blob: b9d784a6298400fd7f3b31d42f67f0e48942a0cd [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 Stinner3c1e4812012-03-26 22:10:51 +02001234
Antoine Pitroub52ec782009-01-25 16:34:23 +00001235/* Computed GOTOs, or
1236 the-optimization-commonly-but-improperly-known-as-"threaded code"
1237 using gcc's labels-as-values extension
1238 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1239
1240 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001242 combined with a lookup table of jump addresses. However, since the
1243 indirect jump instruction is shared by all opcodes, the CPU will have a
1244 hard time making the right prediction for where to jump next (actually,
1245 it will be always wrong except in the uncommon case of a sequence of
1246 several identical opcodes).
1247
1248 "Threaded code" in contrast, uses an explicit jump table and an explicit
1249 indirect jump instruction at the end of each opcode. Since the jump
1250 instruction is at a different address for each opcode, the CPU will make a
1251 separate prediction for each of these instructions, which is equivalent to
1252 predicting the second opcode of each opcode pair. These predictions have
1253 a much better chance to turn out valid, especially in small bytecode loops.
1254
1255 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001257 and potentially many more instructions (depending on the pipeline width).
1258 A correctly predicted branch, however, is nearly free.
1259
1260 At the time of this writing, the "threaded code" version is up to 15-20%
1261 faster than the normal "switch" version, depending on the compiler and the
1262 CPU architecture.
1263
1264 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1265 because it would render the measurements invalid.
1266
1267
1268 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1269 indirect jumps by sharing them between all opcodes. Such optimizations
1270 can be disabled on gcc by using the -fno-gcse flag (or possibly
1271 -fno-crossjumping).
1272*/
1273
Antoine Pitrou042b1282010-08-13 21:15:58 +00001274#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001275#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001276#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001277#endif
1278
Antoine Pitrou042b1282010-08-13 21:15:58 +00001279#ifdef HAVE_COMPUTED_GOTOS
1280 #ifndef USE_COMPUTED_GOTOS
1281 #define USE_COMPUTED_GOTOS 1
1282 #endif
1283#else
1284 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1285 #error "Computed gotos are not supported on this compiler."
1286 #endif
1287 #undef USE_COMPUTED_GOTOS
1288 #define USE_COMPUTED_GOTOS 0
1289#endif
1290
1291#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001292#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001293 op: \
1294 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001295
Antoine Pitroub52ec782009-01-25 16:34:23 +00001296#ifdef LLTRACE
Mark Shannon4958f5d2021-03-24 17:56:12 +00001297#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001299 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001301 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001302 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 } \
1304 goto fast_next_opcode; \
1305 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001306#else
Mark Shannon4958f5d2021-03-24 17:56:12 +00001307#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001309 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001311 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001312 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 } \
1314 goto fast_next_opcode; \
1315 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001316#endif
1317
1318#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001319#define TARGET(op) op
Mark Shannon4958f5d2021-03-24 17:56:12 +00001320#define DISPATCH() goto fast_next_opcode
1321
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322#endif
1323
Mark Shannon4958f5d2021-03-24 17:56:12 +00001324#define CHECK_EVAL_BREAKER() \
1325 if (_Py_atomic_load_relaxed(eval_breaker)) { \
1326 continue; \
1327 }
1328
Antoine Pitroub52ec782009-01-25 16:34:23 +00001329
Neal Norwitza81d2202002-07-14 00:27:26 +00001330/* Tuple access macros */
1331
1332#ifndef Py_DEBUG
1333#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1334#else
1335#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1336#endif
1337
Guido van Rossum374a9221991-04-04 10:40:29 +00001338/* Code access macros */
1339
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001340/* The integer overflow is checked by an assertion below. */
Mark Shannonfcb55c02021-04-01 16:00:31 +01001341#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001342#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001343 _Py_CODEUNIT word = *next_instr; \
1344 opcode = _Py_OPCODE(word); \
1345 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001346 next_instr++; \
1347 } while (0)
Mark Shannonfcb55c02021-04-01 16:00:31 +01001348#define JUMPTO(x) (next_instr = first_instr + (x))
1349#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001350
Raymond Hettingerf606f872003-03-16 03:11:04 +00001351/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Some opcodes tend to come in pairs thus making it possible to
1353 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001354 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 Verifying the prediction costs a single high-speed test of a register
1357 variable against a constant. If the pairing was good, then the
1358 processor's own internal branch predication has a high likelihood of
1359 success, resulting in a nearly zero-overhead transition to the
1360 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001361 including its unpredictable switch-case branch. Combined with the
1362 processor's internal branch prediction, a successful PREDICT has the
1363 effect of making the two opcodes run as if they were a single new opcode
1364 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001365
Georg Brandl86b2fb92008-07-16 03:43:04 +00001366 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 predictions turned-on and interpret the results as if some opcodes
1368 had been combined or turn-off predictions so that the opcode frequency
1369 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001370
1371 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 the CPU to record separate branch prediction information for each
1373 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001374
Raymond Hettingerf606f872003-03-16 03:11:04 +00001375*/
1376
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001377#define PREDICT_ID(op) PRED_##op
1378
Antoine Pitrou042b1282010-08-13 21:15:58 +00001379#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001380#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001381#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001382#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001383 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001384 _Py_CODEUNIT word = *next_instr; \
1385 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001386 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001387 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001388 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001389 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001390 } \
1391 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001392#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001393#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001394
Raymond Hettingerf606f872003-03-16 03:11:04 +00001395
Guido van Rossum374a9221991-04-04 10:40:29 +00001396/* Stack manipulation macros */
1397
Martin v. Löwis18e16552006-02-15 17:27:45 +00001398/* The stack can grow at most MAXINT deep, as co_nlocals and
1399 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001400#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1401#define EMPTY() (STACK_LEVEL() == 0)
1402#define TOP() (stack_pointer[-1])
1403#define SECOND() (stack_pointer[-2])
1404#define THIRD() (stack_pointer[-3])
1405#define FOURTH() (stack_pointer[-4])
1406#define PEEK(n) (stack_pointer[-(n)])
1407#define SET_TOP(v) (stack_pointer[-1] = (v))
1408#define SET_SECOND(v) (stack_pointer[-2] = (v))
1409#define SET_THIRD(v) (stack_pointer[-3] = (v))
1410#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001411#define BASIC_STACKADJ(n) (stack_pointer += n)
1412#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1413#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001414
Guido van Rossum96a42c81992-01-12 02:29:51 +00001415#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001417 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001418 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001419#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001420 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001421#define STACK_GROW(n) do { \
1422 assert(n >= 0); \
1423 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001424 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001425 assert(STACK_LEVEL() <= co->co_stacksize); \
1426 } while (0)
1427#define STACK_SHRINK(n) do { \
1428 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001429 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001430 (void)(BASIC_STACKADJ(-n)); \
1431 assert(STACK_LEVEL() <= co->co_stacksize); \
1432 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001433#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001434 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001435 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001436#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001437#define PUSH(v) BASIC_PUSH(v)
1438#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001439#define STACK_GROW(n) BASIC_STACKADJ(n)
1440#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001441#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001442#endif
1443
Guido van Rossum681d79a1995-07-18 14:51:37 +00001444/* Local variable macros */
1445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001447
1448/* The SETLOCAL() macro must not DECREF the local variable in-place and
1449 then store the new value; it must copy the old value to a temporary
1450 value, then store the new value, and then DECREF the temporary value.
1451 This is because it is possible that during the DECREF the frame is
1452 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1453 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001455 GETLOCAL(i) = value; \
1456 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001457
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001458
1459#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 while (STACK_LEVEL() > (b)->b_level) { \
1461 PyObject *v = POP(); \
1462 Py_XDECREF(v); \
1463 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001464
1465#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001466 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001468 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1470 while (STACK_LEVEL() > (b)->b_level + 3) { \
1471 value = POP(); \
1472 Py_XDECREF(value); \
1473 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001474 exc_info = tstate->exc_info; \
1475 type = exc_info->exc_type; \
1476 value = exc_info->exc_value; \
1477 traceback = exc_info->exc_traceback; \
1478 exc_info->exc_type = POP(); \
1479 exc_info->exc_value = POP(); \
1480 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 Py_XDECREF(type); \
1482 Py_XDECREF(value); \
1483 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001484 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001485
Inada Naoki91234a12019-06-03 21:30:58 +09001486 /* macros for opcode cache */
1487#define OPCACHE_CHECK() \
1488 do { \
1489 co_opcache = NULL; \
1490 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001491 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001492 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001493 if (co_opcache_offset > 0) { \
1494 assert(co_opcache_offset <= co->co_opcache_size); \
1495 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001496 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001497 } \
1498 } \
1499 } while (0)
1500
Pablo Galindo109826c2020-10-20 06:22:44 +01001501#define OPCACHE_DEOPT() \
1502 do { \
1503 if (co_opcache != NULL) { \
1504 co_opcache->optimized = -1; \
1505 unsigned char co_opcache_offset = \
1506 co->co_opcache_map[next_instr - first_instr]; \
1507 assert(co_opcache_offset <= co->co_opcache_size); \
1508 co->co_opcache_map[co_opcache_offset] = 0; \
1509 co_opcache = NULL; \
1510 } \
1511 } while (0)
1512
1513#define OPCACHE_DEOPT_LOAD_ATTR() \
1514 do { \
1515 if (co_opcache != NULL) { \
1516 OPCACHE_STAT_ATTR_DEOPT(); \
1517 OPCACHE_DEOPT(); \
1518 } \
1519 } while (0)
1520
1521#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1522 do { \
1523 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1524 OPCACHE_DEOPT_LOAD_ATTR(); \
1525 } \
1526 } while (0)
1527
Inada Naoki91234a12019-06-03 21:30:58 +09001528#if OPCACHE_STATS
1529
1530#define OPCACHE_STAT_GLOBAL_HIT() \
1531 do { \
1532 if (co->co_opcache != NULL) opcache_global_hits++; \
1533 } while (0)
1534
1535#define OPCACHE_STAT_GLOBAL_MISS() \
1536 do { \
1537 if (co->co_opcache != NULL) opcache_global_misses++; \
1538 } while (0)
1539
1540#define OPCACHE_STAT_GLOBAL_OPT() \
1541 do { \
1542 if (co->co_opcache != NULL) opcache_global_opts++; \
1543 } while (0)
1544
Pablo Galindo109826c2020-10-20 06:22:44 +01001545#define OPCACHE_STAT_ATTR_HIT() \
1546 do { \
1547 if (co->co_opcache != NULL) opcache_attr_hits++; \
1548 } while (0)
1549
1550#define OPCACHE_STAT_ATTR_MISS() \
1551 do { \
1552 if (co->co_opcache != NULL) opcache_attr_misses++; \
1553 } while (0)
1554
1555#define OPCACHE_STAT_ATTR_OPT() \
1556 do { \
1557 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1558 } while (0)
1559
1560#define OPCACHE_STAT_ATTR_DEOPT() \
1561 do { \
1562 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1563 } while (0)
1564
1565#define OPCACHE_STAT_ATTR_TOTAL() \
1566 do { \
1567 if (co->co_opcache != NULL) opcache_attr_total++; \
1568 } while (0)
1569
Inada Naoki91234a12019-06-03 21:30:58 +09001570#else /* OPCACHE_STATS */
1571
1572#define OPCACHE_STAT_GLOBAL_HIT()
1573#define OPCACHE_STAT_GLOBAL_MISS()
1574#define OPCACHE_STAT_GLOBAL_OPT()
1575
Pablo Galindo109826c2020-10-20 06:22:44 +01001576#define OPCACHE_STAT_ATTR_HIT()
1577#define OPCACHE_STAT_ATTR_MISS()
1578#define OPCACHE_STAT_ATTR_OPT()
1579#define OPCACHE_STAT_ATTR_DEOPT()
1580#define OPCACHE_STAT_ATTR_TOTAL()
1581
Inada Naoki91234a12019-06-03 21:30:58 +09001582#endif
1583
Mark Shannond41bddd2021-03-25 12:00:30 +00001584
1585PyObject* _Py_HOT_FUNCTION
1586_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1587{
1588 _Py_EnsureTstateNotNULL(tstate);
1589
1590#if USE_COMPUTED_GOTOS
1591/* Import the static jump table */
1592#include "opcode_targets.h"
1593#endif
1594
1595#ifdef DXPAIRS
1596 int lastopcode = 0;
1597#endif
1598 PyObject **stack_pointer; /* Next free slot in value stack */
1599 const _Py_CODEUNIT *next_instr;
1600 int opcode; /* Current opcode */
1601 int oparg; /* Current opcode argument, if any */
1602 PyObject **fastlocals, **freevars;
1603 PyObject *retval = NULL; /* Return value */
1604 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
1605 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
1606 PyCodeObject *co;
1607
1608 /* when tracing we set things up so that
1609
1610 not (instr_lb <= current_bytecode_offset < instr_ub)
1611
1612 is true when the line being executed has changed. The
1613 initial values are such as to make this false the first
1614 time it is tested. */
1615
1616 const _Py_CODEUNIT *first_instr;
1617 PyObject *names;
1618 PyObject *consts;
1619 _PyOpcache *co_opcache;
1620
1621#ifdef LLTRACE
1622 _Py_IDENTIFIER(__ltrace__);
1623#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001624
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001625 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001627 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001628
Mark Shannon8e1b4062021-03-05 14:45:50 +00001629 PyTraceInfo trace_info;
1630 /* Mark trace_info as initialized */
1631 trace_info.code = NULL;
1632
1633 /* push frame */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001635 co = f->f_code;
Tim Peters5ca576e2001-06-18 22:08:13 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if (tstate->use_tracing) {
1638 if (tstate->c_tracefunc != NULL) {
1639 /* tstate->c_tracefunc, if defined, is a
1640 function that will be called on *every* entry
1641 to a code block. Its return value, if not
1642 None, is a function that will be called at
1643 the start of each executed line of code.
1644 (Actually, the function must return itself
1645 in order to continue tracing.) The trace
1646 functions are called with three arguments:
1647 a pointer to the current frame, a string
1648 indicating why the function is called, and
1649 an argument which depends on the situation.
1650 The global trace function is also called
1651 whenever an exception is detected. */
1652 if (call_trace_protected(tstate->c_tracefunc,
1653 tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001654 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001655 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 /* Trace function raised an error */
1657 goto exit_eval_frame;
1658 }
1659 }
1660 if (tstate->c_profilefunc != NULL) {
1661 /* Similar for c_profilefunc, except it needn't
1662 return itself and isn't called for "line" events */
1663 if (call_trace_protected(tstate->c_profilefunc,
1664 tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001665 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001666 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 /* Profile function raised an error */
1668 goto exit_eval_frame;
1669 }
1670 }
1671 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001672
Łukasz Langaa785c872016-09-09 17:37:37 -07001673 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1674 dtrace_function_entry(f);
1675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 names = co->co_names;
1677 consts = co->co_consts;
1678 fastlocals = f->f_localsplus;
1679 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001680 assert(PyBytes_Check(co->co_code));
1681 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001682 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1683 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1684 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001685 /*
1686 f->f_lasti refers to the index of the last instruction,
1687 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001688
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001689 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001690 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 When the PREDICT() macros are enabled, some opcode pairs follow in
1693 direct succession without updating f->f_lasti. A successful
1694 prediction effectively links the two codes together as if they
1695 were a single new opcode; accordingly,f->f_lasti will point to
1696 the first code in the pair (for instance, GET_ITER followed by
1697 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001698 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001700 assert(f->f_lasti >= -1);
Mark Shannonfcb55c02021-04-01 16:00:31 +01001701 next_instr = first_instr + f->f_lasti + 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01001702 stack_pointer = f->f_valuestack + f->f_stackdepth;
1703 /* Set f->f_stackdepth to -1.
1704 * Update when returning or calling trace function.
1705 Having f_stackdepth <= 0 ensures that invalid
1706 values are not visible to the cycle GC.
1707 We choose -1 rather than 0 to assist debugging.
1708 */
1709 f->f_stackdepth = -1;
1710 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001711
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001712 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001713 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001714 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001715 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001716 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001717 }
1718#if OPCACHE_STATS
1719 opcache_code_objects_extra_mem +=
1720 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1721 sizeof(_PyOpcache) * co->co_opcache_size;
1722 opcache_code_objects++;
1723#endif
1724 }
1725 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001726
Tim Peters5ca576e2001-06-18 22:08:13 +00001727#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001728 {
1729 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1730 if (r < 0) {
1731 goto exit_eval_frame;
1732 }
1733 lltrace = r;
1734 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001735#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001737 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001739 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001740
Victor Stinnerace47d72013-07-18 01:41:08 +02001741#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001742 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001743 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001744 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001745 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001746#endif
1747
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1751 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001752 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 /* Do periodic things. Doing this every time through
1755 the loop would add too much overhead, so we do it
1756 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001757 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 event needs attention (e.g. a signal handler or
1759 async I/O handler); see Py_AddPendingCall() and
1760 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Eric Snow7bda9de2019-03-08 17:25:54 -07001762 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001763 opcode = _Py_OPCODE(*next_instr);
1764 if (opcode == SETUP_FINALLY ||
1765 opcode == SETUP_WITH ||
1766 opcode == BEFORE_ASYNC_WITH ||
1767 opcode == YIELD_FROM) {
1768 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001769 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001770 - If we're about to enter the 'with:'. It will prevent
1771 emitting a resource warning in the common idiom
1772 'with open(path) as file:'.
1773 - If we're about to enter the 'async with:'.
1774 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001775 *very* useful, but might help in some cases and it's
1776 traditional)
1777 - If we're resuming a chain of nested 'yield from' or
1778 'await' calls, then each frame is parked with YIELD_FROM
1779 as its next opcode. If the user hit control-C we want to
1780 wait until we've reached the innermost frame before
1781 running the signal handler and raising KeyboardInterrupt
1782 (see bpo-30039).
1783 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 goto fast_next_opcode;
1785 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001786
Victor Stinnerda2914d2020-03-20 09:29:08 +01001787 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 }
1790 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 fast_next_opcode:
1793 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Łukasz Langaa785c872016-09-09 17:37:37 -07001795 if (PyDTrace_LINE_ENABLED())
Mark Shannon8e1b4062021-03-05 14:45:50 +00001796 maybe_dtrace_line(f, &trace_info);
Łukasz Langaa785c872016-09-09 17:37:37 -07001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001799
Victor Stinnerdab84232020-03-17 18:56:44 +01001800 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001801 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001803 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001805 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 err = maybe_call_line_trace(tstate->c_tracefunc,
1808 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001809 tstate, f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001810 &trace_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* Reload possibly changed frame fields */
1812 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001813 stack_pointer = f->f_valuestack+f->f_stackdepth;
1814 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001821
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001822 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001823 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001824#ifdef DYNAMIC_EXECUTION_PROFILE
1825#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 dxpairs[lastopcode][opcode]++;
1827 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001828#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001830#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001831
Guido van Rossum96a42c81992-01-12 02:29:51 +00001832#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (lltrace) {
1836 if (HAS_ARG(opcode)) {
1837 printf("%d: %d, %d\n",
1838 f->f_lasti, opcode, oparg);
1839 }
1840 else {
1841 printf("%d: %d\n",
1842 f->f_lasti, opcode);
1843 }
1844 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001845#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001850 It is essential that any operation that fails must goto error
1851 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001854 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001856
Benjamin Petersonddd19492018-09-16 22:38:02 -07001857 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 PyObject *value = GETLOCAL(oparg);
1859 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001860 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 UNBOUNDLOCAL_ERROR_MSG,
1862 PyTuple_GetItem(co->co_varnames, oparg));
1863 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 Py_INCREF(value);
1866 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001867 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 }
1869
Benjamin Petersonddd19492018-09-16 22:38:02 -07001870 case TARGET(LOAD_CONST): {
1871 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 PyObject *value = GETITEM(consts, oparg);
1873 Py_INCREF(value);
1874 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001875 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(STORE_FAST): {
1879 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *value = POP();
1881 SETLOCAL(oparg, value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001882 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001884
Benjamin Petersonddd19492018-09-16 22:38:02 -07001885 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 PyObject *value = POP();
1887 Py_DECREF(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001888 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *top = TOP();
1893 PyObject *second = SECOND();
1894 SET_TOP(second);
1895 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001896 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Benjamin Petersonddd19492018-09-16 22:38:02 -07001899 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 PyObject *top = TOP();
1901 PyObject *second = SECOND();
1902 PyObject *third = THIRD();
1903 SET_TOP(second);
1904 SET_SECOND(third);
1905 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001906 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Benjamin Petersonddd19492018-09-16 22:38:02 -07001909 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001910 PyObject *top = TOP();
1911 PyObject *second = SECOND();
1912 PyObject *third = THIRD();
1913 PyObject *fourth = FOURTH();
1914 SET_TOP(second);
1915 SET_SECOND(third);
1916 SET_THIRD(fourth);
1917 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001918 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001919 }
1920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 PyObject *top = TOP();
1923 Py_INCREF(top);
1924 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001925 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001931 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001933 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001934 SET_TOP(top);
1935 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001936 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001938
Benjamin Petersonddd19492018-09-16 22:38:02 -07001939 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 PyObject *value = TOP();
1941 PyObject *res = PyNumber_Positive(value);
1942 Py_DECREF(value);
1943 SET_TOP(res);
1944 if (res == NULL)
1945 goto error;
1946 DISPATCH();
1947 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Benjamin Petersonddd19492018-09-16 22:38:02 -07001949 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 PyObject *value = TOP();
1951 PyObject *res = PyNumber_Negative(value);
1952 Py_DECREF(value);
1953 SET_TOP(res);
1954 if (res == NULL)
1955 goto error;
1956 DISPATCH();
1957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Benjamin Petersonddd19492018-09-16 22:38:02 -07001959 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PyObject *value = TOP();
1961 int err = PyObject_IsTrue(value);
1962 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 if (err == 0) {
1964 Py_INCREF(Py_True);
1965 SET_TOP(Py_True);
1966 DISPATCH();
1967 }
1968 else if (err > 0) {
1969 Py_INCREF(Py_False);
1970 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 DISPATCH();
1972 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001973 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 goto error;
1975 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 PyObject *value = TOP();
1979 PyObject *res = PyNumber_Invert(value);
1980 Py_DECREF(value);
1981 SET_TOP(res);
1982 if (res == NULL)
1983 goto error;
1984 DISPATCH();
1985 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001986
Benjamin Petersonddd19492018-09-16 22:38:02 -07001987 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 PyObject *exp = POP();
1989 PyObject *base = TOP();
1990 PyObject *res = PyNumber_Power(base, exp, Py_None);
1991 Py_DECREF(base);
1992 Py_DECREF(exp);
1993 SET_TOP(res);
1994 if (res == NULL)
1995 goto error;
1996 DISPATCH();
1997 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Benjamin Petersonddd19492018-09-16 22:38:02 -07001999 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002000 PyObject *right = POP();
2001 PyObject *left = TOP();
2002 PyObject *res = PyNumber_Multiply(left, right);
2003 Py_DECREF(left);
2004 Py_DECREF(right);
2005 SET_TOP(res);
2006 if (res == NULL)
2007 goto error;
2008 DISPATCH();
2009 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002010
Benjamin Petersonddd19492018-09-16 22:38:02 -07002011 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002012 PyObject *right = POP();
2013 PyObject *left = TOP();
2014 PyObject *res = PyNumber_MatrixMultiply(left, right);
2015 Py_DECREF(left);
2016 Py_DECREF(right);
2017 SET_TOP(res);
2018 if (res == NULL)
2019 goto error;
2020 DISPATCH();
2021 }
2022
Benjamin Petersonddd19492018-09-16 22:38:02 -07002023 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 PyObject *divisor = POP();
2025 PyObject *dividend = TOP();
2026 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2027 Py_DECREF(dividend);
2028 Py_DECREF(divisor);
2029 SET_TOP(quotient);
2030 if (quotient == NULL)
2031 goto error;
2032 DISPATCH();
2033 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Benjamin Petersonddd19492018-09-16 22:38:02 -07002035 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 PyObject *divisor = POP();
2037 PyObject *dividend = TOP();
2038 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2039 Py_DECREF(dividend);
2040 Py_DECREF(divisor);
2041 SET_TOP(quotient);
2042 if (quotient == NULL)
2043 goto error;
2044 DISPATCH();
2045 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002046
Benjamin Petersonddd19492018-09-16 22:38:02 -07002047 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 PyObject *divisor = POP();
2049 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002050 PyObject *res;
2051 if (PyUnicode_CheckExact(dividend) && (
2052 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2053 // fast path; string formatting, but not if the RHS is a str subclass
2054 // (see issue28598)
2055 res = PyUnicode_Format(dividend, divisor);
2056 } else {
2057 res = PyNumber_Remainder(dividend, divisor);
2058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 Py_DECREF(divisor);
2060 Py_DECREF(dividend);
2061 SET_TOP(res);
2062 if (res == NULL)
2063 goto error;
2064 DISPATCH();
2065 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002066
Benjamin Petersonddd19492018-09-16 22:38:02 -07002067 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 PyObject *right = POP();
2069 PyObject *left = TOP();
2070 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002071 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002072 CPython using bytecode, it is simply worthless.
2073 See http://bugs.python.org/issue21955 and
2074 http://bugs.python.org/issue10044 for the discussion. In short,
2075 no patch shown any impact on a realistic benchmark, only a minor
2076 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 if (PyUnicode_CheckExact(left) &&
2078 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002079 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002080 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002081 }
2082 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 sum = PyNumber_Add(left, right);
2084 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 Py_DECREF(right);
2087 SET_TOP(sum);
2088 if (sum == NULL)
2089 goto error;
2090 DISPATCH();
2091 }
2092
Benjamin Petersonddd19492018-09-16 22:38:02 -07002093 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 PyObject *right = POP();
2095 PyObject *left = TOP();
2096 PyObject *diff = PyNumber_Subtract(left, right);
2097 Py_DECREF(right);
2098 Py_DECREF(left);
2099 SET_TOP(diff);
2100 if (diff == NULL)
2101 goto error;
2102 DISPATCH();
2103 }
2104
Benjamin Petersonddd19492018-09-16 22:38:02 -07002105 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 PyObject *sub = POP();
2107 PyObject *container = TOP();
2108 PyObject *res = PyObject_GetItem(container, sub);
2109 Py_DECREF(container);
2110 Py_DECREF(sub);
2111 SET_TOP(res);
2112 if (res == NULL)
2113 goto error;
2114 DISPATCH();
2115 }
2116
Benjamin Petersonddd19492018-09-16 22:38:02 -07002117 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 PyObject *right = POP();
2119 PyObject *left = TOP();
2120 PyObject *res = PyNumber_Lshift(left, right);
2121 Py_DECREF(left);
2122 Py_DECREF(right);
2123 SET_TOP(res);
2124 if (res == NULL)
2125 goto error;
2126 DISPATCH();
2127 }
2128
Benjamin Petersonddd19492018-09-16 22:38:02 -07002129 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 PyObject *right = POP();
2131 PyObject *left = TOP();
2132 PyObject *res = PyNumber_Rshift(left, right);
2133 Py_DECREF(left);
2134 Py_DECREF(right);
2135 SET_TOP(res);
2136 if (res == NULL)
2137 goto error;
2138 DISPATCH();
2139 }
2140
Benjamin Petersonddd19492018-09-16 22:38:02 -07002141 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 PyObject *right = POP();
2143 PyObject *left = TOP();
2144 PyObject *res = PyNumber_And(left, right);
2145 Py_DECREF(left);
2146 Py_DECREF(right);
2147 SET_TOP(res);
2148 if (res == NULL)
2149 goto error;
2150 DISPATCH();
2151 }
2152
Benjamin Petersonddd19492018-09-16 22:38:02 -07002153 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 PyObject *right = POP();
2155 PyObject *left = TOP();
2156 PyObject *res = PyNumber_Xor(left, right);
2157 Py_DECREF(left);
2158 Py_DECREF(right);
2159 SET_TOP(res);
2160 if (res == NULL)
2161 goto error;
2162 DISPATCH();
2163 }
2164
Benjamin Petersonddd19492018-09-16 22:38:02 -07002165 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 PyObject *right = POP();
2167 PyObject *left = TOP();
2168 PyObject *res = PyNumber_Or(left, right);
2169 Py_DECREF(left);
2170 Py_DECREF(right);
2171 SET_TOP(res);
2172 if (res == NULL)
2173 goto error;
2174 DISPATCH();
2175 }
2176
Benjamin Petersonddd19492018-09-16 22:38:02 -07002177 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 PyObject *v = POP();
2179 PyObject *list = PEEK(oparg);
2180 int err;
2181 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 if (err != 0)
2184 goto error;
2185 PREDICT(JUMP_ABSOLUTE);
2186 DISPATCH();
2187 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002188
Benjamin Petersonddd19492018-09-16 22:38:02 -07002189 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002191 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 int err;
2193 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 if (err != 0)
2196 goto error;
2197 PREDICT(JUMP_ABSOLUTE);
2198 DISPATCH();
2199 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Benjamin Petersonddd19492018-09-16 22:38:02 -07002201 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 PyObject *exp = POP();
2203 PyObject *base = TOP();
2204 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2205 Py_DECREF(base);
2206 Py_DECREF(exp);
2207 SET_TOP(res);
2208 if (res == NULL)
2209 goto error;
2210 DISPATCH();
2211 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002212
Benjamin Petersonddd19492018-09-16 22:38:02 -07002213 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 PyObject *right = POP();
2215 PyObject *left = TOP();
2216 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2217 Py_DECREF(left);
2218 Py_DECREF(right);
2219 SET_TOP(res);
2220 if (res == NULL)
2221 goto error;
2222 DISPATCH();
2223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Benjamin Petersonddd19492018-09-16 22:38:02 -07002225 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002226 PyObject *right = POP();
2227 PyObject *left = TOP();
2228 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2229 Py_DECREF(left);
2230 Py_DECREF(right);
2231 SET_TOP(res);
2232 if (res == NULL)
2233 goto error;
2234 DISPATCH();
2235 }
2236
Benjamin Petersonddd19492018-09-16 22:38:02 -07002237 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 PyObject *divisor = POP();
2239 PyObject *dividend = TOP();
2240 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2241 Py_DECREF(dividend);
2242 Py_DECREF(divisor);
2243 SET_TOP(quotient);
2244 if (quotient == NULL)
2245 goto error;
2246 DISPATCH();
2247 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002248
Benjamin Petersonddd19492018-09-16 22:38:02 -07002249 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 PyObject *divisor = POP();
2251 PyObject *dividend = TOP();
2252 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2253 Py_DECREF(dividend);
2254 Py_DECREF(divisor);
2255 SET_TOP(quotient);
2256 if (quotient == NULL)
2257 goto error;
2258 DISPATCH();
2259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Benjamin Petersonddd19492018-09-16 22:38:02 -07002261 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 PyObject *right = POP();
2263 PyObject *left = TOP();
2264 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2265 Py_DECREF(left);
2266 Py_DECREF(right);
2267 SET_TOP(mod);
2268 if (mod == NULL)
2269 goto error;
2270 DISPATCH();
2271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Petersonddd19492018-09-16 22:38:02 -07002273 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 PyObject *right = POP();
2275 PyObject *left = TOP();
2276 PyObject *sum;
2277 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002278 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002279 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002280 }
2281 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 sum = PyNumber_InPlaceAdd(left, right);
2283 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002284 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 Py_DECREF(right);
2286 SET_TOP(sum);
2287 if (sum == NULL)
2288 goto error;
2289 DISPATCH();
2290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Benjamin Petersonddd19492018-09-16 22:38:02 -07002292 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 PyObject *right = POP();
2294 PyObject *left = TOP();
2295 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2296 Py_DECREF(left);
2297 Py_DECREF(right);
2298 SET_TOP(diff);
2299 if (diff == NULL)
2300 goto error;
2301 DISPATCH();
2302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002303
Benjamin Petersonddd19492018-09-16 22:38:02 -07002304 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 PyObject *right = POP();
2306 PyObject *left = TOP();
2307 PyObject *res = PyNumber_InPlaceLshift(left, right);
2308 Py_DECREF(left);
2309 Py_DECREF(right);
2310 SET_TOP(res);
2311 if (res == NULL)
2312 goto error;
2313 DISPATCH();
2314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Benjamin Petersonddd19492018-09-16 22:38:02 -07002316 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 PyObject *right = POP();
2318 PyObject *left = TOP();
2319 PyObject *res = PyNumber_InPlaceRshift(left, right);
2320 Py_DECREF(left);
2321 Py_DECREF(right);
2322 SET_TOP(res);
2323 if (res == NULL)
2324 goto error;
2325 DISPATCH();
2326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Benjamin Petersonddd19492018-09-16 22:38:02 -07002328 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 PyObject *right = POP();
2330 PyObject *left = TOP();
2331 PyObject *res = PyNumber_InPlaceAnd(left, right);
2332 Py_DECREF(left);
2333 Py_DECREF(right);
2334 SET_TOP(res);
2335 if (res == NULL)
2336 goto error;
2337 DISPATCH();
2338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002339
Benjamin Petersonddd19492018-09-16 22:38:02 -07002340 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 PyObject *right = POP();
2342 PyObject *left = TOP();
2343 PyObject *res = PyNumber_InPlaceXor(left, right);
2344 Py_DECREF(left);
2345 Py_DECREF(right);
2346 SET_TOP(res);
2347 if (res == NULL)
2348 goto error;
2349 DISPATCH();
2350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002351
Benjamin Petersonddd19492018-09-16 22:38:02 -07002352 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 PyObject *right = POP();
2354 PyObject *left = TOP();
2355 PyObject *res = PyNumber_InPlaceOr(left, right);
2356 Py_DECREF(left);
2357 Py_DECREF(right);
2358 SET_TOP(res);
2359 if (res == NULL)
2360 goto error;
2361 DISPATCH();
2362 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002363
Benjamin Petersonddd19492018-09-16 22:38:02 -07002364 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PyObject *sub = TOP();
2366 PyObject *container = SECOND();
2367 PyObject *v = THIRD();
2368 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002369 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002370 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 Py_DECREF(container);
2374 Py_DECREF(sub);
2375 if (err != 0)
2376 goto error;
2377 DISPATCH();
2378 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Benjamin Petersonddd19492018-09-16 22:38:02 -07002380 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 PyObject *sub = TOP();
2382 PyObject *container = SECOND();
2383 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002384 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002385 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 err = PyObject_DelItem(container, sub);
2387 Py_DECREF(container);
2388 Py_DECREF(sub);
2389 if (err != 0)
2390 goto error;
2391 DISPATCH();
2392 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002393
Benjamin Petersonddd19492018-09-16 22:38:02 -07002394 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002395 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002397 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002398 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002400 _PyErr_SetString(tstate, PyExc_RuntimeError,
2401 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 Py_DECREF(value);
2403 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002405 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 Py_DECREF(value);
2407 if (res == NULL)
2408 goto error;
2409 Py_DECREF(res);
2410 DISPATCH();
2411 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002412
Benjamin Petersonddd19492018-09-16 22:38:02 -07002413 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 switch (oparg) {
2416 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002418 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002421 /* fall through */
2422 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002423 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002424 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 break;
2427 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002428 _PyErr_SetString(tstate, PyExc_SystemError,
2429 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 break;
2431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 goto error;
2433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002434
Benjamin Petersonddd19492018-09-16 22:38:02 -07002435 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002437 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002438 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002439 f->f_state = FRAME_RETURNED;
2440 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002441 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002443
Benjamin Petersonddd19492018-09-16 22:38:02 -07002444 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002445 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002446 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002447 PyObject *obj = TOP();
2448 PyTypeObject *type = Py_TYPE(obj);
2449
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002450 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002451 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002452 }
Yury Selivanov75445082015-05-11 22:57:16 -04002453
2454 if (getter != NULL) {
2455 iter = (*getter)(obj);
2456 Py_DECREF(obj);
2457 if (iter == NULL) {
2458 SET_TOP(NULL);
2459 goto error;
2460 }
2461 }
2462 else {
2463 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002464 _PyErr_Format(tstate, PyExc_TypeError,
2465 "'async for' requires an object with "
2466 "__aiter__ method, got %.100s",
2467 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002468 Py_DECREF(obj);
2469 goto error;
2470 }
2471
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002472 if (Py_TYPE(iter)->tp_as_async == NULL ||
2473 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002474
Yury Selivanov398ff912017-03-02 22:20:00 -05002475 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 _PyErr_Format(tstate, PyExc_TypeError,
2477 "'async for' received an object from __aiter__ "
2478 "that does not implement __anext__: %.100s",
2479 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002480 Py_DECREF(iter);
2481 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002482 }
2483
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002484 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002485 DISPATCH();
2486 }
2487
Benjamin Petersonddd19492018-09-16 22:38:02 -07002488 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002489 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002490 PyObject *next_iter = NULL;
2491 PyObject *awaitable = NULL;
2492 PyObject *aiter = TOP();
2493 PyTypeObject *type = Py_TYPE(aiter);
2494
Yury Selivanoveb636452016-09-08 22:01:51 -07002495 if (PyAsyncGen_CheckExact(aiter)) {
2496 awaitable = type->tp_as_async->am_anext(aiter);
2497 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002498 goto error;
2499 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002500 } else {
2501 if (type->tp_as_async != NULL){
2502 getter = type->tp_as_async->am_anext;
2503 }
Yury Selivanov75445082015-05-11 22:57:16 -04002504
Yury Selivanoveb636452016-09-08 22:01:51 -07002505 if (getter != NULL) {
2506 next_iter = (*getter)(aiter);
2507 if (next_iter == NULL) {
2508 goto error;
2509 }
2510 }
2511 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002512 _PyErr_Format(tstate, PyExc_TypeError,
2513 "'async for' requires an iterator with "
2514 "__anext__ method, got %.100s",
2515 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002516 goto error;
2517 }
Yury Selivanov75445082015-05-11 22:57:16 -04002518
Yury Selivanoveb636452016-09-08 22:01:51 -07002519 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2520 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002521 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002522 PyExc_TypeError,
2523 "'async for' received an invalid object "
2524 "from __anext__: %.100s",
2525 Py_TYPE(next_iter)->tp_name);
2526
2527 Py_DECREF(next_iter);
2528 goto error;
2529 } else {
2530 Py_DECREF(next_iter);
2531 }
2532 }
Yury Selivanov75445082015-05-11 22:57:16 -04002533
2534 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002535 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002536 DISPATCH();
2537 }
2538
Benjamin Petersonddd19492018-09-16 22:38:02 -07002539 case TARGET(GET_AWAITABLE): {
2540 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002541 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002542 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002543
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002544 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002545 int opcode_at_minus_3 = 0;
2546 if ((next_instr - first_instr) > 2) {
2547 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2548 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002549 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002550 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002551 _Py_OPCODE(next_instr[-2]));
2552 }
2553
Yury Selivanov75445082015-05-11 22:57:16 -04002554 Py_DECREF(iterable);
2555
Yury Selivanovc724bae2016-03-02 11:30:46 -05002556 if (iter != NULL && PyCoro_CheckExact(iter)) {
2557 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2558 if (yf != NULL) {
2559 /* `iter` is a coroutine object that is being
2560 awaited, `yf` is a pointer to the current awaitable
2561 being awaited on. */
2562 Py_DECREF(yf);
2563 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002564 _PyErr_SetString(tstate, PyExc_RuntimeError,
2565 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002566 /* The code below jumps to `error` if `iter` is NULL. */
2567 }
2568 }
2569
Yury Selivanov75445082015-05-11 22:57:16 -04002570 SET_TOP(iter); /* Even if it's NULL */
2571
2572 if (iter == NULL) {
2573 goto error;
2574 }
2575
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002576 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002577 DISPATCH();
2578 }
2579
Benjamin Petersonddd19492018-09-16 22:38:02 -07002580 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002582 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002583 PySendResult gen_status;
2584 if (tstate->c_tracefunc == NULL) {
2585 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002586 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002587 _Py_IDENTIFIER(send);
2588 if (v == Py_None && PyIter_Check(receiver)) {
2589 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002590 }
2591 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002592 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002593 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002594 if (retval == NULL) {
2595 if (tstate->c_tracefunc != NULL
2596 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002597 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002598 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2599 gen_status = PYGEN_RETURN;
2600 }
2601 else {
2602 gen_status = PYGEN_ERROR;
2603 }
2604 }
2605 else {
2606 gen_status = PYGEN_NEXT;
2607 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002608 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002610 if (gen_status == PYGEN_ERROR) {
2611 assert (retval == NULL);
2612 goto error;
2613 }
2614 if (gen_status == PYGEN_RETURN) {
2615 assert (retval != NULL);
2616
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002617 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002618 SET_TOP(retval);
2619 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002621 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002622 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002623 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002624 /* and repeat... */
Mark Shannonfcb55c02021-04-01 16:00:31 +01002625 assert(f->f_lasti > 0);
2626 f->f_lasti -= 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01002627 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002628 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002629 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002631
Benjamin Petersonddd19492018-09-16 22:38:02 -07002632 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002634
2635 if (co->co_flags & CO_ASYNC_GENERATOR) {
2636 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2637 Py_DECREF(retval);
2638 if (w == NULL) {
2639 retval = NULL;
2640 goto error;
2641 }
2642 retval = w;
2643 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002644 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002645 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002646 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002648
Benjamin Petersonddd19492018-09-16 22:38:02 -07002649 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002650 PyObject *type, *value, *traceback;
2651 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 PyTryBlock *b = PyFrame_BlockPop(f);
2653 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 _PyErr_SetString(tstate, PyExc_SystemError,
2655 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002658 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2659 STACK_LEVEL() <= (b)->b_level + 4);
2660 exc_info = tstate->exc_info;
2661 type = exc_info->exc_type;
2662 value = exc_info->exc_value;
2663 traceback = exc_info->exc_traceback;
2664 exc_info->exc_type = POP();
2665 exc_info->exc_value = POP();
2666 exc_info->exc_traceback = POP();
2667 Py_XDECREF(type);
2668 Py_XDECREF(value);
2669 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002672
Benjamin Petersonddd19492018-09-16 22:38:02 -07002673 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002674 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002677
Mark Shannonfee55262019-11-21 09:11:43 +00002678 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002679 assert(f->f_iblock > 0);
2680 if (oparg) {
2681 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2682 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002683 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002684 PyObject *val = POP();
2685 PyObject *tb = POP();
2686 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002687 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002688 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Benjamin Petersonddd19492018-09-16 22:38:02 -07002691 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002692 PyObject *exc = POP();
2693 assert(PyExceptionClass_Check(exc));
2694 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2695 PyTryBlock *b = PyFrame_BlockPop(f);
2696 assert(b->b_type == EXCEPT_HANDLER);
2697 Py_DECREF(exc);
2698 UNWIND_EXCEPT_HANDLER(b);
2699 Py_DECREF(POP());
2700 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002701 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002702 }
2703 else {
2704 PyObject *val = POP();
2705 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002706 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002707 goto exception_unwind;
2708 }
2709 }
2710
Zackery Spytzce6a0702019-08-25 03:44:09 -06002711 case TARGET(LOAD_ASSERTION_ERROR): {
2712 PyObject *value = PyExc_AssertionError;
2713 Py_INCREF(value);
2714 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002715 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002716 }
2717
Benjamin Petersonddd19492018-09-16 22:38:02 -07002718 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002719 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002722 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002723 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002725 if (!_PyErr_Occurred(tstate)) {
2726 _PyErr_SetString(tstate, PyExc_NameError,
2727 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002730 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002732 }
2733 else {
2734 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2735 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002736 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2738 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002739 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2740 _PyErr_SetString(tstate, PyExc_NameError,
2741 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002746 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002748
Benjamin Petersonddd19492018-09-16 22:38:02 -07002749 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 PyObject *name = GETITEM(names, oparg);
2751 PyObject *v = POP();
2752 PyObject *ns = f->f_locals;
2753 int err;
2754 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002755 _PyErr_Format(tstate, PyExc_SystemError,
2756 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 if (PyDict_CheckExact(ns))
2761 err = PyDict_SetItem(ns, name, v);
2762 else
2763 err = PyObject_SetItem(ns, name, v);
2764 Py_DECREF(v);
2765 if (err != 0)
2766 goto error;
2767 DISPATCH();
2768 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002769
Benjamin Petersonddd19492018-09-16 22:38:02 -07002770 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 PyObject *name = GETITEM(names, oparg);
2772 PyObject *ns = f->f_locals;
2773 int err;
2774 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002775 _PyErr_Format(tstate, PyExc_SystemError,
2776 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 err = PyObject_DelItem(ns, name);
2780 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002781 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 NAME_ERROR_MSG,
2783 name);
2784 goto error;
2785 }
2786 DISPATCH();
2787 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002788
Benjamin Petersonddd19492018-09-16 22:38:02 -07002789 case TARGET(UNPACK_SEQUENCE): {
2790 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 PyObject *seq = POP(), *item, **items;
2792 if (PyTuple_CheckExact(seq) &&
2793 PyTuple_GET_SIZE(seq) == oparg) {
2794 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 item = items[oparg];
2797 Py_INCREF(item);
2798 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 } else if (PyList_CheckExact(seq) &&
2801 PyList_GET_SIZE(seq) == oparg) {
2802 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 item = items[oparg];
2805 Py_INCREF(item);
2806 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002808 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002810 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 } else {
2812 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 Py_DECREF(seq);
2814 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002817 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002819
Benjamin Petersonddd19492018-09-16 22:38:02 -07002820 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2822 PyObject *seq = POP();
2823
Victor Stinner438a12d2019-05-24 17:01:38 +02002824 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 stack_pointer + totalargs)) {
2826 stack_pointer += totalargs;
2827 } else {
2828 Py_DECREF(seq);
2829 goto error;
2830 }
2831 Py_DECREF(seq);
2832 DISPATCH();
2833 }
2834
Benjamin Petersonddd19492018-09-16 22:38:02 -07002835 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 PyObject *name = GETITEM(names, oparg);
2837 PyObject *owner = TOP();
2838 PyObject *v = SECOND();
2839 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002840 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 Py_DECREF(owner);
2844 if (err != 0)
2845 goto error;
2846 DISPATCH();
2847 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002848
Benjamin Petersonddd19492018-09-16 22:38:02 -07002849 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 PyObject *name = GETITEM(names, oparg);
2851 PyObject *owner = POP();
2852 int err;
2853 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2854 Py_DECREF(owner);
2855 if (err != 0)
2856 goto error;
2857 DISPATCH();
2858 }
2859
Benjamin Petersonddd19492018-09-16 22:38:02 -07002860 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 PyObject *name = GETITEM(names, oparg);
2862 PyObject *v = POP();
2863 int err;
2864 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 if (err != 0)
2867 goto error;
2868 DISPATCH();
2869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Benjamin Petersonddd19492018-09-16 22:38:02 -07002871 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 PyObject *name = GETITEM(names, oparg);
2873 int err;
2874 err = PyDict_DelItem(f->f_globals, name);
2875 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002876 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2877 format_exc_check_arg(tstate, PyExc_NameError,
2878 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002881 }
2882 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002884
Benjamin Petersonddd19492018-09-16 22:38:02 -07002885 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 PyObject *name = GETITEM(names, oparg);
2887 PyObject *locals = f->f_locals;
2888 PyObject *v;
2889 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002890 _PyErr_Format(tstate, PyExc_SystemError,
2891 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002895 v = PyDict_GetItemWithError(locals, name);
2896 if (v != NULL) {
2897 Py_INCREF(v);
2898 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002899 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002900 goto error;
2901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 }
2903 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002905 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002906 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002907 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002908 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 }
2910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002912 v = PyDict_GetItemWithError(f->f_globals, name);
2913 if (v != NULL) {
2914 Py_INCREF(v);
2915 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002916 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002917 goto error;
2918 }
2919 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002920 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002921 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002923 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002924 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002925 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002929 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002931 }
2932 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 v = PyObject_GetItem(f->f_builtins, name);
2934 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002935 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002936 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002937 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002941 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002948
Benjamin Petersonddd19492018-09-16 22:38:02 -07002949 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002950 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002952 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002953 && PyDict_CheckExact(f->f_builtins))
2954 {
Inada Naoki91234a12019-06-03 21:30:58 +09002955 OPCACHE_CHECK();
2956 if (co_opcache != NULL && co_opcache->optimized > 0) {
2957 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2958
2959 if (lg->globals_ver ==
2960 ((PyDictObject *)f->f_globals)->ma_version_tag
2961 && lg->builtins_ver ==
2962 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2963 {
2964 PyObject *ptr = lg->ptr;
2965 OPCACHE_STAT_GLOBAL_HIT();
2966 assert(ptr != NULL);
2967 Py_INCREF(ptr);
2968 PUSH(ptr);
2969 DISPATCH();
2970 }
2971 }
2972
2973 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 name);
2977 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002978 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002979 /* _PyDict_LoadGlobal() returns NULL without raising
2980 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002981 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002982 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002983 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 }
Inada Naoki91234a12019-06-03 21:30:58 +09002986
2987 if (co_opcache != NULL) {
2988 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2989
2990 if (co_opcache->optimized == 0) {
2991 /* Wasn't optimized before. */
2992 OPCACHE_STAT_GLOBAL_OPT();
2993 } else {
2994 OPCACHE_STAT_GLOBAL_MISS();
2995 }
2996
2997 co_opcache->optimized = 1;
2998 lg->globals_ver =
2999 ((PyDictObject *)f->f_globals)->ma_version_tag;
3000 lg->builtins_ver =
3001 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3002 lg->ptr = v; /* borrowed */
3003 }
3004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003007 else {
3008 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003009
3010 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003011 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 v = PyObject_GetItem(f->f_globals, name);
3013 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003014 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003015 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003016 }
3017 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003018
Victor Stinnerb4efc962015-11-20 09:24:02 +01003019 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 v = PyObject_GetItem(f->f_builtins, name);
3021 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003022 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003023 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003024 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003025 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003028 }
3029 }
3030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003034
Benjamin Petersonddd19492018-09-16 22:38:02 -07003035 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 PyObject *v = GETLOCAL(oparg);
3037 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 SETLOCAL(oparg, NULL);
3039 DISPATCH();
3040 }
3041 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003042 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 UNBOUNDLOCAL_ERROR_MSG,
3044 PyTuple_GetItem(co->co_varnames, oparg)
3045 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 goto error;
3047 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003048
Benjamin Petersonddd19492018-09-16 22:38:02 -07003049 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003051 PyObject *oldobj = PyCell_GET(cell);
3052 if (oldobj != NULL) {
3053 PyCell_SET(cell, NULL);
3054 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003055 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003056 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003057 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 goto error;
3059 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003060
Benjamin Petersonddd19492018-09-16 22:38:02 -07003061 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 PyObject *cell = freevars[oparg];
3063 Py_INCREF(cell);
3064 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003067
Benjamin Petersonddd19492018-09-16 22:38:02 -07003068 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003069 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003070 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003071 assert(locals);
3072 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3073 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3074 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3075 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3076 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003077 value = PyDict_GetItemWithError(locals, name);
3078 if (value != NULL) {
3079 Py_INCREF(value);
3080 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003081 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003082 goto error;
3083 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003084 }
3085 else {
3086 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003087 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003088 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003089 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003090 }
3091 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003092 }
3093 }
3094 if (!value) {
3095 PyObject *cell = freevars[oparg];
3096 value = PyCell_GET(cell);
3097 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003098 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003099 goto error;
3100 }
3101 Py_INCREF(value);
3102 }
3103 PUSH(value);
3104 DISPATCH();
3105 }
3106
Benjamin Petersonddd19492018-09-16 22:38:02 -07003107 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 PyObject *cell = freevars[oparg];
3109 PyObject *value = PyCell_GET(cell);
3110 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003111 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 Py_INCREF(value);
3115 PUSH(value);
3116 DISPATCH();
3117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003118
Benjamin Petersonddd19492018-09-16 22:38:02 -07003119 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *v = POP();
3121 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003122 PyObject *oldobj = PyCell_GET(cell);
3123 PyCell_SET(cell, v);
3124 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 DISPATCH();
3126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003127
Benjamin Petersonddd19492018-09-16 22:38:02 -07003128 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003129 PyObject *str;
3130 PyObject *empty = PyUnicode_New(0, 0);
3131 if (empty == NULL) {
3132 goto error;
3133 }
3134 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3135 Py_DECREF(empty);
3136 if (str == NULL)
3137 goto error;
3138 while (--oparg >= 0) {
3139 PyObject *item = POP();
3140 Py_DECREF(item);
3141 }
3142 PUSH(str);
3143 DISPATCH();
3144 }
3145
Benjamin Petersonddd19492018-09-16 22:38:02 -07003146 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 PyObject *tup = PyTuple_New(oparg);
3148 if (tup == NULL)
3149 goto error;
3150 while (--oparg >= 0) {
3151 PyObject *item = POP();
3152 PyTuple_SET_ITEM(tup, oparg, item);
3153 }
3154 PUSH(tup);
3155 DISPATCH();
3156 }
3157
Benjamin Petersonddd19492018-09-16 22:38:02 -07003158 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PyObject *list = PyList_New(oparg);
3160 if (list == NULL)
3161 goto error;
3162 while (--oparg >= 0) {
3163 PyObject *item = POP();
3164 PyList_SET_ITEM(list, oparg, item);
3165 }
3166 PUSH(list);
3167 DISPATCH();
3168 }
3169
Mark Shannon13bc1392020-01-23 09:25:17 +00003170 case TARGET(LIST_TO_TUPLE): {
3171 PyObject *list = POP();
3172 PyObject *tuple = PyList_AsTuple(list);
3173 Py_DECREF(list);
3174 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003175 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003176 }
3177 PUSH(tuple);
3178 DISPATCH();
3179 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003180
Mark Shannon13bc1392020-01-23 09:25:17 +00003181 case TARGET(LIST_EXTEND): {
3182 PyObject *iterable = POP();
3183 PyObject *list = PEEK(oparg);
3184 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3185 if (none_val == NULL) {
3186 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003187 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003188 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003189 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003190 _PyErr_Format(tstate, PyExc_TypeError,
3191 "Value after * must be an iterable, not %.200s",
3192 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003193 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003194 Py_DECREF(iterable);
3195 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003196 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003197 Py_DECREF(none_val);
3198 Py_DECREF(iterable);
3199 DISPATCH();
3200 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003201
Mark Shannon13bc1392020-01-23 09:25:17 +00003202 case TARGET(SET_UPDATE): {
3203 PyObject *iterable = POP();
3204 PyObject *set = PEEK(oparg);
3205 int err = _PySet_Update(set, iterable);
3206 Py_DECREF(iterable);
3207 if (err < 0) {
3208 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003209 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003210 DISPATCH();
3211 }
3212
Benjamin Petersonddd19492018-09-16 22:38:02 -07003213 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 PyObject *set = PySet_New(NULL);
3215 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003216 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 if (set == NULL)
3218 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003219 for (i = oparg; i > 0; i--) {
3220 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 if (err == 0)
3222 err = PySet_Add(set, item);
3223 Py_DECREF(item);
3224 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003225 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 if (err != 0) {
3227 Py_DECREF(set);
3228 goto error;
3229 }
3230 PUSH(set);
3231 DISPATCH();
3232 }
3233
Benjamin Petersonddd19492018-09-16 22:38:02 -07003234 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003235 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3237 if (map == NULL)
3238 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003239 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003240 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003241 PyObject *key = PEEK(2*i);
3242 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003243 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003244 if (err != 0) {
3245 Py_DECREF(map);
3246 goto error;
3247 }
3248 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003249
3250 while (oparg--) {
3251 Py_DECREF(POP());
3252 Py_DECREF(POP());
3253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 PUSH(map);
3255 DISPATCH();
3256 }
3257
Benjamin Petersonddd19492018-09-16 22:38:02 -07003258 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003259 _Py_IDENTIFIER(__annotations__);
3260 int err;
3261 PyObject *ann_dict;
3262 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003263 _PyErr_Format(tstate, PyExc_SystemError,
3264 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003265 goto error;
3266 }
3267 /* check if __annotations__ in locals()... */
3268 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003269 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003270 &PyId___annotations__);
3271 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003272 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003273 goto error;
3274 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003275 /* ...if not, create a new one */
3276 ann_dict = PyDict_New();
3277 if (ann_dict == NULL) {
3278 goto error;
3279 }
3280 err = _PyDict_SetItemId(f->f_locals,
3281 &PyId___annotations__, ann_dict);
3282 Py_DECREF(ann_dict);
3283 if (err != 0) {
3284 goto error;
3285 }
3286 }
3287 }
3288 else {
3289 /* do the same if locals() is not a dict */
3290 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3291 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003292 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003293 }
3294 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3295 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003296 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003297 goto error;
3298 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003299 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003300 ann_dict = PyDict_New();
3301 if (ann_dict == NULL) {
3302 goto error;
3303 }
3304 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3305 Py_DECREF(ann_dict);
3306 if (err != 0) {
3307 goto error;
3308 }
3309 }
3310 else {
3311 Py_DECREF(ann_dict);
3312 }
3313 }
3314 DISPATCH();
3315 }
3316
Benjamin Petersonddd19492018-09-16 22:38:02 -07003317 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003318 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003319 PyObject *map;
3320 PyObject *keys = TOP();
3321 if (!PyTuple_CheckExact(keys) ||
3322 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003323 _PyErr_SetString(tstate, PyExc_SystemError,
3324 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003325 goto error;
3326 }
3327 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3328 if (map == NULL) {
3329 goto error;
3330 }
3331 for (i = oparg; i > 0; i--) {
3332 int err;
3333 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3334 PyObject *value = PEEK(i + 1);
3335 err = PyDict_SetItem(map, key, value);
3336 if (err != 0) {
3337 Py_DECREF(map);
3338 goto error;
3339 }
3340 }
3341
3342 Py_DECREF(POP());
3343 while (oparg--) {
3344 Py_DECREF(POP());
3345 }
3346 PUSH(map);
3347 DISPATCH();
3348 }
3349
Mark Shannon8a4cd702020-01-27 09:57:45 +00003350 case TARGET(DICT_UPDATE): {
3351 PyObject *update = POP();
3352 PyObject *dict = PEEK(oparg);
3353 if (PyDict_Update(dict, update) < 0) {
3354 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3355 _PyErr_Format(tstate, PyExc_TypeError,
3356 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003357 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003358 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003359 Py_DECREF(update);
3360 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003362 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003363 DISPATCH();
3364 }
3365
Mark Shannon8a4cd702020-01-27 09:57:45 +00003366 case TARGET(DICT_MERGE): {
3367 PyObject *update = POP();
3368 PyObject *dict = PEEK(oparg);
3369
3370 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3371 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3372 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003373 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003374 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003375 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003376 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003377 DISPATCH();
3378 }
3379
Benjamin Petersonddd19492018-09-16 22:38:02 -07003380 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003381 PyObject *value = TOP();
3382 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 PyObject *map;
3384 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003385 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003386 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003388 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003389 Py_DECREF(value);
3390 Py_DECREF(key);
3391 if (err != 0)
3392 goto error;
3393 PREDICT(JUMP_ABSOLUTE);
3394 DISPATCH();
3395 }
3396
Benjamin Petersonddd19492018-09-16 22:38:02 -07003397 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 PyObject *name = GETITEM(names, oparg);
3399 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003400
3401 PyTypeObject *type = Py_TYPE(owner);
3402 PyObject *res;
3403 PyObject **dictptr;
3404 PyObject *dict;
3405 _PyOpCodeOpt_LoadAttr *la;
3406
3407 OPCACHE_STAT_ATTR_TOTAL();
3408
3409 OPCACHE_CHECK();
3410 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3411 {
3412 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003413 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003414 la = &co_opcache->u.la;
3415 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3416 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003417 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3418 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3419 // so ~offset is strictly < -1 (assuming 2's complement).
3420 if (la->hint < -1) {
3421 // Even faster path -- slot hint.
3422 Py_ssize_t offset = ~la->hint;
3423 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3424 char *addr = (char *)owner + offset;
3425 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003426 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003427 Py_INCREF(res);
3428 SET_TOP(res);
3429 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003430 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003431 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003432 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3433 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003434 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003435 // Fast path for dict.
3436 assert(type->tp_dict != NULL);
3437 assert(type->tp_dictoffset > 0);
3438
3439 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3440 dict = *dictptr;
3441 if (dict != NULL && PyDict_CheckExact(dict)) {
3442 Py_ssize_t hint = la->hint;
3443 Py_INCREF(dict);
3444 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003445 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003446 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003447 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003448 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003449 if (la->hint == hint && hint >= 0) {
3450 // Our hint has helped -- cache hit.
3451 OPCACHE_STAT_ATTR_HIT();
3452 } else {
3453 // The hint we provided didn't work.
3454 // Maybe next time?
3455 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3456 }
3457
3458 Py_INCREF(res);
3459 SET_TOP(res);
3460 Py_DECREF(owner);
3461 Py_DECREF(dict);
3462 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003463 }
3464 else {
3465 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003466 // This attribute can be missing sometimes;
3467 // we don't want to optimize this lookup.
3468 OPCACHE_DEOPT_LOAD_ATTR();
3469 Py_DECREF(dict);
3470 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003471 }
3472 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003473 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3474 OPCACHE_DEOPT_LOAD_ATTR();
3475 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003476 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003477 }
3478 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003479 // The type of the object has either been updated,
3480 // or is different. Maybe it will stabilize?
3481 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3482 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003483 OPCACHE_STAT_ATTR_MISS();
3484 }
3485
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003486 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003487 type->tp_getattro == PyObject_GenericGetAttr)
3488 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003489 if (type->tp_dict == NULL) {
3490 if (PyType_Ready(type) < 0) {
3491 Py_DECREF(owner);
3492 SET_TOP(NULL);
3493 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003494 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003495 }
3496 PyObject *descr = _PyType_Lookup(type, name);
3497 if (descr != NULL) {
3498 // We found an attribute with a data-like descriptor.
3499 PyTypeObject *dtype = Py_TYPE(descr);
3500 if (dtype == &PyMemberDescr_Type) { // It's a slot
3501 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3502 struct PyMemberDef *dmem = member->d_member;
3503 if (dmem->type == T_OBJECT_EX) {
3504 Py_ssize_t offset = dmem->offset;
3505 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003506
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003507 if (co_opcache->optimized == 0) {
3508 // First time we optimize this opcode.
3509 OPCACHE_STAT_ATTR_OPT();
3510 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3511 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3512 }
3513
3514 la = &co_opcache->u.la;
3515 la->type = type;
3516 la->tp_version_tag = type->tp_version_tag;
3517 la->hint = ~offset;
3518
3519 char *addr = (char *)owner + offset;
3520 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003521 if (res != NULL) {
3522 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003523 Py_DECREF(owner);
3524 SET_TOP(res);
3525
Pablo Galindo109826c2020-10-20 06:22:44 +01003526 DISPATCH();
3527 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003528 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003529 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003530 // Else it's a slot of a different type. We don't handle those.
3531 }
3532 // Else it's some other kind of descriptor that we don't handle.
3533 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003534 }
3535 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003536 // We found an instance with a __dict__.
3537 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3538 dict = *dictptr;
3539
3540 if (dict != NULL && PyDict_CheckExact(dict)) {
3541 Py_INCREF(dict);
3542 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003543 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003544 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3545 if (res != NULL) {
3546 Py_INCREF(res);
3547 Py_DECREF(dict);
3548 Py_DECREF(owner);
3549 SET_TOP(res);
3550
3551 if (co_opcache->optimized == 0) {
3552 // First time we optimize this opcode.
3553 OPCACHE_STAT_ATTR_OPT();
3554 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3555 }
3556
3557 la = &co_opcache->u.la;
3558 la->type = type;
3559 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003560 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003561 la->hint = hint;
3562
3563 DISPATCH();
3564 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003565 else {
3566 _PyErr_Clear(tstate);
3567 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003568 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003569 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003570 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003571 OPCACHE_DEOPT_LOAD_ATTR();
3572 }
3573 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003574 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003575 OPCACHE_DEOPT_LOAD_ATTR();
3576 }
3577 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3578 OPCACHE_DEOPT_LOAD_ATTR();
3579 }
3580 }
3581
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003582 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003583 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 Py_DECREF(owner);
3585 SET_TOP(res);
3586 if (res == NULL)
3587 goto error;
3588 DISPATCH();
3589 }
3590
Benjamin Petersonddd19492018-09-16 22:38:02 -07003591 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003592 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 PyObject *right = POP();
3594 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003595 PyObject *res = PyObject_RichCompare(left, right, oparg);
3596 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003597 Py_DECREF(left);
3598 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 if (res == NULL)
3600 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PREDICT(POP_JUMP_IF_FALSE);
3602 PREDICT(POP_JUMP_IF_TRUE);
3603 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003604 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003605
Mark Shannon9af0e472020-01-14 10:12:45 +00003606 case TARGET(IS_OP): {
3607 PyObject *right = POP();
3608 PyObject *left = TOP();
3609 int res = (left == right)^oparg;
3610 PyObject *b = res ? Py_True : Py_False;
3611 Py_INCREF(b);
3612 SET_TOP(b);
3613 Py_DECREF(left);
3614 Py_DECREF(right);
3615 PREDICT(POP_JUMP_IF_FALSE);
3616 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003617 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003618 }
3619
3620 case TARGET(CONTAINS_OP): {
3621 PyObject *right = POP();
3622 PyObject *left = POP();
3623 int res = PySequence_Contains(right, left);
3624 Py_DECREF(left);
3625 Py_DECREF(right);
3626 if (res < 0) {
3627 goto error;
3628 }
3629 PyObject *b = (res^oparg) ? Py_True : Py_False;
3630 Py_INCREF(b);
3631 PUSH(b);
3632 PREDICT(POP_JUMP_IF_FALSE);
3633 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003634 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003635 }
3636
3637#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3638 "BaseException is not allowed"
3639
3640 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3641 PyObject *right = POP();
3642 PyObject *left = POP();
3643 if (PyTuple_Check(right)) {
3644 Py_ssize_t i, length;
3645 length = PyTuple_GET_SIZE(right);
3646 for (i = 0; i < length; i++) {
3647 PyObject *exc = PyTuple_GET_ITEM(right, i);
3648 if (!PyExceptionClass_Check(exc)) {
3649 _PyErr_SetString(tstate, PyExc_TypeError,
3650 CANNOT_CATCH_MSG);
3651 Py_DECREF(left);
3652 Py_DECREF(right);
3653 goto error;
3654 }
3655 }
3656 }
3657 else {
3658 if (!PyExceptionClass_Check(right)) {
3659 _PyErr_SetString(tstate, PyExc_TypeError,
3660 CANNOT_CATCH_MSG);
3661 Py_DECREF(left);
3662 Py_DECREF(right);
3663 goto error;
3664 }
3665 }
3666 int res = PyErr_GivenExceptionMatches(left, right);
3667 Py_DECREF(left);
3668 Py_DECREF(right);
3669 if (res > 0) {
3670 /* Exception matches -- Do nothing */;
3671 }
3672 else if (res == 0) {
3673 JUMPTO(oparg);
3674 }
3675 else {
3676 goto error;
3677 }
3678 DISPATCH();
3679 }
3680
Benjamin Petersonddd19492018-09-16 22:38:02 -07003681 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003682 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003683 PyObject *fromlist = POP();
3684 PyObject *level = TOP();
3685 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003686 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003687 Py_DECREF(level);
3688 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003689 SET_TOP(res);
3690 if (res == NULL)
3691 goto error;
3692 DISPATCH();
3693 }
3694
Benjamin Petersonddd19492018-09-16 22:38:02 -07003695 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003696 PyObject *from = POP(), *locals;
3697 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003698 if (PyFrame_FastToLocalsWithError(f) < 0) {
3699 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003700 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003701 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003702
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003703 locals = f->f_locals;
3704 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003705 _PyErr_SetString(tstate, PyExc_SystemError,
3706 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003707 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003708 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003710 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003712 Py_DECREF(from);
3713 if (err != 0)
3714 goto error;
3715 DISPATCH();
3716 }
Guido van Rossum25831651993-05-19 14:50:45 +00003717
Benjamin Petersonddd19492018-09-16 22:38:02 -07003718 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003719 PyObject *name = GETITEM(names, oparg);
3720 PyObject *from = TOP();
3721 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003722 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003723 PUSH(res);
3724 if (res == NULL)
3725 goto error;
3726 DISPATCH();
3727 }
Thomas Wouters52152252000-08-17 22:55:00 +00003728
Benjamin Petersonddd19492018-09-16 22:38:02 -07003729 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003731 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003733
Benjamin Petersonddd19492018-09-16 22:38:02 -07003734 case TARGET(POP_JUMP_IF_FALSE): {
3735 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003736 PyObject *cond = POP();
3737 int err;
3738 if (cond == Py_True) {
3739 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003740 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003742 if (cond == Py_False) {
3743 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003745 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 err = PyObject_IsTrue(cond);
3748 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003750 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 else if (err == 0)
3752 JUMPTO(oparg);
3753 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003754 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003756 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003757
Benjamin Petersonddd19492018-09-16 22:38:02 -07003758 case TARGET(POP_JUMP_IF_TRUE): {
3759 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003760 PyObject *cond = POP();
3761 int err;
3762 if (cond == Py_False) {
3763 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003764 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003766 if (cond == Py_True) {
3767 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003769 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003771 err = PyObject_IsTrue(cond);
3772 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 JUMPTO(oparg);
3775 }
3776 else if (err == 0)
3777 ;
3778 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003779 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003781 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003782
Benjamin Petersonddd19492018-09-16 22:38:02 -07003783 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003784 PyObject *cond = TOP();
3785 int err;
3786 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003787 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003788 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003789 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003791 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003793 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003795 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003797 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
3800 else if (err == 0)
3801 JUMPTO(oparg);
3802 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003803 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003805 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003806
Benjamin Petersonddd19492018-09-16 22:38:02 -07003807 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003808 PyObject *cond = TOP();
3809 int err;
3810 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003811 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003812 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003813 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003815 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003817 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003819 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 JUMPTO(oparg);
3822 }
3823 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003824 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003825 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 }
3827 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003828 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003830 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003831
Benjamin Petersonddd19492018-09-16 22:38:02 -07003832 case TARGET(JUMP_ABSOLUTE): {
3833 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003835 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003837 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003838
Brandt Bucher145bf262021-02-26 14:51:55 -08003839 case TARGET(GET_LEN): {
3840 // PUSH(len(TOS))
3841 Py_ssize_t len_i = PyObject_Length(TOP());
3842 if (len_i < 0) {
3843 goto error;
3844 }
3845 PyObject *len_o = PyLong_FromSsize_t(len_i);
3846 if (len_o == NULL) {
3847 goto error;
3848 }
3849 PUSH(len_o);
3850 DISPATCH();
3851 }
3852
3853 case TARGET(MATCH_CLASS): {
3854 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3855 // attributes. On failure, set TOS to False.
3856 PyObject *names = POP();
3857 PyObject *type = TOP();
3858 PyObject *subject = SECOND();
3859 assert(PyTuple_CheckExact(names));
3860 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3861 Py_DECREF(names);
3862 if (attrs) {
3863 // Success!
3864 assert(PyTuple_CheckExact(attrs));
3865 Py_DECREF(subject);
3866 SET_SECOND(attrs);
3867 }
3868 else if (_PyErr_Occurred(tstate)) {
3869 goto error;
3870 }
3871 Py_DECREF(type);
3872 SET_TOP(PyBool_FromLong(!!attrs));
3873 DISPATCH();
3874 }
3875
3876 case TARGET(MATCH_MAPPING): {
3877 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3878 PyObject *subject = TOP();
3879 // Fast path for dicts:
3880 if (PyDict_Check(subject)) {
3881 Py_INCREF(Py_True);
3882 PUSH(Py_True);
3883 DISPATCH();
3884 }
3885 // Lazily import _collections_abc.Mapping, and keep it handy on the
3886 // PyInterpreterState struct (it gets cleaned up at exit):
3887 PyInterpreterState *interp = PyInterpreterState_Get();
3888 if (interp->map_abc == NULL) {
3889 PyObject *abc = PyImport_ImportModule("_collections_abc");
3890 if (abc == NULL) {
3891 goto error;
3892 }
3893 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3894 if (interp->map_abc == NULL) {
3895 goto error;
3896 }
3897 }
3898 int match = PyObject_IsInstance(subject, interp->map_abc);
3899 if (match < 0) {
3900 goto error;
3901 }
3902 PUSH(PyBool_FromLong(match));
3903 DISPATCH();
3904 }
3905
3906 case TARGET(MATCH_SEQUENCE): {
3907 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3908 // and isinstance(TOS, _collections_abc.Sequence))
3909 PyObject *subject = TOP();
3910 // Fast path for lists and tuples:
3911 if (PyType_FastSubclass(Py_TYPE(subject),
3912 Py_TPFLAGS_LIST_SUBCLASS |
3913 Py_TPFLAGS_TUPLE_SUBCLASS))
3914 {
3915 Py_INCREF(Py_True);
3916 PUSH(Py_True);
3917 DISPATCH();
3918 }
3919 // Bail on some possible Sequences that we intentionally exclude:
3920 if (PyType_FastSubclass(Py_TYPE(subject),
3921 Py_TPFLAGS_BYTES_SUBCLASS |
3922 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3923 PyByteArray_Check(subject))
3924 {
3925 Py_INCREF(Py_False);
3926 PUSH(Py_False);
3927 DISPATCH();
3928 }
3929 // Lazily import _collections_abc.Sequence, and keep it handy on the
3930 // PyInterpreterState struct (it gets cleaned up at exit):
3931 PyInterpreterState *interp = PyInterpreterState_Get();
3932 if (interp->seq_abc == NULL) {
3933 PyObject *abc = PyImport_ImportModule("_collections_abc");
3934 if (abc == NULL) {
3935 goto error;
3936 }
3937 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3938 if (interp->seq_abc == NULL) {
3939 goto error;
3940 }
3941 }
3942 int match = PyObject_IsInstance(subject, interp->seq_abc);
3943 if (match < 0) {
3944 goto error;
3945 }
3946 PUSH(PyBool_FromLong(match));
3947 DISPATCH();
3948 }
3949
3950 case TARGET(MATCH_KEYS): {
3951 // On successful match for all keys, PUSH(values) and PUSH(True).
3952 // Otherwise, PUSH(None) and PUSH(False).
3953 PyObject *keys = TOP();
3954 PyObject *subject = SECOND();
3955 PyObject *values_or_none = match_keys(tstate, subject, keys);
3956 if (values_or_none == NULL) {
3957 goto error;
3958 }
3959 PUSH(values_or_none);
3960 if (values_or_none == Py_None) {
3961 Py_INCREF(Py_False);
3962 PUSH(Py_False);
3963 DISPATCH();
3964 }
3965 assert(PyTuple_CheckExact(values_or_none));
3966 Py_INCREF(Py_True);
3967 PUSH(Py_True);
3968 DISPATCH();
3969 }
3970
3971 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3972 // rest = dict(TOS1)
3973 // for key in TOS:
3974 // del rest[key]
3975 // SET_TOP(rest)
3976 PyObject *keys = TOP();
3977 PyObject *subject = SECOND();
3978 PyObject *rest = PyDict_New();
3979 if (rest == NULL || PyDict_Update(rest, subject)) {
3980 Py_XDECREF(rest);
3981 goto error;
3982 }
3983 // This may seem a bit inefficient, but keys is rarely big enough to
3984 // actually impact runtime.
3985 assert(PyTuple_CheckExact(keys));
3986 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3987 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3988 Py_DECREF(rest);
3989 goto error;
3990 }
3991 }
3992 Py_DECREF(keys);
3993 SET_TOP(rest);
3994 DISPATCH();
3995 }
3996
Benjamin Petersonddd19492018-09-16 22:38:02 -07003997 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003999 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004000 PyObject *iter = PyObject_GetIter(iterable);
4001 Py_DECREF(iterable);
4002 SET_TOP(iter);
4003 if (iter == NULL)
4004 goto error;
4005 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004006 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004007 DISPATCH();
4008 }
4009
Benjamin Petersonddd19492018-09-16 22:38:02 -07004010 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004011 /* before: [obj]; after [getiter(obj)] */
4012 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004013 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004014 if (PyCoro_CheckExact(iterable)) {
4015 /* `iterable` is a coroutine */
4016 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4017 /* and it is used in a 'yield from' expression of a
4018 regular generator. */
4019 Py_DECREF(iterable);
4020 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004021 _PyErr_SetString(tstate, PyExc_TypeError,
4022 "cannot 'yield from' a coroutine object "
4023 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004024 goto error;
4025 }
4026 }
4027 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004028 /* `iterable` is not a generator. */
4029 iter = PyObject_GetIter(iterable);
4030 Py_DECREF(iterable);
4031 SET_TOP(iter);
4032 if (iter == NULL)
4033 goto error;
4034 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004035 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004036 DISPATCH();
4037 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004038
Benjamin Petersonddd19492018-09-16 22:38:02 -07004039 case TARGET(FOR_ITER): {
4040 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004042 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004043 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004044 if (next != NULL) {
4045 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PREDICT(STORE_FAST);
4047 PREDICT(UNPACK_SEQUENCE);
4048 DISPATCH();
4049 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004050 if (_PyErr_Occurred(tstate)) {
4051 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004052 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004053 }
4054 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004055 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004056 }
4057 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 }
4059 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004060 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004061 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 JUMPBY(oparg);
4063 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004064 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004065
Benjamin Petersonddd19492018-09-16 22:38:02 -07004066 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004067 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 STACK_LEVEL());
4069 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004070 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004071
Benjamin Petersonddd19492018-09-16 22:38:02 -07004072 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004073 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004074 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004075 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004076 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004077 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004078 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004079 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004080 }
4081 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4082 if (exit == NULL) {
4083 Py_DECREF(enter);
4084 goto error;
4085 }
Yury Selivanov75445082015-05-11 22:57:16 -04004086 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004087 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004088 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004089 Py_DECREF(enter);
4090 if (res == NULL)
4091 goto error;
4092 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004093 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004094 DISPATCH();
4095 }
4096
Benjamin Petersonddd19492018-09-16 22:38:02 -07004097 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004098 PyObject *res = POP();
4099 /* Setup the finally block before pushing the result
4100 of __aenter__ on the stack. */
4101 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4102 STACK_LEVEL());
4103 PUSH(res);
4104 DISPATCH();
4105 }
4106
Benjamin Petersonddd19492018-09-16 22:38:02 -07004107 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004108 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004109 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004110 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004111 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004112 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004113 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004114 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004115 }
4116 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004117 if (exit == NULL) {
4118 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004119 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004120 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004121 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004122 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004123 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004124 Py_DECREF(enter);
4125 if (res == NULL)
4126 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 /* Setup the finally block before pushing the result
4128 of __enter__ on the stack. */
4129 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4130 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004131
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004132 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 DISPATCH();
4134 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004135
Mark Shannonfee55262019-11-21 09:11:43 +00004136 case TARGET(WITH_EXCEPT_START): {
4137 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004139 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4140 - SEVENTH: the context.__exit__ bound method
4141 We call SEVENTH(TOP, SECOND, THIRD).
4142 Then we push again the TOP exception and the __exit__
4143 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004146 PyObject *exc, *val, *tb, *res;
4147
Victor Stinner842cfff2016-12-01 14:45:31 +01004148 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004149 val = SECOND();
4150 tb = THIRD();
4151 assert(exc != Py_None);
4152 assert(!PyLong_Check(exc));
4153 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004154 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004155 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004156 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004157 if (res == NULL)
4158 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004159
Yury Selivanov75445082015-05-11 22:57:16 -04004160 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004161 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004163
Benjamin Petersonddd19492018-09-16 22:38:02 -07004164 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004165 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004166 PyObject *name = GETITEM(names, oparg);
4167 PyObject *obj = TOP();
4168 PyObject *meth = NULL;
4169
4170 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4171
Yury Selivanovf2392132016-12-13 19:03:51 -05004172 if (meth == NULL) {
4173 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004174 goto error;
4175 }
4176
4177 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004178 /* We can bypass temporary bound method object.
4179 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004180
INADA Naoki015bce62017-01-16 17:23:30 +09004181 meth | self | arg1 | ... | argN
4182 */
4183 SET_TOP(meth);
4184 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004185 }
4186 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004187 /* meth is not an unbound method (but a regular attr, or
4188 something was returned by a descriptor protocol). Set
4189 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004190 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004191
4192 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004193 */
INADA Naoki015bce62017-01-16 17:23:30 +09004194 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004195 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004196 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004197 }
4198 DISPATCH();
4199 }
4200
Benjamin Petersonddd19492018-09-16 22:38:02 -07004201 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004202 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004203 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004204
4205 sp = stack_pointer;
4206
INADA Naoki015bce62017-01-16 17:23:30 +09004207 meth = PEEK(oparg + 2);
4208 if (meth == NULL) {
4209 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4210 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004211
4212 Stack layout:
4213
INADA Naoki015bce62017-01-16 17:23:30 +09004214 ... | NULL | callable | arg1 | ... | argN
4215 ^- TOP()
4216 ^- (-oparg)
4217 ^- (-oparg-1)
4218 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004219
Ville Skyttä49b27342017-08-03 09:00:59 +03004220 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004221 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004222 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004223 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004224 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004225 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004226 }
4227 else {
4228 /* This is a method call. Stack layout:
4229
INADA Naoki015bce62017-01-16 17:23:30 +09004230 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004231 ^- TOP()
4232 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004233 ^- (-oparg-1)
4234 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004235
INADA Naoki015bce62017-01-16 17:23:30 +09004236 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004237 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004238 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004239 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004240 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004241 stack_pointer = sp;
4242 }
4243
4244 PUSH(res);
4245 if (res == NULL)
4246 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004247 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004248 DISPATCH();
4249 }
4250
Benjamin Petersonddd19492018-09-16 22:38:02 -07004251 case TARGET(CALL_FUNCTION): {
4252 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004253 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004255 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004257 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004258 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004259 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004260 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004261 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004262 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004264
Benjamin Petersonddd19492018-09-16 22:38:02 -07004265 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004266 PyObject **sp, *res, *names;
4267
4268 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004269 assert(PyTuple_Check(names));
4270 assert(PyTuple_GET_SIZE(names) <= oparg);
4271 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004273 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004275 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004276 Py_DECREF(names);
4277
4278 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004279 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004280 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004281 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004282 DISPATCH();
4283 }
4284
Benjamin Petersonddd19492018-09-16 22:38:02 -07004285 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004286 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004287 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004288 if (oparg & 0x01) {
4289 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004290 if (!PyDict_CheckExact(kwargs)) {
4291 PyObject *d = PyDict_New();
4292 if (d == NULL)
4293 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004294 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004295 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004296 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004297 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004298 goto error;
4299 }
4300 Py_DECREF(kwargs);
4301 kwargs = d;
4302 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004303 assert(PyDict_CheckExact(kwargs));
4304 }
4305 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004306 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004307 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004308 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004309 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004310 goto error;
4311 }
4312 Py_SETREF(callargs, PySequence_Tuple(callargs));
4313 if (callargs == NULL) {
4314 goto error;
4315 }
4316 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004317 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004318
Mark Shannon8e1b4062021-03-05 14:45:50 +00004319 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004320 Py_DECREF(func);
4321 Py_DECREF(callargs);
4322 Py_XDECREF(kwargs);
4323
4324 SET_TOP(result);
4325 if (result == NULL) {
4326 goto error;
4327 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004328 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004329 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004331
Benjamin Petersonddd19492018-09-16 22:38:02 -07004332 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004333 PyObject *qualname = POP();
4334 PyObject *codeobj = POP();
4335 PyFunctionObject *func = (PyFunctionObject *)
4336 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004337
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004338 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004339 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004340 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004341 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004343
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004344 if (oparg & 0x08) {
4345 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004346 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004347 }
4348 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004349 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004350 func->func_annotations = POP();
4351 }
4352 if (oparg & 0x02) {
4353 assert(PyDict_CheckExact(TOP()));
4354 func->func_kwdefaults = POP();
4355 }
4356 if (oparg & 0x01) {
4357 assert(PyTuple_CheckExact(TOP()));
4358 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004360
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004361 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004362 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004364
Benjamin Petersonddd19492018-09-16 22:38:02 -07004365 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004366 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004368 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 step = NULL;
4371 stop = POP();
4372 start = TOP();
4373 slice = PySlice_New(start, stop, step);
4374 Py_DECREF(start);
4375 Py_DECREF(stop);
4376 Py_XDECREF(step);
4377 SET_TOP(slice);
4378 if (slice == NULL)
4379 goto error;
4380 DISPATCH();
4381 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004382
Benjamin Petersonddd19492018-09-16 22:38:02 -07004383 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004384 /* Handles f-string value formatting. */
4385 PyObject *result;
4386 PyObject *fmt_spec;
4387 PyObject *value;
4388 PyObject *(*conv_fn)(PyObject *);
4389 int which_conversion = oparg & FVC_MASK;
4390 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4391
4392 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004393 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004394
4395 /* See if any conversion is specified. */
4396 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004397 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004398 case FVC_STR: conv_fn = PyObject_Str; break;
4399 case FVC_REPR: conv_fn = PyObject_Repr; break;
4400 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004401 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004402 _PyErr_Format(tstate, PyExc_SystemError,
4403 "unexpected conversion flag %d",
4404 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004405 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004406 }
4407
4408 /* If there's a conversion function, call it and replace
4409 value with that result. Otherwise, just use value,
4410 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004411 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004412 result = conv_fn(value);
4413 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004414 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004415 Py_XDECREF(fmt_spec);
4416 goto error;
4417 }
4418 value = result;
4419 }
4420
4421 /* If value is a unicode object, and there's no fmt_spec,
4422 then we know the result of format(value) is value
4423 itself. In that case, skip calling format(). I plan to
4424 move this optimization in to PyObject_Format()
4425 itself. */
4426 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4427 /* Do nothing, just transfer ownership to result. */
4428 result = value;
4429 } else {
4430 /* Actually call format(). */
4431 result = PyObject_Format(value, fmt_spec);
4432 Py_DECREF(value);
4433 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004434 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004435 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004436 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004437 }
4438
Eric V. Smith135d5f42016-02-05 18:23:08 -05004439 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004440 DISPATCH();
4441 }
4442
Benjamin Petersonddd19492018-09-16 22:38:02 -07004443 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004444 int oldoparg = oparg;
4445 NEXTOPARG();
4446 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004448 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004449
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004450
Antoine Pitrou042b1282010-08-13 21:15:58 +00004451#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 default:
4455 fprintf(stderr,
4456 "XXX lineno: %d, opcode: %d\n",
4457 PyFrame_GetLineNumber(f),
4458 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004460 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004464 /* This should never be reached. Every opcode should end with DISPATCH()
4465 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004466 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004468error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004469 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004470#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004471 if (!_PyErr_Occurred(tstate)) {
4472 _PyErr_SetString(tstate, PyExc_SystemError,
4473 "error return without exception set");
4474 }
Victor Stinner365b6932013-07-12 00:11:58 +02004475#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004477#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004478
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004479 /* Log traceback info. */
4480 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004481
Mark Shannoncb9879b2020-07-17 11:44:23 +01004482 if (tstate->c_tracefunc != NULL) {
4483 /* Make sure state is set to FRAME_EXECUTING for tracing */
4484 assert(f->f_state == FRAME_EXECUTING);
4485 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004486 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004487 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004488 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004489exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004490 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004491 /* Unwind stacks if an exception occurred */
4492 while (f->f_iblock > 0) {
4493 /* Pop the current block. */
4494 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 if (b->b_type == EXCEPT_HANDLER) {
4497 UNWIND_EXCEPT_HANDLER(b);
4498 continue;
4499 }
4500 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004501 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 PyObject *exc, *val, *tb;
4503 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004504 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004506 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004507 PUSH(exc_info->exc_traceback);
4508 PUSH(exc_info->exc_value);
4509 if (exc_info->exc_type != NULL) {
4510 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 }
4512 else {
4513 Py_INCREF(Py_None);
4514 PUSH(Py_None);
4515 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004516 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 /* Make the raw exception data
4518 available to the handler,
4519 so a program can emulate the
4520 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004521 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004522 if (tb != NULL)
4523 PyException_SetTraceback(val, tb);
4524 else
4525 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004527 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004529 exc_info->exc_value = val;
4530 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 if (tb == NULL)
4532 tb = Py_None;
4533 Py_INCREF(tb);
4534 PUSH(tb);
4535 PUSH(val);
4536 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004538 if (_Py_TracingPossible(ceval2)) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004539 trace_info.instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004540 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004541 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004542 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004543 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 }
4545 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004546
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004547 /* End the loop as we still have an error */
4548 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004550
Pablo Galindof00828a2019-05-09 16:52:02 +01004551 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004552 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 /* Pop remaining stack entries. */
4555 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004556 PyObject *o = POP();
4557 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004559 f->f_stackdepth = 0;
4560 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004561exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004563 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004564 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004565 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004566 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 }
4568 }
4569 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004570 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004571 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004572 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 }
4574 }
4575 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004578exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004579 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4580 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004581 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004583
Victor Stinner0b72b232020-03-12 23:18:39 +01004584 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004585}
4586
Benjamin Petersonb204a422011-06-05 22:04:07 -05004587static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004588format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004589 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004590{
4591 int err;
4592 Py_ssize_t len = PyList_GET_SIZE(names);
4593 PyObject *name_str, *comma, *tail, *tmp;
4594
4595 assert(PyList_CheckExact(names));
4596 assert(len >= 1);
4597 /* Deal with the joys of natural language. */
4598 switch (len) {
4599 case 1:
4600 name_str = PyList_GET_ITEM(names, 0);
4601 Py_INCREF(name_str);
4602 break;
4603 case 2:
4604 name_str = PyUnicode_FromFormat("%U and %U",
4605 PyList_GET_ITEM(names, len - 2),
4606 PyList_GET_ITEM(names, len - 1));
4607 break;
4608 default:
4609 tail = PyUnicode_FromFormat(", %U, and %U",
4610 PyList_GET_ITEM(names, len - 2),
4611 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004612 if (tail == NULL)
4613 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004614 /* Chop off the last two objects in the list. This shouldn't actually
4615 fail, but we can't be too careful. */
4616 err = PyList_SetSlice(names, len - 2, len, NULL);
4617 if (err == -1) {
4618 Py_DECREF(tail);
4619 return;
4620 }
4621 /* Stitch everything up into a nice comma-separated list. */
4622 comma = PyUnicode_FromString(", ");
4623 if (comma == NULL) {
4624 Py_DECREF(tail);
4625 return;
4626 }
4627 tmp = PyUnicode_Join(comma, names);
4628 Py_DECREF(comma);
4629 if (tmp == NULL) {
4630 Py_DECREF(tail);
4631 return;
4632 }
4633 name_str = PyUnicode_Concat(tmp, tail);
4634 Py_DECREF(tmp);
4635 Py_DECREF(tail);
4636 break;
4637 }
4638 if (name_str == NULL)
4639 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004640 _PyErr_Format(tstate, PyExc_TypeError,
4641 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004642 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004643 len,
4644 kind,
4645 len == 1 ? "" : "s",
4646 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004647 Py_DECREF(name_str);
4648}
4649
4650static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004651missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4652 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004653 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004654{
Victor Stinner74319ae2016-08-25 00:04:09 +02004655 Py_ssize_t i, j = 0;
4656 Py_ssize_t start, end;
4657 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004658 const char *kind = positional ? "positional" : "keyword-only";
4659 PyObject *missing_names;
4660
4661 /* Compute the names of the arguments that are missing. */
4662 missing_names = PyList_New(missing);
4663 if (missing_names == NULL)
4664 return;
4665 if (positional) {
4666 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004667 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004668 }
4669 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004670 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004671 end = start + co->co_kwonlyargcount;
4672 }
4673 for (i = start; i < end; i++) {
4674 if (GETLOCAL(i) == NULL) {
4675 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4676 PyObject *name = PyObject_Repr(raw);
4677 if (name == NULL) {
4678 Py_DECREF(missing_names);
4679 return;
4680 }
4681 PyList_SET_ITEM(missing_names, j++, name);
4682 }
4683 }
4684 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004685 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004686 Py_DECREF(missing_names);
4687}
4688
4689static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004690too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004691 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004692 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004693{
4694 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004695 Py_ssize_t kwonly_given = 0;
4696 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004697 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004698 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004699
Benjamin Petersone109c702011-06-24 09:37:26 -05004700 assert((co->co_flags & CO_VARARGS) == 0);
4701 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004702 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004703 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004704 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004705 }
4706 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004707 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004708 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004709 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004710 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004711 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004712 }
4713 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004714 plural = (co_argcount != 1);
4715 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004716 }
4717 if (sig == NULL)
4718 return;
4719 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004720 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4721 kwonly_sig = PyUnicode_FromFormat(format,
4722 given != 1 ? "s" : "",
4723 kwonly_given,
4724 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004725 if (kwonly_sig == NULL) {
4726 Py_DECREF(sig);
4727 return;
4728 }
4729 }
4730 else {
4731 /* This will not fail. */
4732 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004733 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004734 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004735 _PyErr_Format(tstate, PyExc_TypeError,
4736 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004737 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 sig,
4739 plural ? "s" : "",
4740 given,
4741 kwonly_sig,
4742 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004743 Py_DECREF(sig);
4744 Py_DECREF(kwonly_sig);
4745}
4746
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004747static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004748positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004749 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004750 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004751{
4752 int posonly_conflicts = 0;
4753 PyObject* posonly_names = PyList_New(0);
4754
4755 for(int k=0; k < co->co_posonlyargcount; k++){
4756 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4757
4758 for (int k2=0; k2<kwcount; k2++){
4759 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004760 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004761 if (kwname == posonly_name){
4762 if(PyList_Append(posonly_names, kwname) != 0) {
4763 goto fail;
4764 }
4765 posonly_conflicts++;
4766 continue;
4767 }
4768
4769 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4770
4771 if ( cmp > 0) {
4772 if(PyList_Append(posonly_names, kwname) != 0) {
4773 goto fail;
4774 }
4775 posonly_conflicts++;
4776 } else if (cmp < 0) {
4777 goto fail;
4778 }
4779
4780 }
4781 }
4782 if (posonly_conflicts) {
4783 PyObject* comma = PyUnicode_FromString(", ");
4784 if (comma == NULL) {
4785 goto fail;
4786 }
4787 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4788 Py_DECREF(comma);
4789 if (error_names == NULL) {
4790 goto fail;
4791 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004792 _PyErr_Format(tstate, PyExc_TypeError,
4793 "%U() got some positional-only arguments passed"
4794 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004795 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004796 Py_DECREF(error_names);
4797 goto fail;
4798 }
4799
4800 Py_DECREF(posonly_names);
4801 return 0;
4802
4803fail:
4804 Py_XDECREF(posonly_names);
4805 return 1;
4806
4807}
4808
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004809
Mark Shannon0332e562021-02-01 10:42:03 +00004810PyFrameObject *
4811_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004812 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004813 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004814 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004815{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004816 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004817
Mark Shannond6c33fb2021-01-29 13:24:55 +00004818 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4819 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004820 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004821
Victor Stinnerc7020012016-08-16 23:40:29 +02004822 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004823 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004824 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004826 }
Victor Stinner232dda62020-06-04 15:19:02 +02004827 PyObject **fastlocals = f->f_localsplus;
4828 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004829
Victor Stinnerc7020012016-08-16 23:40:29 +02004830 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004831 PyObject *kwdict;
4832 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004833 if (co->co_flags & CO_VARKEYWORDS) {
4834 kwdict = PyDict_New();
4835 if (kwdict == NULL)
4836 goto fail;
4837 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004838 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004839 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004840 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004841 SETLOCAL(i, kwdict);
4842 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004843 else {
4844 kwdict = NULL;
4845 }
4846
Pablo Galindocd74e662019-06-01 18:08:04 +01004847 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004848 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004849 if (argcount > co->co_argcount) {
4850 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004851 }
4852 else {
4853 n = argcount;
4854 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004855 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004856 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004857 Py_INCREF(x);
4858 SETLOCAL(j, x);
4859 }
4860
Victor Stinnerc7020012016-08-16 23:40:29 +02004861 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004862 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004863 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004864 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004865 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004866 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004867 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004868 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004869
Mark Shannon0332e562021-02-01 10:42:03 +00004870 /* Handle keyword arguments */
4871 if (kwnames != NULL) {
4872 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4873 for (i = 0; i < kwcount; i++) {
4874 PyObject **co_varnames;
4875 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4876 PyObject *value = args[i+argcount];
4877 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004878
Mark Shannon0332e562021-02-01 10:42:03 +00004879 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4880 _PyErr_Format(tstate, PyExc_TypeError,
4881 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004882 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004884 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004885
Mark Shannon0332e562021-02-01 10:42:03 +00004886 /* Speed hack: do raw pointer compares. As names are
4887 normally interned this should almost always hit. */
4888 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4889 for (j = co->co_posonlyargcount; j < total_args; j++) {
4890 PyObject *varname = co_varnames[j];
4891 if (varname == keyword) {
4892 goto kw_found;
4893 }
4894 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004895
Mark Shannon0332e562021-02-01 10:42:03 +00004896 /* Slow fallback, just in case */
4897 for (j = co->co_posonlyargcount; j < total_args; j++) {
4898 PyObject *varname = co_varnames[j];
4899 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4900 if (cmp > 0) {
4901 goto kw_found;
4902 }
4903 else if (cmp < 0) {
4904 goto fail;
4905 }
4906 }
4907
4908 assert(j >= total_args);
4909 if (kwdict == NULL) {
4910
4911 if (co->co_posonlyargcount
4912 && positional_only_passed_as_keyword(tstate, co,
4913 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004914 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004915 {
4916 goto fail;
4917 }
4918
4919 _PyErr_Format(tstate, PyExc_TypeError,
4920 "%U() got an unexpected keyword argument '%S'",
4921 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004922 goto fail;
4923 }
4924
Mark Shannon0332e562021-02-01 10:42:03 +00004925 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4926 goto fail;
4927 }
4928 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004929
Mark Shannon0332e562021-02-01 10:42:03 +00004930 kw_found:
4931 if (GETLOCAL(j) != NULL) {
4932 _PyErr_Format(tstate, PyExc_TypeError,
4933 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004934 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004935 goto fail;
4936 }
4937 Py_INCREF(value);
4938 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004941
4942 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004943 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004944 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4945 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 goto fail;
4947 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004948
4949 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004950 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004951 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004952 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004953 Py_ssize_t missing = 0;
4954 for (i = argcount; i < m; i++) {
4955 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004956 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004957 }
4958 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004959 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004960 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004961 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004962 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004963 }
4964 if (n > m)
4965 i = n - m;
4966 else
4967 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004968 if (defcount) {
4969 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4970 for (; i < defcount; i++) {
4971 if (GETLOCAL(m+i) == NULL) {
4972 PyObject *def = defs[i];
4973 Py_INCREF(def);
4974 SETLOCAL(m+i, def);
4975 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004976 }
4977 }
4978 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004979
4980 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004981 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004982 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004983 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004984 if (GETLOCAL(i) != NULL)
4985 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004986 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004987 if (con->fc_kwdefaults != NULL) {
4988 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004989 if (def) {
4990 Py_INCREF(def);
4991 SETLOCAL(i, def);
4992 continue;
4993 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004994 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004995 goto fail;
4996 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004997 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004998 missing++;
4999 }
5000 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005001 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005002 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005003 goto fail;
5004 }
5005 }
5006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005008 vars into frame. */
5009 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005011 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005012 /* Possibly account for the cell variable being an argument. */
5013 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005014 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005015 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005016 /* Clear the local copy. */
5017 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005018 }
5019 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005020 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005021 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005022 if (c == NULL)
5023 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005024 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005026
5027 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005028 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005029 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005030 Py_INCREF(o);
5031 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005033
Mark Shannon0332e562021-02-01 10:42:03 +00005034 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005035
Thomas Woutersce272b62007-09-19 21:19:28 +00005036fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 /* decref'ing the frame can cause __del__ methods to get invoked,
5039 which can call back into Python. While we're done with the
5040 current Python frame (f), the associated C stack is still in use,
5041 so recursion_depth must be boosted for the duration.
5042 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005043 if (Py_REFCNT(f) > 1) {
5044 Py_DECREF(f);
5045 _PyObject_GC_TRACK(f);
5046 }
5047 else {
5048 ++tstate->recursion_depth;
5049 Py_DECREF(f);
5050 --tstate->recursion_depth;
5051 }
Mark Shannon0332e562021-02-01 10:42:03 +00005052 return NULL;
5053}
5054
5055static PyObject *
5056make_coro(PyFrameConstructor *con, PyFrameObject *f)
5057{
5058 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5059 PyObject *gen;
5060 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5061
5062 /* Don't need to keep the reference to f_back, it will be set
5063 * when the generator is resumed. */
5064 Py_CLEAR(f->f_back);
5065
5066 /* Create a new generator that owns the ready to run frame
5067 * and return that as the value. */
5068 if (is_coro) {
5069 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5070 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5071 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5072 } else {
5073 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5074 }
5075 if (gen == NULL) {
5076 return NULL;
5077 }
5078
5079 _PyObject_GC_TRACK(f);
5080
5081 return gen;
5082}
5083
5084PyObject *
5085_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5086 PyObject *locals,
5087 PyObject* const* args, size_t argcount,
5088 PyObject *kwnames)
5089{
5090 PyFrameObject *f = _PyEval_MakeFrameVector(
5091 tstate, con, locals, args, argcount, kwnames);
5092 if (f == NULL) {
5093 return NULL;
5094 }
5095 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5096 return make_coro(con, f);
5097 }
5098 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5099
5100 /* decref'ing the frame can cause __del__ methods to get invoked,
5101 which can call back into Python. While we're done with the
5102 current Python frame (f), the associated C stack is still in use,
5103 so recursion_depth must be boosted for the duration.
5104 */
5105 if (Py_REFCNT(f) > 1) {
5106 Py_DECREF(f);
5107 _PyObject_GC_TRACK(f);
5108 }
5109 else {
5110 ++tstate->recursion_depth;
5111 Py_DECREF(f);
5112 --tstate->recursion_depth;
5113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005115}
5116
Mark Shannond6c33fb2021-01-29 13:24:55 +00005117/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005118PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005119PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5120 PyObject *const *args, int argcount,
5121 PyObject *const *kws, int kwcount,
5122 PyObject *const *defs, int defcount,
5123 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005124{
Victor Stinner46496f92021-02-20 15:17:18 +01005125 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005126 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005127 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5128 if (defaults == NULL) {
5129 return NULL;
5130 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005131 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005132 if (builtins == NULL) {
5133 Py_DECREF(defaults);
5134 return NULL;
5135 }
Mark Shannon0332e562021-02-01 10:42:03 +00005136 if (locals == NULL) {
5137 locals = globals;
5138 }
5139 PyObject *kwnames;
5140 PyObject *const *allargs;
5141 PyObject **newargs;
5142 if (kwcount == 0) {
5143 allargs = args;
5144 kwnames = NULL;
5145 }
5146 else {
5147 kwnames = PyTuple_New(kwcount);
5148 if (kwnames == NULL) {
5149 res = NULL;
5150 goto fail;
5151 }
5152 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5153 if (newargs == NULL) {
5154 res = NULL;
5155 Py_DECREF(kwnames);
5156 goto fail;
5157 }
5158 for (int i = 0; i < argcount; i++) {
5159 newargs[i] = args[i];
5160 }
5161 for (int i = 0; i < kwcount; i++) {
5162 Py_INCREF(kws[2*i]);
5163 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5164 newargs[argcount+i] = kws[2*i+1];
5165 }
5166 allargs = newargs;
5167 }
5168 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5169 if (kwargs == NULL) {
5170 res = NULL;
5171 Py_DECREF(kwnames);
5172 goto fail;
5173 }
5174 for (int i = 0; i < kwcount; i++) {
5175 Py_INCREF(kws[2*i]);
5176 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5177 kwargs[i] = kws[2*i+1];
5178 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005179 PyFrameConstructor constr = {
5180 .fc_globals = globals,
5181 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005182 .fc_name = ((PyCodeObject *)_co)->co_name,
5183 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005184 .fc_code = _co,
5185 .fc_defaults = defaults,
5186 .fc_kwdefaults = kwdefs,
5187 .fc_closure = closure
5188 };
Mark Shannon0332e562021-02-01 10:42:03 +00005189 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005190 allargs, argcount,
5191 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005192 if (kwcount) {
5193 Py_DECREF(kwnames);
5194 PyMem_Free(newargs);
5195 }
5196fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005197 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005198 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005199}
5200
Tim Peters5ca576e2001-06-18 22:08:13 +00005201
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005202static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005203special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005206 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005207 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005208 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 return NULL;
5210 }
5211 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005212}
5213
5214
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005215/* Logic for the raise statement (too complicated for inlining).
5216 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005217static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005218do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 if (exc == NULL) {
5223 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005224 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005226 type = exc_info->exc_type;
5227 value = exc_info->exc_value;
5228 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005229 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005230 _PyErr_SetString(tstate, PyExc_RuntimeError,
5231 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005232 return 0;
5233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 Py_XINCREF(type);
5235 Py_XINCREF(value);
5236 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005237 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005238 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 /* We support the following forms of raise:
5242 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005243 raise <instance>
5244 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 if (PyExceptionClass_Check(exc)) {
5247 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005248 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 if (value == NULL)
5250 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005251 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005252 _PyErr_Format(tstate, PyExc_TypeError,
5253 "calling %R should have returned an instance of "
5254 "BaseException, not %R",
5255 type, Py_TYPE(value));
5256 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 }
5259 else if (PyExceptionInstance_Check(exc)) {
5260 value = exc;
5261 type = PyExceptionInstance_Class(exc);
5262 Py_INCREF(type);
5263 }
5264 else {
5265 /* Not something you can raise. You get an exception
5266 anyway, just not what you specified :-) */
5267 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005268 _PyErr_SetString(tstate, PyExc_TypeError,
5269 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 goto raise_error;
5271 }
Collin Winter828f04a2007-08-31 00:04:24 +00005272
Serhiy Storchakac0191582016-09-27 11:37:10 +03005273 assert(type != NULL);
5274 assert(value != NULL);
5275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 if (cause) {
5277 PyObject *fixed_cause;
5278 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005279 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 if (fixed_cause == NULL)
5281 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005282 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005284 else if (PyExceptionInstance_Check(cause)) {
5285 fixed_cause = cause;
5286 }
5287 else if (cause == Py_None) {
5288 Py_DECREF(cause);
5289 fixed_cause = NULL;
5290 }
5291 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005292 _PyErr_SetString(tstate, PyExc_TypeError,
5293 "exception causes must derive from "
5294 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 goto raise_error;
5296 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005297 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 }
Collin Winter828f04a2007-08-31 00:04:24 +00005299
Victor Stinner438a12d2019-05-24 17:01:38 +02005300 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005301 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005302 Py_DECREF(value);
5303 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005304 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005305
5306raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 Py_XDECREF(value);
5308 Py_XDECREF(type);
5309 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005310 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005311}
5312
Tim Petersd6d010b2001-06-21 02:49:55 +00005313/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005314 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005315
Guido van Rossum0368b722007-05-11 16:50:42 +00005316 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5317 with a variable target.
5318*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005319
Barry Warsawe42b18f1997-08-25 22:13:04 +00005320static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005321unpack_iterable(PyThreadState *tstate, PyObject *v,
5322 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 int i = 0, j = 0;
5325 Py_ssize_t ll = 0;
5326 PyObject *it; /* iter(v) */
5327 PyObject *w;
5328 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005333 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005334 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005335 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005336 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005337 _PyErr_Format(tstate, PyExc_TypeError,
5338 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005339 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005340 }
5341 return 0;
5342 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 for (; i < argcnt; i++) {
5345 w = PyIter_Next(it);
5346 if (w == NULL) {
5347 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005348 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005349 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005350 _PyErr_Format(tstate, PyExc_ValueError,
5351 "not enough values to unpack "
5352 "(expected %d, got %d)",
5353 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005354 }
5355 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 _PyErr_Format(tstate, PyExc_ValueError,
5357 "not enough values to unpack "
5358 "(expected at least %d, got %d)",
5359 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 }
5362 goto Error;
5363 }
5364 *--sp = w;
5365 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 if (argcntafter == -1) {
5368 /* We better have exhausted the iterator now. */
5369 w = PyIter_Next(it);
5370 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005371 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 goto Error;
5373 Py_DECREF(it);
5374 return 1;
5375 }
5376 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005377 _PyErr_Format(tstate, PyExc_ValueError,
5378 "too many values to unpack (expected %d)",
5379 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 goto Error;
5381 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 l = PySequence_List(it);
5384 if (l == NULL)
5385 goto Error;
5386 *--sp = l;
5387 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 ll = PyList_GET_SIZE(l);
5390 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005391 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005392 "not enough values to unpack (expected at least %d, got %zd)",
5393 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 goto Error;
5395 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 /* Pop the "after-variable" args off the list. */
5398 for (j = argcntafter; j > 0; j--, i++) {
5399 *--sp = PyList_GET_ITEM(l, ll - j);
5400 }
5401 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005402 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 Py_DECREF(it);
5404 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005405
Tim Petersd6d010b2001-06-21 02:49:55 +00005406Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 for (; i > 0; i--, sp++)
5408 Py_DECREF(*sp);
5409 Py_XDECREF(it);
5410 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005411}
5412
5413
Guido van Rossum96a42c81992-01-12 02:29:51 +00005414#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005415static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005416prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005419 if (PyObject_Print(v, stdout, 0) != 0) {
5420 /* Don't know what else to do */
5421 _PyErr_Clear(tstate);
5422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 printf("\n");
5424 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005425}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005426#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005427
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005428static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005429call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005430 PyThreadState *tstate,
5431 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005432 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005433{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005434 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005436 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 if (value == NULL) {
5438 value = Py_None;
5439 Py_INCREF(value);
5440 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005441 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005442 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 arg = PyTuple_Pack(3, type, value, traceback);
5444 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 return;
5447 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005448 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005450 if (err == 0) {
5451 _PyErr_Restore(tstate, type, value, orig_traceback);
5452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 else {
5454 Py_XDECREF(type);
5455 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005456 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005457 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005458}
5459
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005460static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005461call_trace_protected(Py_tracefunc func, PyObject *obj,
5462 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005463 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005466 PyObject *type, *value, *traceback;
5467 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005468 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005469 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 if (err == 0)
5471 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005472 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 return 0;
5474 }
5475 else {
5476 Py_XDECREF(type);
5477 Py_XDECREF(value);
5478 Py_XDECREF(traceback);
5479 return -1;
5480 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005481}
5482
Mark Shannon8e1b4062021-03-05 14:45:50 +00005483static void
5484initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5485{
5486 if (trace_info->code != frame->f_code) {
5487 trace_info->code = frame->f_code;
5488 trace_info->instr_prev = -1;
5489 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5490 }
5491}
5492
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005493static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005494call_trace(Py_tracefunc func, PyObject *obj,
5495 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005496 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005497 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005499 int result;
5500 if (tstate->tracing)
5501 return 0;
5502 tstate->tracing++;
5503 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005504 if (frame->f_lasti < 0) {
5505 frame->f_lineno = frame->f_code->co_firstlineno;
5506 }
5507 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005508 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005509 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005510 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005511 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005512 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005513 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5514 || (tstate->c_profilefunc != NULL));
5515 tstate->tracing--;
5516 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005517}
5518
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005519PyObject *
5520_PyEval_CallTracing(PyObject *func, PyObject *args)
5521{
Victor Stinner50b48572018-11-01 01:51:40 +01005522 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 int save_tracing = tstate->tracing;
5524 int save_use_tracing = tstate->use_tracing;
5525 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005527 tstate->tracing = 0;
5528 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5529 || (tstate->c_profilefunc != NULL));
5530 result = PyObject_Call(func, args, NULL);
5531 tstate->tracing = save_tracing;
5532 tstate->use_tracing = save_use_tracing;
5533 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005534}
5535
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005536/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005537static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005538maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005539 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005540 PyTraceInfo *trace_info)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005543
Nick Coghlan5a851672017-09-08 10:14:16 +10005544 /* If the last instruction falls at the start of a line or if it
5545 represents a jump backwards, update the frame's line number and
5546 then call the trace function if we're tracing source lines.
5547 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005548 initialize_trace_info(trace_info, frame);
5549 int lastline = trace_info->bounds.ar_line;
Mark Shannonfcb55c02021-04-01 16:00:31 +01005550 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005551 if (line != -1 && frame->f_trace_lines) {
5552 /* Trace backward edges or first instruction of a new line */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005553 if (frame->f_lasti < trace_info->instr_prev ||
Mark Shannonfcb55c02021-04-01 16:00:31 +01005554 (line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
Mark Shannonee9f98d2021-01-05 12:04:10 +00005555 {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005556 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005557 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 }
George King20faa682017-10-18 17:44:22 -07005559 /* Always emit an opcode event if we're tracing all opcodes. */
5560 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005561 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005562 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005563 trace_info->instr_prev = frame->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005565}
5566
Victor Stinner309d7cc2020-03-13 16:39:12 +01005567int
5568_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5569{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005570 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005571 /* The caller must hold the GIL */
5572 assert(PyGILState_Check());
5573
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005574 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005575 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005576 PyThreadState *current_tstate = _PyThreadState_GET();
5577 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005578 return -1;
5579 }
5580
5581 PyObject *profileobj = tstate->c_profileobj;
5582
5583 tstate->c_profilefunc = NULL;
5584 tstate->c_profileobj = NULL;
5585 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5586 tstate->use_tracing = tstate->c_tracefunc != NULL;
5587 Py_XDECREF(profileobj);
5588
5589 Py_XINCREF(arg);
5590 tstate->c_profileobj = arg;
5591 tstate->c_profilefunc = func;
5592
5593 /* Flag that tracing or profiling is turned on */
5594 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5595 return 0;
5596}
5597
Fred Drake5755ce62001-06-27 19:19:46 +00005598void
5599PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005600{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005601 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005602 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005603 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005604 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5605 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005606}
5607
5608int
5609_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5610{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005611 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005612 /* The caller must hold the GIL */
5613 assert(PyGILState_Check());
5614
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005615 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005616 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005617 PyThreadState *current_tstate = _PyThreadState_GET();
5618 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005619 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005620 }
5621
Victor Stinnerda2914d2020-03-20 09:29:08 +01005622 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005623 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005624 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005625
5626 tstate->c_tracefunc = NULL;
5627 tstate->c_traceobj = NULL;
5628 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5629 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5630 Py_XDECREF(traceobj);
5631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005633 tstate->c_traceobj = arg;
5634 tstate->c_tracefunc = func;
5635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005636 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005637 tstate->use_tracing = ((func != NULL)
5638 || (tstate->c_profilefunc != NULL));
5639
5640 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005641}
5642
5643void
5644PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5645{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005646 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005647 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005648 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005649 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5650 }
Fred Draked0838392001-06-16 21:02:31 +00005651}
5652
Victor Stinner309d7cc2020-03-13 16:39:12 +01005653
Yury Selivanov75445082015-05-11 22:57:16 -04005654void
Victor Stinner838f2642019-06-13 22:41:23 +02005655_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005656{
5657 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005658 tstate->coroutine_origin_tracking_depth = new_depth;
5659}
5660
5661int
5662_PyEval_GetCoroutineOriginTrackingDepth(void)
5663{
Victor Stinner50b48572018-11-01 01:51:40 +01005664 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005665 return tstate->coroutine_origin_tracking_depth;
5666}
5667
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005668int
Yury Selivanoveb636452016-09-08 22:01:51 -07005669_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5670{
Victor Stinner50b48572018-11-01 01:51:40 +01005671 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005672
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005673 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005674 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005675 }
5676
Yury Selivanoveb636452016-09-08 22:01:51 -07005677 Py_XINCREF(firstiter);
5678 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005679 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005680}
5681
5682PyObject *
5683_PyEval_GetAsyncGenFirstiter(void)
5684{
Victor Stinner50b48572018-11-01 01:51:40 +01005685 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005686 return tstate->async_gen_firstiter;
5687}
5688
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005689int
Yury Selivanoveb636452016-09-08 22:01:51 -07005690_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5691{
Victor Stinner50b48572018-11-01 01:51:40 +01005692 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005693
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005694 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005695 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005696 }
5697
Yury Selivanoveb636452016-09-08 22:01:51 -07005698 Py_XINCREF(finalizer);
5699 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005700 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005701}
5702
5703PyObject *
5704_PyEval_GetAsyncGenFinalizer(void)
5705{
Victor Stinner50b48572018-11-01 01:51:40 +01005706 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005707 return tstate->async_gen_finalizer;
5708}
5709
Victor Stinner438a12d2019-05-24 17:01:38 +02005710PyFrameObject *
5711PyEval_GetFrame(void)
5712{
5713 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005714 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005715}
5716
Guido van Rossumb209a111997-04-29 18:18:01 +00005717PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005718_PyEval_GetBuiltins(PyThreadState *tstate)
5719{
5720 PyFrameObject *frame = tstate->frame;
5721 if (frame != NULL) {
5722 return frame->f_builtins;
5723 }
5724 return tstate->interp->builtins;
5725}
5726
5727PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005728PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005729{
Victor Stinner438a12d2019-05-24 17:01:38 +02005730 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005731 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005732}
5733
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005734/* Convenience function to get a builtin from its name */
5735PyObject *
5736_PyEval_GetBuiltinId(_Py_Identifier *name)
5737{
Victor Stinner438a12d2019-05-24 17:01:38 +02005738 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005739 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5740 if (attr) {
5741 Py_INCREF(attr);
5742 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005743 else if (!_PyErr_Occurred(tstate)) {
5744 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005745 }
5746 return attr;
5747}
5748
Guido van Rossumb209a111997-04-29 18:18:01 +00005749PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005750PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005751{
Victor Stinner438a12d2019-05-24 17:01:38 +02005752 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005753 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005754 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005755 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005756 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005757 }
5758
Victor Stinner438a12d2019-05-24 17:01:38 +02005759 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005760 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005761 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005762
5763 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005764 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005765}
5766
Guido van Rossumb209a111997-04-29 18:18:01 +00005767PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005768PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005769{
Victor Stinner438a12d2019-05-24 17:01:38 +02005770 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005771 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005772 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005773 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005774 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005775
5776 assert(current_frame->f_globals != NULL);
5777 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005778}
5779
Guido van Rossum6135a871995-01-09 17:53:26 +00005780int
Tim Peters5ba58662001-07-16 02:29:45 +00005781PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005782{
Victor Stinner438a12d2019-05-24 17:01:38 +02005783 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005784 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005785 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005787 if (current_frame != NULL) {
5788 const int codeflags = current_frame->f_code->co_flags;
5789 const int compilerflags = codeflags & PyCF_MASK;
5790 if (compilerflags) {
5791 result = 1;
5792 cf->cf_flags |= compilerflags;
5793 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005794#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005795 if (codeflags & CO_GENERATOR_ALLOWED) {
5796 result = 1;
5797 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5798 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005800 }
5801 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005802}
5803
Guido van Rossum3f5da241990-12-20 15:06:42 +00005804
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005805const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005806PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005808 if (PyMethod_Check(func))
5809 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5810 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005811 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005812 else if (PyCFunction_Check(func))
5813 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5814 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005815 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005816}
5817
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005818const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005819PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005821 if (PyMethod_Check(func))
5822 return "()";
5823 else if (PyFunction_Check(func))
5824 return "()";
5825 else if (PyCFunction_Check(func))
5826 return "()";
5827 else
5828 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005829}
5830
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005831#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005832if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005833 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005834 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005835 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005836 x = NULL; \
5837 } \
5838 else { \
5839 x = call; \
5840 if (tstate->c_profilefunc != NULL) { \
5841 if (x == NULL) { \
5842 call_trace_protected(tstate->c_profilefunc, \
5843 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005844 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005845 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005846 /* XXX should pass (type, value, tb) */ \
5847 } else { \
5848 if (call_trace(tstate->c_profilefunc, \
5849 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005850 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005851 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 Py_DECREF(x); \
5853 x = NULL; \
5854 } \
5855 } \
5856 } \
5857 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005858} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005859 x = call; \
5860 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005861
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005862
5863static PyObject *
5864trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005865 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005866 PyObject *func,
5867 PyObject **args, Py_ssize_t nargs,
5868 PyObject *kwnames)
5869{
5870 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005871 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005872 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005873 return x;
5874 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005875 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005876 /* We need to create a temporary bound method as argument
5877 for profiling.
5878
5879 If nargs == 0, then this cannot work because we have no
5880 "self". In any case, the call itself would raise
5881 TypeError (foo needs an argument), so we just skip
5882 profiling. */
5883 PyObject *self = args[0];
5884 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5885 if (func == NULL) {
5886 return NULL;
5887 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005888 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005889 args+1, nargs-1,
5890 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005891 Py_DECREF(func);
5892 return x;
5893 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005894 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005895}
5896
Victor Stinner415c5102017-01-11 00:54:57 +01005897/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5898 to reduce the stack consumption. */
5899Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005900call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005901 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005902 PyObject ***pp_stack,
5903 Py_ssize_t oparg,
5904 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005905{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005906 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005907 PyObject *func = *pfunc;
5908 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005909 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5910 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005911 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005912
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005913 if (tstate->use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005914 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005915 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005916 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005917 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005918 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005919
Victor Stinner438a12d2019-05-24 17:01:38 +02005920 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005921
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005922 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923 while ((*pp_stack) > pfunc) {
5924 w = EXT_POP(*pp_stack);
5925 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005926 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005928 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005929}
5930
Jeremy Hylton52820442001-01-03 23:52:36 +00005931static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005932do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005933 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005934 PyObject *func,
5935 PyObject *callargs,
5936 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005937{
jdemeyere89de732018-09-19 12:06:20 +02005938 PyObject *result;
5939
scoder4c9ea092020-05-12 16:12:41 +02005940 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005941 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005942 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005943 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005944 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005945 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5946 if (nargs > 0 && tstate->use_tracing) {
5947 /* We need to create a temporary bound method as argument
5948 for profiling.
5949
5950 If nargs == 0, then this cannot work because we have no
5951 "self". In any case, the call itself would raise
5952 TypeError (foo needs an argument), so we just skip
5953 profiling. */
5954 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5955 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5956 if (func == NULL) {
5957 return NULL;
5958 }
5959
Victor Stinner4d231bc2019-11-14 13:36:21 +01005960 C_TRACE(result, _PyObject_FastCallDictTstate(
5961 tstate, func,
5962 &_PyTuple_ITEMS(callargs)[1],
5963 nargs - 1,
5964 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005965 Py_DECREF(func);
5966 return result;
5967 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005968 }
jdemeyere89de732018-09-19 12:06:20 +02005969 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005970}
5971
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005972/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005973 nb_index slot defined, and store in *pi.
5974 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005975 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005976 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005977*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005978int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005979_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005980{
Victor Stinner438a12d2019-05-24 17:01:38 +02005981 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005982 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005983 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005984 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005985 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005986 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005987 return 0;
5988 }
5989 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005990 _PyErr_SetString(tstate, PyExc_TypeError,
5991 "slice indices must be integers or "
5992 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005993 return 0;
5994 }
5995 *pi = x;
5996 }
5997 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005998}
5999
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006000int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006001_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006002{
Victor Stinner438a12d2019-05-24 17:01:38 +02006003 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006004 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02006005 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006006 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006007 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006008 return 0;
6009 }
6010 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006011 _PyErr_SetString(tstate, PyExc_TypeError,
6012 "slice indices must be integers or "
6013 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006014 return 0;
6015 }
6016 *pi = x;
6017 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006018}
6019
Thomas Wouters52152252000-08-17 22:55:00 +00006020static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006021import_name(PyThreadState *tstate, PyFrameObject *f,
6022 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006023{
6024 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006025 PyObject *import_func, *res;
6026 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006027
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006028 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006029 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006030 if (!_PyErr_Occurred(tstate)) {
6031 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006032 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006033 return NULL;
6034 }
6035
6036 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006037 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006038 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006039 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006040 return NULL;
6041 }
6042 res = PyImport_ImportModuleLevelObject(
6043 name,
6044 f->f_globals,
6045 f->f_locals == NULL ? Py_None : f->f_locals,
6046 fromlist,
6047 ilevel);
6048 return res;
6049 }
6050
6051 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006052
6053 stack[0] = name;
6054 stack[1] = f->f_globals;
6055 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6056 stack[3] = fromlist;
6057 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006058 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006059 Py_DECREF(import_func);
6060 return res;
6061}
6062
6063static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006064import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006066 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006067 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006068
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006069 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006070 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006071 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006072 /* Issue #17636: in case this failed because of a circular relative
6073 import, try to fallback on reading the module directly from
6074 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006075 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006076 if (pkgname == NULL) {
6077 goto error;
6078 }
Oren Milman6db70332017-09-19 14:23:01 +03006079 if (!PyUnicode_Check(pkgname)) {
6080 Py_CLEAR(pkgname);
6081 goto error;
6082 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006083 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006084 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006085 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006086 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006087 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006088 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006089 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006090 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006091 goto error;
6092 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006093 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006094 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006095 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006096 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006097 if (pkgname == NULL) {
6098 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6099 if (pkgname_or_unknown == NULL) {
6100 Py_XDECREF(pkgpath);
6101 return NULL;
6102 }
6103 } else {
6104 pkgname_or_unknown = pkgname;
6105 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006106
6107 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006108 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006109 errmsg = PyUnicode_FromFormat(
6110 "cannot import name %R from %R (unknown location)",
6111 name, pkgname_or_unknown
6112 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006113 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006114 PyErr_SetImportError(errmsg, pkgname, NULL);
6115 }
6116 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006117 _Py_IDENTIFIER(__spec__);
6118 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006119 const char *fmt =
6120 _PyModuleSpec_IsInitializing(spec) ?
6121 "cannot import name %R from partially initialized module %R "
6122 "(most likely due to a circular import) (%S)" :
6123 "cannot import name %R from %R (%S)";
6124 Py_XDECREF(spec);
6125
6126 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006127 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006128 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006129 }
6130
Xiang Zhang4830f582017-03-21 11:13:42 +08006131 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006132 Py_XDECREF(pkgname_or_unknown);
6133 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006134 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006135}
Guido van Rossumac7be682001-01-17 15:42:30 +00006136
Thomas Wouters52152252000-08-17 22:55:00 +00006137static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006138import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006139{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006140 _Py_IDENTIFIER(__all__);
6141 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006142 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006143 int skip_leading_underscores = 0;
6144 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006145
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006146 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6147 return -1; /* Unexpected error */
6148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006149 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006150 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6151 return -1;
6152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006153 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006154 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006155 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006156 return -1;
6157 }
6158 all = PyMapping_Keys(dict);
6159 Py_DECREF(dict);
6160 if (all == NULL)
6161 return -1;
6162 skip_leading_underscores = 1;
6163 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006165 for (pos = 0, err = 0; ; pos++) {
6166 name = PySequence_GetItem(all, pos);
6167 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006168 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006170 }
6171 else {
6172 _PyErr_Clear(tstate);
6173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006174 break;
6175 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006176 if (!PyUnicode_Check(name)) {
6177 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6178 if (modname == NULL) {
6179 Py_DECREF(name);
6180 err = -1;
6181 break;
6182 }
6183 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006184 _PyErr_Format(tstate, PyExc_TypeError,
6185 "module __name__ must be a string, not %.100s",
6186 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006187 }
6188 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006189 _PyErr_Format(tstate, PyExc_TypeError,
6190 "%s in %U.%s must be str, not %.100s",
6191 skip_leading_underscores ? "Key" : "Item",
6192 modname,
6193 skip_leading_underscores ? "__dict__" : "__all__",
6194 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006195 }
6196 Py_DECREF(modname);
6197 Py_DECREF(name);
6198 err = -1;
6199 break;
6200 }
6201 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006202 if (PyUnicode_READY(name) == -1) {
6203 Py_DECREF(name);
6204 err = -1;
6205 break;
6206 }
6207 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6208 Py_DECREF(name);
6209 continue;
6210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006211 }
6212 value = PyObject_GetAttr(v, name);
6213 if (value == NULL)
6214 err = -1;
6215 else if (PyDict_CheckExact(locals))
6216 err = PyDict_SetItem(locals, name, value);
6217 else
6218 err = PyObject_SetItem(locals, name, value);
6219 Py_DECREF(name);
6220 Py_XDECREF(value);
6221 if (err != 0)
6222 break;
6223 }
6224 Py_DECREF(all);
6225 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006226}
6227
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006228static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006229check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006230{
Victor Stinnera102ed72020-02-07 02:24:48 +01006231 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006232 /* check_args_iterable() may be called with a live exception:
6233 * clear it to prevent calling _PyObject_FunctionStr() with an
6234 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006235 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006236 PyObject *funcstr = _PyObject_FunctionStr(func);
6237 if (funcstr != NULL) {
6238 _PyErr_Format(tstate, PyExc_TypeError,
6239 "%U argument after * must be an iterable, not %.200s",
6240 funcstr, Py_TYPE(args)->tp_name);
6241 Py_DECREF(funcstr);
6242 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006243 return -1;
6244 }
6245 return 0;
6246}
6247
6248static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006249format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006250{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006251 /* _PyDict_MergeEx raises attribute
6252 * error (percolated from an attempt
6253 * to get 'keys' attribute) instead of
6254 * a type error if its second argument
6255 * is not a mapping.
6256 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006257 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006258 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006259 PyObject *funcstr = _PyObject_FunctionStr(func);
6260 if (funcstr != NULL) {
6261 _PyErr_Format(
6262 tstate, PyExc_TypeError,
6263 "%U argument after ** must be a mapping, not %.200s",
6264 funcstr, Py_TYPE(kwargs)->tp_name);
6265 Py_DECREF(funcstr);
6266 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006267 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006268 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006269 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006270 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006271 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006272 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006273 PyObject *funcstr = _PyObject_FunctionStr(func);
6274 if (funcstr != NULL) {
6275 PyObject *key = PyTuple_GET_ITEM(val, 0);
6276 _PyErr_Format(
6277 tstate, PyExc_TypeError,
6278 "%U got multiple values for keyword argument '%S'",
6279 funcstr, key);
6280 Py_DECREF(funcstr);
6281 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006282 Py_XDECREF(exc);
6283 Py_XDECREF(val);
6284 Py_XDECREF(tb);
6285 }
6286 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006287 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006288 }
6289 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006290}
6291
Guido van Rossumac7be682001-01-17 15:42:30 +00006292static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006293format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6294 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006296 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006298 if (!obj)
6299 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006300
Serhiy Storchaka06515832016-11-20 09:13:07 +02006301 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006302 if (!obj_str)
6303 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006304
Victor Stinner438a12d2019-05-24 17:01:38 +02006305 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006306}
Guido van Rossum950361c1997-01-24 13:49:28 +00006307
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006308static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006309format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006310{
6311 PyObject *name;
6312 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006313 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006314 return;
6315 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6316 name = PyTuple_GET_ITEM(co->co_cellvars,
6317 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006318 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006319 PyExc_UnboundLocalError,
6320 UNBOUNDLOCAL_ERROR_MSG,
6321 name);
6322 } else {
6323 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6324 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006325 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006326 UNBOUNDFREE_ERROR_MSG, name);
6327 }
6328}
6329
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006330static void
Mark Shannonfee55262019-11-21 09:11:43 +00006331format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006332{
6333 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6334 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006335 _PyErr_Format(tstate, PyExc_TypeError,
6336 "'async with' received an object from __aenter__ "
6337 "that does not implement __await__: %.100s",
6338 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006339 }
Mark Shannonfee55262019-11-21 09:11:43 +00006340 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006341 _PyErr_Format(tstate, PyExc_TypeError,
6342 "'async with' received an object from __aexit__ "
6343 "that does not implement __await__: %.100s",
6344 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006345 }
6346 }
6347}
6348
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006349static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006350unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006351 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006352{
6353 PyObject *res;
6354 if (Py_REFCNT(v) == 2) {
6355 /* In the common case, there are 2 references to the value
6356 * stored in 'variable' when the += is performed: one on the
6357 * value stack (in 'v') and one still stored in the
6358 * 'variable'. We try to delete the variable now to reduce
6359 * the refcnt to 1.
6360 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006361 int opcode, oparg;
6362 NEXTOPARG();
6363 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006364 case STORE_FAST:
6365 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006366 PyObject **fastlocals = f->f_localsplus;
6367 if (GETLOCAL(oparg) == v)
6368 SETLOCAL(oparg, NULL);
6369 break;
6370 }
6371 case STORE_DEREF:
6372 {
6373 PyObject **freevars = (f->f_localsplus +
6374 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006375 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006376 if (PyCell_GET(c) == v) {
6377 PyCell_SET(c, NULL);
6378 Py_DECREF(v);
6379 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006380 break;
6381 }
6382 case STORE_NAME:
6383 {
6384 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006385 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006386 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006387 if (locals && PyDict_CheckExact(locals)) {
6388 PyObject *w = PyDict_GetItemWithError(locals, name);
6389 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006390 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006391 {
6392 Py_DECREF(v);
6393 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006394 }
6395 }
6396 break;
6397 }
6398 }
6399 }
6400 res = v;
6401 PyUnicode_Append(&res, w);
6402 return res;
6403}
6404
Guido van Rossum950361c1997-01-24 13:49:28 +00006405#ifdef DYNAMIC_EXECUTION_PROFILE
6406
Skip Montanarof118cb12001-10-15 20:51:38 +00006407static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006408getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006410 int i;
6411 PyObject *l = PyList_New(256);
6412 if (l == NULL) return NULL;
6413 for (i = 0; i < 256; i++) {
6414 PyObject *x = PyLong_FromLong(a[i]);
6415 if (x == NULL) {
6416 Py_DECREF(l);
6417 return NULL;
6418 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006419 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 }
6421 for (i = 0; i < 256; i++)
6422 a[i] = 0;
6423 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006424}
6425
6426PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006427_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006428{
6429#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006430 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006431#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006432 int i;
6433 PyObject *l = PyList_New(257);
6434 if (l == NULL) return NULL;
6435 for (i = 0; i < 257; i++) {
6436 PyObject *x = getarray(dxpairs[i]);
6437 if (x == NULL) {
6438 Py_DECREF(l);
6439 return NULL;
6440 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006441 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006442 }
6443 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006444#endif
6445}
6446
6447#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006448
6449Py_ssize_t
6450_PyEval_RequestCodeExtraIndex(freefunc free)
6451{
Victor Stinner81a7be32020-04-14 15:14:01 +02006452 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006453 Py_ssize_t new_index;
6454
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006455 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006456 return -1;
6457 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006458 new_index = interp->co_extra_user_count++;
6459 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006460 return new_index;
6461}
Łukasz Langaa785c872016-09-09 17:37:37 -07006462
6463static void
6464dtrace_function_entry(PyFrameObject *f)
6465{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006466 const char *filename;
6467 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006468 int lineno;
6469
Victor Stinner6d86a232020-04-29 00:56:58 +02006470 PyCodeObject *code = f->f_code;
6471 filename = PyUnicode_AsUTF8(code->co_filename);
6472 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006473 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006474
Andy Lestere6be9b52020-02-11 20:28:35 -06006475 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006476}
6477
6478static void
6479dtrace_function_return(PyFrameObject *f)
6480{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006481 const char *filename;
6482 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006483 int lineno;
6484
Victor Stinner6d86a232020-04-29 00:56:58 +02006485 PyCodeObject *code = f->f_code;
6486 filename = PyUnicode_AsUTF8(code->co_filename);
6487 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006488 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006489
Andy Lestere6be9b52020-02-11 20:28:35 -06006490 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006491}
6492
6493/* DTrace equivalent of maybe_call_line_trace. */
6494static void
6495maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00006496 PyTraceInfo *trace_info)
Łukasz Langaa785c872016-09-09 17:37:37 -07006497{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006498 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006499
6500 /* If the last instruction executed isn't in the current
6501 instruction window, reset the window.
6502 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006503 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006504 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006505 /* If the last instruction falls at the start of a line or if
6506 it represents a jump backwards, update the frame's line
6507 number and call the trace function. */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006508 if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006509 if (line != -1) {
6510 frame->f_lineno = line;
6511 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6512 if (!co_filename)
6513 co_filename = "?";
6514 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6515 if (!co_name)
6516 co_name = "?";
6517 PyDTrace_LINE(co_filename, co_name, line);
6518 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006519 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00006520 trace_info->instr_prev = frame->f_lasti;
Łukasz Langaa785c872016-09-09 17:37:37 -07006521}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006522
6523
6524/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6525 for the limited API. */
6526
6527#undef Py_EnterRecursiveCall
6528
6529int Py_EnterRecursiveCall(const char *where)
6530{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006531 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006532}
6533
6534#undef Py_LeaveRecursiveCall
6535
6536void Py_LeaveRecursiveCall(void)
6537{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006538 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006539}