blob: d9a754fb9107b04e7977e4ed5845d8a23988b377 [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) {
Brandt Bucher145bf262021-02-26 14:51:55 -08001032 if (!PyTuple_CheckExact(match_args)) {
Brandt Bucherf84d5a12021-04-05 19:17:08 -07001033 const char *e = "%s.__match_args__ must be a tuple (got %s)";
Brandt Bucher145bf262021-02-26 14:51:55 -08001034 _PyErr_Format(tstate, PyExc_TypeError, e,
1035 ((PyTypeObject *)type)->tp_name,
1036 Py_TYPE(match_args)->tp_name);
1037 goto fail;
1038 }
1039 }
1040 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1041 _PyErr_Clear(tstate);
1042 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1043 // define __match_args__. This is natural behavior for subclasses:
1044 // it's as if __match_args__ is some "magic" value that is lost as
1045 // soon as they redefine it.
1046 match_args = PyTuple_New(0);
1047 match_self = PyType_HasFeature((PyTypeObject*)type,
1048 _Py_TPFLAGS_MATCH_SELF);
1049 }
1050 else {
1051 goto fail;
1052 }
1053 assert(PyTuple_CheckExact(match_args));
1054 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1055 if (allowed < nargs) {
1056 const char *plural = (allowed == 1) ? "" : "s";
1057 _PyErr_Format(tstate, PyExc_TypeError,
1058 "%s() accepts %d positional sub-pattern%s (%d given)",
1059 ((PyTypeObject*)type)->tp_name,
1060 allowed, plural, nargs);
1061 goto fail;
1062 }
1063 if (match_self) {
1064 // Easy. Copy the subject itself, and move on to kwargs.
1065 PyList_Append(attrs, subject);
1066 }
1067 else {
1068 for (Py_ssize_t i = 0; i < nargs; i++) {
1069 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1070 if (!PyUnicode_CheckExact(name)) {
1071 _PyErr_Format(tstate, PyExc_TypeError,
1072 "__match_args__ elements must be strings "
1073 "(got %s)", Py_TYPE(name)->tp_name);
1074 goto fail;
1075 }
1076 PyObject *attr = match_class_attr(tstate, subject, type, name,
1077 seen);
1078 if (attr == NULL) {
1079 goto fail;
1080 }
1081 PyList_Append(attrs, attr);
1082 Py_DECREF(attr);
1083 }
1084 }
1085 Py_CLEAR(match_args);
1086 }
1087 // Finally, the keyword subpatterns:
1088 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1089 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1090 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1091 if (attr == NULL) {
1092 goto fail;
1093 }
1094 PyList_Append(attrs, attr);
1095 Py_DECREF(attr);
1096 }
1097 Py_SETREF(attrs, PyList_AsTuple(attrs));
1098 Py_DECREF(seen);
1099 return attrs;
1100fail:
1101 // We really don't care whether an error was raised or not... that's our
1102 // caller's problem. All we know is that the match failed.
1103 Py_XDECREF(match_args);
1104 Py_DECREF(seen);
1105 Py_DECREF(attrs);
1106 return NULL;
1107}
1108
1109
Victor Stinner09532fe2019-05-10 23:39:09 +02001110static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001111static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001112
Victor Stinnere225beb2019-06-03 18:14:24 +02001113#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001114
Guido van Rossum374a9221991-04-04 10:40:29 +00001115
Guido van Rossumb209a111997-04-29 18:18:01 +00001116PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001117PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001118{
Victor Stinner46496f92021-02-20 15:17:18 +01001119 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001120 if (locals == NULL) {
1121 locals = globals;
1122 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01001123 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannon0332e562021-02-01 10:42:03 +00001124 if (builtins == NULL) {
1125 return NULL;
1126 }
1127 PyFrameConstructor desc = {
1128 .fc_globals = globals,
1129 .fc_builtins = builtins,
1130 .fc_name = ((PyCodeObject *)co)->co_name,
1131 .fc_qualname = ((PyCodeObject *)co)->co_name,
1132 .fc_code = co,
1133 .fc_defaults = NULL,
1134 .fc_kwdefaults = NULL,
1135 .fc_closure = NULL
1136 };
Victor Stinner46496f92021-02-20 15:17:18 +01001137 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001138}
1139
1140
1141/* Interpreter main loop */
1142
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001143PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001144PyEval_EvalFrame(PyFrameObject *f)
1145{
Victor Stinner0b72b232020-03-12 23:18:39 +01001146 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001147 PyThreadState *tstate = _PyThreadState_GET();
1148 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001149}
1150
1151PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001153{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001154 PyThreadState *tstate = _PyThreadState_GET();
1155 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001156}
1157
Victor Stinnerda2914d2020-03-20 09:29:08 +01001158
1159/* Handle signals, pending calls, GIL drop request
1160 and asynchronous exception */
1161static int
1162eval_frame_handle_pending(PyThreadState *tstate)
1163{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001164 _PyRuntimeState * const runtime = &_PyRuntime;
1165 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001166
1167 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001168 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001169 if (handle_signals(tstate) != 0) {
1170 return -1;
1171 }
1172 }
1173
1174 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001175 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001176 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001177 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001178 return -1;
1179 }
1180 }
1181
1182 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001183 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001184 /* Give another thread a chance */
1185 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1186 Py_FatalError("tstate mix-up");
1187 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001188 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001189
1190 /* Other threads may run now */
1191
1192 take_gil(tstate);
1193
Victor Stinnere838a932020-05-05 19:56:48 +02001194#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1195 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1196#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001197 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1198 Py_FatalError("orphan tstate");
1199 }
Victor Stinnere838a932020-05-05 19:56:48 +02001200#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001201 }
1202
1203 /* Check for asynchronous exception. */
1204 if (tstate->async_exc != NULL) {
1205 PyObject *exc = tstate->async_exc;
1206 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001207 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001208 _PyErr_SetNone(tstate, exc);
1209 Py_DECREF(exc);
1210 return -1;
1211 }
1212
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001213#ifdef MS_WINDOWS
1214 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1215 // different thread than the Python thread, in which case
1216 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1217 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1218 // value. It prevents to interrupt the eval loop at every instruction if
1219 // the current Python thread cannot handle signals (if
1220 // _Py_ThreadCanHandleSignals() is false).
1221 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1222#endif
1223
Victor Stinnerda2914d2020-03-20 09:29:08 +01001224 return 0;
1225}
1226
Victor Stinner3c1e4812012-03-26 22:10:51 +02001227
Antoine Pitroub52ec782009-01-25 16:34:23 +00001228/* Computed GOTOs, or
1229 the-optimization-commonly-but-improperly-known-as-"threaded code"
1230 using gcc's labels-as-values extension
1231 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1232
1233 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001235 combined with a lookup table of jump addresses. However, since the
1236 indirect jump instruction is shared by all opcodes, the CPU will have a
1237 hard time making the right prediction for where to jump next (actually,
1238 it will be always wrong except in the uncommon case of a sequence of
1239 several identical opcodes).
1240
1241 "Threaded code" in contrast, uses an explicit jump table and an explicit
1242 indirect jump instruction at the end of each opcode. Since the jump
1243 instruction is at a different address for each opcode, the CPU will make a
1244 separate prediction for each of these instructions, which is equivalent to
1245 predicting the second opcode of each opcode pair. These predictions have
1246 a much better chance to turn out valid, especially in small bytecode loops.
1247
1248 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001250 and potentially many more instructions (depending on the pipeline width).
1251 A correctly predicted branch, however, is nearly free.
1252
1253 At the time of this writing, the "threaded code" version is up to 15-20%
1254 faster than the normal "switch" version, depending on the compiler and the
1255 CPU architecture.
1256
1257 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1258 because it would render the measurements invalid.
1259
1260
1261 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1262 indirect jumps by sharing them between all opcodes. Such optimizations
1263 can be disabled on gcc by using the -fno-gcse flag (or possibly
1264 -fno-crossjumping).
1265*/
1266
Antoine Pitrou042b1282010-08-13 21:15:58 +00001267#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001268#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001269#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001270#endif
1271
Antoine Pitrou042b1282010-08-13 21:15:58 +00001272#ifdef HAVE_COMPUTED_GOTOS
1273 #ifndef USE_COMPUTED_GOTOS
1274 #define USE_COMPUTED_GOTOS 1
1275 #endif
1276#else
1277 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1278 #error "Computed gotos are not supported on this compiler."
1279 #endif
1280 #undef USE_COMPUTED_GOTOS
1281 #define USE_COMPUTED_GOTOS 0
1282#endif
1283
1284#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001285#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001286 op: \
1287 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001288
Antoine Pitroub52ec782009-01-25 16:34:23 +00001289#ifdef LLTRACE
Mark Shannon4958f5d2021-03-24 17:56:12 +00001290#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001292 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001294 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001295 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 } \
1297 goto fast_next_opcode; \
1298 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299#else
Mark Shannon4958f5d2021-03-24 17:56:12 +00001300#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001302 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001304 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001305 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 } \
1307 goto fast_next_opcode; \
1308 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001309#endif
1310
1311#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001312#define TARGET(op) op
Mark Shannon4958f5d2021-03-24 17:56:12 +00001313#define DISPATCH() goto fast_next_opcode
1314
Antoine Pitroub52ec782009-01-25 16:34:23 +00001315#endif
1316
Mark Shannon4958f5d2021-03-24 17:56:12 +00001317#define CHECK_EVAL_BREAKER() \
1318 if (_Py_atomic_load_relaxed(eval_breaker)) { \
1319 continue; \
1320 }
1321
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322
Neal Norwitza81d2202002-07-14 00:27:26 +00001323/* Tuple access macros */
1324
1325#ifndef Py_DEBUG
1326#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1327#else
1328#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1329#endif
1330
Guido van Rossum374a9221991-04-04 10:40:29 +00001331/* Code access macros */
1332
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001333/* The integer overflow is checked by an assertion below. */
Mark Shannonfcb55c02021-04-01 16:00:31 +01001334#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001335#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001336 _Py_CODEUNIT word = *next_instr; \
1337 opcode = _Py_OPCODE(word); \
1338 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001339 next_instr++; \
1340 } while (0)
Mark Shannonfcb55c02021-04-01 16:00:31 +01001341#define JUMPTO(x) (next_instr = first_instr + (x))
1342#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001343
Raymond Hettingerf606f872003-03-16 03:11:04 +00001344/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 Some opcodes tend to come in pairs thus making it possible to
1346 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001347 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 Verifying the prediction costs a single high-speed test of a register
1350 variable against a constant. If the pairing was good, then the
1351 processor's own internal branch predication has a high likelihood of
1352 success, resulting in a nearly zero-overhead transition to the
1353 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001354 including its unpredictable switch-case branch. Combined with the
1355 processor's internal branch prediction, a successful PREDICT has the
1356 effect of making the two opcodes run as if they were a single new opcode
1357 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001358
Georg Brandl86b2fb92008-07-16 03:43:04 +00001359 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 predictions turned-on and interpret the results as if some opcodes
1361 had been combined or turn-off predictions so that the opcode frequency
1362 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001363
1364 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 the CPU to record separate branch prediction information for each
1366 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001367
Raymond Hettingerf606f872003-03-16 03:11:04 +00001368*/
1369
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001370#define PREDICT_ID(op) PRED_##op
1371
Antoine Pitrou042b1282010-08-13 21:15:58 +00001372#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001373#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001374#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001375#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001376 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001377 _Py_CODEUNIT word = *next_instr; \
1378 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001379 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001380 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001381 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001382 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001383 } \
1384 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001385#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001386#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001387
Raymond Hettingerf606f872003-03-16 03:11:04 +00001388
Guido van Rossum374a9221991-04-04 10:40:29 +00001389/* Stack manipulation macros */
1390
Martin v. Löwis18e16552006-02-15 17:27:45 +00001391/* The stack can grow at most MAXINT deep, as co_nlocals and
1392 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001393#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1394#define EMPTY() (STACK_LEVEL() == 0)
1395#define TOP() (stack_pointer[-1])
1396#define SECOND() (stack_pointer[-2])
1397#define THIRD() (stack_pointer[-3])
1398#define FOURTH() (stack_pointer[-4])
1399#define PEEK(n) (stack_pointer[-(n)])
1400#define SET_TOP(v) (stack_pointer[-1] = (v))
1401#define SET_SECOND(v) (stack_pointer[-2] = (v))
1402#define SET_THIRD(v) (stack_pointer[-3] = (v))
1403#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001404#define BASIC_STACKADJ(n) (stack_pointer += n)
1405#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1406#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001407
Guido van Rossum96a42c81992-01-12 02:29:51 +00001408#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001410 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001411 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001412#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001413 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001414#define STACK_GROW(n) do { \
1415 assert(n >= 0); \
1416 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001417 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001418 assert(STACK_LEVEL() <= co->co_stacksize); \
1419 } while (0)
1420#define STACK_SHRINK(n) do { \
1421 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001422 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001423 (void)(BASIC_STACKADJ(-n)); \
1424 assert(STACK_LEVEL() <= co->co_stacksize); \
1425 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001426#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001427 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001428 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001429#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001430#define PUSH(v) BASIC_PUSH(v)
1431#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001432#define STACK_GROW(n) BASIC_STACKADJ(n)
1433#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001434#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001435#endif
1436
Guido van Rossum681d79a1995-07-18 14:51:37 +00001437/* Local variable macros */
1438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001440
1441/* The SETLOCAL() macro must not DECREF the local variable in-place and
1442 then store the new value; it must copy the old value to a temporary
1443 value, then store the new value, and then DECREF the temporary value.
1444 This is because it is possible that during the DECREF the frame is
1445 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1446 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001448 GETLOCAL(i) = value; \
1449 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001450
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001451
1452#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 while (STACK_LEVEL() > (b)->b_level) { \
1454 PyObject *v = POP(); \
1455 Py_XDECREF(v); \
1456 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001457
1458#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001459 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001461 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1463 while (STACK_LEVEL() > (b)->b_level + 3) { \
1464 value = POP(); \
1465 Py_XDECREF(value); \
1466 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001467 exc_info = tstate->exc_info; \
1468 type = exc_info->exc_type; \
1469 value = exc_info->exc_value; \
1470 traceback = exc_info->exc_traceback; \
1471 exc_info->exc_type = POP(); \
1472 exc_info->exc_value = POP(); \
1473 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 Py_XDECREF(type); \
1475 Py_XDECREF(value); \
1476 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001477 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001478
Inada Naoki91234a12019-06-03 21:30:58 +09001479 /* macros for opcode cache */
1480#define OPCACHE_CHECK() \
1481 do { \
1482 co_opcache = NULL; \
1483 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001484 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001485 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001486 if (co_opcache_offset > 0) { \
1487 assert(co_opcache_offset <= co->co_opcache_size); \
1488 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001489 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001490 } \
1491 } \
1492 } while (0)
1493
Pablo Galindo109826c2020-10-20 06:22:44 +01001494#define OPCACHE_DEOPT() \
1495 do { \
1496 if (co_opcache != NULL) { \
1497 co_opcache->optimized = -1; \
1498 unsigned char co_opcache_offset = \
1499 co->co_opcache_map[next_instr - first_instr]; \
1500 assert(co_opcache_offset <= co->co_opcache_size); \
1501 co->co_opcache_map[co_opcache_offset] = 0; \
1502 co_opcache = NULL; \
1503 } \
1504 } while (0)
1505
1506#define OPCACHE_DEOPT_LOAD_ATTR() \
1507 do { \
1508 if (co_opcache != NULL) { \
1509 OPCACHE_STAT_ATTR_DEOPT(); \
1510 OPCACHE_DEOPT(); \
1511 } \
1512 } while (0)
1513
1514#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1515 do { \
1516 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1517 OPCACHE_DEOPT_LOAD_ATTR(); \
1518 } \
1519 } while (0)
1520
Inada Naoki91234a12019-06-03 21:30:58 +09001521#if OPCACHE_STATS
1522
1523#define OPCACHE_STAT_GLOBAL_HIT() \
1524 do { \
1525 if (co->co_opcache != NULL) opcache_global_hits++; \
1526 } while (0)
1527
1528#define OPCACHE_STAT_GLOBAL_MISS() \
1529 do { \
1530 if (co->co_opcache != NULL) opcache_global_misses++; \
1531 } while (0)
1532
1533#define OPCACHE_STAT_GLOBAL_OPT() \
1534 do { \
1535 if (co->co_opcache != NULL) opcache_global_opts++; \
1536 } while (0)
1537
Pablo Galindo109826c2020-10-20 06:22:44 +01001538#define OPCACHE_STAT_ATTR_HIT() \
1539 do { \
1540 if (co->co_opcache != NULL) opcache_attr_hits++; \
1541 } while (0)
1542
1543#define OPCACHE_STAT_ATTR_MISS() \
1544 do { \
1545 if (co->co_opcache != NULL) opcache_attr_misses++; \
1546 } while (0)
1547
1548#define OPCACHE_STAT_ATTR_OPT() \
1549 do { \
1550 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1551 } while (0)
1552
1553#define OPCACHE_STAT_ATTR_DEOPT() \
1554 do { \
1555 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1556 } while (0)
1557
1558#define OPCACHE_STAT_ATTR_TOTAL() \
1559 do { \
1560 if (co->co_opcache != NULL) opcache_attr_total++; \
1561 } while (0)
1562
Inada Naoki91234a12019-06-03 21:30:58 +09001563#else /* OPCACHE_STATS */
1564
1565#define OPCACHE_STAT_GLOBAL_HIT()
1566#define OPCACHE_STAT_GLOBAL_MISS()
1567#define OPCACHE_STAT_GLOBAL_OPT()
1568
Pablo Galindo109826c2020-10-20 06:22:44 +01001569#define OPCACHE_STAT_ATTR_HIT()
1570#define OPCACHE_STAT_ATTR_MISS()
1571#define OPCACHE_STAT_ATTR_OPT()
1572#define OPCACHE_STAT_ATTR_DEOPT()
1573#define OPCACHE_STAT_ATTR_TOTAL()
1574
Inada Naoki91234a12019-06-03 21:30:58 +09001575#endif
1576
Mark Shannond41bddd2021-03-25 12:00:30 +00001577
1578PyObject* _Py_HOT_FUNCTION
1579_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1580{
1581 _Py_EnsureTstateNotNULL(tstate);
1582
1583#if USE_COMPUTED_GOTOS
1584/* Import the static jump table */
1585#include "opcode_targets.h"
1586#endif
1587
1588#ifdef DXPAIRS
1589 int lastopcode = 0;
1590#endif
1591 PyObject **stack_pointer; /* Next free slot in value stack */
1592 const _Py_CODEUNIT *next_instr;
1593 int opcode; /* Current opcode */
1594 int oparg; /* Current opcode argument, if any */
1595 PyObject **fastlocals, **freevars;
1596 PyObject *retval = NULL; /* Return value */
1597 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
1598 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
1599 PyCodeObject *co;
1600
1601 /* when tracing we set things up so that
1602
1603 not (instr_lb <= current_bytecode_offset < instr_ub)
1604
1605 is true when the line being executed has changed. The
1606 initial values are such as to make this false the first
1607 time it is tested. */
1608
1609 const _Py_CODEUNIT *first_instr;
1610 PyObject *names;
1611 PyObject *consts;
1612 _PyOpcache *co_opcache;
1613
1614#ifdef LLTRACE
1615 _Py_IDENTIFIER(__ltrace__);
1616#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001617
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001618 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001620 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001621
Mark Shannon8e1b4062021-03-05 14:45:50 +00001622 PyTraceInfo trace_info;
1623 /* Mark trace_info as initialized */
1624 trace_info.code = NULL;
1625
1626 /* push frame */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001628 co = f->f_code;
Tim Peters5ca576e2001-06-18 22:08:13 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (tstate->use_tracing) {
1631 if (tstate->c_tracefunc != NULL) {
1632 /* tstate->c_tracefunc, if defined, is a
1633 function that will be called on *every* entry
1634 to a code block. Its return value, if not
1635 None, is a function that will be called at
1636 the start of each executed line of code.
1637 (Actually, the function must return itself
1638 in order to continue tracing.) The trace
1639 functions are called with three arguments:
1640 a pointer to the current frame, a string
1641 indicating why the function is called, and
1642 an argument which depends on the situation.
1643 The global trace function is also called
1644 whenever an exception is detected. */
1645 if (call_trace_protected(tstate->c_tracefunc,
1646 tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001647 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001648 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 /* Trace function raised an error */
1650 goto exit_eval_frame;
1651 }
1652 }
1653 if (tstate->c_profilefunc != NULL) {
1654 /* Similar for c_profilefunc, except it needn't
1655 return itself and isn't called for "line" events */
1656 if (call_trace_protected(tstate->c_profilefunc,
1657 tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001658 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001659 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 /* Profile function raised an error */
1661 goto exit_eval_frame;
1662 }
1663 }
1664 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001665
Łukasz Langaa785c872016-09-09 17:37:37 -07001666 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1667 dtrace_function_entry(f);
1668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 names = co->co_names;
1670 consts = co->co_consts;
1671 fastlocals = f->f_localsplus;
1672 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001673 assert(PyBytes_Check(co->co_code));
1674 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001675 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1676 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1677 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001678 /*
1679 f->f_lasti refers to the index of the last instruction,
1680 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001681
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001682 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001683 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 When the PREDICT() macros are enabled, some opcode pairs follow in
1686 direct succession without updating f->f_lasti. A successful
1687 prediction effectively links the two codes together as if they
1688 were a single new opcode; accordingly,f->f_lasti will point to
1689 the first code in the pair (for instance, GET_ITER followed by
1690 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001691 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001693 assert(f->f_lasti >= -1);
Mark Shannonfcb55c02021-04-01 16:00:31 +01001694 next_instr = first_instr + f->f_lasti + 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01001695 stack_pointer = f->f_valuestack + f->f_stackdepth;
1696 /* Set f->f_stackdepth to -1.
1697 * Update when returning or calling trace function.
1698 Having f_stackdepth <= 0 ensures that invalid
1699 values are not visible to the cycle GC.
1700 We choose -1 rather than 0 to assist debugging.
1701 */
1702 f->f_stackdepth = -1;
1703 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001704
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001705 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001706 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001707 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001708 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001709 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001710 }
1711#if OPCACHE_STATS
1712 opcache_code_objects_extra_mem +=
1713 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1714 sizeof(_PyOpcache) * co->co_opcache_size;
1715 opcache_code_objects++;
1716#endif
1717 }
1718 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001719
Tim Peters5ca576e2001-06-18 22:08:13 +00001720#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001721 {
1722 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1723 if (r < 0) {
1724 goto exit_eval_frame;
1725 }
1726 lltrace = r;
1727 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001728#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001730 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001732 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001733
Victor Stinnerace47d72013-07-18 01:41:08 +02001734#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001735 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001736 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001737 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001738 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001739#endif
1740
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001741main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1744 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001745 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 /* Do periodic things. Doing this every time through
1748 the loop would add too much overhead, so we do it
1749 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001750 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 event needs attention (e.g. a signal handler or
1752 async I/O handler); see Py_AddPendingCall() and
1753 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Eric Snow7bda9de2019-03-08 17:25:54 -07001755 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001756 opcode = _Py_OPCODE(*next_instr);
1757 if (opcode == SETUP_FINALLY ||
1758 opcode == SETUP_WITH ||
1759 opcode == BEFORE_ASYNC_WITH ||
1760 opcode == YIELD_FROM) {
1761 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001762 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001763 - If we're about to enter the 'with:'. It will prevent
1764 emitting a resource warning in the common idiom
1765 'with open(path) as file:'.
1766 - If we're about to enter the 'async with:'.
1767 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001768 *very* useful, but might help in some cases and it's
1769 traditional)
1770 - If we're resuming a chain of nested 'yield from' or
1771 'await' calls, then each frame is parked with YIELD_FROM
1772 as its next opcode. If the user hit control-C we want to
1773 wait until we've reached the innermost frame before
1774 running the signal handler and raising KeyboardInterrupt
1775 (see bpo-30039).
1776 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 goto fast_next_opcode;
1778 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001779
Victor Stinnerda2914d2020-03-20 09:29:08 +01001780 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 }
1783 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 fast_next_opcode:
1786 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Łukasz Langaa785c872016-09-09 17:37:37 -07001788 if (PyDTrace_LINE_ENABLED())
Mark Shannon8e1b4062021-03-05 14:45:50 +00001789 maybe_dtrace_line(f, &trace_info);
Łukasz Langaa785c872016-09-09 17:37:37 -07001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001792
Victor Stinnerdab84232020-03-17 18:56:44 +01001793 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001794 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001796 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001798 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 err = maybe_call_line_trace(tstate->c_tracefunc,
1801 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001802 tstate, f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001803 &trace_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 /* Reload possibly changed frame fields */
1805 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001806 stack_pointer = f->f_valuestack+f->f_stackdepth;
1807 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001814
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001815 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001816 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001817#ifdef DYNAMIC_EXECUTION_PROFILE
1818#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 dxpairs[lastopcode][opcode]++;
1820 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001821#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001823#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001824
Guido van Rossum96a42c81992-01-12 02:29:51 +00001825#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 if (lltrace) {
1829 if (HAS_ARG(opcode)) {
1830 printf("%d: %d, %d\n",
1831 f->f_lasti, opcode, oparg);
1832 }
1833 else {
1834 printf("%d: %d\n",
1835 f->f_lasti, opcode);
1836 }
1837 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001838#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001843 It is essential that any operation that fails must goto error
1844 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Benjamin Petersonddd19492018-09-16 22:38:02 -07001846 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001847 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001848 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001849
Benjamin Petersonddd19492018-09-16 22:38:02 -07001850 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 PyObject *value = GETLOCAL(oparg);
1852 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001853 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 UNBOUNDLOCAL_ERROR_MSG,
1855 PyTuple_GetItem(co->co_varnames, oparg));
1856 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 Py_INCREF(value);
1859 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001860 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 }
1862
Benjamin Petersonddd19492018-09-16 22:38:02 -07001863 case TARGET(LOAD_CONST): {
1864 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 PyObject *value = GETITEM(consts, oparg);
1866 Py_INCREF(value);
1867 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001868 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(STORE_FAST): {
1872 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 PyObject *value = POP();
1874 SETLOCAL(oparg, 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(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *value = POP();
1880 Py_DECREF(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *top = TOP();
1886 PyObject *second = SECOND();
1887 SET_TOP(second);
1888 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001889 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *top = TOP();
1894 PyObject *second = SECOND();
1895 PyObject *third = THIRD();
1896 SET_TOP(second);
1897 SET_SECOND(third);
1898 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001899 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Benjamin Petersonddd19492018-09-16 22:38:02 -07001902 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001903 PyObject *top = TOP();
1904 PyObject *second = SECOND();
1905 PyObject *third = THIRD();
1906 PyObject *fourth = FOURTH();
1907 SET_TOP(second);
1908 SET_SECOND(third);
1909 SET_THIRD(fourth);
1910 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001911 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 }
1913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 PyObject *top = TOP();
1916 Py_INCREF(top);
1917 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001924 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001926 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 SET_TOP(top);
1928 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001929 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001931
Benjamin Petersonddd19492018-09-16 22:38:02 -07001932 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 PyObject *value = TOP();
1934 PyObject *res = PyNumber_Positive(value);
1935 Py_DECREF(value);
1936 SET_TOP(res);
1937 if (res == NULL)
1938 goto error;
1939 DISPATCH();
1940 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Benjamin Petersonddd19492018-09-16 22:38:02 -07001942 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 PyObject *value = TOP();
1944 PyObject *res = PyNumber_Negative(value);
1945 Py_DECREF(value);
1946 SET_TOP(res);
1947 if (res == NULL)
1948 goto error;
1949 DISPATCH();
1950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Benjamin Petersonddd19492018-09-16 22:38:02 -07001952 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *value = TOP();
1954 int err = PyObject_IsTrue(value);
1955 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (err == 0) {
1957 Py_INCREF(Py_True);
1958 SET_TOP(Py_True);
1959 DISPATCH();
1960 }
1961 else if (err > 0) {
1962 Py_INCREF(Py_False);
1963 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 DISPATCH();
1965 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001966 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 goto error;
1968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Benjamin Petersonddd19492018-09-16 22:38:02 -07001970 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 PyObject *value = TOP();
1972 PyObject *res = PyNumber_Invert(value);
1973 Py_DECREF(value);
1974 SET_TOP(res);
1975 if (res == NULL)
1976 goto error;
1977 DISPATCH();
1978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Benjamin Petersonddd19492018-09-16 22:38:02 -07001980 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 PyObject *exp = POP();
1982 PyObject *base = TOP();
1983 PyObject *res = PyNumber_Power(base, exp, Py_None);
1984 Py_DECREF(base);
1985 Py_DECREF(exp);
1986 SET_TOP(res);
1987 if (res == NULL)
1988 goto error;
1989 DISPATCH();
1990 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Benjamin Petersonddd19492018-09-16 22:38:02 -07001992 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *right = POP();
1994 PyObject *left = TOP();
1995 PyObject *res = PyNumber_Multiply(left, right);
1996 Py_DECREF(left);
1997 Py_DECREF(right);
1998 SET_TOP(res);
1999 if (res == NULL)
2000 goto error;
2001 DISPATCH();
2002 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002003
Benjamin Petersonddd19492018-09-16 22:38:02 -07002004 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002005 PyObject *right = POP();
2006 PyObject *left = TOP();
2007 PyObject *res = PyNumber_MatrixMultiply(left, right);
2008 Py_DECREF(left);
2009 Py_DECREF(right);
2010 SET_TOP(res);
2011 if (res == NULL)
2012 goto error;
2013 DISPATCH();
2014 }
2015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 PyObject *divisor = POP();
2018 PyObject *dividend = TOP();
2019 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2020 Py_DECREF(dividend);
2021 Py_DECREF(divisor);
2022 SET_TOP(quotient);
2023 if (quotient == NULL)
2024 goto error;
2025 DISPATCH();
2026 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 PyObject *divisor = POP();
2030 PyObject *dividend = TOP();
2031 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2032 Py_DECREF(dividend);
2033 Py_DECREF(divisor);
2034 SET_TOP(quotient);
2035 if (quotient == NULL)
2036 goto error;
2037 DISPATCH();
2038 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002039
Benjamin Petersonddd19492018-09-16 22:38:02 -07002040 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 PyObject *divisor = POP();
2042 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002043 PyObject *res;
2044 if (PyUnicode_CheckExact(dividend) && (
2045 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2046 // fast path; string formatting, but not if the RHS is a str subclass
2047 // (see issue28598)
2048 res = PyUnicode_Format(dividend, divisor);
2049 } else {
2050 res = PyNumber_Remainder(dividend, divisor);
2051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 Py_DECREF(divisor);
2053 Py_DECREF(dividend);
2054 SET_TOP(res);
2055 if (res == NULL)
2056 goto error;
2057 DISPATCH();
2058 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002059
Benjamin Petersonddd19492018-09-16 22:38:02 -07002060 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 PyObject *right = POP();
2062 PyObject *left = TOP();
2063 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002064 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002065 CPython using bytecode, it is simply worthless.
2066 See http://bugs.python.org/issue21955 and
2067 http://bugs.python.org/issue10044 for the discussion. In short,
2068 no patch shown any impact on a realistic benchmark, only a minor
2069 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 if (PyUnicode_CheckExact(left) &&
2071 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002072 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002073 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002074 }
2075 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 sum = PyNumber_Add(left, right);
2077 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 Py_DECREF(right);
2080 SET_TOP(sum);
2081 if (sum == NULL)
2082 goto error;
2083 DISPATCH();
2084 }
2085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 PyObject *right = POP();
2088 PyObject *left = TOP();
2089 PyObject *diff = PyNumber_Subtract(left, right);
2090 Py_DECREF(right);
2091 Py_DECREF(left);
2092 SET_TOP(diff);
2093 if (diff == NULL)
2094 goto error;
2095 DISPATCH();
2096 }
2097
Benjamin Petersonddd19492018-09-16 22:38:02 -07002098 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 PyObject *sub = POP();
2100 PyObject *container = TOP();
2101 PyObject *res = PyObject_GetItem(container, sub);
2102 Py_DECREF(container);
2103 Py_DECREF(sub);
2104 SET_TOP(res);
2105 if (res == NULL)
2106 goto error;
2107 DISPATCH();
2108 }
2109
Benjamin Petersonddd19492018-09-16 22:38:02 -07002110 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 PyObject *right = POP();
2112 PyObject *left = TOP();
2113 PyObject *res = PyNumber_Lshift(left, right);
2114 Py_DECREF(left);
2115 Py_DECREF(right);
2116 SET_TOP(res);
2117 if (res == NULL)
2118 goto error;
2119 DISPATCH();
2120 }
2121
Benjamin Petersonddd19492018-09-16 22:38:02 -07002122 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 PyObject *right = POP();
2124 PyObject *left = TOP();
2125 PyObject *res = PyNumber_Rshift(left, right);
2126 Py_DECREF(left);
2127 Py_DECREF(right);
2128 SET_TOP(res);
2129 if (res == NULL)
2130 goto error;
2131 DISPATCH();
2132 }
2133
Benjamin Petersonddd19492018-09-16 22:38:02 -07002134 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 PyObject *right = POP();
2136 PyObject *left = TOP();
2137 PyObject *res = PyNumber_And(left, right);
2138 Py_DECREF(left);
2139 Py_DECREF(right);
2140 SET_TOP(res);
2141 if (res == NULL)
2142 goto error;
2143 DISPATCH();
2144 }
2145
Benjamin Petersonddd19492018-09-16 22:38:02 -07002146 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 PyObject *right = POP();
2148 PyObject *left = TOP();
2149 PyObject *res = PyNumber_Xor(left, right);
2150 Py_DECREF(left);
2151 Py_DECREF(right);
2152 SET_TOP(res);
2153 if (res == NULL)
2154 goto error;
2155 DISPATCH();
2156 }
2157
Benjamin Petersonddd19492018-09-16 22:38:02 -07002158 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 PyObject *right = POP();
2160 PyObject *left = TOP();
2161 PyObject *res = PyNumber_Or(left, right);
2162 Py_DECREF(left);
2163 Py_DECREF(right);
2164 SET_TOP(res);
2165 if (res == NULL)
2166 goto error;
2167 DISPATCH();
2168 }
2169
Benjamin Petersonddd19492018-09-16 22:38:02 -07002170 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *v = POP();
2172 PyObject *list = PEEK(oparg);
2173 int err;
2174 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 if (err != 0)
2177 goto error;
2178 PREDICT(JUMP_ABSOLUTE);
2179 DISPATCH();
2180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002181
Benjamin Petersonddd19492018-09-16 22:38:02 -07002182 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002184 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 int err;
2186 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 if (err != 0)
2189 goto error;
2190 PREDICT(JUMP_ABSOLUTE);
2191 DISPATCH();
2192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PyObject *exp = POP();
2196 PyObject *base = TOP();
2197 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2198 Py_DECREF(base);
2199 Py_DECREF(exp);
2200 SET_TOP(res);
2201 if (res == NULL)
2202 goto error;
2203 DISPATCH();
2204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002205
Benjamin Petersonddd19492018-09-16 22:38:02 -07002206 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 PyObject *right = POP();
2208 PyObject *left = TOP();
2209 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2210 Py_DECREF(left);
2211 Py_DECREF(right);
2212 SET_TOP(res);
2213 if (res == NULL)
2214 goto error;
2215 DISPATCH();
2216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002219 PyObject *right = POP();
2220 PyObject *left = TOP();
2221 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2222 Py_DECREF(left);
2223 Py_DECREF(right);
2224 SET_TOP(res);
2225 if (res == NULL)
2226 goto error;
2227 DISPATCH();
2228 }
2229
Benjamin Petersonddd19492018-09-16 22:38:02 -07002230 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 PyObject *divisor = POP();
2232 PyObject *dividend = TOP();
2233 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2234 Py_DECREF(dividend);
2235 Py_DECREF(divisor);
2236 SET_TOP(quotient);
2237 if (quotient == NULL)
2238 goto error;
2239 DISPATCH();
2240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 PyObject *divisor = POP();
2244 PyObject *dividend = TOP();
2245 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2246 Py_DECREF(dividend);
2247 Py_DECREF(divisor);
2248 SET_TOP(quotient);
2249 if (quotient == NULL)
2250 goto error;
2251 DISPATCH();
2252 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Benjamin Petersonddd19492018-09-16 22:38:02 -07002254 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 PyObject *right = POP();
2256 PyObject *left = TOP();
2257 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2258 Py_DECREF(left);
2259 Py_DECREF(right);
2260 SET_TOP(mod);
2261 if (mod == NULL)
2262 goto error;
2263 DISPATCH();
2264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Benjamin Petersonddd19492018-09-16 22:38:02 -07002266 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 PyObject *right = POP();
2268 PyObject *left = TOP();
2269 PyObject *sum;
2270 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002271 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002272 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002273 }
2274 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 sum = PyNumber_InPlaceAdd(left, right);
2276 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002277 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 Py_DECREF(right);
2279 SET_TOP(sum);
2280 if (sum == NULL)
2281 goto error;
2282 DISPATCH();
2283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Benjamin Petersonddd19492018-09-16 22:38:02 -07002285 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 PyObject *right = POP();
2287 PyObject *left = TOP();
2288 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2289 Py_DECREF(left);
2290 Py_DECREF(right);
2291 SET_TOP(diff);
2292 if (diff == NULL)
2293 goto error;
2294 DISPATCH();
2295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Benjamin Petersonddd19492018-09-16 22:38:02 -07002297 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PyObject *right = POP();
2299 PyObject *left = TOP();
2300 PyObject *res = PyNumber_InPlaceLshift(left, right);
2301 Py_DECREF(left);
2302 Py_DECREF(right);
2303 SET_TOP(res);
2304 if (res == NULL)
2305 goto error;
2306 DISPATCH();
2307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Benjamin Petersonddd19492018-09-16 22:38:02 -07002309 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 PyObject *right = POP();
2311 PyObject *left = TOP();
2312 PyObject *res = PyNumber_InPlaceRshift(left, right);
2313 Py_DECREF(left);
2314 Py_DECREF(right);
2315 SET_TOP(res);
2316 if (res == NULL)
2317 goto error;
2318 DISPATCH();
2319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Benjamin Petersonddd19492018-09-16 22:38:02 -07002321 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PyObject *right = POP();
2323 PyObject *left = TOP();
2324 PyObject *res = PyNumber_InPlaceAnd(left, right);
2325 Py_DECREF(left);
2326 Py_DECREF(right);
2327 SET_TOP(res);
2328 if (res == NULL)
2329 goto error;
2330 DISPATCH();
2331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Benjamin Petersonddd19492018-09-16 22:38:02 -07002333 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 PyObject *right = POP();
2335 PyObject *left = TOP();
2336 PyObject *res = PyNumber_InPlaceXor(left, right);
2337 Py_DECREF(left);
2338 Py_DECREF(right);
2339 SET_TOP(res);
2340 if (res == NULL)
2341 goto error;
2342 DISPATCH();
2343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002344
Benjamin Petersonddd19492018-09-16 22:38:02 -07002345 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 PyObject *right = POP();
2347 PyObject *left = TOP();
2348 PyObject *res = PyNumber_InPlaceOr(left, right);
2349 Py_DECREF(left);
2350 Py_DECREF(right);
2351 SET_TOP(res);
2352 if (res == NULL)
2353 goto error;
2354 DISPATCH();
2355 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002356
Benjamin Petersonddd19492018-09-16 22:38:02 -07002357 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 PyObject *sub = TOP();
2359 PyObject *container = SECOND();
2360 PyObject *v = THIRD();
2361 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002362 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002363 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 Py_DECREF(container);
2367 Py_DECREF(sub);
2368 if (err != 0)
2369 goto error;
2370 DISPATCH();
2371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 PyObject *sub = TOP();
2375 PyObject *container = SECOND();
2376 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002377 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002378 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 err = PyObject_DelItem(container, sub);
2380 Py_DECREF(container);
2381 Py_DECREF(sub);
2382 if (err != 0)
2383 goto error;
2384 DISPATCH();
2385 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002386
Benjamin Petersonddd19492018-09-16 22:38:02 -07002387 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002388 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002390 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002391 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002393 _PyErr_SetString(tstate, PyExc_RuntimeError,
2394 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 Py_DECREF(value);
2396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002398 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 Py_DECREF(value);
2400 if (res == NULL)
2401 goto error;
2402 Py_DECREF(res);
2403 DISPATCH();
2404 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 switch (oparg) {
2409 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002411 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002414 /* fall through */
2415 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002416 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002417 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 break;
2420 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002421 _PyErr_SetString(tstate, PyExc_SystemError,
2422 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 break;
2424 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 goto error;
2426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002427
Benjamin Petersonddd19492018-09-16 22:38:02 -07002428 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002430 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002431 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002432 f->f_state = FRAME_RETURNED;
2433 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002434 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002438 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002439 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002440 PyObject *obj = TOP();
2441 PyTypeObject *type = Py_TYPE(obj);
2442
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002443 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002444 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002445 }
Yury Selivanov75445082015-05-11 22:57:16 -04002446
2447 if (getter != NULL) {
2448 iter = (*getter)(obj);
2449 Py_DECREF(obj);
2450 if (iter == NULL) {
2451 SET_TOP(NULL);
2452 goto error;
2453 }
2454 }
2455 else {
2456 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002457 _PyErr_Format(tstate, PyExc_TypeError,
2458 "'async for' requires an object with "
2459 "__aiter__ method, got %.100s",
2460 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002461 Py_DECREF(obj);
2462 goto error;
2463 }
2464
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002465 if (Py_TYPE(iter)->tp_as_async == NULL ||
2466 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002467
Yury Selivanov398ff912017-03-02 22:20:00 -05002468 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002469 _PyErr_Format(tstate, PyExc_TypeError,
2470 "'async for' received an object from __aiter__ "
2471 "that does not implement __anext__: %.100s",
2472 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002473 Py_DECREF(iter);
2474 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002475 }
2476
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002477 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002478 DISPATCH();
2479 }
2480
Benjamin Petersonddd19492018-09-16 22:38:02 -07002481 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002482 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002483 PyObject *next_iter = NULL;
2484 PyObject *awaitable = NULL;
2485 PyObject *aiter = TOP();
2486 PyTypeObject *type = Py_TYPE(aiter);
2487
Yury Selivanoveb636452016-09-08 22:01:51 -07002488 if (PyAsyncGen_CheckExact(aiter)) {
2489 awaitable = type->tp_as_async->am_anext(aiter);
2490 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002491 goto error;
2492 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002493 } else {
2494 if (type->tp_as_async != NULL){
2495 getter = type->tp_as_async->am_anext;
2496 }
Yury Selivanov75445082015-05-11 22:57:16 -04002497
Yury Selivanoveb636452016-09-08 22:01:51 -07002498 if (getter != NULL) {
2499 next_iter = (*getter)(aiter);
2500 if (next_iter == NULL) {
2501 goto error;
2502 }
2503 }
2504 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002505 _PyErr_Format(tstate, PyExc_TypeError,
2506 "'async for' requires an iterator with "
2507 "__anext__ method, got %.100s",
2508 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002509 goto error;
2510 }
Yury Selivanov75445082015-05-11 22:57:16 -04002511
Yury Selivanoveb636452016-09-08 22:01:51 -07002512 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2513 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002514 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002515 PyExc_TypeError,
2516 "'async for' received an invalid object "
2517 "from __anext__: %.100s",
2518 Py_TYPE(next_iter)->tp_name);
2519
2520 Py_DECREF(next_iter);
2521 goto error;
2522 } else {
2523 Py_DECREF(next_iter);
2524 }
2525 }
Yury Selivanov75445082015-05-11 22:57:16 -04002526
2527 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002528 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002529 DISPATCH();
2530 }
2531
Benjamin Petersonddd19492018-09-16 22:38:02 -07002532 case TARGET(GET_AWAITABLE): {
2533 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002534 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002535 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002536
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002537 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002538 int opcode_at_minus_3 = 0;
2539 if ((next_instr - first_instr) > 2) {
2540 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2541 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002543 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002544 _Py_OPCODE(next_instr[-2]));
2545 }
2546
Yury Selivanov75445082015-05-11 22:57:16 -04002547 Py_DECREF(iterable);
2548
Yury Selivanovc724bae2016-03-02 11:30:46 -05002549 if (iter != NULL && PyCoro_CheckExact(iter)) {
2550 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2551 if (yf != NULL) {
2552 /* `iter` is a coroutine object that is being
2553 awaited, `yf` is a pointer to the current awaitable
2554 being awaited on. */
2555 Py_DECREF(yf);
2556 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002557 _PyErr_SetString(tstate, PyExc_RuntimeError,
2558 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002559 /* The code below jumps to `error` if `iter` is NULL. */
2560 }
2561 }
2562
Yury Selivanov75445082015-05-11 22:57:16 -04002563 SET_TOP(iter); /* Even if it's NULL */
2564
2565 if (iter == NULL) {
2566 goto error;
2567 }
2568
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002569 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002570 DISPATCH();
2571 }
2572
Benjamin Petersonddd19492018-09-16 22:38:02 -07002573 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002575 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002576 PySendResult gen_status;
2577 if (tstate->c_tracefunc == NULL) {
2578 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002579 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002580 _Py_IDENTIFIER(send);
2581 if (v == Py_None && PyIter_Check(receiver)) {
2582 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002583 }
2584 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002585 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002586 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002587 if (retval == NULL) {
2588 if (tstate->c_tracefunc != NULL
2589 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002590 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002591 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2592 gen_status = PYGEN_RETURN;
2593 }
2594 else {
2595 gen_status = PYGEN_ERROR;
2596 }
2597 }
2598 else {
2599 gen_status = PYGEN_NEXT;
2600 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002601 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002603 if (gen_status == PYGEN_ERROR) {
2604 assert (retval == NULL);
2605 goto error;
2606 }
2607 if (gen_status == PYGEN_RETURN) {
2608 assert (retval != NULL);
2609
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002610 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002611 SET_TOP(retval);
2612 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002614 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002615 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002616 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002617 /* and repeat... */
Mark Shannonfcb55c02021-04-01 16:00:31 +01002618 assert(f->f_lasti > 0);
2619 f->f_lasti -= 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01002620 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002621 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002622 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002627
2628 if (co->co_flags & CO_ASYNC_GENERATOR) {
2629 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2630 Py_DECREF(retval);
2631 if (w == NULL) {
2632 retval = NULL;
2633 goto error;
2634 }
2635 retval = w;
2636 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002637 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002638 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002639 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002641
Benjamin Petersonddd19492018-09-16 22:38:02 -07002642 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002643 PyObject *type, *value, *traceback;
2644 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 PyTryBlock *b = PyFrame_BlockPop(f);
2646 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 _PyErr_SetString(tstate, PyExc_SystemError,
2648 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002651 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2652 STACK_LEVEL() <= (b)->b_level + 4);
2653 exc_info = tstate->exc_info;
2654 type = exc_info->exc_type;
2655 value = exc_info->exc_value;
2656 traceback = exc_info->exc_traceback;
2657 exc_info->exc_type = POP();
2658 exc_info->exc_value = POP();
2659 exc_info->exc_traceback = POP();
2660 Py_XDECREF(type);
2661 Py_XDECREF(value);
2662 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002665
Benjamin Petersonddd19492018-09-16 22:38:02 -07002666 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002667 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002670
Mark Shannonfee55262019-11-21 09:11:43 +00002671 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002672 assert(f->f_iblock > 0);
2673 if (oparg) {
2674 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2675 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002676 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002677 PyObject *val = POP();
2678 PyObject *tb = POP();
2679 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002680 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002681 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002683
Benjamin Petersonddd19492018-09-16 22:38:02 -07002684 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002685 PyObject *exc = POP();
2686 assert(PyExceptionClass_Check(exc));
2687 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2688 PyTryBlock *b = PyFrame_BlockPop(f);
2689 assert(b->b_type == EXCEPT_HANDLER);
2690 Py_DECREF(exc);
2691 UNWIND_EXCEPT_HANDLER(b);
2692 Py_DECREF(POP());
2693 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002694 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002695 }
2696 else {
2697 PyObject *val = POP();
2698 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002699 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002700 goto exception_unwind;
2701 }
2702 }
2703
Zackery Spytzce6a0702019-08-25 03:44:09 -06002704 case TARGET(LOAD_ASSERTION_ERROR): {
2705 PyObject *value = PyExc_AssertionError;
2706 Py_INCREF(value);
2707 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002708 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002709 }
2710
Benjamin Petersonddd19492018-09-16 22:38:02 -07002711 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002712 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002713
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002715 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002716 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002718 if (!_PyErr_Occurred(tstate)) {
2719 _PyErr_SetString(tstate, PyExc_NameError,
2720 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002721 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002723 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002725 }
2726 else {
2727 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2728 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002729 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2731 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002732 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2733 _PyErr_SetString(tstate, PyExc_NameError,
2734 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002739 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002741
Benjamin Petersonddd19492018-09-16 22:38:02 -07002742 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 PyObject *name = GETITEM(names, oparg);
2744 PyObject *v = POP();
2745 PyObject *ns = f->f_locals;
2746 int err;
2747 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002748 _PyErr_Format(tstate, PyExc_SystemError,
2749 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 if (PyDict_CheckExact(ns))
2754 err = PyDict_SetItem(ns, name, v);
2755 else
2756 err = PyObject_SetItem(ns, name, v);
2757 Py_DECREF(v);
2758 if (err != 0)
2759 goto error;
2760 DISPATCH();
2761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 PyObject *name = GETITEM(names, oparg);
2765 PyObject *ns = f->f_locals;
2766 int err;
2767 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002768 _PyErr_Format(tstate, PyExc_SystemError,
2769 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 err = PyObject_DelItem(ns, name);
2773 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002774 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 NAME_ERROR_MSG,
2776 name);
2777 goto error;
2778 }
2779 DISPATCH();
2780 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002781
Benjamin Petersonddd19492018-09-16 22:38:02 -07002782 case TARGET(UNPACK_SEQUENCE): {
2783 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 PyObject *seq = POP(), *item, **items;
2785 if (PyTuple_CheckExact(seq) &&
2786 PyTuple_GET_SIZE(seq) == oparg) {
2787 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 item = items[oparg];
2790 Py_INCREF(item);
2791 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 } else if (PyList_CheckExact(seq) &&
2794 PyList_GET_SIZE(seq) == oparg) {
2795 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 item = items[oparg];
2798 Py_INCREF(item);
2799 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002801 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002803 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 } else {
2805 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 Py_DECREF(seq);
2807 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002810 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002812
Benjamin Petersonddd19492018-09-16 22:38:02 -07002813 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2815 PyObject *seq = POP();
2816
Victor Stinner438a12d2019-05-24 17:01:38 +02002817 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 stack_pointer + totalargs)) {
2819 stack_pointer += totalargs;
2820 } else {
2821 Py_DECREF(seq);
2822 goto error;
2823 }
2824 Py_DECREF(seq);
2825 DISPATCH();
2826 }
2827
Benjamin Petersonddd19492018-09-16 22:38:02 -07002828 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 PyObject *name = GETITEM(names, oparg);
2830 PyObject *owner = TOP();
2831 PyObject *v = SECOND();
2832 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002833 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 Py_DECREF(owner);
2837 if (err != 0)
2838 goto error;
2839 DISPATCH();
2840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002841
Benjamin Petersonddd19492018-09-16 22:38:02 -07002842 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 PyObject *name = GETITEM(names, oparg);
2844 PyObject *owner = POP();
2845 int err;
2846 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2847 Py_DECREF(owner);
2848 if (err != 0)
2849 goto error;
2850 DISPATCH();
2851 }
2852
Benjamin Petersonddd19492018-09-16 22:38:02 -07002853 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 PyObject *name = GETITEM(names, oparg);
2855 PyObject *v = POP();
2856 int err;
2857 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 if (err != 0)
2860 goto error;
2861 DISPATCH();
2862 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002863
Benjamin Petersonddd19492018-09-16 22:38:02 -07002864 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 PyObject *name = GETITEM(names, oparg);
2866 int err;
2867 err = PyDict_DelItem(f->f_globals, name);
2868 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002869 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2870 format_exc_check_arg(tstate, PyExc_NameError,
2871 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002874 }
2875 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002877
Benjamin Petersonddd19492018-09-16 22:38:02 -07002878 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 PyObject *name = GETITEM(names, oparg);
2880 PyObject *locals = f->f_locals;
2881 PyObject *v;
2882 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002883 _PyErr_Format(tstate, PyExc_SystemError,
2884 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002888 v = PyDict_GetItemWithError(locals, name);
2889 if (v != NULL) {
2890 Py_INCREF(v);
2891 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002892 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002893 goto error;
2894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 }
2896 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002898 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002899 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002900 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002901 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 }
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002905 v = PyDict_GetItemWithError(f->f_globals, name);
2906 if (v != NULL) {
2907 Py_INCREF(v);
2908 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002909 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002910 goto error;
2911 }
2912 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002913 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002914 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002916 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002917 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002918 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002920 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002924 }
2925 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 v = PyObject_GetItem(f->f_builtins, name);
2927 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002928 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002929 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002930 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002934 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002941
Benjamin Petersonddd19492018-09-16 22:38:02 -07002942 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002943 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002945 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002946 && PyDict_CheckExact(f->f_builtins))
2947 {
Inada Naoki91234a12019-06-03 21:30:58 +09002948 OPCACHE_CHECK();
2949 if (co_opcache != NULL && co_opcache->optimized > 0) {
2950 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2951
2952 if (lg->globals_ver ==
2953 ((PyDictObject *)f->f_globals)->ma_version_tag
2954 && lg->builtins_ver ==
2955 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2956 {
2957 PyObject *ptr = lg->ptr;
2958 OPCACHE_STAT_GLOBAL_HIT();
2959 assert(ptr != NULL);
2960 Py_INCREF(ptr);
2961 PUSH(ptr);
2962 DISPATCH();
2963 }
2964 }
2965
2966 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002968 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 name);
2970 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002971 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002972 /* _PyDict_LoadGlobal() returns NULL without raising
2973 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002974 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002975 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002976 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 }
Inada Naoki91234a12019-06-03 21:30:58 +09002979
2980 if (co_opcache != NULL) {
2981 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2982
2983 if (co_opcache->optimized == 0) {
2984 /* Wasn't optimized before. */
2985 OPCACHE_STAT_GLOBAL_OPT();
2986 } else {
2987 OPCACHE_STAT_GLOBAL_MISS();
2988 }
2989
2990 co_opcache->optimized = 1;
2991 lg->globals_ver =
2992 ((PyDictObject *)f->f_globals)->ma_version_tag;
2993 lg->builtins_ver =
2994 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2995 lg->ptr = v; /* borrowed */
2996 }
2997
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003000 else {
3001 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003002
3003 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003004 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 v = PyObject_GetItem(f->f_globals, name);
3006 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003007 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003008 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003009 }
3010 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003011
Victor Stinnerb4efc962015-11-20 09:24:02 +01003012 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 v = PyObject_GetItem(f->f_builtins, name);
3014 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003015 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003016 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003017 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003018 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003019 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003021 }
3022 }
3023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003027
Benjamin Petersonddd19492018-09-16 22:38:02 -07003028 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PyObject *v = GETLOCAL(oparg);
3030 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 SETLOCAL(oparg, NULL);
3032 DISPATCH();
3033 }
3034 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003035 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 UNBOUNDLOCAL_ERROR_MSG,
3037 PyTuple_GetItem(co->co_varnames, oparg)
3038 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 goto error;
3040 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003041
Benjamin Petersonddd19492018-09-16 22:38:02 -07003042 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003044 PyObject *oldobj = PyCell_GET(cell);
3045 if (oldobj != NULL) {
3046 PyCell_SET(cell, NULL);
3047 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003048 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003049 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003050 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 goto error;
3052 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003053
Benjamin Petersonddd19492018-09-16 22:38:02 -07003054 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 PyObject *cell = freevars[oparg];
3056 Py_INCREF(cell);
3057 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003060
Benjamin Petersonddd19492018-09-16 22:38:02 -07003061 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003062 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003063 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003064 assert(locals);
3065 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3066 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3067 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3068 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3069 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003070 value = PyDict_GetItemWithError(locals, name);
3071 if (value != NULL) {
3072 Py_INCREF(value);
3073 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003074 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003075 goto error;
3076 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003077 }
3078 else {
3079 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003080 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003081 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003082 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003083 }
3084 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003085 }
3086 }
3087 if (!value) {
3088 PyObject *cell = freevars[oparg];
3089 value = PyCell_GET(cell);
3090 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003091 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003092 goto error;
3093 }
3094 Py_INCREF(value);
3095 }
3096 PUSH(value);
3097 DISPATCH();
3098 }
3099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 PyObject *cell = freevars[oparg];
3102 PyObject *value = PyCell_GET(cell);
3103 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003104 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 Py_INCREF(value);
3108 PUSH(value);
3109 DISPATCH();
3110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003111
Benjamin Petersonddd19492018-09-16 22:38:02 -07003112 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 PyObject *v = POP();
3114 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003115 PyObject *oldobj = PyCell_GET(cell);
3116 PyCell_SET(cell, v);
3117 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 DISPATCH();
3119 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003120
Benjamin Petersonddd19492018-09-16 22:38:02 -07003121 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003122 PyObject *str;
3123 PyObject *empty = PyUnicode_New(0, 0);
3124 if (empty == NULL) {
3125 goto error;
3126 }
3127 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3128 Py_DECREF(empty);
3129 if (str == NULL)
3130 goto error;
3131 while (--oparg >= 0) {
3132 PyObject *item = POP();
3133 Py_DECREF(item);
3134 }
3135 PUSH(str);
3136 DISPATCH();
3137 }
3138
Benjamin Petersonddd19492018-09-16 22:38:02 -07003139 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 PyObject *tup = PyTuple_New(oparg);
3141 if (tup == NULL)
3142 goto error;
3143 while (--oparg >= 0) {
3144 PyObject *item = POP();
3145 PyTuple_SET_ITEM(tup, oparg, item);
3146 }
3147 PUSH(tup);
3148 DISPATCH();
3149 }
3150
Benjamin Petersonddd19492018-09-16 22:38:02 -07003151 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 PyObject *list = PyList_New(oparg);
3153 if (list == NULL)
3154 goto error;
3155 while (--oparg >= 0) {
3156 PyObject *item = POP();
3157 PyList_SET_ITEM(list, oparg, item);
3158 }
3159 PUSH(list);
3160 DISPATCH();
3161 }
3162
Mark Shannon13bc1392020-01-23 09:25:17 +00003163 case TARGET(LIST_TO_TUPLE): {
3164 PyObject *list = POP();
3165 PyObject *tuple = PyList_AsTuple(list);
3166 Py_DECREF(list);
3167 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003168 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003169 }
3170 PUSH(tuple);
3171 DISPATCH();
3172 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003173
Mark Shannon13bc1392020-01-23 09:25:17 +00003174 case TARGET(LIST_EXTEND): {
3175 PyObject *iterable = POP();
3176 PyObject *list = PEEK(oparg);
3177 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3178 if (none_val == NULL) {
3179 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003180 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003181 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003182 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003183 _PyErr_Format(tstate, PyExc_TypeError,
3184 "Value after * must be an iterable, not %.200s",
3185 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003186 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003187 Py_DECREF(iterable);
3188 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003189 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003190 Py_DECREF(none_val);
3191 Py_DECREF(iterable);
3192 DISPATCH();
3193 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003194
Mark Shannon13bc1392020-01-23 09:25:17 +00003195 case TARGET(SET_UPDATE): {
3196 PyObject *iterable = POP();
3197 PyObject *set = PEEK(oparg);
3198 int err = _PySet_Update(set, iterable);
3199 Py_DECREF(iterable);
3200 if (err < 0) {
3201 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003202 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003203 DISPATCH();
3204 }
3205
Benjamin Petersonddd19492018-09-16 22:38:02 -07003206 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 PyObject *set = PySet_New(NULL);
3208 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003209 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 if (set == NULL)
3211 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003212 for (i = oparg; i > 0; i--) {
3213 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 if (err == 0)
3215 err = PySet_Add(set, item);
3216 Py_DECREF(item);
3217 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003218 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 if (err != 0) {
3220 Py_DECREF(set);
3221 goto error;
3222 }
3223 PUSH(set);
3224 DISPATCH();
3225 }
3226
Benjamin Petersonddd19492018-09-16 22:38:02 -07003227 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003228 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3230 if (map == NULL)
3231 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003232 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003233 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003234 PyObject *key = PEEK(2*i);
3235 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003236 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003237 if (err != 0) {
3238 Py_DECREF(map);
3239 goto error;
3240 }
3241 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003242
3243 while (oparg--) {
3244 Py_DECREF(POP());
3245 Py_DECREF(POP());
3246 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 PUSH(map);
3248 DISPATCH();
3249 }
3250
Benjamin Petersonddd19492018-09-16 22:38:02 -07003251 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003252 _Py_IDENTIFIER(__annotations__);
3253 int err;
3254 PyObject *ann_dict;
3255 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003256 _PyErr_Format(tstate, PyExc_SystemError,
3257 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003258 goto error;
3259 }
3260 /* check if __annotations__ in locals()... */
3261 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003262 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003263 &PyId___annotations__);
3264 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003265 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003266 goto error;
3267 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003268 /* ...if not, create a new one */
3269 ann_dict = PyDict_New();
3270 if (ann_dict == NULL) {
3271 goto error;
3272 }
3273 err = _PyDict_SetItemId(f->f_locals,
3274 &PyId___annotations__, ann_dict);
3275 Py_DECREF(ann_dict);
3276 if (err != 0) {
3277 goto error;
3278 }
3279 }
3280 }
3281 else {
3282 /* do the same if locals() is not a dict */
3283 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3284 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003285 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003286 }
3287 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3288 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003289 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003290 goto error;
3291 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003292 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003293 ann_dict = PyDict_New();
3294 if (ann_dict == NULL) {
3295 goto error;
3296 }
3297 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3298 Py_DECREF(ann_dict);
3299 if (err != 0) {
3300 goto error;
3301 }
3302 }
3303 else {
3304 Py_DECREF(ann_dict);
3305 }
3306 }
3307 DISPATCH();
3308 }
3309
Benjamin Petersonddd19492018-09-16 22:38:02 -07003310 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003311 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003312 PyObject *map;
3313 PyObject *keys = TOP();
3314 if (!PyTuple_CheckExact(keys) ||
3315 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003316 _PyErr_SetString(tstate, PyExc_SystemError,
3317 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003318 goto error;
3319 }
3320 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3321 if (map == NULL) {
3322 goto error;
3323 }
3324 for (i = oparg; i > 0; i--) {
3325 int err;
3326 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3327 PyObject *value = PEEK(i + 1);
3328 err = PyDict_SetItem(map, key, value);
3329 if (err != 0) {
3330 Py_DECREF(map);
3331 goto error;
3332 }
3333 }
3334
3335 Py_DECREF(POP());
3336 while (oparg--) {
3337 Py_DECREF(POP());
3338 }
3339 PUSH(map);
3340 DISPATCH();
3341 }
3342
Mark Shannon8a4cd702020-01-27 09:57:45 +00003343 case TARGET(DICT_UPDATE): {
3344 PyObject *update = POP();
3345 PyObject *dict = PEEK(oparg);
3346 if (PyDict_Update(dict, update) < 0) {
3347 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3348 _PyErr_Format(tstate, PyExc_TypeError,
3349 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003350 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003351 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003352 Py_DECREF(update);
3353 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003354 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003355 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003356 DISPATCH();
3357 }
3358
Mark Shannon8a4cd702020-01-27 09:57:45 +00003359 case TARGET(DICT_MERGE): {
3360 PyObject *update = POP();
3361 PyObject *dict = PEEK(oparg);
3362
3363 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3364 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3365 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003366 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003367 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003368 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003369 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003370 DISPATCH();
3371 }
3372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003374 PyObject *value = TOP();
3375 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 PyObject *map;
3377 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003378 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003379 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003381 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 Py_DECREF(value);
3383 Py_DECREF(key);
3384 if (err != 0)
3385 goto error;
3386 PREDICT(JUMP_ABSOLUTE);
3387 DISPATCH();
3388 }
3389
Benjamin Petersonddd19492018-09-16 22:38:02 -07003390 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 PyObject *name = GETITEM(names, oparg);
3392 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003393
3394 PyTypeObject *type = Py_TYPE(owner);
3395 PyObject *res;
3396 PyObject **dictptr;
3397 PyObject *dict;
3398 _PyOpCodeOpt_LoadAttr *la;
3399
3400 OPCACHE_STAT_ATTR_TOTAL();
3401
3402 OPCACHE_CHECK();
3403 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3404 {
3405 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003406 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003407 la = &co_opcache->u.la;
3408 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3409 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003410 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3411 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3412 // so ~offset is strictly < -1 (assuming 2's complement).
3413 if (la->hint < -1) {
3414 // Even faster path -- slot hint.
3415 Py_ssize_t offset = ~la->hint;
3416 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3417 char *addr = (char *)owner + offset;
3418 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003419 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003420 Py_INCREF(res);
3421 SET_TOP(res);
3422 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003423 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003424 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003425 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3426 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003427 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003428 // Fast path for dict.
3429 assert(type->tp_dict != NULL);
3430 assert(type->tp_dictoffset > 0);
3431
3432 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3433 dict = *dictptr;
3434 if (dict != NULL && PyDict_CheckExact(dict)) {
3435 Py_ssize_t hint = la->hint;
3436 Py_INCREF(dict);
3437 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003438 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003439 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003440 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003441 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003442 if (la->hint == hint && hint >= 0) {
3443 // Our hint has helped -- cache hit.
3444 OPCACHE_STAT_ATTR_HIT();
3445 } else {
3446 // The hint we provided didn't work.
3447 // Maybe next time?
3448 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3449 }
3450
3451 Py_INCREF(res);
3452 SET_TOP(res);
3453 Py_DECREF(owner);
3454 Py_DECREF(dict);
3455 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003456 }
3457 else {
3458 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003459 // This attribute can be missing sometimes;
3460 // we don't want to optimize this lookup.
3461 OPCACHE_DEOPT_LOAD_ATTR();
3462 Py_DECREF(dict);
3463 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003464 }
3465 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003466 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3467 OPCACHE_DEOPT_LOAD_ATTR();
3468 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003469 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003470 }
3471 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003472 // The type of the object has either been updated,
3473 // or is different. Maybe it will stabilize?
3474 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3475 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003476 OPCACHE_STAT_ATTR_MISS();
3477 }
3478
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003479 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003480 type->tp_getattro == PyObject_GenericGetAttr)
3481 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003482 if (type->tp_dict == NULL) {
3483 if (PyType_Ready(type) < 0) {
3484 Py_DECREF(owner);
3485 SET_TOP(NULL);
3486 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003487 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003488 }
3489 PyObject *descr = _PyType_Lookup(type, name);
3490 if (descr != NULL) {
3491 // We found an attribute with a data-like descriptor.
3492 PyTypeObject *dtype = Py_TYPE(descr);
3493 if (dtype == &PyMemberDescr_Type) { // It's a slot
3494 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3495 struct PyMemberDef *dmem = member->d_member;
3496 if (dmem->type == T_OBJECT_EX) {
3497 Py_ssize_t offset = dmem->offset;
3498 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003499
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003500 if (co_opcache->optimized == 0) {
3501 // First time we optimize this opcode.
3502 OPCACHE_STAT_ATTR_OPT();
3503 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3504 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3505 }
3506
3507 la = &co_opcache->u.la;
3508 la->type = type;
3509 la->tp_version_tag = type->tp_version_tag;
3510 la->hint = ~offset;
3511
3512 char *addr = (char *)owner + offset;
3513 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003514 if (res != NULL) {
3515 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003516 Py_DECREF(owner);
3517 SET_TOP(res);
3518
Pablo Galindo109826c2020-10-20 06:22:44 +01003519 DISPATCH();
3520 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003521 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003522 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003523 // Else it's a slot of a different type. We don't handle those.
3524 }
3525 // Else it's some other kind of descriptor that we don't handle.
3526 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003527 }
3528 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003529 // We found an instance with a __dict__.
3530 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3531 dict = *dictptr;
3532
3533 if (dict != NULL && PyDict_CheckExact(dict)) {
3534 Py_INCREF(dict);
3535 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003536 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003537 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3538 if (res != NULL) {
3539 Py_INCREF(res);
3540 Py_DECREF(dict);
3541 Py_DECREF(owner);
3542 SET_TOP(res);
3543
3544 if (co_opcache->optimized == 0) {
3545 // First time we optimize this opcode.
3546 OPCACHE_STAT_ATTR_OPT();
3547 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3548 }
3549
3550 la = &co_opcache->u.la;
3551 la->type = type;
3552 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003553 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003554 la->hint = hint;
3555
3556 DISPATCH();
3557 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003558 else {
3559 _PyErr_Clear(tstate);
3560 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003561 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003562 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003563 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003564 OPCACHE_DEOPT_LOAD_ATTR();
3565 }
3566 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003567 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003568 OPCACHE_DEOPT_LOAD_ATTR();
3569 }
3570 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3571 OPCACHE_DEOPT_LOAD_ATTR();
3572 }
3573 }
3574
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003575 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003576 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003577 Py_DECREF(owner);
3578 SET_TOP(res);
3579 if (res == NULL)
3580 goto error;
3581 DISPATCH();
3582 }
3583
Benjamin Petersonddd19492018-09-16 22:38:02 -07003584 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003585 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 PyObject *right = POP();
3587 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003588 PyObject *res = PyObject_RichCompare(left, right, oparg);
3589 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003590 Py_DECREF(left);
3591 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 if (res == NULL)
3593 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 PREDICT(POP_JUMP_IF_FALSE);
3595 PREDICT(POP_JUMP_IF_TRUE);
3596 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003598
Mark Shannon9af0e472020-01-14 10:12:45 +00003599 case TARGET(IS_OP): {
3600 PyObject *right = POP();
3601 PyObject *left = TOP();
3602 int res = (left == right)^oparg;
3603 PyObject *b = res ? Py_True : Py_False;
3604 Py_INCREF(b);
3605 SET_TOP(b);
3606 Py_DECREF(left);
3607 Py_DECREF(right);
3608 PREDICT(POP_JUMP_IF_FALSE);
3609 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003610 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003611 }
3612
3613 case TARGET(CONTAINS_OP): {
3614 PyObject *right = POP();
3615 PyObject *left = POP();
3616 int res = PySequence_Contains(right, left);
3617 Py_DECREF(left);
3618 Py_DECREF(right);
3619 if (res < 0) {
3620 goto error;
3621 }
3622 PyObject *b = (res^oparg) ? Py_True : Py_False;
3623 Py_INCREF(b);
3624 PUSH(b);
3625 PREDICT(POP_JUMP_IF_FALSE);
3626 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003627 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003628 }
3629
3630#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3631 "BaseException is not allowed"
3632
3633 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3634 PyObject *right = POP();
3635 PyObject *left = POP();
3636 if (PyTuple_Check(right)) {
3637 Py_ssize_t i, length;
3638 length = PyTuple_GET_SIZE(right);
3639 for (i = 0; i < length; i++) {
3640 PyObject *exc = PyTuple_GET_ITEM(right, i);
3641 if (!PyExceptionClass_Check(exc)) {
3642 _PyErr_SetString(tstate, PyExc_TypeError,
3643 CANNOT_CATCH_MSG);
3644 Py_DECREF(left);
3645 Py_DECREF(right);
3646 goto error;
3647 }
3648 }
3649 }
3650 else {
3651 if (!PyExceptionClass_Check(right)) {
3652 _PyErr_SetString(tstate, PyExc_TypeError,
3653 CANNOT_CATCH_MSG);
3654 Py_DECREF(left);
3655 Py_DECREF(right);
3656 goto error;
3657 }
3658 }
3659 int res = PyErr_GivenExceptionMatches(left, right);
3660 Py_DECREF(left);
3661 Py_DECREF(right);
3662 if (res > 0) {
3663 /* Exception matches -- Do nothing */;
3664 }
3665 else if (res == 0) {
3666 JUMPTO(oparg);
3667 }
3668 else {
3669 goto error;
3670 }
3671 DISPATCH();
3672 }
3673
Benjamin Petersonddd19492018-09-16 22:38:02 -07003674 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003675 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003676 PyObject *fromlist = POP();
3677 PyObject *level = TOP();
3678 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003679 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003680 Py_DECREF(level);
3681 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003682 SET_TOP(res);
3683 if (res == NULL)
3684 goto error;
3685 DISPATCH();
3686 }
3687
Benjamin Petersonddd19492018-09-16 22:38:02 -07003688 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003689 PyObject *from = POP(), *locals;
3690 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003691 if (PyFrame_FastToLocalsWithError(f) < 0) {
3692 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003693 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003694 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003696 locals = f->f_locals;
3697 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003698 _PyErr_SetString(tstate, PyExc_SystemError,
3699 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003700 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003703 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705 Py_DECREF(from);
3706 if (err != 0)
3707 goto error;
3708 DISPATCH();
3709 }
Guido van Rossum25831651993-05-19 14:50:45 +00003710
Benjamin Petersonddd19492018-09-16 22:38:02 -07003711 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003712 PyObject *name = GETITEM(names, oparg);
3713 PyObject *from = TOP();
3714 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003715 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003716 PUSH(res);
3717 if (res == NULL)
3718 goto error;
3719 DISPATCH();
3720 }
Thomas Wouters52152252000-08-17 22:55:00 +00003721
Benjamin Petersonddd19492018-09-16 22:38:02 -07003722 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003724 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003726
Benjamin Petersonddd19492018-09-16 22:38:02 -07003727 case TARGET(POP_JUMP_IF_FALSE): {
3728 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003729 PyObject *cond = POP();
3730 int err;
3731 if (cond == Py_True) {
3732 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003733 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003735 if (cond == Py_False) {
3736 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003738 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003740 err = PyObject_IsTrue(cond);
3741 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003743 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 else if (err == 0)
3745 JUMPTO(oparg);
3746 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003750
Benjamin Petersonddd19492018-09-16 22:38:02 -07003751 case TARGET(POP_JUMP_IF_TRUE): {
3752 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003753 PyObject *cond = POP();
3754 int err;
3755 if (cond == Py_False) {
3756 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003757 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003759 if (cond == Py_True) {
3760 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003762 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003764 err = PyObject_IsTrue(cond);
3765 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 JUMPTO(oparg);
3768 }
3769 else if (err == 0)
3770 ;
3771 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003772 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003774 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003775
Benjamin Petersonddd19492018-09-16 22:38:02 -07003776 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003777 PyObject *cond = TOP();
3778 int err;
3779 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003780 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003781 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003782 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003784 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003786 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003788 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003790 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003791 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 }
3793 else if (err == 0)
3794 JUMPTO(oparg);
3795 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003799
Benjamin Petersonddd19492018-09-16 22:38:02 -07003800 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 PyObject *cond = TOP();
3802 int err;
3803 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003804 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003805 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003806 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003808 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003810 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003812 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 JUMPTO(oparg);
3815 }
3816 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003817 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003818 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 }
3820 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003821 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003824
Benjamin Petersonddd19492018-09-16 22:38:02 -07003825 case TARGET(JUMP_ABSOLUTE): {
3826 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003828 CHECK_EVAL_BREAKER();
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
Brandt Bucher145bf262021-02-26 14:51:55 -08003832 case TARGET(GET_LEN): {
3833 // PUSH(len(TOS))
3834 Py_ssize_t len_i = PyObject_Length(TOP());
3835 if (len_i < 0) {
3836 goto error;
3837 }
3838 PyObject *len_o = PyLong_FromSsize_t(len_i);
3839 if (len_o == NULL) {
3840 goto error;
3841 }
3842 PUSH(len_o);
3843 DISPATCH();
3844 }
3845
3846 case TARGET(MATCH_CLASS): {
3847 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3848 // attributes. On failure, set TOS to False.
3849 PyObject *names = POP();
3850 PyObject *type = TOP();
3851 PyObject *subject = SECOND();
3852 assert(PyTuple_CheckExact(names));
3853 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3854 Py_DECREF(names);
3855 if (attrs) {
3856 // Success!
3857 assert(PyTuple_CheckExact(attrs));
3858 Py_DECREF(subject);
3859 SET_SECOND(attrs);
3860 }
3861 else if (_PyErr_Occurred(tstate)) {
3862 goto error;
3863 }
3864 Py_DECREF(type);
3865 SET_TOP(PyBool_FromLong(!!attrs));
3866 DISPATCH();
3867 }
3868
3869 case TARGET(MATCH_MAPPING): {
3870 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3871 PyObject *subject = TOP();
3872 // Fast path for dicts:
3873 if (PyDict_Check(subject)) {
3874 Py_INCREF(Py_True);
3875 PUSH(Py_True);
3876 DISPATCH();
3877 }
3878 // Lazily import _collections_abc.Mapping, and keep it handy on the
3879 // PyInterpreterState struct (it gets cleaned up at exit):
3880 PyInterpreterState *interp = PyInterpreterState_Get();
3881 if (interp->map_abc == NULL) {
3882 PyObject *abc = PyImport_ImportModule("_collections_abc");
3883 if (abc == NULL) {
3884 goto error;
3885 }
3886 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3887 if (interp->map_abc == NULL) {
3888 goto error;
3889 }
3890 }
3891 int match = PyObject_IsInstance(subject, interp->map_abc);
3892 if (match < 0) {
3893 goto error;
3894 }
3895 PUSH(PyBool_FromLong(match));
3896 DISPATCH();
3897 }
3898
3899 case TARGET(MATCH_SEQUENCE): {
3900 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3901 // and isinstance(TOS, _collections_abc.Sequence))
3902 PyObject *subject = TOP();
3903 // Fast path for lists and tuples:
3904 if (PyType_FastSubclass(Py_TYPE(subject),
3905 Py_TPFLAGS_LIST_SUBCLASS |
3906 Py_TPFLAGS_TUPLE_SUBCLASS))
3907 {
3908 Py_INCREF(Py_True);
3909 PUSH(Py_True);
3910 DISPATCH();
3911 }
3912 // Bail on some possible Sequences that we intentionally exclude:
3913 if (PyType_FastSubclass(Py_TYPE(subject),
3914 Py_TPFLAGS_BYTES_SUBCLASS |
3915 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3916 PyByteArray_Check(subject))
3917 {
3918 Py_INCREF(Py_False);
3919 PUSH(Py_False);
3920 DISPATCH();
3921 }
3922 // Lazily import _collections_abc.Sequence, and keep it handy on the
3923 // PyInterpreterState struct (it gets cleaned up at exit):
3924 PyInterpreterState *interp = PyInterpreterState_Get();
3925 if (interp->seq_abc == NULL) {
3926 PyObject *abc = PyImport_ImportModule("_collections_abc");
3927 if (abc == NULL) {
3928 goto error;
3929 }
3930 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3931 if (interp->seq_abc == NULL) {
3932 goto error;
3933 }
3934 }
3935 int match = PyObject_IsInstance(subject, interp->seq_abc);
3936 if (match < 0) {
3937 goto error;
3938 }
3939 PUSH(PyBool_FromLong(match));
3940 DISPATCH();
3941 }
3942
3943 case TARGET(MATCH_KEYS): {
3944 // On successful match for all keys, PUSH(values) and PUSH(True).
3945 // Otherwise, PUSH(None) and PUSH(False).
3946 PyObject *keys = TOP();
3947 PyObject *subject = SECOND();
3948 PyObject *values_or_none = match_keys(tstate, subject, keys);
3949 if (values_or_none == NULL) {
3950 goto error;
3951 }
3952 PUSH(values_or_none);
3953 if (values_or_none == Py_None) {
3954 Py_INCREF(Py_False);
3955 PUSH(Py_False);
3956 DISPATCH();
3957 }
3958 assert(PyTuple_CheckExact(values_or_none));
3959 Py_INCREF(Py_True);
3960 PUSH(Py_True);
3961 DISPATCH();
3962 }
3963
3964 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3965 // rest = dict(TOS1)
3966 // for key in TOS:
3967 // del rest[key]
3968 // SET_TOP(rest)
3969 PyObject *keys = TOP();
3970 PyObject *subject = SECOND();
3971 PyObject *rest = PyDict_New();
3972 if (rest == NULL || PyDict_Update(rest, subject)) {
3973 Py_XDECREF(rest);
3974 goto error;
3975 }
3976 // This may seem a bit inefficient, but keys is rarely big enough to
3977 // actually impact runtime.
3978 assert(PyTuple_CheckExact(keys));
3979 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3980 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3981 Py_DECREF(rest);
3982 goto error;
3983 }
3984 }
3985 Py_DECREF(keys);
3986 SET_TOP(rest);
3987 DISPATCH();
3988 }
3989
Benjamin Petersonddd19492018-09-16 22:38:02 -07003990 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003992 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003993 PyObject *iter = PyObject_GetIter(iterable);
3994 Py_DECREF(iterable);
3995 SET_TOP(iter);
3996 if (iter == NULL)
3997 goto error;
3998 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003999 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004000 DISPATCH();
4001 }
4002
Benjamin Petersonddd19492018-09-16 22:38:02 -07004003 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004004 /* before: [obj]; after [getiter(obj)] */
4005 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004006 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004007 if (PyCoro_CheckExact(iterable)) {
4008 /* `iterable` is a coroutine */
4009 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4010 /* and it is used in a 'yield from' expression of a
4011 regular generator. */
4012 Py_DECREF(iterable);
4013 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004014 _PyErr_SetString(tstate, PyExc_TypeError,
4015 "cannot 'yield from' a coroutine object "
4016 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004017 goto error;
4018 }
4019 }
4020 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004021 /* `iterable` is not a generator. */
4022 iter = PyObject_GetIter(iterable);
4023 Py_DECREF(iterable);
4024 SET_TOP(iter);
4025 if (iter == NULL)
4026 goto error;
4027 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004028 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004029 DISPATCH();
4030 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004031
Benjamin Petersonddd19492018-09-16 22:38:02 -07004032 case TARGET(FOR_ITER): {
4033 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004035 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004036 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004037 if (next != NULL) {
4038 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PREDICT(STORE_FAST);
4040 PREDICT(UNPACK_SEQUENCE);
4041 DISPATCH();
4042 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004043 if (_PyErr_Occurred(tstate)) {
4044 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004045 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004046 }
4047 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004048 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004049 }
4050 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 }
4052 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004053 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004054 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 JUMPBY(oparg);
4056 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004057 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004058
Benjamin Petersonddd19492018-09-16 22:38:02 -07004059 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004060 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 STACK_LEVEL());
4062 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004064
Benjamin Petersonddd19492018-09-16 22:38:02 -07004065 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004066 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004067 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004068 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004069 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004070 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004071 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004072 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004073 }
4074 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4075 if (exit == NULL) {
4076 Py_DECREF(enter);
4077 goto error;
4078 }
Yury Selivanov75445082015-05-11 22:57:16 -04004079 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004080 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004081 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004082 Py_DECREF(enter);
4083 if (res == NULL)
4084 goto error;
4085 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004086 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004087 DISPATCH();
4088 }
4089
Benjamin Petersonddd19492018-09-16 22:38:02 -07004090 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004091 PyObject *res = POP();
4092 /* Setup the finally block before pushing the result
4093 of __aenter__ on the stack. */
4094 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4095 STACK_LEVEL());
4096 PUSH(res);
4097 DISPATCH();
4098 }
4099
Benjamin Petersonddd19492018-09-16 22:38:02 -07004100 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004101 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004102 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004103 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004104 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004105 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004106 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004107 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004108 }
4109 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004110 if (exit == NULL) {
4111 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004112 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004113 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004114 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004115 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004116 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004117 Py_DECREF(enter);
4118 if (res == NULL)
4119 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 /* Setup the finally block before pushing the result
4121 of __enter__ on the stack. */
4122 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4123 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004124
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004125 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 DISPATCH();
4127 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004128
Mark Shannonfee55262019-11-21 09:11:43 +00004129 case TARGET(WITH_EXCEPT_START): {
4130 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004132 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4133 - SEVENTH: the context.__exit__ bound method
4134 We call SEVENTH(TOP, SECOND, THIRD).
4135 Then we push again the TOP exception and the __exit__
4136 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004139 PyObject *exc, *val, *tb, *res;
4140
Victor Stinner842cfff2016-12-01 14:45:31 +01004141 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004142 val = SECOND();
4143 tb = THIRD();
4144 assert(exc != Py_None);
4145 assert(!PyLong_Check(exc));
4146 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004147 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004148 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004149 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004150 if (res == NULL)
4151 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004152
Yury Selivanov75445082015-05-11 22:57:16 -04004153 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004154 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004156
Benjamin Petersonddd19492018-09-16 22:38:02 -07004157 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004158 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004159 PyObject *name = GETITEM(names, oparg);
4160 PyObject *obj = TOP();
4161 PyObject *meth = NULL;
4162
4163 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4164
Yury Selivanovf2392132016-12-13 19:03:51 -05004165 if (meth == NULL) {
4166 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004167 goto error;
4168 }
4169
4170 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004171 /* We can bypass temporary bound method object.
4172 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004173
INADA Naoki015bce62017-01-16 17:23:30 +09004174 meth | self | arg1 | ... | argN
4175 */
4176 SET_TOP(meth);
4177 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004178 }
4179 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004180 /* meth is not an unbound method (but a regular attr, or
4181 something was returned by a descriptor protocol). Set
4182 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004183 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004184
4185 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004186 */
INADA Naoki015bce62017-01-16 17:23:30 +09004187 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004188 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004189 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004190 }
4191 DISPATCH();
4192 }
4193
Benjamin Petersonddd19492018-09-16 22:38:02 -07004194 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004195 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004196 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004197
4198 sp = stack_pointer;
4199
INADA Naoki015bce62017-01-16 17:23:30 +09004200 meth = PEEK(oparg + 2);
4201 if (meth == NULL) {
4202 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4203 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004204
4205 Stack layout:
4206
INADA Naoki015bce62017-01-16 17:23:30 +09004207 ... | NULL | callable | arg1 | ... | argN
4208 ^- TOP()
4209 ^- (-oparg)
4210 ^- (-oparg-1)
4211 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004212
Ville Skyttä49b27342017-08-03 09:00:59 +03004213 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004214 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004215 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004216 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004217 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004218 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004219 }
4220 else {
4221 /* This is a method call. Stack layout:
4222
INADA Naoki015bce62017-01-16 17:23:30 +09004223 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004224 ^- TOP()
4225 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004226 ^- (-oparg-1)
4227 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004228
INADA Naoki015bce62017-01-16 17:23:30 +09004229 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004230 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004231 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004232 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004233 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004234 stack_pointer = sp;
4235 }
4236
4237 PUSH(res);
4238 if (res == NULL)
4239 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004240 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004241 DISPATCH();
4242 }
4243
Benjamin Petersonddd19492018-09-16 22:38:02 -07004244 case TARGET(CALL_FUNCTION): {
4245 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004246 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004248 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004250 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004251 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004252 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004253 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004254 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004255 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004257
Benjamin Petersonddd19492018-09-16 22:38:02 -07004258 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004259 PyObject **sp, *res, *names;
4260
4261 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004262 assert(PyTuple_Check(names));
4263 assert(PyTuple_GET_SIZE(names) <= oparg);
4264 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004266 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004268 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004269 Py_DECREF(names);
4270
4271 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004272 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004273 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004274 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004275 DISPATCH();
4276 }
4277
Benjamin Petersonddd19492018-09-16 22:38:02 -07004278 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004279 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004280 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004281 if (oparg & 0x01) {
4282 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004283 if (!PyDict_CheckExact(kwargs)) {
4284 PyObject *d = PyDict_New();
4285 if (d == NULL)
4286 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004287 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004288 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004289 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004290 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004291 goto error;
4292 }
4293 Py_DECREF(kwargs);
4294 kwargs = d;
4295 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004296 assert(PyDict_CheckExact(kwargs));
4297 }
4298 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004299 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004300 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004301 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004302 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004303 goto error;
4304 }
4305 Py_SETREF(callargs, PySequence_Tuple(callargs));
4306 if (callargs == NULL) {
4307 goto error;
4308 }
4309 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004310 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004311
Mark Shannon8e1b4062021-03-05 14:45:50 +00004312 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004313 Py_DECREF(func);
4314 Py_DECREF(callargs);
4315 Py_XDECREF(kwargs);
4316
4317 SET_TOP(result);
4318 if (result == NULL) {
4319 goto error;
4320 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004321 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004322 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004324
Benjamin Petersonddd19492018-09-16 22:38:02 -07004325 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004326 PyObject *qualname = POP();
4327 PyObject *codeobj = POP();
4328 PyFunctionObject *func = (PyFunctionObject *)
4329 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004330
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004331 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004332 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004333 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004336
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004337 if (oparg & 0x08) {
4338 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004339 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004340 }
4341 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004342 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004343 func->func_annotations = POP();
4344 }
4345 if (oparg & 0x02) {
4346 assert(PyDict_CheckExact(TOP()));
4347 func->func_kwdefaults = POP();
4348 }
4349 if (oparg & 0x01) {
4350 assert(PyTuple_CheckExact(TOP()));
4351 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004353
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004354 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004355 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004357
Benjamin Petersonddd19492018-09-16 22:38:02 -07004358 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004359 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004361 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004363 step = NULL;
4364 stop = POP();
4365 start = TOP();
4366 slice = PySlice_New(start, stop, step);
4367 Py_DECREF(start);
4368 Py_DECREF(stop);
4369 Py_XDECREF(step);
4370 SET_TOP(slice);
4371 if (slice == NULL)
4372 goto error;
4373 DISPATCH();
4374 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004375
Benjamin Petersonddd19492018-09-16 22:38:02 -07004376 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004377 /* Handles f-string value formatting. */
4378 PyObject *result;
4379 PyObject *fmt_spec;
4380 PyObject *value;
4381 PyObject *(*conv_fn)(PyObject *);
4382 int which_conversion = oparg & FVC_MASK;
4383 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4384
4385 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004386 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004387
4388 /* See if any conversion is specified. */
4389 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004390 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004391 case FVC_STR: conv_fn = PyObject_Str; break;
4392 case FVC_REPR: conv_fn = PyObject_Repr; break;
4393 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004394 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004395 _PyErr_Format(tstate, PyExc_SystemError,
4396 "unexpected conversion flag %d",
4397 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004398 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004399 }
4400
4401 /* If there's a conversion function, call it and replace
4402 value with that result. Otherwise, just use value,
4403 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004404 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004405 result = conv_fn(value);
4406 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004407 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004408 Py_XDECREF(fmt_spec);
4409 goto error;
4410 }
4411 value = result;
4412 }
4413
4414 /* If value is a unicode object, and there's no fmt_spec,
4415 then we know the result of format(value) is value
4416 itself. In that case, skip calling format(). I plan to
4417 move this optimization in to PyObject_Format()
4418 itself. */
4419 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4420 /* Do nothing, just transfer ownership to result. */
4421 result = value;
4422 } else {
4423 /* Actually call format(). */
4424 result = PyObject_Format(value, fmt_spec);
4425 Py_DECREF(value);
4426 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004427 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004428 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004429 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004430 }
4431
Eric V. Smith135d5f42016-02-05 18:23:08 -05004432 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004433 DISPATCH();
4434 }
4435
Benjamin Petersonddd19492018-09-16 22:38:02 -07004436 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004437 int oldoparg = oparg;
4438 NEXTOPARG();
4439 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004441 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004442
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004443
Antoine Pitrou042b1282010-08-13 21:15:58 +00004444#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004446#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 default:
4448 fprintf(stderr,
4449 "XXX lineno: %d, opcode: %d\n",
4450 PyFrame_GetLineNumber(f),
4451 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004452 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004453 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004457 /* This should never be reached. Every opcode should end with DISPATCH()
4458 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004459 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004461error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004462 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004463#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004464 if (!_PyErr_Occurred(tstate)) {
4465 _PyErr_SetString(tstate, PyExc_SystemError,
4466 "error return without exception set");
4467 }
Victor Stinner365b6932013-07-12 00:11:58 +02004468#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004469 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004470#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004471
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004472 /* Log traceback info. */
4473 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004474
Mark Shannoncb9879b2020-07-17 11:44:23 +01004475 if (tstate->c_tracefunc != NULL) {
4476 /* Make sure state is set to FRAME_EXECUTING for tracing */
4477 assert(f->f_state == FRAME_EXECUTING);
4478 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004479 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004480 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004481 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004482exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004483 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004484 /* Unwind stacks if an exception occurred */
4485 while (f->f_iblock > 0) {
4486 /* Pop the current block. */
4487 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (b->b_type == EXCEPT_HANDLER) {
4490 UNWIND_EXCEPT_HANDLER(b);
4491 continue;
4492 }
4493 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004494 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 PyObject *exc, *val, *tb;
4496 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004497 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004499 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004500 PUSH(exc_info->exc_traceback);
4501 PUSH(exc_info->exc_value);
4502 if (exc_info->exc_type != NULL) {
4503 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 }
4505 else {
4506 Py_INCREF(Py_None);
4507 PUSH(Py_None);
4508 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 /* Make the raw exception data
4511 available to the handler,
4512 so a program can emulate the
4513 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004514 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004515 if (tb != NULL)
4516 PyException_SetTraceback(val, tb);
4517 else
4518 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004520 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004522 exc_info->exc_value = val;
4523 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 if (tb == NULL)
4525 tb = Py_None;
4526 Py_INCREF(tb);
4527 PUSH(tb);
4528 PUSH(val);
4529 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004531 if (_Py_TracingPossible(ceval2)) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004532 trace_info.instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004533 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004534 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004535 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004536 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 }
4538 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004539
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004540 /* End the loop as we still have an error */
4541 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004543
Pablo Galindof00828a2019-05-09 16:52:02 +01004544 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004545 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 /* Pop remaining stack entries. */
4548 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004549 PyObject *o = POP();
4550 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004552 f->f_stackdepth = 0;
4553 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004554exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004556 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004557 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004558 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004559 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 }
4561 }
4562 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004563 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004564 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004565 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 }
4567 }
4568 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004571exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004572 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4573 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004574 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004576
Victor Stinner0b72b232020-03-12 23:18:39 +01004577 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004578}
4579
Benjamin Petersonb204a422011-06-05 22:04:07 -05004580static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004581format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004582 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004583{
4584 int err;
4585 Py_ssize_t len = PyList_GET_SIZE(names);
4586 PyObject *name_str, *comma, *tail, *tmp;
4587
4588 assert(PyList_CheckExact(names));
4589 assert(len >= 1);
4590 /* Deal with the joys of natural language. */
4591 switch (len) {
4592 case 1:
4593 name_str = PyList_GET_ITEM(names, 0);
4594 Py_INCREF(name_str);
4595 break;
4596 case 2:
4597 name_str = PyUnicode_FromFormat("%U and %U",
4598 PyList_GET_ITEM(names, len - 2),
4599 PyList_GET_ITEM(names, len - 1));
4600 break;
4601 default:
4602 tail = PyUnicode_FromFormat(", %U, and %U",
4603 PyList_GET_ITEM(names, len - 2),
4604 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004605 if (tail == NULL)
4606 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004607 /* Chop off the last two objects in the list. This shouldn't actually
4608 fail, but we can't be too careful. */
4609 err = PyList_SetSlice(names, len - 2, len, NULL);
4610 if (err == -1) {
4611 Py_DECREF(tail);
4612 return;
4613 }
4614 /* Stitch everything up into a nice comma-separated list. */
4615 comma = PyUnicode_FromString(", ");
4616 if (comma == NULL) {
4617 Py_DECREF(tail);
4618 return;
4619 }
4620 tmp = PyUnicode_Join(comma, names);
4621 Py_DECREF(comma);
4622 if (tmp == NULL) {
4623 Py_DECREF(tail);
4624 return;
4625 }
4626 name_str = PyUnicode_Concat(tmp, tail);
4627 Py_DECREF(tmp);
4628 Py_DECREF(tail);
4629 break;
4630 }
4631 if (name_str == NULL)
4632 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004633 _PyErr_Format(tstate, PyExc_TypeError,
4634 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004635 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004636 len,
4637 kind,
4638 len == 1 ? "" : "s",
4639 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004640 Py_DECREF(name_str);
4641}
4642
4643static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004644missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4645 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004646 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004647{
Victor Stinner74319ae2016-08-25 00:04:09 +02004648 Py_ssize_t i, j = 0;
4649 Py_ssize_t start, end;
4650 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004651 const char *kind = positional ? "positional" : "keyword-only";
4652 PyObject *missing_names;
4653
4654 /* Compute the names of the arguments that are missing. */
4655 missing_names = PyList_New(missing);
4656 if (missing_names == NULL)
4657 return;
4658 if (positional) {
4659 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004660 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004661 }
4662 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004663 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004664 end = start + co->co_kwonlyargcount;
4665 }
4666 for (i = start; i < end; i++) {
4667 if (GETLOCAL(i) == NULL) {
4668 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4669 PyObject *name = PyObject_Repr(raw);
4670 if (name == NULL) {
4671 Py_DECREF(missing_names);
4672 return;
4673 }
4674 PyList_SET_ITEM(missing_names, j++, name);
4675 }
4676 }
4677 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004678 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004679 Py_DECREF(missing_names);
4680}
4681
4682static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004683too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004684 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004685 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004686{
4687 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004688 Py_ssize_t kwonly_given = 0;
4689 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004690 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004691 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004692
Benjamin Petersone109c702011-06-24 09:37:26 -05004693 assert((co->co_flags & CO_VARARGS) == 0);
4694 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004695 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004696 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004697 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004698 }
4699 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004700 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004701 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004702 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004703 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004704 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004705 }
4706 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004707 plural = (co_argcount != 1);
4708 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004709 }
4710 if (sig == NULL)
4711 return;
4712 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004713 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4714 kwonly_sig = PyUnicode_FromFormat(format,
4715 given != 1 ? "s" : "",
4716 kwonly_given,
4717 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004718 if (kwonly_sig == NULL) {
4719 Py_DECREF(sig);
4720 return;
4721 }
4722 }
4723 else {
4724 /* This will not fail. */
4725 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004726 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004727 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004728 _PyErr_Format(tstate, PyExc_TypeError,
4729 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004730 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004731 sig,
4732 plural ? "s" : "",
4733 given,
4734 kwonly_sig,
4735 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004736 Py_DECREF(sig);
4737 Py_DECREF(kwonly_sig);
4738}
4739
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004740static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004741positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004742 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004743 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004744{
4745 int posonly_conflicts = 0;
4746 PyObject* posonly_names = PyList_New(0);
4747
4748 for(int k=0; k < co->co_posonlyargcount; k++){
4749 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4750
4751 for (int k2=0; k2<kwcount; k2++){
4752 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004753 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004754 if (kwname == posonly_name){
4755 if(PyList_Append(posonly_names, kwname) != 0) {
4756 goto fail;
4757 }
4758 posonly_conflicts++;
4759 continue;
4760 }
4761
4762 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4763
4764 if ( cmp > 0) {
4765 if(PyList_Append(posonly_names, kwname) != 0) {
4766 goto fail;
4767 }
4768 posonly_conflicts++;
4769 } else if (cmp < 0) {
4770 goto fail;
4771 }
4772
4773 }
4774 }
4775 if (posonly_conflicts) {
4776 PyObject* comma = PyUnicode_FromString(", ");
4777 if (comma == NULL) {
4778 goto fail;
4779 }
4780 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4781 Py_DECREF(comma);
4782 if (error_names == NULL) {
4783 goto fail;
4784 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004785 _PyErr_Format(tstate, PyExc_TypeError,
4786 "%U() got some positional-only arguments passed"
4787 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004788 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004789 Py_DECREF(error_names);
4790 goto fail;
4791 }
4792
4793 Py_DECREF(posonly_names);
4794 return 0;
4795
4796fail:
4797 Py_XDECREF(posonly_names);
4798 return 1;
4799
4800}
4801
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004802
Mark Shannon0332e562021-02-01 10:42:03 +00004803PyFrameObject *
4804_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004805 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004806 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004807 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004808{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004809 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004810
Mark Shannond6c33fb2021-01-29 13:24:55 +00004811 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4812 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004813 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004814
Victor Stinnerc7020012016-08-16 23:40:29 +02004815 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004816 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004817 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004819 }
Victor Stinner232dda62020-06-04 15:19:02 +02004820 PyObject **fastlocals = f->f_localsplus;
4821 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004822
Victor Stinnerc7020012016-08-16 23:40:29 +02004823 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004824 PyObject *kwdict;
4825 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004826 if (co->co_flags & CO_VARKEYWORDS) {
4827 kwdict = PyDict_New();
4828 if (kwdict == NULL)
4829 goto fail;
4830 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004831 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004832 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004833 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004834 SETLOCAL(i, kwdict);
4835 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004836 else {
4837 kwdict = NULL;
4838 }
4839
Pablo Galindocd74e662019-06-01 18:08:04 +01004840 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004841 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004842 if (argcount > co->co_argcount) {
4843 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004844 }
4845 else {
4846 n = argcount;
4847 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004848 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004849 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004850 Py_INCREF(x);
4851 SETLOCAL(j, x);
4852 }
4853
Victor Stinnerc7020012016-08-16 23:40:29 +02004854 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004855 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004856 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004857 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004858 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004859 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004860 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004861 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004862
Mark Shannon0332e562021-02-01 10:42:03 +00004863 /* Handle keyword arguments */
4864 if (kwnames != NULL) {
4865 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4866 for (i = 0; i < kwcount; i++) {
4867 PyObject **co_varnames;
4868 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4869 PyObject *value = args[i+argcount];
4870 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004871
Mark Shannon0332e562021-02-01 10:42:03 +00004872 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4873 _PyErr_Format(tstate, PyExc_TypeError,
4874 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004875 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004877 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004878
Mark Shannon0332e562021-02-01 10:42:03 +00004879 /* Speed hack: do raw pointer compares. As names are
4880 normally interned this should almost always hit. */
4881 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4882 for (j = co->co_posonlyargcount; j < total_args; j++) {
4883 PyObject *varname = co_varnames[j];
4884 if (varname == keyword) {
4885 goto kw_found;
4886 }
4887 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004888
Mark Shannon0332e562021-02-01 10:42:03 +00004889 /* Slow fallback, just in case */
4890 for (j = co->co_posonlyargcount; j < total_args; j++) {
4891 PyObject *varname = co_varnames[j];
4892 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4893 if (cmp > 0) {
4894 goto kw_found;
4895 }
4896 else if (cmp < 0) {
4897 goto fail;
4898 }
4899 }
4900
4901 assert(j >= total_args);
4902 if (kwdict == NULL) {
4903
4904 if (co->co_posonlyargcount
4905 && positional_only_passed_as_keyword(tstate, co,
4906 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004907 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004908 {
4909 goto fail;
4910 }
4911
4912 _PyErr_Format(tstate, PyExc_TypeError,
4913 "%U() got an unexpected keyword argument '%S'",
4914 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004915 goto fail;
4916 }
4917
Mark Shannon0332e562021-02-01 10:42:03 +00004918 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4919 goto fail;
4920 }
4921 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004922
Mark Shannon0332e562021-02-01 10:42:03 +00004923 kw_found:
4924 if (GETLOCAL(j) != NULL) {
4925 _PyErr_Format(tstate, PyExc_TypeError,
4926 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004927 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004928 goto fail;
4929 }
4930 Py_INCREF(value);
4931 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004934
4935 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004936 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004937 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4938 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 goto fail;
4940 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004941
4942 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004943 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004944 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004945 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004946 Py_ssize_t missing = 0;
4947 for (i = argcount; i < m; i++) {
4948 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004949 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004950 }
4951 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004952 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004953 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004954 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004955 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004956 }
4957 if (n > m)
4958 i = n - m;
4959 else
4960 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004961 if (defcount) {
4962 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4963 for (; i < defcount; i++) {
4964 if (GETLOCAL(m+i) == NULL) {
4965 PyObject *def = defs[i];
4966 Py_INCREF(def);
4967 SETLOCAL(m+i, def);
4968 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004969 }
4970 }
4971 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004972
4973 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004974 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004975 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004976 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004977 if (GETLOCAL(i) != NULL)
4978 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004979 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004980 if (con->fc_kwdefaults != NULL) {
4981 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004982 if (def) {
4983 Py_INCREF(def);
4984 SETLOCAL(i, def);
4985 continue;
4986 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004987 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004988 goto fail;
4989 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004990 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004991 missing++;
4992 }
4993 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004994 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004995 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004996 goto fail;
4997 }
4998 }
4999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005001 vars into frame. */
5002 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005004 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005005 /* Possibly account for the cell variable being an argument. */
5006 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005007 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005008 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005009 /* Clear the local copy. */
5010 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005011 }
5012 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005013 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005014 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005015 if (c == NULL)
5016 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005017 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005019
5020 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005021 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005022 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005023 Py_INCREF(o);
5024 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005026
Mark Shannon0332e562021-02-01 10:42:03 +00005027 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005028
Thomas Woutersce272b62007-09-19 21:19:28 +00005029fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 /* decref'ing the frame can cause __del__ methods to get invoked,
5032 which can call back into Python. While we're done with the
5033 current Python frame (f), the associated C stack is still in use,
5034 so recursion_depth must be boosted for the duration.
5035 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005036 if (Py_REFCNT(f) > 1) {
5037 Py_DECREF(f);
5038 _PyObject_GC_TRACK(f);
5039 }
5040 else {
5041 ++tstate->recursion_depth;
5042 Py_DECREF(f);
5043 --tstate->recursion_depth;
5044 }
Mark Shannon0332e562021-02-01 10:42:03 +00005045 return NULL;
5046}
5047
5048static PyObject *
5049make_coro(PyFrameConstructor *con, PyFrameObject *f)
5050{
5051 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5052 PyObject *gen;
5053 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5054
5055 /* Don't need to keep the reference to f_back, it will be set
5056 * when the generator is resumed. */
5057 Py_CLEAR(f->f_back);
5058
5059 /* Create a new generator that owns the ready to run frame
5060 * and return that as the value. */
5061 if (is_coro) {
5062 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5063 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5064 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5065 } else {
5066 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5067 }
5068 if (gen == NULL) {
5069 return NULL;
5070 }
5071
5072 _PyObject_GC_TRACK(f);
5073
5074 return gen;
5075}
5076
5077PyObject *
5078_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5079 PyObject *locals,
5080 PyObject* const* args, size_t argcount,
5081 PyObject *kwnames)
5082{
5083 PyFrameObject *f = _PyEval_MakeFrameVector(
5084 tstate, con, locals, args, argcount, kwnames);
5085 if (f == NULL) {
5086 return NULL;
5087 }
5088 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5089 return make_coro(con, f);
5090 }
5091 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5092
5093 /* decref'ing the frame can cause __del__ methods to get invoked,
5094 which can call back into Python. While we're done with the
5095 current Python frame (f), the associated C stack is still in use,
5096 so recursion_depth must be boosted for the duration.
5097 */
5098 if (Py_REFCNT(f) > 1) {
5099 Py_DECREF(f);
5100 _PyObject_GC_TRACK(f);
5101 }
5102 else {
5103 ++tstate->recursion_depth;
5104 Py_DECREF(f);
5105 --tstate->recursion_depth;
5106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005108}
5109
Mark Shannond6c33fb2021-01-29 13:24:55 +00005110/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005111PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005112PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5113 PyObject *const *args, int argcount,
5114 PyObject *const *kws, int kwcount,
5115 PyObject *const *defs, int defcount,
5116 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005117{
Victor Stinner46496f92021-02-20 15:17:18 +01005118 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005119 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005120 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5121 if (defaults == NULL) {
5122 return NULL;
5123 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005124 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005125 if (builtins == NULL) {
5126 Py_DECREF(defaults);
5127 return NULL;
5128 }
Mark Shannon0332e562021-02-01 10:42:03 +00005129 if (locals == NULL) {
5130 locals = globals;
5131 }
5132 PyObject *kwnames;
5133 PyObject *const *allargs;
5134 PyObject **newargs;
5135 if (kwcount == 0) {
5136 allargs = args;
5137 kwnames = NULL;
5138 }
5139 else {
5140 kwnames = PyTuple_New(kwcount);
5141 if (kwnames == NULL) {
5142 res = NULL;
5143 goto fail;
5144 }
5145 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5146 if (newargs == NULL) {
5147 res = NULL;
5148 Py_DECREF(kwnames);
5149 goto fail;
5150 }
5151 for (int i = 0; i < argcount; i++) {
5152 newargs[i] = args[i];
5153 }
5154 for (int i = 0; i < kwcount; i++) {
5155 Py_INCREF(kws[2*i]);
5156 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5157 newargs[argcount+i] = kws[2*i+1];
5158 }
5159 allargs = newargs;
5160 }
5161 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5162 if (kwargs == NULL) {
5163 res = NULL;
5164 Py_DECREF(kwnames);
5165 goto fail;
5166 }
5167 for (int i = 0; i < kwcount; i++) {
5168 Py_INCREF(kws[2*i]);
5169 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5170 kwargs[i] = kws[2*i+1];
5171 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005172 PyFrameConstructor constr = {
5173 .fc_globals = globals,
5174 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005175 .fc_name = ((PyCodeObject *)_co)->co_name,
5176 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005177 .fc_code = _co,
5178 .fc_defaults = defaults,
5179 .fc_kwdefaults = kwdefs,
5180 .fc_closure = closure
5181 };
Mark Shannon0332e562021-02-01 10:42:03 +00005182 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005183 allargs, argcount,
5184 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005185 if (kwcount) {
5186 Py_DECREF(kwnames);
5187 PyMem_Free(newargs);
5188 }
5189fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005190 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005191 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005192}
5193
Tim Peters5ca576e2001-06-18 22:08:13 +00005194
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005195static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005196special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005199 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005200 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005201 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 return NULL;
5203 }
5204 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005205}
5206
5207
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005208/* Logic for the raise statement (too complicated for inlining).
5209 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005210static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005211do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 if (exc == NULL) {
5216 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005217 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005219 type = exc_info->exc_type;
5220 value = exc_info->exc_value;
5221 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005222 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005223 _PyErr_SetString(tstate, PyExc_RuntimeError,
5224 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005225 return 0;
5226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 Py_XINCREF(type);
5228 Py_XINCREF(value);
5229 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005230 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005231 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 /* We support the following forms of raise:
5235 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005236 raise <instance>
5237 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 if (PyExceptionClass_Check(exc)) {
5240 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005241 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 if (value == NULL)
5243 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005244 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005245 _PyErr_Format(tstate, PyExc_TypeError,
5246 "calling %R should have returned an instance of "
5247 "BaseException, not %R",
5248 type, Py_TYPE(value));
5249 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 }
5252 else if (PyExceptionInstance_Check(exc)) {
5253 value = exc;
5254 type = PyExceptionInstance_Class(exc);
5255 Py_INCREF(type);
5256 }
5257 else {
5258 /* Not something you can raise. You get an exception
5259 anyway, just not what you specified :-) */
5260 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005261 _PyErr_SetString(tstate, PyExc_TypeError,
5262 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 goto raise_error;
5264 }
Collin Winter828f04a2007-08-31 00:04:24 +00005265
Serhiy Storchakac0191582016-09-27 11:37:10 +03005266 assert(type != NULL);
5267 assert(value != NULL);
5268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 if (cause) {
5270 PyObject *fixed_cause;
5271 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005272 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 if (fixed_cause == NULL)
5274 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005275 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005277 else if (PyExceptionInstance_Check(cause)) {
5278 fixed_cause = cause;
5279 }
5280 else if (cause == Py_None) {
5281 Py_DECREF(cause);
5282 fixed_cause = NULL;
5283 }
5284 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005285 _PyErr_SetString(tstate, PyExc_TypeError,
5286 "exception causes must derive from "
5287 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 goto raise_error;
5289 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005290 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 }
Collin Winter828f04a2007-08-31 00:04:24 +00005292
Victor Stinner438a12d2019-05-24 17:01:38 +02005293 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005294 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005295 Py_DECREF(value);
5296 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005297 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005298
5299raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 Py_XDECREF(value);
5301 Py_XDECREF(type);
5302 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005303 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005304}
5305
Tim Petersd6d010b2001-06-21 02:49:55 +00005306/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005307 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005308
Guido van Rossum0368b722007-05-11 16:50:42 +00005309 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5310 with a variable target.
5311*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005312
Barry Warsawe42b18f1997-08-25 22:13:04 +00005313static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005314unpack_iterable(PyThreadState *tstate, PyObject *v,
5315 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005317 int i = 0, j = 0;
5318 Py_ssize_t ll = 0;
5319 PyObject *it; /* iter(v) */
5320 PyObject *w;
5321 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005323 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005326 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005327 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005328 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005329 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005330 _PyErr_Format(tstate, PyExc_TypeError,
5331 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005332 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005333 }
5334 return 0;
5335 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005337 for (; i < argcnt; i++) {
5338 w = PyIter_Next(it);
5339 if (w == NULL) {
5340 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005341 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005342 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005343 _PyErr_Format(tstate, PyExc_ValueError,
5344 "not enough values to unpack "
5345 "(expected %d, got %d)",
5346 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005347 }
5348 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005349 _PyErr_Format(tstate, PyExc_ValueError,
5350 "not enough values to unpack "
5351 "(expected at least %d, got %d)",
5352 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 }
5355 goto Error;
5356 }
5357 *--sp = w;
5358 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 if (argcntafter == -1) {
5361 /* We better have exhausted the iterator now. */
5362 w = PyIter_Next(it);
5363 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005364 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 goto Error;
5366 Py_DECREF(it);
5367 return 1;
5368 }
5369 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005370 _PyErr_Format(tstate, PyExc_ValueError,
5371 "too many values to unpack (expected %d)",
5372 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 goto Error;
5374 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 l = PySequence_List(it);
5377 if (l == NULL)
5378 goto Error;
5379 *--sp = l;
5380 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 ll = PyList_GET_SIZE(l);
5383 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005384 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005385 "not enough values to unpack (expected at least %d, got %zd)",
5386 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 goto Error;
5388 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 /* Pop the "after-variable" args off the list. */
5391 for (j = argcntafter; j > 0; j--, i++) {
5392 *--sp = PyList_GET_ITEM(l, ll - j);
5393 }
5394 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005395 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 Py_DECREF(it);
5397 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005398
Tim Petersd6d010b2001-06-21 02:49:55 +00005399Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 for (; i > 0; i--, sp++)
5401 Py_DECREF(*sp);
5402 Py_XDECREF(it);
5403 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005404}
5405
5406
Guido van Rossum96a42c81992-01-12 02:29:51 +00005407#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005408static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005409prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005412 if (PyObject_Print(v, stdout, 0) != 0) {
5413 /* Don't know what else to do */
5414 _PyErr_Clear(tstate);
5415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005416 printf("\n");
5417 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005418}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005419#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005420
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005421static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005422call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005423 PyThreadState *tstate,
5424 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005425 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005426{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005427 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005429 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 if (value == NULL) {
5431 value = Py_None;
5432 Py_INCREF(value);
5433 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005434 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005435 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 arg = PyTuple_Pack(3, type, value, traceback);
5437 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005438 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 return;
5440 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005441 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 if (err == 0) {
5444 _PyErr_Restore(tstate, type, value, orig_traceback);
5445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 else {
5447 Py_XDECREF(type);
5448 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005449 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005451}
5452
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005453static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005454call_trace_protected(Py_tracefunc func, PyObject *obj,
5455 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005456 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005457 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005459 PyObject *type, *value, *traceback;
5460 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005461 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005462 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 if (err == 0)
5464 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005465 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005466 return 0;
5467 }
5468 else {
5469 Py_XDECREF(type);
5470 Py_XDECREF(value);
5471 Py_XDECREF(traceback);
5472 return -1;
5473 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005474}
5475
Mark Shannon8e1b4062021-03-05 14:45:50 +00005476static void
5477initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5478{
5479 if (trace_info->code != frame->f_code) {
5480 trace_info->code = frame->f_code;
5481 trace_info->instr_prev = -1;
5482 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5483 }
5484}
5485
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005486static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005487call_trace(Py_tracefunc func, PyObject *obj,
5488 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005489 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005492 int result;
5493 if (tstate->tracing)
5494 return 0;
5495 tstate->tracing++;
5496 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005497 if (frame->f_lasti < 0) {
5498 frame->f_lineno = frame->f_code->co_firstlineno;
5499 }
5500 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005501 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005502 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005503 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005505 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005506 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5507 || (tstate->c_profilefunc != NULL));
5508 tstate->tracing--;
5509 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005510}
5511
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005512PyObject *
5513_PyEval_CallTracing(PyObject *func, PyObject *args)
5514{
Victor Stinner50b48572018-11-01 01:51:40 +01005515 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 int save_tracing = tstate->tracing;
5517 int save_use_tracing = tstate->use_tracing;
5518 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 tstate->tracing = 0;
5521 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5522 || (tstate->c_profilefunc != NULL));
5523 result = PyObject_Call(func, args, NULL);
5524 tstate->tracing = save_tracing;
5525 tstate->use_tracing = save_use_tracing;
5526 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005527}
5528
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005529/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005530static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005531maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005532 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005533 PyTraceInfo *trace_info)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005536
Nick Coghlan5a851672017-09-08 10:14:16 +10005537 /* If the last instruction falls at the start of a line or if it
5538 represents a jump backwards, update the frame's line number and
5539 then call the trace function if we're tracing source lines.
5540 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005541 initialize_trace_info(trace_info, frame);
5542 int lastline = trace_info->bounds.ar_line;
Mark Shannonfcb55c02021-04-01 16:00:31 +01005543 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005544 if (line != -1 && frame->f_trace_lines) {
5545 /* Trace backward edges or first instruction of a new line */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005546 if (frame->f_lasti < trace_info->instr_prev ||
Mark Shannonfcb55c02021-04-01 16:00:31 +01005547 (line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
Mark Shannonee9f98d2021-01-05 12:04:10 +00005548 {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005549 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 }
George King20faa682017-10-18 17:44:22 -07005552 /* Always emit an opcode event if we're tracing all opcodes. */
5553 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005554 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005555 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005556 trace_info->instr_prev = frame->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005558}
5559
Victor Stinner309d7cc2020-03-13 16:39:12 +01005560int
5561_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5562{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005563 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005564 /* The caller must hold the GIL */
5565 assert(PyGILState_Check());
5566
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005567 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005568 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005569 PyThreadState *current_tstate = _PyThreadState_GET();
5570 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005571 return -1;
5572 }
5573
5574 PyObject *profileobj = tstate->c_profileobj;
5575
5576 tstate->c_profilefunc = NULL;
5577 tstate->c_profileobj = NULL;
5578 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5579 tstate->use_tracing = tstate->c_tracefunc != NULL;
5580 Py_XDECREF(profileobj);
5581
5582 Py_XINCREF(arg);
5583 tstate->c_profileobj = arg;
5584 tstate->c_profilefunc = func;
5585
5586 /* Flag that tracing or profiling is turned on */
5587 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5588 return 0;
5589}
5590
Fred Drake5755ce62001-06-27 19:19:46 +00005591void
5592PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005593{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005594 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005595 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005596 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005597 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5598 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005599}
5600
5601int
5602_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5603{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005604 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005605 /* The caller must hold the GIL */
5606 assert(PyGILState_Check());
5607
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005608 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005609 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005610 PyThreadState *current_tstate = _PyThreadState_GET();
5611 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005612 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005613 }
5614
Victor Stinnerda2914d2020-03-20 09:29:08 +01005615 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005616 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005617 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005618
5619 tstate->c_tracefunc = NULL;
5620 tstate->c_traceobj = NULL;
5621 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5622 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5623 Py_XDECREF(traceobj);
5624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005625 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005626 tstate->c_traceobj = arg;
5627 tstate->c_tracefunc = func;
5628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005630 tstate->use_tracing = ((func != NULL)
5631 || (tstate->c_profilefunc != NULL));
5632
5633 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005634}
5635
5636void
5637PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5638{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005639 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005640 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005641 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005642 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5643 }
Fred Draked0838392001-06-16 21:02:31 +00005644}
5645
Victor Stinner309d7cc2020-03-13 16:39:12 +01005646
Yury Selivanov75445082015-05-11 22:57:16 -04005647void
Victor Stinner838f2642019-06-13 22:41:23 +02005648_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005649{
5650 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005651 tstate->coroutine_origin_tracking_depth = new_depth;
5652}
5653
5654int
5655_PyEval_GetCoroutineOriginTrackingDepth(void)
5656{
Victor Stinner50b48572018-11-01 01:51:40 +01005657 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005658 return tstate->coroutine_origin_tracking_depth;
5659}
5660
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005661int
Yury Selivanoveb636452016-09-08 22:01:51 -07005662_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5663{
Victor Stinner50b48572018-11-01 01:51:40 +01005664 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005665
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005666 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005667 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005668 }
5669
Yury Selivanoveb636452016-09-08 22:01:51 -07005670 Py_XINCREF(firstiter);
5671 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005672 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005673}
5674
5675PyObject *
5676_PyEval_GetAsyncGenFirstiter(void)
5677{
Victor Stinner50b48572018-11-01 01:51:40 +01005678 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005679 return tstate->async_gen_firstiter;
5680}
5681
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005682int
Yury Selivanoveb636452016-09-08 22:01:51 -07005683_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5684{
Victor Stinner50b48572018-11-01 01:51:40 +01005685 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005686
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005687 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005688 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005689 }
5690
Yury Selivanoveb636452016-09-08 22:01:51 -07005691 Py_XINCREF(finalizer);
5692 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005693 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005694}
5695
5696PyObject *
5697_PyEval_GetAsyncGenFinalizer(void)
5698{
Victor Stinner50b48572018-11-01 01:51:40 +01005699 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005700 return tstate->async_gen_finalizer;
5701}
5702
Victor Stinner438a12d2019-05-24 17:01:38 +02005703PyFrameObject *
5704PyEval_GetFrame(void)
5705{
5706 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005707 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005708}
5709
Guido van Rossumb209a111997-04-29 18:18:01 +00005710PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005711_PyEval_GetBuiltins(PyThreadState *tstate)
5712{
5713 PyFrameObject *frame = tstate->frame;
5714 if (frame != NULL) {
5715 return frame->f_builtins;
5716 }
5717 return tstate->interp->builtins;
5718}
5719
5720PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005721PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005722{
Victor Stinner438a12d2019-05-24 17:01:38 +02005723 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005724 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005725}
5726
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005727/* Convenience function to get a builtin from its name */
5728PyObject *
5729_PyEval_GetBuiltinId(_Py_Identifier *name)
5730{
Victor Stinner438a12d2019-05-24 17:01:38 +02005731 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005732 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5733 if (attr) {
5734 Py_INCREF(attr);
5735 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005736 else if (!_PyErr_Occurred(tstate)) {
5737 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005738 }
5739 return attr;
5740}
5741
Guido van Rossumb209a111997-04-29 18:18:01 +00005742PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005743PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005744{
Victor Stinner438a12d2019-05-24 17:01:38 +02005745 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005746 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005747 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005748 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005750 }
5751
Victor Stinner438a12d2019-05-24 17:01:38 +02005752 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005753 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005754 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005755
5756 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005757 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005758}
5759
Guido van Rossumb209a111997-04-29 18:18:01 +00005760PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005761PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005762{
Victor Stinner438a12d2019-05-24 17:01:38 +02005763 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005764 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005765 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005766 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005767 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005768
5769 assert(current_frame->f_globals != NULL);
5770 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005771}
5772
Guido van Rossum6135a871995-01-09 17:53:26 +00005773int
Tim Peters5ba58662001-07-16 02:29:45 +00005774PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005775{
Victor Stinner438a12d2019-05-24 17:01:38 +02005776 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005777 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005778 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005780 if (current_frame != NULL) {
5781 const int codeflags = current_frame->f_code->co_flags;
5782 const int compilerflags = codeflags & PyCF_MASK;
5783 if (compilerflags) {
5784 result = 1;
5785 cf->cf_flags |= compilerflags;
5786 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005787#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005788 if (codeflags & CO_GENERATOR_ALLOWED) {
5789 result = 1;
5790 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5791 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005792#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005793 }
5794 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005795}
5796
Guido van Rossum3f5da241990-12-20 15:06:42 +00005797
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005798const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005799PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005801 if (PyMethod_Check(func))
5802 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5803 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005804 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005805 else if (PyCFunction_Check(func))
5806 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5807 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005808 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005809}
5810
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005811const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005812PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005814 if (PyMethod_Check(func))
5815 return "()";
5816 else if (PyFunction_Check(func))
5817 return "()";
5818 else if (PyCFunction_Check(func))
5819 return "()";
5820 else
5821 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005822}
5823
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005824#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005825if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005826 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005827 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005828 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 x = NULL; \
5830 } \
5831 else { \
5832 x = call; \
5833 if (tstate->c_profilefunc != NULL) { \
5834 if (x == NULL) { \
5835 call_trace_protected(tstate->c_profilefunc, \
5836 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005837 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005838 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005839 /* XXX should pass (type, value, tb) */ \
5840 } else { \
5841 if (call_trace(tstate->c_profilefunc, \
5842 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005843 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005844 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005845 Py_DECREF(x); \
5846 x = NULL; \
5847 } \
5848 } \
5849 } \
5850 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005851} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 x = call; \
5853 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005854
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005855
5856static PyObject *
5857trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005858 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005859 PyObject *func,
5860 PyObject **args, Py_ssize_t nargs,
5861 PyObject *kwnames)
5862{
5863 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005864 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005865 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005866 return x;
5867 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005868 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005869 /* We need to create a temporary bound method as argument
5870 for profiling.
5871
5872 If nargs == 0, then this cannot work because we have no
5873 "self". In any case, the call itself would raise
5874 TypeError (foo needs an argument), so we just skip
5875 profiling. */
5876 PyObject *self = args[0];
5877 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5878 if (func == NULL) {
5879 return NULL;
5880 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005881 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005882 args+1, nargs-1,
5883 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005884 Py_DECREF(func);
5885 return x;
5886 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005887 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005888}
5889
Victor Stinner415c5102017-01-11 00:54:57 +01005890/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5891 to reduce the stack consumption. */
5892Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005893call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005894 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005895 PyObject ***pp_stack,
5896 Py_ssize_t oparg,
5897 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005898{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005899 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005900 PyObject *func = *pfunc;
5901 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005902 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5903 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005904 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005905
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005906 if (tstate->use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005907 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005908 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005909 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005910 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005911 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005912
Victor Stinner438a12d2019-05-24 17:01:38 +02005913 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005914
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005915 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005916 while ((*pp_stack) > pfunc) {
5917 w = EXT_POP(*pp_stack);
5918 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005919 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005921 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005922}
5923
Jeremy Hylton52820442001-01-03 23:52:36 +00005924static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005925do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005926 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005927 PyObject *func,
5928 PyObject *callargs,
5929 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005930{
jdemeyere89de732018-09-19 12:06:20 +02005931 PyObject *result;
5932
scoder4c9ea092020-05-12 16:12:41 +02005933 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005934 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005935 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005936 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005937 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005938 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5939 if (nargs > 0 && tstate->use_tracing) {
5940 /* We need to create a temporary bound method as argument
5941 for profiling.
5942
5943 If nargs == 0, then this cannot work because we have no
5944 "self". In any case, the call itself would raise
5945 TypeError (foo needs an argument), so we just skip
5946 profiling. */
5947 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5948 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5949 if (func == NULL) {
5950 return NULL;
5951 }
5952
Victor Stinner4d231bc2019-11-14 13:36:21 +01005953 C_TRACE(result, _PyObject_FastCallDictTstate(
5954 tstate, func,
5955 &_PyTuple_ITEMS(callargs)[1],
5956 nargs - 1,
5957 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005958 Py_DECREF(func);
5959 return result;
5960 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005961 }
jdemeyere89de732018-09-19 12:06:20 +02005962 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005963}
5964
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005965/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005966 nb_index slot defined, and store in *pi.
5967 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005968 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005969 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005970*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005971int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005972_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005973{
Victor Stinner438a12d2019-05-24 17:01:38 +02005974 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005975 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005976 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005977 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005978 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005979 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005980 return 0;
5981 }
5982 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005983 _PyErr_SetString(tstate, PyExc_TypeError,
5984 "slice indices must be integers or "
5985 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005986 return 0;
5987 }
5988 *pi = x;
5989 }
5990 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005991}
5992
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005993int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005994_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005995{
Victor Stinner438a12d2019-05-24 17:01:38 +02005996 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005997 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005998 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005999 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006000 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006001 return 0;
6002 }
6003 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006004 _PyErr_SetString(tstate, PyExc_TypeError,
6005 "slice indices must be integers or "
6006 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006007 return 0;
6008 }
6009 *pi = x;
6010 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006011}
6012
Thomas Wouters52152252000-08-17 22:55:00 +00006013static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006014import_name(PyThreadState *tstate, PyFrameObject *f,
6015 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006016{
6017 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006018 PyObject *import_func, *res;
6019 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006020
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006021 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006022 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006023 if (!_PyErr_Occurred(tstate)) {
6024 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006025 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006026 return NULL;
6027 }
6028
6029 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006030 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006031 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006032 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006033 return NULL;
6034 }
6035 res = PyImport_ImportModuleLevelObject(
6036 name,
6037 f->f_globals,
6038 f->f_locals == NULL ? Py_None : f->f_locals,
6039 fromlist,
6040 ilevel);
6041 return res;
6042 }
6043
6044 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006045
6046 stack[0] = name;
6047 stack[1] = f->f_globals;
6048 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6049 stack[3] = fromlist;
6050 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006051 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006052 Py_DECREF(import_func);
6053 return res;
6054}
6055
6056static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006057import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006059 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006060 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006061
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006062 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006063 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006064 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006065 /* Issue #17636: in case this failed because of a circular relative
6066 import, try to fallback on reading the module directly from
6067 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006068 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006069 if (pkgname == NULL) {
6070 goto error;
6071 }
Oren Milman6db70332017-09-19 14:23:01 +03006072 if (!PyUnicode_Check(pkgname)) {
6073 Py_CLEAR(pkgname);
6074 goto error;
6075 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006076 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006077 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006078 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006079 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006080 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006081 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006082 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006083 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006084 goto error;
6085 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006086 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006087 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006088 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006089 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006090 if (pkgname == NULL) {
6091 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6092 if (pkgname_or_unknown == NULL) {
6093 Py_XDECREF(pkgpath);
6094 return NULL;
6095 }
6096 } else {
6097 pkgname_or_unknown = pkgname;
6098 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006099
6100 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006101 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006102 errmsg = PyUnicode_FromFormat(
6103 "cannot import name %R from %R (unknown location)",
6104 name, pkgname_or_unknown
6105 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006106 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006107 PyErr_SetImportError(errmsg, pkgname, NULL);
6108 }
6109 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006110 _Py_IDENTIFIER(__spec__);
6111 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006112 const char *fmt =
6113 _PyModuleSpec_IsInitializing(spec) ?
6114 "cannot import name %R from partially initialized module %R "
6115 "(most likely due to a circular import) (%S)" :
6116 "cannot import name %R from %R (%S)";
6117 Py_XDECREF(spec);
6118
6119 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006120 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006121 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006122 }
6123
Xiang Zhang4830f582017-03-21 11:13:42 +08006124 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006125 Py_XDECREF(pkgname_or_unknown);
6126 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006127 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006128}
Guido van Rossumac7be682001-01-17 15:42:30 +00006129
Thomas Wouters52152252000-08-17 22:55:00 +00006130static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006131import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006132{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006133 _Py_IDENTIFIER(__all__);
6134 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006135 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006136 int skip_leading_underscores = 0;
6137 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006138
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006139 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6140 return -1; /* Unexpected error */
6141 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006142 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006143 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6144 return -1;
6145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006146 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006147 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006148 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006149 return -1;
6150 }
6151 all = PyMapping_Keys(dict);
6152 Py_DECREF(dict);
6153 if (all == NULL)
6154 return -1;
6155 skip_leading_underscores = 1;
6156 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006158 for (pos = 0, err = 0; ; pos++) {
6159 name = PySequence_GetItem(all, pos);
6160 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006161 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006162 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006163 }
6164 else {
6165 _PyErr_Clear(tstate);
6166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006167 break;
6168 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006169 if (!PyUnicode_Check(name)) {
6170 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6171 if (modname == NULL) {
6172 Py_DECREF(name);
6173 err = -1;
6174 break;
6175 }
6176 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006177 _PyErr_Format(tstate, PyExc_TypeError,
6178 "module __name__ must be a string, not %.100s",
6179 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006180 }
6181 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006182 _PyErr_Format(tstate, PyExc_TypeError,
6183 "%s in %U.%s must be str, not %.100s",
6184 skip_leading_underscores ? "Key" : "Item",
6185 modname,
6186 skip_leading_underscores ? "__dict__" : "__all__",
6187 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006188 }
6189 Py_DECREF(modname);
6190 Py_DECREF(name);
6191 err = -1;
6192 break;
6193 }
6194 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006195 if (PyUnicode_READY(name) == -1) {
6196 Py_DECREF(name);
6197 err = -1;
6198 break;
6199 }
6200 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6201 Py_DECREF(name);
6202 continue;
6203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006204 }
6205 value = PyObject_GetAttr(v, name);
6206 if (value == NULL)
6207 err = -1;
6208 else if (PyDict_CheckExact(locals))
6209 err = PyDict_SetItem(locals, name, value);
6210 else
6211 err = PyObject_SetItem(locals, name, value);
6212 Py_DECREF(name);
6213 Py_XDECREF(value);
6214 if (err != 0)
6215 break;
6216 }
6217 Py_DECREF(all);
6218 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006219}
6220
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006221static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006222check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006223{
Victor Stinnera102ed72020-02-07 02:24:48 +01006224 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006225 /* check_args_iterable() may be called with a live exception:
6226 * clear it to prevent calling _PyObject_FunctionStr() with an
6227 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006228 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006229 PyObject *funcstr = _PyObject_FunctionStr(func);
6230 if (funcstr != NULL) {
6231 _PyErr_Format(tstate, PyExc_TypeError,
6232 "%U argument after * must be an iterable, not %.200s",
6233 funcstr, Py_TYPE(args)->tp_name);
6234 Py_DECREF(funcstr);
6235 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006236 return -1;
6237 }
6238 return 0;
6239}
6240
6241static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006242format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006243{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006244 /* _PyDict_MergeEx raises attribute
6245 * error (percolated from an attempt
6246 * to get 'keys' attribute) instead of
6247 * a type error if its second argument
6248 * is not a mapping.
6249 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006250 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006251 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006252 PyObject *funcstr = _PyObject_FunctionStr(func);
6253 if (funcstr != NULL) {
6254 _PyErr_Format(
6255 tstate, PyExc_TypeError,
6256 "%U argument after ** must be a mapping, not %.200s",
6257 funcstr, Py_TYPE(kwargs)->tp_name);
6258 Py_DECREF(funcstr);
6259 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006260 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006261 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006262 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006263 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006264 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006265 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006266 PyObject *funcstr = _PyObject_FunctionStr(func);
6267 if (funcstr != NULL) {
6268 PyObject *key = PyTuple_GET_ITEM(val, 0);
6269 _PyErr_Format(
6270 tstate, PyExc_TypeError,
6271 "%U got multiple values for keyword argument '%S'",
6272 funcstr, key);
6273 Py_DECREF(funcstr);
6274 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006275 Py_XDECREF(exc);
6276 Py_XDECREF(val);
6277 Py_XDECREF(tb);
6278 }
6279 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006280 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006281 }
6282 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006283}
6284
Guido van Rossumac7be682001-01-17 15:42:30 +00006285static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006286format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6287 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006289 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291 if (!obj)
6292 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006293
Serhiy Storchaka06515832016-11-20 09:13:07 +02006294 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006295 if (!obj_str)
6296 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006297
Victor Stinner438a12d2019-05-24 17:01:38 +02006298 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006299}
Guido van Rossum950361c1997-01-24 13:49:28 +00006300
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006301static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006302format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006303{
6304 PyObject *name;
6305 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006306 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006307 return;
6308 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6309 name = PyTuple_GET_ITEM(co->co_cellvars,
6310 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006311 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006312 PyExc_UnboundLocalError,
6313 UNBOUNDLOCAL_ERROR_MSG,
6314 name);
6315 } else {
6316 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6317 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006318 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006319 UNBOUNDFREE_ERROR_MSG, name);
6320 }
6321}
6322
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006323static void
Mark Shannonfee55262019-11-21 09:11:43 +00006324format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006325{
6326 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6327 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006328 _PyErr_Format(tstate, PyExc_TypeError,
6329 "'async with' received an object from __aenter__ "
6330 "that does not implement __await__: %.100s",
6331 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006332 }
Mark Shannonfee55262019-11-21 09:11:43 +00006333 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006334 _PyErr_Format(tstate, PyExc_TypeError,
6335 "'async with' received an object from __aexit__ "
6336 "that does not implement __await__: %.100s",
6337 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006338 }
6339 }
6340}
6341
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006342static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006343unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006344 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006345{
6346 PyObject *res;
6347 if (Py_REFCNT(v) == 2) {
6348 /* In the common case, there are 2 references to the value
6349 * stored in 'variable' when the += is performed: one on the
6350 * value stack (in 'v') and one still stored in the
6351 * 'variable'. We try to delete the variable now to reduce
6352 * the refcnt to 1.
6353 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006354 int opcode, oparg;
6355 NEXTOPARG();
6356 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006357 case STORE_FAST:
6358 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006359 PyObject **fastlocals = f->f_localsplus;
6360 if (GETLOCAL(oparg) == v)
6361 SETLOCAL(oparg, NULL);
6362 break;
6363 }
6364 case STORE_DEREF:
6365 {
6366 PyObject **freevars = (f->f_localsplus +
6367 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006368 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006369 if (PyCell_GET(c) == v) {
6370 PyCell_SET(c, NULL);
6371 Py_DECREF(v);
6372 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006373 break;
6374 }
6375 case STORE_NAME:
6376 {
6377 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006378 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006379 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006380 if (locals && PyDict_CheckExact(locals)) {
6381 PyObject *w = PyDict_GetItemWithError(locals, name);
6382 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006383 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006384 {
6385 Py_DECREF(v);
6386 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006387 }
6388 }
6389 break;
6390 }
6391 }
6392 }
6393 res = v;
6394 PyUnicode_Append(&res, w);
6395 return res;
6396}
6397
Guido van Rossum950361c1997-01-24 13:49:28 +00006398#ifdef DYNAMIC_EXECUTION_PROFILE
6399
Skip Montanarof118cb12001-10-15 20:51:38 +00006400static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006401getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006403 int i;
6404 PyObject *l = PyList_New(256);
6405 if (l == NULL) return NULL;
6406 for (i = 0; i < 256; i++) {
6407 PyObject *x = PyLong_FromLong(a[i]);
6408 if (x == NULL) {
6409 Py_DECREF(l);
6410 return NULL;
6411 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006412 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006413 }
6414 for (i = 0; i < 256; i++)
6415 a[i] = 0;
6416 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006417}
6418
6419PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006420_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006421{
6422#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006423 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006424#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006425 int i;
6426 PyObject *l = PyList_New(257);
6427 if (l == NULL) return NULL;
6428 for (i = 0; i < 257; i++) {
6429 PyObject *x = getarray(dxpairs[i]);
6430 if (x == NULL) {
6431 Py_DECREF(l);
6432 return NULL;
6433 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006434 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006435 }
6436 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006437#endif
6438}
6439
6440#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006441
6442Py_ssize_t
6443_PyEval_RequestCodeExtraIndex(freefunc free)
6444{
Victor Stinner81a7be32020-04-14 15:14:01 +02006445 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006446 Py_ssize_t new_index;
6447
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006448 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006449 return -1;
6450 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006451 new_index = interp->co_extra_user_count++;
6452 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006453 return new_index;
6454}
Łukasz Langaa785c872016-09-09 17:37:37 -07006455
6456static void
6457dtrace_function_entry(PyFrameObject *f)
6458{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006459 const char *filename;
6460 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006461 int lineno;
6462
Victor Stinner6d86a232020-04-29 00:56:58 +02006463 PyCodeObject *code = f->f_code;
6464 filename = PyUnicode_AsUTF8(code->co_filename);
6465 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006466 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006467
Andy Lestere6be9b52020-02-11 20:28:35 -06006468 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006469}
6470
6471static void
6472dtrace_function_return(PyFrameObject *f)
6473{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006474 const char *filename;
6475 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006476 int lineno;
6477
Victor Stinner6d86a232020-04-29 00:56:58 +02006478 PyCodeObject *code = f->f_code;
6479 filename = PyUnicode_AsUTF8(code->co_filename);
6480 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006481 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006482
Andy Lestere6be9b52020-02-11 20:28:35 -06006483 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006484}
6485
6486/* DTrace equivalent of maybe_call_line_trace. */
6487static void
6488maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00006489 PyTraceInfo *trace_info)
Łukasz Langaa785c872016-09-09 17:37:37 -07006490{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006491 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006492
6493 /* If the last instruction executed isn't in the current
6494 instruction window, reset the window.
6495 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006496 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006497 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006498 /* If the last instruction falls at the start of a line or if
6499 it represents a jump backwards, update the frame's line
6500 number and call the trace function. */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006501 if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006502 if (line != -1) {
6503 frame->f_lineno = line;
6504 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6505 if (!co_filename)
6506 co_filename = "?";
6507 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6508 if (!co_name)
6509 co_name = "?";
6510 PyDTrace_LINE(co_filename, co_name, line);
6511 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006512 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00006513 trace_info->instr_prev = frame->f_lasti;
Łukasz Langaa785c872016-09-09 17:37:37 -07006514}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006515
6516
6517/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6518 for the limited API. */
6519
6520#undef Py_EnterRecursiveCall
6521
6522int Py_EnterRecursiveCall(const char *where)
6523{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006524 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006525}
6526
6527#undef Py_LeaveRecursiveCall
6528
6529void Py_LeaveRecursiveCall(void)
6530{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006531 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006532}