blob: 53b596b304c213e535c4a0fc8c8cf789bab55795 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002
Guido van Rossum681d79a1995-07-18 14:51:37 +00003/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00004 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX document it!
6 */
7
Thomas Wouters477c8d52006-05-27 19:21:47 +00008/* enable more aggressive intra-module optimizations, where available */
db3l131d5512021-03-03 22:09:48 -05009/* affects both release and debug builds - see bpo-43271 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020014#include "pycore_call.h" // _PyObject_FastCallDictTstate()
15#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
16#include "pycore_code.h" // _PyCode_InitOpcache()
17#include "pycore_initconfig.h" // _PyStatus_OK()
18#include "pycore_object.h" // _PyObject_GC_TRACK()
19#include "pycore_pyerrors.h" // _PyErr_Fetch()
20#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020023#include "pycore_sysmodule.h" // _PySys_Audit()
24#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum5c5a9382021-01-29 18:02:29 -080032#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossumc6004111993-11-05 10:22:19 +000034#include <ctype.h>
35
Mark Shannon8e1b4062021-03-05 14:45:50 +000036typedef struct {
37 PyCodeObject *code; // The code object for the bounds. May be NULL.
38 int instr_prev; // Only valid if code != NULL.
39 PyCodeAddressRange bounds; // Only valid if code != NULL.
Mark Shannon9e7b2072021-04-13 11:08:14 +010040 CFrame cframe;
Mark Shannon8e1b4062021-03-05 14:45:50 +000041} 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
Guido van Rossum374a9221991-04-04 10:40:29 +00001113
Guido van Rossumb209a111997-04-29 18:18:01 +00001114PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001115PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001116{
Victor Stinner46496f92021-02-20 15:17:18 +01001117 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001118 if (locals == NULL) {
1119 locals = globals;
1120 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01001121 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannon0332e562021-02-01 10:42:03 +00001122 if (builtins == NULL) {
1123 return NULL;
1124 }
1125 PyFrameConstructor desc = {
1126 .fc_globals = globals,
1127 .fc_builtins = builtins,
1128 .fc_name = ((PyCodeObject *)co)->co_name,
1129 .fc_qualname = ((PyCodeObject *)co)->co_name,
1130 .fc_code = co,
1131 .fc_defaults = NULL,
1132 .fc_kwdefaults = NULL,
1133 .fc_closure = NULL
1134 };
Victor Stinner46496f92021-02-20 15:17:18 +01001135 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001136}
1137
1138
1139/* Interpreter main loop */
1140
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001141PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001142PyEval_EvalFrame(PyFrameObject *f)
1143{
Victor Stinner0b72b232020-03-12 23:18:39 +01001144 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001145 PyThreadState *tstate = _PyThreadState_GET();
1146 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001147}
1148
1149PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001151{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001152 PyThreadState *tstate = _PyThreadState_GET();
1153 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001154}
1155
Victor Stinnerda2914d2020-03-20 09:29:08 +01001156
1157/* Handle signals, pending calls, GIL drop request
1158 and asynchronous exception */
1159static int
1160eval_frame_handle_pending(PyThreadState *tstate)
1161{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001162 _PyRuntimeState * const runtime = &_PyRuntime;
1163 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001164
1165 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001166 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001167 if (handle_signals(tstate) != 0) {
1168 return -1;
1169 }
1170 }
1171
1172 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001173 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001174 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001175 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001176 return -1;
1177 }
1178 }
1179
1180 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001181 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001182 /* Give another thread a chance */
1183 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1184 Py_FatalError("tstate mix-up");
1185 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001186 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001187
1188 /* Other threads may run now */
1189
1190 take_gil(tstate);
1191
Victor Stinnere838a932020-05-05 19:56:48 +02001192#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1193 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1194#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001195 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1196 Py_FatalError("orphan tstate");
1197 }
Victor Stinnere838a932020-05-05 19:56:48 +02001198#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001199 }
1200
1201 /* Check for asynchronous exception. */
1202 if (tstate->async_exc != NULL) {
1203 PyObject *exc = tstate->async_exc;
1204 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001205 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001206 _PyErr_SetNone(tstate, exc);
1207 Py_DECREF(exc);
1208 return -1;
1209 }
1210
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001211#ifdef MS_WINDOWS
1212 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1213 // different thread than the Python thread, in which case
1214 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1215 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1216 // value. It prevents to interrupt the eval loop at every instruction if
1217 // the current Python thread cannot handle signals (if
1218 // _Py_ThreadCanHandleSignals() is false).
1219 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1220#endif
1221
Victor Stinnerda2914d2020-03-20 09:29:08 +01001222 return 0;
1223}
1224
Victor Stinner3c1e4812012-03-26 22:10:51 +02001225
Antoine Pitroub52ec782009-01-25 16:34:23 +00001226/* Computed GOTOs, or
1227 the-optimization-commonly-but-improperly-known-as-"threaded code"
1228 using gcc's labels-as-values extension
1229 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1230
1231 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001233 combined with a lookup table of jump addresses. However, since the
1234 indirect jump instruction is shared by all opcodes, the CPU will have a
1235 hard time making the right prediction for where to jump next (actually,
1236 it will be always wrong except in the uncommon case of a sequence of
1237 several identical opcodes).
1238
1239 "Threaded code" in contrast, uses an explicit jump table and an explicit
1240 indirect jump instruction at the end of each opcode. Since the jump
1241 instruction is at a different address for each opcode, the CPU will make a
1242 separate prediction for each of these instructions, which is equivalent to
1243 predicting the second opcode of each opcode pair. These predictions have
1244 a much better chance to turn out valid, especially in small bytecode loops.
1245
1246 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001248 and potentially many more instructions (depending on the pipeline width).
1249 A correctly predicted branch, however, is nearly free.
1250
1251 At the time of this writing, the "threaded code" version is up to 15-20%
1252 faster than the normal "switch" version, depending on the compiler and the
1253 CPU architecture.
1254
1255 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1256 because it would render the measurements invalid.
1257
1258
1259 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1260 indirect jumps by sharing them between all opcodes. Such optimizations
1261 can be disabled on gcc by using the -fno-gcse flag (or possibly
1262 -fno-crossjumping).
1263*/
1264
Mark Shannon28d28e02021-04-08 11:22:55 +01001265/* Use macros rather than inline functions, to make it as clear as possible
1266 * to the C compiler that the tracing check is a simple test then branch.
1267 * We want to be sure that the compiler knows this before it generates
1268 * the CFG.
1269 */
1270#ifdef LLTRACE
1271#define OR_LLTRACE || lltrace
1272#else
1273#define OR_LLTRACE
1274#endif
1275
1276#ifdef WITH_DTRACE
1277#define OR_DTRACE_LINE || PyDTrace_LINE_ENABLED()
1278#else
1279#define OR_DTRACE_LINE
1280#endif
1281
Antoine Pitrou042b1282010-08-13 21:15:58 +00001282#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001283#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001284#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001285#endif
1286
Antoine Pitrou042b1282010-08-13 21:15:58 +00001287#ifdef HAVE_COMPUTED_GOTOS
1288 #ifndef USE_COMPUTED_GOTOS
1289 #define USE_COMPUTED_GOTOS 1
1290 #endif
1291#else
1292 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1293 #error "Computed gotos are not supported on this compiler."
1294 #endif
1295 #undef USE_COMPUTED_GOTOS
1296 #define USE_COMPUTED_GOTOS 0
1297#endif
1298
1299#if USE_COMPUTED_GOTOS
Mark Shannon28d28e02021-04-08 11:22:55 +01001300#define TARGET(op) op: TARGET_##op
1301#define DISPATCH_GOTO() goto *opcode_targets[opcode]
Antoine Pitroub52ec782009-01-25 16:34:23 +00001302#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001303#define TARGET(op) op
Mark Shannon28d28e02021-04-08 11:22:55 +01001304#define DISPATCH_GOTO() goto dispatch_opcode
Antoine Pitroub52ec782009-01-25 16:34:23 +00001305#endif
1306
Mark Shannon28d28e02021-04-08 11:22:55 +01001307#define DISPATCH() \
1308 { \
Mark Shannon9e7b2072021-04-13 11:08:14 +01001309 if (trace_info.cframe.use_tracing OR_DTRACE_LINE OR_LLTRACE) { \
Mark Shannon28d28e02021-04-08 11:22:55 +01001310 goto tracing_dispatch; \
1311 } \
1312 f->f_lasti = INSTR_OFFSET(); \
1313 NEXTOPARG(); \
1314 DISPATCH_GOTO(); \
1315 }
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 */
Mark Shannon9e7b2072021-04-13 11:08:14 +01001597 _Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker;
Mark Shannond41bddd2021-03-25 12:00:30 +00001598 PyCodeObject *co;
1599
Mark Shannond41bddd2021-03-25 12:00:30 +00001600 const _Py_CODEUNIT *first_instr;
1601 PyObject *names;
1602 PyObject *consts;
1603 _PyOpcache *co_opcache;
1604
1605#ifdef LLTRACE
1606 _Py_IDENTIFIER(__ltrace__);
1607#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001608
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001609 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001611 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001612
Mark Shannon8e1b4062021-03-05 14:45:50 +00001613 PyTraceInfo trace_info;
Mark Shannon28d28e02021-04-08 11:22:55 +01001614 /* Mark trace_info as uninitialized */
Mark Shannon8e1b4062021-03-05 14:45:50 +00001615 trace_info.code = NULL;
1616
Mark Shannon9e7b2072021-04-13 11:08:14 +01001617 /* WARNING: Because the CFrame lives on the C stack,
1618 * but can be accessed from a heap allocated object (tstate)
1619 * strict stack discipline must be maintained.
1620 */
1621 CFrame *prev_cframe = tstate->cframe;
1622 trace_info.cframe.use_tracing = prev_cframe->use_tracing;
1623 trace_info.cframe.previous = prev_cframe;
1624 tstate->cframe = &trace_info.cframe;
1625
Mark Shannon8e1b4062021-03-05 14:45:50 +00001626 /* 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
Mark Shannon9e7b2072021-04-13 11:08:14 +01001630 if (trace_info.cframe.use_tracing) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 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);
Mark Shannon28d28e02021-04-08 11:22:55 +01001757 if (opcode != SETUP_FINALLY &&
1758 opcode != SETUP_WITH &&
1759 opcode != BEFORE_ASYNC_WITH &&
1760 opcode != YIELD_FROM) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001761 /* 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 */
Mark Shannon28d28e02021-04-08 11:22:55 +01001777 if (eval_frame_handle_pending(tstate) != 0) {
1778 goto error;
1779 }
1780 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001782
Mark Shannon28d28e02021-04-08 11:22:55 +01001783 tracing_dispatch:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 f->f_lasti = INSTR_OFFSET();
Mark Shannon28d28e02021-04-08 11:22:55 +01001785 NEXTOPARG();
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Łukasz Langaa785c872016-09-09 17:37:37 -07001787 if (PyDTrace_LINE_ENABLED())
Mark Shannon8e1b4062021-03-05 14:45:50 +00001788 maybe_dtrace_line(f, &trace_info);
Łukasz Langaa785c872016-09-09 17:37:37 -07001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001791
Mark Shannon9e7b2072021-04-13 11:08:14 +01001792 if (trace_info.cframe.use_tracing &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001793 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001795 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001797 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 err = maybe_call_line_trace(tstate->c_tracefunc,
1800 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001801 tstate, f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001802 &trace_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 /* Reload possibly changed frame fields */
1804 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001805 stack_pointer = f->f_valuestack+f->f_stackdepth;
1806 f->f_stackdepth = -1;
Mark Shannon28d28e02021-04-08 11:22:55 +01001807 if (err) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 goto error;
Mark Shannon28d28e02021-04-08 11:22:55 +01001810 }
1811 NEXTOPARG();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001813
Guido van Rossum96a42c81992-01-12 02:29:51 +00001814#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (lltrace) {
1818 if (HAS_ARG(opcode)) {
1819 printf("%d: %d, %d\n",
1820 f->f_lasti, opcode, oparg);
1821 }
1822 else {
1823 printf("%d: %d\n",
1824 f->f_lasti, opcode);
1825 }
1826 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001827#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001828
Mark Shannon28d28e02021-04-08 11:22:55 +01001829 dispatch_opcode:
1830#ifdef DYNAMIC_EXECUTION_PROFILE
1831#ifdef DXPAIRS
1832 dxpairs[lastopcode][opcode]++;
1833 lastopcode = opcode;
1834#endif
1835 dxp[opcode]++;
1836#endif
1837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001841 It is essential that any operation that fails must goto error
Mark Shannon28d28e02021-04-08 11:22:55 +01001842 and that all operation that succeed call DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Benjamin Petersonddd19492018-09-16 22:38:02 -07001844 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001845 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001846 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001847
Benjamin Petersonddd19492018-09-16 22:38:02 -07001848 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001849 PyObject *value = GETLOCAL(oparg);
1850 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001851 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 UNBOUNDLOCAL_ERROR_MSG,
1853 PyTuple_GetItem(co->co_varnames, oparg));
1854 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 Py_INCREF(value);
1857 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001858 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 }
1860
Benjamin Petersonddd19492018-09-16 22:38:02 -07001861 case TARGET(LOAD_CONST): {
1862 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 PyObject *value = GETITEM(consts, oparg);
1864 Py_INCREF(value);
1865 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001866 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(STORE_FAST): {
1870 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 PyObject *value = POP();
1872 SETLOCAL(oparg, value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001873 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001875
Benjamin Petersonddd19492018-09-16 22:38:02 -07001876 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 PyObject *value = POP();
1878 Py_DECREF(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001879 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Benjamin Petersonddd19492018-09-16 22:38:02 -07001882 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 PyObject *top = TOP();
1884 PyObject *second = SECOND();
1885 SET_TOP(second);
1886 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001887 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *top = TOP();
1892 PyObject *second = SECOND();
1893 PyObject *third = THIRD();
1894 SET_TOP(second);
1895 SET_SECOND(third);
1896 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001897 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Benjamin Petersonddd19492018-09-16 22:38:02 -07001900 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001901 PyObject *top = TOP();
1902 PyObject *second = SECOND();
1903 PyObject *third = THIRD();
1904 PyObject *fourth = FOURTH();
1905 SET_TOP(second);
1906 SET_SECOND(third);
1907 SET_THIRD(fourth);
1908 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001909 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001910 }
1911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 PyObject *top = TOP();
1914 Py_INCREF(top);
1915 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001916 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001922 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001924 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 SET_TOP(top);
1926 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001927 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001929
Benjamin Petersonddd19492018-09-16 22:38:02 -07001930 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 PyObject *value = TOP();
1932 PyObject *res = PyNumber_Positive(value);
1933 Py_DECREF(value);
1934 SET_TOP(res);
1935 if (res == NULL)
1936 goto error;
1937 DISPATCH();
1938 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Benjamin Petersonddd19492018-09-16 22:38:02 -07001940 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *value = TOP();
1942 PyObject *res = PyNumber_Negative(value);
1943 Py_DECREF(value);
1944 SET_TOP(res);
1945 if (res == NULL)
1946 goto error;
1947 DISPATCH();
1948 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Benjamin Petersonddd19492018-09-16 22:38:02 -07001950 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 PyObject *value = TOP();
1952 int err = PyObject_IsTrue(value);
1953 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (err == 0) {
1955 Py_INCREF(Py_True);
1956 SET_TOP(Py_True);
1957 DISPATCH();
1958 }
1959 else if (err > 0) {
1960 Py_INCREF(Py_False);
1961 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 DISPATCH();
1963 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001964 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 goto error;
1966 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 PyObject *value = TOP();
1970 PyObject *res = PyNumber_Invert(value);
1971 Py_DECREF(value);
1972 SET_TOP(res);
1973 if (res == NULL)
1974 goto error;
1975 DISPATCH();
1976 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Benjamin Petersonddd19492018-09-16 22:38:02 -07001978 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 PyObject *exp = POP();
1980 PyObject *base = TOP();
1981 PyObject *res = PyNumber_Power(base, exp, Py_None);
1982 Py_DECREF(base);
1983 Py_DECREF(exp);
1984 SET_TOP(res);
1985 if (res == NULL)
1986 goto error;
1987 DISPATCH();
1988 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Benjamin Petersonddd19492018-09-16 22:38:02 -07001990 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 PyObject *right = POP();
1992 PyObject *left = TOP();
1993 PyObject *res = PyNumber_Multiply(left, right);
1994 Py_DECREF(left);
1995 Py_DECREF(right);
1996 SET_TOP(res);
1997 if (res == NULL)
1998 goto error;
1999 DISPATCH();
2000 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Benjamin Petersonddd19492018-09-16 22:38:02 -07002002 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002003 PyObject *right = POP();
2004 PyObject *left = TOP();
2005 PyObject *res = PyNumber_MatrixMultiply(left, right);
2006 Py_DECREF(left);
2007 Py_DECREF(right);
2008 SET_TOP(res);
2009 if (res == NULL)
2010 goto error;
2011 DISPATCH();
2012 }
2013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 PyObject *divisor = POP();
2016 PyObject *dividend = TOP();
2017 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2018 Py_DECREF(dividend);
2019 Py_DECREF(divisor);
2020 SET_TOP(quotient);
2021 if (quotient == NULL)
2022 goto error;
2023 DISPATCH();
2024 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Benjamin Petersonddd19492018-09-16 22:38:02 -07002026 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PyObject *divisor = POP();
2028 PyObject *dividend = TOP();
2029 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2030 Py_DECREF(dividend);
2031 Py_DECREF(divisor);
2032 SET_TOP(quotient);
2033 if (quotient == NULL)
2034 goto error;
2035 DISPATCH();
2036 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 PyObject *divisor = POP();
2040 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002041 PyObject *res;
2042 if (PyUnicode_CheckExact(dividend) && (
2043 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2044 // fast path; string formatting, but not if the RHS is a str subclass
2045 // (see issue28598)
2046 res = PyUnicode_Format(dividend, divisor);
2047 } else {
2048 res = PyNumber_Remainder(dividend, divisor);
2049 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 Py_DECREF(divisor);
2051 Py_DECREF(dividend);
2052 SET_TOP(res);
2053 if (res == NULL)
2054 goto error;
2055 DISPATCH();
2056 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Benjamin Petersonddd19492018-09-16 22:38:02 -07002058 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 PyObject *right = POP();
2060 PyObject *left = TOP();
2061 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002062 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002063 CPython using bytecode, it is simply worthless.
2064 See http://bugs.python.org/issue21955 and
2065 http://bugs.python.org/issue10044 for the discussion. In short,
2066 no patch shown any impact on a realistic benchmark, only a minor
2067 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 if (PyUnicode_CheckExact(left) &&
2069 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002070 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002071 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002072 }
2073 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 sum = PyNumber_Add(left, right);
2075 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002076 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 Py_DECREF(right);
2078 SET_TOP(sum);
2079 if (sum == NULL)
2080 goto error;
2081 DISPATCH();
2082 }
2083
Benjamin Petersonddd19492018-09-16 22:38:02 -07002084 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 PyObject *right = POP();
2086 PyObject *left = TOP();
2087 PyObject *diff = PyNumber_Subtract(left, right);
2088 Py_DECREF(right);
2089 Py_DECREF(left);
2090 SET_TOP(diff);
2091 if (diff == NULL)
2092 goto error;
2093 DISPATCH();
2094 }
2095
Benjamin Petersonddd19492018-09-16 22:38:02 -07002096 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 PyObject *sub = POP();
2098 PyObject *container = TOP();
2099 PyObject *res = PyObject_GetItem(container, sub);
2100 Py_DECREF(container);
2101 Py_DECREF(sub);
2102 SET_TOP(res);
2103 if (res == NULL)
2104 goto error;
2105 DISPATCH();
2106 }
2107
Benjamin Petersonddd19492018-09-16 22:38:02 -07002108 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 PyObject *right = POP();
2110 PyObject *left = TOP();
2111 PyObject *res = PyNumber_Lshift(left, right);
2112 Py_DECREF(left);
2113 Py_DECREF(right);
2114 SET_TOP(res);
2115 if (res == NULL)
2116 goto error;
2117 DISPATCH();
2118 }
2119
Benjamin Petersonddd19492018-09-16 22:38:02 -07002120 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 PyObject *right = POP();
2122 PyObject *left = TOP();
2123 PyObject *res = PyNumber_Rshift(left, right);
2124 Py_DECREF(left);
2125 Py_DECREF(right);
2126 SET_TOP(res);
2127 if (res == NULL)
2128 goto error;
2129 DISPATCH();
2130 }
2131
Benjamin Petersonddd19492018-09-16 22:38:02 -07002132 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 PyObject *right = POP();
2134 PyObject *left = TOP();
2135 PyObject *res = PyNumber_And(left, right);
2136 Py_DECREF(left);
2137 Py_DECREF(right);
2138 SET_TOP(res);
2139 if (res == NULL)
2140 goto error;
2141 DISPATCH();
2142 }
2143
Benjamin Petersonddd19492018-09-16 22:38:02 -07002144 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 PyObject *right = POP();
2146 PyObject *left = TOP();
2147 PyObject *res = PyNumber_Xor(left, right);
2148 Py_DECREF(left);
2149 Py_DECREF(right);
2150 SET_TOP(res);
2151 if (res == NULL)
2152 goto error;
2153 DISPATCH();
2154 }
2155
Benjamin Petersonddd19492018-09-16 22:38:02 -07002156 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 PyObject *right = POP();
2158 PyObject *left = TOP();
2159 PyObject *res = PyNumber_Or(left, right);
2160 Py_DECREF(left);
2161 Py_DECREF(right);
2162 SET_TOP(res);
2163 if (res == NULL)
2164 goto error;
2165 DISPATCH();
2166 }
2167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 PyObject *v = POP();
2170 PyObject *list = PEEK(oparg);
2171 int err;
2172 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (err != 0)
2175 goto error;
2176 PREDICT(JUMP_ABSOLUTE);
2177 DISPATCH();
2178 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Benjamin Petersonddd19492018-09-16 22:38:02 -07002180 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002182 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 int err;
2184 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 if (err != 0)
2187 goto error;
2188 PREDICT(JUMP_ABSOLUTE);
2189 DISPATCH();
2190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Benjamin Petersonddd19492018-09-16 22:38:02 -07002192 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 PyObject *exp = POP();
2194 PyObject *base = TOP();
2195 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2196 Py_DECREF(base);
2197 Py_DECREF(exp);
2198 SET_TOP(res);
2199 if (res == NULL)
2200 goto error;
2201 DISPATCH();
2202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Benjamin Petersonddd19492018-09-16 22:38:02 -07002204 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PyObject *right = POP();
2206 PyObject *left = TOP();
2207 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2208 Py_DECREF(left);
2209 Py_DECREF(right);
2210 SET_TOP(res);
2211 if (res == NULL)
2212 goto error;
2213 DISPATCH();
2214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Benjamin Petersonddd19492018-09-16 22:38:02 -07002216 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002217 PyObject *right = POP();
2218 PyObject *left = TOP();
2219 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2220 Py_DECREF(left);
2221 Py_DECREF(right);
2222 SET_TOP(res);
2223 if (res == NULL)
2224 goto error;
2225 DISPATCH();
2226 }
2227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 PyObject *divisor = POP();
2230 PyObject *dividend = TOP();
2231 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2232 Py_DECREF(dividend);
2233 Py_DECREF(divisor);
2234 SET_TOP(quotient);
2235 if (quotient == NULL)
2236 goto error;
2237 DISPATCH();
2238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 PyObject *divisor = POP();
2242 PyObject *dividend = TOP();
2243 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2244 Py_DECREF(dividend);
2245 Py_DECREF(divisor);
2246 SET_TOP(quotient);
2247 if (quotient == NULL)
2248 goto error;
2249 DISPATCH();
2250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 PyObject *right = POP();
2254 PyObject *left = TOP();
2255 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2256 Py_DECREF(left);
2257 Py_DECREF(right);
2258 SET_TOP(mod);
2259 if (mod == NULL)
2260 goto error;
2261 DISPATCH();
2262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Benjamin Petersonddd19492018-09-16 22:38:02 -07002264 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 PyObject *right = POP();
2266 PyObject *left = TOP();
2267 PyObject *sum;
2268 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002269 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002270 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002271 }
2272 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 sum = PyNumber_InPlaceAdd(left, right);
2274 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 Py_DECREF(right);
2277 SET_TOP(sum);
2278 if (sum == NULL)
2279 goto error;
2280 DISPATCH();
2281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Benjamin Petersonddd19492018-09-16 22:38:02 -07002283 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 PyObject *right = POP();
2285 PyObject *left = TOP();
2286 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2287 Py_DECREF(left);
2288 Py_DECREF(right);
2289 SET_TOP(diff);
2290 if (diff == NULL)
2291 goto error;
2292 DISPATCH();
2293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Benjamin Petersonddd19492018-09-16 22:38:02 -07002295 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 PyObject *right = POP();
2297 PyObject *left = TOP();
2298 PyObject *res = PyNumber_InPlaceLshift(left, right);
2299 Py_DECREF(left);
2300 Py_DECREF(right);
2301 SET_TOP(res);
2302 if (res == NULL)
2303 goto error;
2304 DISPATCH();
2305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002306
Benjamin Petersonddd19492018-09-16 22:38:02 -07002307 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 PyObject *right = POP();
2309 PyObject *left = TOP();
2310 PyObject *res = PyNumber_InPlaceRshift(left, right);
2311 Py_DECREF(left);
2312 Py_DECREF(right);
2313 SET_TOP(res);
2314 if (res == NULL)
2315 goto error;
2316 DISPATCH();
2317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Benjamin Petersonddd19492018-09-16 22:38:02 -07002319 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PyObject *right = POP();
2321 PyObject *left = TOP();
2322 PyObject *res = PyNumber_InPlaceAnd(left, right);
2323 Py_DECREF(left);
2324 Py_DECREF(right);
2325 SET_TOP(res);
2326 if (res == NULL)
2327 goto error;
2328 DISPATCH();
2329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Benjamin Petersonddd19492018-09-16 22:38:02 -07002331 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 PyObject *right = POP();
2333 PyObject *left = TOP();
2334 PyObject *res = PyNumber_InPlaceXor(left, right);
2335 Py_DECREF(left);
2336 Py_DECREF(right);
2337 SET_TOP(res);
2338 if (res == NULL)
2339 goto error;
2340 DISPATCH();
2341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Benjamin Petersonddd19492018-09-16 22:38:02 -07002343 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 PyObject *right = POP();
2345 PyObject *left = TOP();
2346 PyObject *res = PyNumber_InPlaceOr(left, right);
2347 Py_DECREF(left);
2348 Py_DECREF(right);
2349 SET_TOP(res);
2350 if (res == NULL)
2351 goto error;
2352 DISPATCH();
2353 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002354
Benjamin Petersonddd19492018-09-16 22:38:02 -07002355 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 PyObject *sub = TOP();
2357 PyObject *container = SECOND();
2358 PyObject *v = THIRD();
2359 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002360 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002361 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 Py_DECREF(container);
2365 Py_DECREF(sub);
2366 if (err != 0)
2367 goto error;
2368 DISPATCH();
2369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002370
Benjamin Petersonddd19492018-09-16 22:38:02 -07002371 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 PyObject *sub = TOP();
2373 PyObject *container = SECOND();
2374 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002375 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002376 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 err = PyObject_DelItem(container, sub);
2378 Py_DECREF(container);
2379 Py_DECREF(sub);
2380 if (err != 0)
2381 goto error;
2382 DISPATCH();
2383 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002384
Benjamin Petersonddd19492018-09-16 22:38:02 -07002385 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002386 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002388 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002389 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002391 _PyErr_SetString(tstate, PyExc_RuntimeError,
2392 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 Py_DECREF(value);
2394 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002396 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 Py_DECREF(value);
2398 if (res == NULL)
2399 goto error;
2400 Py_DECREF(res);
2401 DISPATCH();
2402 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002403
Benjamin Petersonddd19492018-09-16 22:38:02 -07002404 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 switch (oparg) {
2407 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002409 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002412 /* fall through */
2413 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002414 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002415 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 break;
2418 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002419 _PyErr_SetString(tstate, PyExc_SystemError,
2420 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 break;
2422 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 goto error;
2424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002425
Benjamin Petersonddd19492018-09-16 22:38:02 -07002426 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002428 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002429 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002430 f->f_state = FRAME_RETURNED;
2431 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002432 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002434
Benjamin Petersonddd19492018-09-16 22:38:02 -07002435 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002436 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002437 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002438 PyObject *obj = TOP();
2439 PyTypeObject *type = Py_TYPE(obj);
2440
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002441 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002442 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002443 }
Yury Selivanov75445082015-05-11 22:57:16 -04002444
2445 if (getter != NULL) {
2446 iter = (*getter)(obj);
2447 Py_DECREF(obj);
2448 if (iter == NULL) {
2449 SET_TOP(NULL);
2450 goto error;
2451 }
2452 }
2453 else {
2454 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002455 _PyErr_Format(tstate, PyExc_TypeError,
2456 "'async for' requires an object with "
2457 "__aiter__ method, got %.100s",
2458 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002459 Py_DECREF(obj);
2460 goto error;
2461 }
2462
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002463 if (Py_TYPE(iter)->tp_as_async == NULL ||
2464 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002465
Yury Selivanov398ff912017-03-02 22:20:00 -05002466 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 _PyErr_Format(tstate, PyExc_TypeError,
2468 "'async for' received an object from __aiter__ "
2469 "that does not implement __anext__: %.100s",
2470 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002471 Py_DECREF(iter);
2472 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002473 }
2474
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002475 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002476 DISPATCH();
2477 }
2478
Benjamin Petersonddd19492018-09-16 22:38:02 -07002479 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002480 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002481 PyObject *next_iter = NULL;
2482 PyObject *awaitable = NULL;
2483 PyObject *aiter = TOP();
2484 PyTypeObject *type = Py_TYPE(aiter);
2485
Yury Selivanoveb636452016-09-08 22:01:51 -07002486 if (PyAsyncGen_CheckExact(aiter)) {
2487 awaitable = type->tp_as_async->am_anext(aiter);
2488 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002489 goto error;
2490 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002491 } else {
2492 if (type->tp_as_async != NULL){
2493 getter = type->tp_as_async->am_anext;
2494 }
Yury Selivanov75445082015-05-11 22:57:16 -04002495
Yury Selivanoveb636452016-09-08 22:01:51 -07002496 if (getter != NULL) {
2497 next_iter = (*getter)(aiter);
2498 if (next_iter == NULL) {
2499 goto error;
2500 }
2501 }
2502 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002503 _PyErr_Format(tstate, PyExc_TypeError,
2504 "'async for' requires an iterator with "
2505 "__anext__ method, got %.100s",
2506 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002507 goto error;
2508 }
Yury Selivanov75445082015-05-11 22:57:16 -04002509
Yury Selivanoveb636452016-09-08 22:01:51 -07002510 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2511 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002512 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002513 PyExc_TypeError,
2514 "'async for' received an invalid object "
2515 "from __anext__: %.100s",
2516 Py_TYPE(next_iter)->tp_name);
2517
2518 Py_DECREF(next_iter);
2519 goto error;
2520 } else {
2521 Py_DECREF(next_iter);
2522 }
2523 }
Yury Selivanov75445082015-05-11 22:57:16 -04002524
2525 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002526 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002527 DISPATCH();
2528 }
2529
Benjamin Petersonddd19492018-09-16 22:38:02 -07002530 case TARGET(GET_AWAITABLE): {
2531 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002532 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002533 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002534
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002535 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002536 int opcode_at_minus_3 = 0;
2537 if ((next_instr - first_instr) > 2) {
2538 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2539 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002541 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002542 _Py_OPCODE(next_instr[-2]));
2543 }
2544
Yury Selivanov75445082015-05-11 22:57:16 -04002545 Py_DECREF(iterable);
2546
Yury Selivanovc724bae2016-03-02 11:30:46 -05002547 if (iter != NULL && PyCoro_CheckExact(iter)) {
2548 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2549 if (yf != NULL) {
2550 /* `iter` is a coroutine object that is being
2551 awaited, `yf` is a pointer to the current awaitable
2552 being awaited on. */
2553 Py_DECREF(yf);
2554 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002555 _PyErr_SetString(tstate, PyExc_RuntimeError,
2556 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002557 /* The code below jumps to `error` if `iter` is NULL. */
2558 }
2559 }
2560
Yury Selivanov75445082015-05-11 22:57:16 -04002561 SET_TOP(iter); /* Even if it's NULL */
2562
2563 if (iter == NULL) {
2564 goto error;
2565 }
2566
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002567 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002568 DISPATCH();
2569 }
2570
Benjamin Petersonddd19492018-09-16 22:38:02 -07002571 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002573 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002574 PySendResult gen_status;
2575 if (tstate->c_tracefunc == NULL) {
2576 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002577 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002578 _Py_IDENTIFIER(send);
Victor Stinner09bbebe2021-04-11 00:17:39 +02002579 if (Py_IsNone(v) && PyIter_Check(receiver)) {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002580 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002581 }
2582 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002583 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002584 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002585 if (retval == NULL) {
2586 if (tstate->c_tracefunc != NULL
2587 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002588 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002589 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2590 gen_status = PYGEN_RETURN;
2591 }
2592 else {
2593 gen_status = PYGEN_ERROR;
2594 }
2595 }
2596 else {
2597 gen_status = PYGEN_NEXT;
2598 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002599 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002601 if (gen_status == PYGEN_ERROR) {
2602 assert (retval == NULL);
2603 goto error;
2604 }
2605 if (gen_status == PYGEN_RETURN) {
2606 assert (retval != NULL);
2607
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002608 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002609 SET_TOP(retval);
2610 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002612 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002613 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002614 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002615 /* and repeat... */
Mark Shannonfcb55c02021-04-01 16:00:31 +01002616 assert(f->f_lasti > 0);
2617 f->f_lasti -= 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01002618 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002619 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002620 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002622
Benjamin Petersonddd19492018-09-16 22:38:02 -07002623 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002625
2626 if (co->co_flags & CO_ASYNC_GENERATOR) {
2627 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2628 Py_DECREF(retval);
2629 if (w == NULL) {
2630 retval = NULL;
2631 goto error;
2632 }
2633 retval = w;
2634 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002635 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002636 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002637 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002638 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002639
Mark Shannonb37181e2021-04-06 11:48:59 +01002640 case TARGET(GEN_START): {
2641 PyObject *none = POP();
2642 Py_DECREF(none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02002643 if (!Py_IsNone(none)) {
Mark Shannonb37181e2021-04-06 11:48:59 +01002644 if (oparg > 2) {
2645 _PyErr_SetString(tstate, PyExc_SystemError,
2646 "Illegal kind for GEN_START");
2647 }
2648 else {
2649 static const char *gen_kind[3] = {
2650 "generator",
2651 "coroutine",
2652 "async generator"
2653 };
2654 _PyErr_Format(tstate, PyExc_TypeError,
2655 "can't send non-None value to a "
2656 "just-started %s",
2657 gen_kind[oparg]);
2658 }
2659 goto error;
2660 }
2661 DISPATCH();
2662 }
2663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002665 PyObject *type, *value, *traceback;
2666 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 PyTryBlock *b = PyFrame_BlockPop(f);
2668 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002669 _PyErr_SetString(tstate, PyExc_SystemError,
2670 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002673 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2674 STACK_LEVEL() <= (b)->b_level + 4);
2675 exc_info = tstate->exc_info;
2676 type = exc_info->exc_type;
2677 value = exc_info->exc_value;
2678 traceback = exc_info->exc_traceback;
2679 exc_info->exc_type = POP();
2680 exc_info->exc_value = POP();
2681 exc_info->exc_traceback = POP();
2682 Py_XDECREF(type);
2683 Py_XDECREF(value);
2684 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002687
Benjamin Petersonddd19492018-09-16 22:38:02 -07002688 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002689 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002692
Mark Shannonfee55262019-11-21 09:11:43 +00002693 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002694 assert(f->f_iblock > 0);
2695 if (oparg) {
2696 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2697 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002698 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002699 PyObject *val = POP();
2700 PyObject *tb = POP();
2701 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002702 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002703 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002705
Benjamin Petersonddd19492018-09-16 22:38:02 -07002706 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002707 PyObject *exc = POP();
2708 assert(PyExceptionClass_Check(exc));
2709 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2710 PyTryBlock *b = PyFrame_BlockPop(f);
2711 assert(b->b_type == EXCEPT_HANDLER);
2712 Py_DECREF(exc);
2713 UNWIND_EXCEPT_HANDLER(b);
2714 Py_DECREF(POP());
2715 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002716 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002717 }
2718 else {
2719 PyObject *val = POP();
2720 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002721 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002722 goto exception_unwind;
2723 }
2724 }
2725
Zackery Spytzce6a0702019-08-25 03:44:09 -06002726 case TARGET(LOAD_ASSERTION_ERROR): {
2727 PyObject *value = PyExc_AssertionError;
2728 Py_INCREF(value);
2729 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002730 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002731 }
2732
Benjamin Petersonddd19492018-09-16 22:38:02 -07002733 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002734 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002735
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002737 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002738 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002740 if (!_PyErr_Occurred(tstate)) {
2741 _PyErr_SetString(tstate, PyExc_NameError,
2742 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002743 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002747 }
2748 else {
2749 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2750 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002751 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2753 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002754 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2755 _PyErr_SetString(tstate, PyExc_NameError,
2756 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002758 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002761 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002763
Benjamin Petersonddd19492018-09-16 22:38:02 -07002764 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 PyObject *name = GETITEM(names, oparg);
2766 PyObject *v = POP();
2767 PyObject *ns = f->f_locals;
2768 int err;
2769 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002770 _PyErr_Format(tstate, PyExc_SystemError,
2771 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 if (PyDict_CheckExact(ns))
2776 err = PyDict_SetItem(ns, name, v);
2777 else
2778 err = PyObject_SetItem(ns, name, v);
2779 Py_DECREF(v);
2780 if (err != 0)
2781 goto error;
2782 DISPATCH();
2783 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 PyObject *name = GETITEM(names, oparg);
2787 PyObject *ns = f->f_locals;
2788 int err;
2789 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002790 _PyErr_Format(tstate, PyExc_SystemError,
2791 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 err = PyObject_DelItem(ns, name);
2795 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002796 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 NAME_ERROR_MSG,
2798 name);
2799 goto error;
2800 }
2801 DISPATCH();
2802 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002803
Benjamin Petersonddd19492018-09-16 22:38:02 -07002804 case TARGET(UNPACK_SEQUENCE): {
2805 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 PyObject *seq = POP(), *item, **items;
2807 if (PyTuple_CheckExact(seq) &&
2808 PyTuple_GET_SIZE(seq) == oparg) {
2809 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 item = items[oparg];
2812 Py_INCREF(item);
2813 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 } else if (PyList_CheckExact(seq) &&
2816 PyList_GET_SIZE(seq) == oparg) {
2817 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 item = items[oparg];
2820 Py_INCREF(item);
2821 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002823 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002825 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 } else {
2827 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 Py_DECREF(seq);
2829 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002832 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002834
Benjamin Petersonddd19492018-09-16 22:38:02 -07002835 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2837 PyObject *seq = POP();
2838
Victor Stinner438a12d2019-05-24 17:01:38 +02002839 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 stack_pointer + totalargs)) {
2841 stack_pointer += totalargs;
2842 } else {
2843 Py_DECREF(seq);
2844 goto error;
2845 }
2846 Py_DECREF(seq);
2847 DISPATCH();
2848 }
2849
Benjamin Petersonddd19492018-09-16 22:38:02 -07002850 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 PyObject *name = GETITEM(names, oparg);
2852 PyObject *owner = TOP();
2853 PyObject *v = SECOND();
2854 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002855 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 Py_DECREF(owner);
2859 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_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 PyObject *name = GETITEM(names, oparg);
2866 PyObject *owner = POP();
2867 int err;
2868 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2869 Py_DECREF(owner);
2870 if (err != 0)
2871 goto error;
2872 DISPATCH();
2873 }
2874
Benjamin Petersonddd19492018-09-16 22:38:02 -07002875 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 PyObject *name = GETITEM(names, oparg);
2877 PyObject *v = POP();
2878 int err;
2879 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 if (err != 0)
2882 goto error;
2883 DISPATCH();
2884 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002885
Benjamin Petersonddd19492018-09-16 22:38:02 -07002886 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *name = GETITEM(names, oparg);
2888 int err;
2889 err = PyDict_DelItem(f->f_globals, name);
2890 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002891 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2892 format_exc_check_arg(tstate, PyExc_NameError,
2893 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002894 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002896 }
2897 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002899
Benjamin Petersonddd19492018-09-16 22:38:02 -07002900 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 PyObject *name = GETITEM(names, oparg);
2902 PyObject *locals = f->f_locals;
2903 PyObject *v;
2904 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002905 _PyErr_Format(tstate, PyExc_SystemError,
2906 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002910 v = PyDict_GetItemWithError(locals, name);
2911 if (v != NULL) {
2912 Py_INCREF(v);
2913 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002914 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002915 goto error;
2916 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 }
2918 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002920 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002921 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002922 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002923 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 }
2925 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002927 v = PyDict_GetItemWithError(f->f_globals, name);
2928 if (v != NULL) {
2929 Py_INCREF(v);
2930 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002931 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002932 goto error;
2933 }
2934 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002935 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002936 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002938 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002939 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002940 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002942 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002946 }
2947 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 v = PyObject_GetItem(f->f_builtins, name);
2949 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002950 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002951 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002952 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002954 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002956 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002963
Benjamin Petersonddd19492018-09-16 22:38:02 -07002964 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002965 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002967 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002968 && PyDict_CheckExact(f->f_builtins))
2969 {
Inada Naoki91234a12019-06-03 21:30:58 +09002970 OPCACHE_CHECK();
2971 if (co_opcache != NULL && co_opcache->optimized > 0) {
2972 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2973
2974 if (lg->globals_ver ==
2975 ((PyDictObject *)f->f_globals)->ma_version_tag
2976 && lg->builtins_ver ==
2977 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2978 {
2979 PyObject *ptr = lg->ptr;
2980 OPCACHE_STAT_GLOBAL_HIT();
2981 assert(ptr != NULL);
2982 Py_INCREF(ptr);
2983 PUSH(ptr);
2984 DISPATCH();
2985 }
2986 }
2987
2988 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002990 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 name);
2992 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002993 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002994 /* _PyDict_LoadGlobal() returns NULL without raising
2995 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002996 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002997 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002998 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 }
Inada Naoki91234a12019-06-03 21:30:58 +09003001
3002 if (co_opcache != NULL) {
3003 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
3004
3005 if (co_opcache->optimized == 0) {
3006 /* Wasn't optimized before. */
3007 OPCACHE_STAT_GLOBAL_OPT();
3008 } else {
3009 OPCACHE_STAT_GLOBAL_MISS();
3010 }
3011
3012 co_opcache->optimized = 1;
3013 lg->globals_ver =
3014 ((PyDictObject *)f->f_globals)->ma_version_tag;
3015 lg->builtins_ver =
3016 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3017 lg->ptr = v; /* borrowed */
3018 }
3019
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003022 else {
3023 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003024
3025 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003026 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 v = PyObject_GetItem(f->f_globals, name);
3028 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003029 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003030 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003031 }
3032 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003033
Victor Stinnerb4efc962015-11-20 09:24:02 +01003034 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 v = PyObject_GetItem(f->f_builtins, name);
3036 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003037 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003038 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003039 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003040 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003041 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003043 }
3044 }
3045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003049
Benjamin Petersonddd19492018-09-16 22:38:02 -07003050 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 PyObject *v = GETLOCAL(oparg);
3052 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 SETLOCAL(oparg, NULL);
3054 DISPATCH();
3055 }
3056 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003057 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 UNBOUNDLOCAL_ERROR_MSG,
3059 PyTuple_GetItem(co->co_varnames, oparg)
3060 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 goto error;
3062 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003066 PyObject *oldobj = PyCell_GET(cell);
3067 if (oldobj != NULL) {
3068 PyCell_SET(cell, NULL);
3069 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003070 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003071 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003072 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 goto error;
3074 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003075
Benjamin Petersonddd19492018-09-16 22:38:02 -07003076 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *cell = freevars[oparg];
3078 Py_INCREF(cell);
3079 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003082
Benjamin Petersonddd19492018-09-16 22:38:02 -07003083 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003084 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003085 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003086 assert(locals);
3087 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3088 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3089 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3090 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3091 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003092 value = PyDict_GetItemWithError(locals, name);
3093 if (value != NULL) {
3094 Py_INCREF(value);
3095 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003096 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003097 goto error;
3098 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003099 }
3100 else {
3101 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003102 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003103 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003104 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003105 }
3106 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003107 }
3108 }
3109 if (!value) {
3110 PyObject *cell = freevars[oparg];
3111 value = PyCell_GET(cell);
3112 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003113 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003114 goto error;
3115 }
3116 Py_INCREF(value);
3117 }
3118 PUSH(value);
3119 DISPATCH();
3120 }
3121
Benjamin Petersonddd19492018-09-16 22:38:02 -07003122 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 PyObject *cell = freevars[oparg];
3124 PyObject *value = PyCell_GET(cell);
3125 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003126 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 Py_INCREF(value);
3130 PUSH(value);
3131 DISPATCH();
3132 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003133
Benjamin Petersonddd19492018-09-16 22:38:02 -07003134 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 PyObject *v = POP();
3136 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003137 PyObject *oldobj = PyCell_GET(cell);
3138 PyCell_SET(cell, v);
3139 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 DISPATCH();
3141 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003142
Benjamin Petersonddd19492018-09-16 22:38:02 -07003143 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003144 PyObject *str;
3145 PyObject *empty = PyUnicode_New(0, 0);
3146 if (empty == NULL) {
3147 goto error;
3148 }
3149 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3150 Py_DECREF(empty);
3151 if (str == NULL)
3152 goto error;
3153 while (--oparg >= 0) {
3154 PyObject *item = POP();
3155 Py_DECREF(item);
3156 }
3157 PUSH(str);
3158 DISPATCH();
3159 }
3160
Benjamin Petersonddd19492018-09-16 22:38:02 -07003161 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 PyObject *tup = PyTuple_New(oparg);
3163 if (tup == NULL)
3164 goto error;
3165 while (--oparg >= 0) {
3166 PyObject *item = POP();
3167 PyTuple_SET_ITEM(tup, oparg, item);
3168 }
3169 PUSH(tup);
3170 DISPATCH();
3171 }
3172
Benjamin Petersonddd19492018-09-16 22:38:02 -07003173 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 PyObject *list = PyList_New(oparg);
3175 if (list == NULL)
3176 goto error;
3177 while (--oparg >= 0) {
3178 PyObject *item = POP();
3179 PyList_SET_ITEM(list, oparg, item);
3180 }
3181 PUSH(list);
3182 DISPATCH();
3183 }
3184
Mark Shannon13bc1392020-01-23 09:25:17 +00003185 case TARGET(LIST_TO_TUPLE): {
3186 PyObject *list = POP();
3187 PyObject *tuple = PyList_AsTuple(list);
3188 Py_DECREF(list);
3189 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003190 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003191 }
3192 PUSH(tuple);
3193 DISPATCH();
3194 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003195
Mark Shannon13bc1392020-01-23 09:25:17 +00003196 case TARGET(LIST_EXTEND): {
3197 PyObject *iterable = POP();
3198 PyObject *list = PEEK(oparg);
3199 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3200 if (none_val == NULL) {
3201 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003202 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003203 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003204 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003205 _PyErr_Format(tstate, PyExc_TypeError,
3206 "Value after * must be an iterable, not %.200s",
3207 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003208 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003209 Py_DECREF(iterable);
3210 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003211 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003212 Py_DECREF(none_val);
3213 Py_DECREF(iterable);
3214 DISPATCH();
3215 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003216
Mark Shannon13bc1392020-01-23 09:25:17 +00003217 case TARGET(SET_UPDATE): {
3218 PyObject *iterable = POP();
3219 PyObject *set = PEEK(oparg);
3220 int err = _PySet_Update(set, iterable);
3221 Py_DECREF(iterable);
3222 if (err < 0) {
3223 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003224 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003225 DISPATCH();
3226 }
3227
Benjamin Petersonddd19492018-09-16 22:38:02 -07003228 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 PyObject *set = PySet_New(NULL);
3230 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003231 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 if (set == NULL)
3233 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003234 for (i = oparg; i > 0; i--) {
3235 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 if (err == 0)
3237 err = PySet_Add(set, item);
3238 Py_DECREF(item);
3239 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003240 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 if (err != 0) {
3242 Py_DECREF(set);
3243 goto error;
3244 }
3245 PUSH(set);
3246 DISPATCH();
3247 }
3248
Benjamin Petersonddd19492018-09-16 22:38:02 -07003249 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003250 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3252 if (map == NULL)
3253 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003254 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003255 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003256 PyObject *key = PEEK(2*i);
3257 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003258 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003259 if (err != 0) {
3260 Py_DECREF(map);
3261 goto error;
3262 }
3263 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003264
3265 while (oparg--) {
3266 Py_DECREF(POP());
3267 Py_DECREF(POP());
3268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 PUSH(map);
3270 DISPATCH();
3271 }
3272
Benjamin Petersonddd19492018-09-16 22:38:02 -07003273 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 _Py_IDENTIFIER(__annotations__);
3275 int err;
3276 PyObject *ann_dict;
3277 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003278 _PyErr_Format(tstate, PyExc_SystemError,
3279 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003280 goto error;
3281 }
3282 /* check if __annotations__ in locals()... */
3283 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003284 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003285 &PyId___annotations__);
3286 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003287 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003288 goto error;
3289 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003290 /* ...if not, create a new one */
3291 ann_dict = PyDict_New();
3292 if (ann_dict == NULL) {
3293 goto error;
3294 }
3295 err = _PyDict_SetItemId(f->f_locals,
3296 &PyId___annotations__, ann_dict);
3297 Py_DECREF(ann_dict);
3298 if (err != 0) {
3299 goto error;
3300 }
3301 }
3302 }
3303 else {
3304 /* do the same if locals() is not a dict */
3305 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3306 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003307 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003308 }
3309 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3310 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003311 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003312 goto error;
3313 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003314 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003315 ann_dict = PyDict_New();
3316 if (ann_dict == NULL) {
3317 goto error;
3318 }
3319 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3320 Py_DECREF(ann_dict);
3321 if (err != 0) {
3322 goto error;
3323 }
3324 }
3325 else {
3326 Py_DECREF(ann_dict);
3327 }
3328 }
3329 DISPATCH();
3330 }
3331
Benjamin Petersonddd19492018-09-16 22:38:02 -07003332 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003333 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003334 PyObject *map;
3335 PyObject *keys = TOP();
3336 if (!PyTuple_CheckExact(keys) ||
3337 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003338 _PyErr_SetString(tstate, PyExc_SystemError,
3339 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003340 goto error;
3341 }
3342 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3343 if (map == NULL) {
3344 goto error;
3345 }
3346 for (i = oparg; i > 0; i--) {
3347 int err;
3348 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3349 PyObject *value = PEEK(i + 1);
3350 err = PyDict_SetItem(map, key, value);
3351 if (err != 0) {
3352 Py_DECREF(map);
3353 goto error;
3354 }
3355 }
3356
3357 Py_DECREF(POP());
3358 while (oparg--) {
3359 Py_DECREF(POP());
3360 }
3361 PUSH(map);
3362 DISPATCH();
3363 }
3364
Mark Shannon8a4cd702020-01-27 09:57:45 +00003365 case TARGET(DICT_UPDATE): {
3366 PyObject *update = POP();
3367 PyObject *dict = PEEK(oparg);
3368 if (PyDict_Update(dict, update) < 0) {
3369 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3370 _PyErr_Format(tstate, PyExc_TypeError,
3371 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003372 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003373 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003374 Py_DECREF(update);
3375 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003376 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003377 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003378 DISPATCH();
3379 }
3380
Mark Shannon8a4cd702020-01-27 09:57:45 +00003381 case TARGET(DICT_MERGE): {
3382 PyObject *update = POP();
3383 PyObject *dict = PEEK(oparg);
3384
3385 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3386 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3387 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003388 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003389 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003390 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003391 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003392 DISPATCH();
3393 }
3394
Benjamin Petersonddd19492018-09-16 22:38:02 -07003395 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003396 PyObject *value = TOP();
3397 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 PyObject *map;
3399 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003400 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003401 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003403 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 Py_DECREF(value);
3405 Py_DECREF(key);
3406 if (err != 0)
3407 goto error;
3408 PREDICT(JUMP_ABSOLUTE);
3409 DISPATCH();
3410 }
3411
Benjamin Petersonddd19492018-09-16 22:38:02 -07003412 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 PyObject *name = GETITEM(names, oparg);
3414 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003415
3416 PyTypeObject *type = Py_TYPE(owner);
3417 PyObject *res;
3418 PyObject **dictptr;
3419 PyObject *dict;
3420 _PyOpCodeOpt_LoadAttr *la;
3421
3422 OPCACHE_STAT_ATTR_TOTAL();
3423
3424 OPCACHE_CHECK();
3425 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3426 {
3427 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003428 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003429 la = &co_opcache->u.la;
3430 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3431 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003432 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3433 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3434 // so ~offset is strictly < -1 (assuming 2's complement).
3435 if (la->hint < -1) {
3436 // Even faster path -- slot hint.
3437 Py_ssize_t offset = ~la->hint;
3438 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3439 char *addr = (char *)owner + offset;
3440 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003441 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003442 Py_INCREF(res);
3443 SET_TOP(res);
3444 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003445 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003446 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003447 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3448 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003449 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003450 // Fast path for dict.
3451 assert(type->tp_dict != NULL);
3452 assert(type->tp_dictoffset > 0);
3453
3454 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3455 dict = *dictptr;
3456 if (dict != NULL && PyDict_CheckExact(dict)) {
3457 Py_ssize_t hint = la->hint;
3458 Py_INCREF(dict);
3459 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003460 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003461 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003462 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003463 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003464 if (la->hint == hint && hint >= 0) {
3465 // Our hint has helped -- cache hit.
3466 OPCACHE_STAT_ATTR_HIT();
3467 } else {
3468 // The hint we provided didn't work.
3469 // Maybe next time?
3470 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3471 }
3472
3473 Py_INCREF(res);
3474 SET_TOP(res);
3475 Py_DECREF(owner);
3476 Py_DECREF(dict);
3477 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003478 }
3479 else {
3480 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003481 // This attribute can be missing sometimes;
3482 // we don't want to optimize this lookup.
3483 OPCACHE_DEOPT_LOAD_ATTR();
3484 Py_DECREF(dict);
3485 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003486 }
3487 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003488 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3489 OPCACHE_DEOPT_LOAD_ATTR();
3490 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003491 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003492 }
3493 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003494 // The type of the object has either been updated,
3495 // or is different. Maybe it will stabilize?
3496 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3497 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003498 OPCACHE_STAT_ATTR_MISS();
3499 }
3500
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003501 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003502 type->tp_getattro == PyObject_GenericGetAttr)
3503 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003504 if (type->tp_dict == NULL) {
3505 if (PyType_Ready(type) < 0) {
3506 Py_DECREF(owner);
3507 SET_TOP(NULL);
3508 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003509 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003510 }
3511 PyObject *descr = _PyType_Lookup(type, name);
3512 if (descr != NULL) {
3513 // We found an attribute with a data-like descriptor.
3514 PyTypeObject *dtype = Py_TYPE(descr);
3515 if (dtype == &PyMemberDescr_Type) { // It's a slot
3516 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3517 struct PyMemberDef *dmem = member->d_member;
3518 if (dmem->type == T_OBJECT_EX) {
3519 Py_ssize_t offset = dmem->offset;
3520 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003521
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003522 if (co_opcache->optimized == 0) {
3523 // First time we optimize this opcode.
3524 OPCACHE_STAT_ATTR_OPT();
3525 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3526 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3527 }
3528
3529 la = &co_opcache->u.la;
3530 la->type = type;
3531 la->tp_version_tag = type->tp_version_tag;
3532 la->hint = ~offset;
3533
3534 char *addr = (char *)owner + offset;
3535 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003536 if (res != NULL) {
3537 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003538 Py_DECREF(owner);
3539 SET_TOP(res);
3540
Pablo Galindo109826c2020-10-20 06:22:44 +01003541 DISPATCH();
3542 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003543 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003544 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003545 // Else it's a slot of a different type. We don't handle those.
3546 }
3547 // Else it's some other kind of descriptor that we don't handle.
3548 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003549 }
3550 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003551 // We found an instance with a __dict__.
3552 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3553 dict = *dictptr;
3554
3555 if (dict != NULL && PyDict_CheckExact(dict)) {
3556 Py_INCREF(dict);
3557 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003558 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003559 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3560 if (res != NULL) {
3561 Py_INCREF(res);
3562 Py_DECREF(dict);
3563 Py_DECREF(owner);
3564 SET_TOP(res);
3565
3566 if (co_opcache->optimized == 0) {
3567 // First time we optimize this opcode.
3568 OPCACHE_STAT_ATTR_OPT();
3569 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3570 }
3571
3572 la = &co_opcache->u.la;
3573 la->type = type;
3574 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003575 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003576 la->hint = hint;
3577
3578 DISPATCH();
3579 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003580 else {
3581 _PyErr_Clear(tstate);
3582 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003583 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003584 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003585 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003586 OPCACHE_DEOPT_LOAD_ATTR();
3587 }
3588 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003589 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003590 OPCACHE_DEOPT_LOAD_ATTR();
3591 }
3592 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3593 OPCACHE_DEOPT_LOAD_ATTR();
3594 }
3595 }
3596
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003597 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003598 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 Py_DECREF(owner);
3600 SET_TOP(res);
3601 if (res == NULL)
3602 goto error;
3603 DISPATCH();
3604 }
3605
Benjamin Petersonddd19492018-09-16 22:38:02 -07003606 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003607 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003608 PyObject *right = POP();
3609 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003610 PyObject *res = PyObject_RichCompare(left, right, oparg);
3611 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003612 Py_DECREF(left);
3613 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 if (res == NULL)
3615 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 PREDICT(POP_JUMP_IF_FALSE);
3617 PREDICT(POP_JUMP_IF_TRUE);
3618 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003620
Mark Shannon9af0e472020-01-14 10:12:45 +00003621 case TARGET(IS_OP): {
3622 PyObject *right = POP();
3623 PyObject *left = TOP();
Victor Stinner09bbebe2021-04-11 00:17:39 +02003624 int res = Py_Is(left, right) ^ oparg;
Mark Shannon9af0e472020-01-14 10:12:45 +00003625 PyObject *b = res ? Py_True : Py_False;
3626 Py_INCREF(b);
3627 SET_TOP(b);
3628 Py_DECREF(left);
3629 Py_DECREF(right);
3630 PREDICT(POP_JUMP_IF_FALSE);
3631 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003632 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003633 }
3634
3635 case TARGET(CONTAINS_OP): {
3636 PyObject *right = POP();
3637 PyObject *left = POP();
3638 int res = PySequence_Contains(right, left);
3639 Py_DECREF(left);
3640 Py_DECREF(right);
3641 if (res < 0) {
3642 goto error;
3643 }
3644 PyObject *b = (res^oparg) ? Py_True : Py_False;
3645 Py_INCREF(b);
3646 PUSH(b);
3647 PREDICT(POP_JUMP_IF_FALSE);
3648 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003649 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003650 }
3651
3652#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3653 "BaseException is not allowed"
3654
3655 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3656 PyObject *right = POP();
3657 PyObject *left = POP();
3658 if (PyTuple_Check(right)) {
3659 Py_ssize_t i, length;
3660 length = PyTuple_GET_SIZE(right);
3661 for (i = 0; i < length; i++) {
3662 PyObject *exc = PyTuple_GET_ITEM(right, i);
3663 if (!PyExceptionClass_Check(exc)) {
3664 _PyErr_SetString(tstate, PyExc_TypeError,
3665 CANNOT_CATCH_MSG);
3666 Py_DECREF(left);
3667 Py_DECREF(right);
3668 goto error;
3669 }
3670 }
3671 }
3672 else {
3673 if (!PyExceptionClass_Check(right)) {
3674 _PyErr_SetString(tstate, PyExc_TypeError,
3675 CANNOT_CATCH_MSG);
3676 Py_DECREF(left);
3677 Py_DECREF(right);
3678 goto error;
3679 }
3680 }
3681 int res = PyErr_GivenExceptionMatches(left, right);
3682 Py_DECREF(left);
3683 Py_DECREF(right);
3684 if (res > 0) {
3685 /* Exception matches -- Do nothing */;
3686 }
3687 else if (res == 0) {
3688 JUMPTO(oparg);
3689 }
3690 else {
3691 goto error;
3692 }
3693 DISPATCH();
3694 }
3695
Benjamin Petersonddd19492018-09-16 22:38:02 -07003696 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003697 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003698 PyObject *fromlist = POP();
3699 PyObject *level = TOP();
3700 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003701 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003702 Py_DECREF(level);
3703 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003704 SET_TOP(res);
3705 if (res == NULL)
3706 goto error;
3707 DISPATCH();
3708 }
3709
Benjamin Petersonddd19492018-09-16 22:38:02 -07003710 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711 PyObject *from = POP(), *locals;
3712 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003713 if (PyFrame_FastToLocalsWithError(f) < 0) {
3714 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003715 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003716 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003717
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003718 locals = f->f_locals;
3719 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003720 _PyErr_SetString(tstate, PyExc_SystemError,
3721 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003722 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003723 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003725 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003727 Py_DECREF(from);
3728 if (err != 0)
3729 goto error;
3730 DISPATCH();
3731 }
Guido van Rossum25831651993-05-19 14:50:45 +00003732
Benjamin Petersonddd19492018-09-16 22:38:02 -07003733 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003734 PyObject *name = GETITEM(names, oparg);
3735 PyObject *from = TOP();
3736 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003737 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003738 PUSH(res);
3739 if (res == NULL)
3740 goto error;
3741 DISPATCH();
3742 }
Thomas Wouters52152252000-08-17 22:55:00 +00003743
Benjamin Petersonddd19492018-09-16 22:38:02 -07003744 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003746 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003748
Benjamin Petersonddd19492018-09-16 22:38:02 -07003749 case TARGET(POP_JUMP_IF_FALSE): {
3750 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003751 PyObject *cond = POP();
3752 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003753 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003754 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003755 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003757 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003758 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003760 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003762 err = PyObject_IsTrue(cond);
3763 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003765 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 else if (err == 0)
3767 JUMPTO(oparg);
3768 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003769 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003772
Benjamin Petersonddd19492018-09-16 22:38:02 -07003773 case TARGET(POP_JUMP_IF_TRUE): {
3774 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003775 PyObject *cond = POP();
3776 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003777 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003778 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003779 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003781 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003782 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003784 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003786 err = PyObject_IsTrue(cond);
3787 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 JUMPTO(oparg);
3790 }
3791 else if (err == 0)
3792 ;
3793 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003794 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003797
Benjamin Petersonddd19492018-09-16 22:38:02 -07003798 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003799 PyObject *cond = TOP();
3800 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003801 if (Py_IsTrue(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003802 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003803 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003804 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003806 if (Py_IsFalse(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003808 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003810 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003812 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003813 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 }
3815 else if (err == 0)
3816 JUMPTO(oparg);
3817 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003818 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003820 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003821
Benjamin Petersonddd19492018-09-16 22:38:02 -07003822 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003823 PyObject *cond = TOP();
3824 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003825 if (Py_IsFalse(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003826 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003827 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003828 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003830 if (Py_IsTrue(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003832 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003834 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 JUMPTO(oparg);
3837 }
3838 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003839 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003840 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 }
3842 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003843 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003846
Benjamin Petersonddd19492018-09-16 22:38:02 -07003847 case TARGET(JUMP_ABSOLUTE): {
3848 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003850 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003853
Brandt Bucher145bf262021-02-26 14:51:55 -08003854 case TARGET(GET_LEN): {
3855 // PUSH(len(TOS))
3856 Py_ssize_t len_i = PyObject_Length(TOP());
3857 if (len_i < 0) {
3858 goto error;
3859 }
3860 PyObject *len_o = PyLong_FromSsize_t(len_i);
3861 if (len_o == NULL) {
3862 goto error;
3863 }
3864 PUSH(len_o);
3865 DISPATCH();
3866 }
3867
3868 case TARGET(MATCH_CLASS): {
3869 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3870 // attributes. On failure, set TOS to False.
3871 PyObject *names = POP();
3872 PyObject *type = TOP();
3873 PyObject *subject = SECOND();
3874 assert(PyTuple_CheckExact(names));
3875 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3876 Py_DECREF(names);
3877 if (attrs) {
3878 // Success!
3879 assert(PyTuple_CheckExact(attrs));
3880 Py_DECREF(subject);
3881 SET_SECOND(attrs);
3882 }
3883 else if (_PyErr_Occurred(tstate)) {
3884 goto error;
3885 }
3886 Py_DECREF(type);
3887 SET_TOP(PyBool_FromLong(!!attrs));
3888 DISPATCH();
3889 }
3890
3891 case TARGET(MATCH_MAPPING): {
3892 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3893 PyObject *subject = TOP();
3894 // Fast path for dicts:
3895 if (PyDict_Check(subject)) {
3896 Py_INCREF(Py_True);
3897 PUSH(Py_True);
3898 DISPATCH();
3899 }
3900 // Lazily import _collections_abc.Mapping, and keep it handy on the
3901 // PyInterpreterState struct (it gets cleaned up at exit):
3902 PyInterpreterState *interp = PyInterpreterState_Get();
3903 if (interp->map_abc == NULL) {
3904 PyObject *abc = PyImport_ImportModule("_collections_abc");
3905 if (abc == NULL) {
3906 goto error;
3907 }
3908 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3909 if (interp->map_abc == NULL) {
3910 goto error;
3911 }
3912 }
3913 int match = PyObject_IsInstance(subject, interp->map_abc);
3914 if (match < 0) {
3915 goto error;
3916 }
3917 PUSH(PyBool_FromLong(match));
3918 DISPATCH();
3919 }
3920
3921 case TARGET(MATCH_SEQUENCE): {
3922 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3923 // and isinstance(TOS, _collections_abc.Sequence))
3924 PyObject *subject = TOP();
3925 // Fast path for lists and tuples:
3926 if (PyType_FastSubclass(Py_TYPE(subject),
3927 Py_TPFLAGS_LIST_SUBCLASS |
3928 Py_TPFLAGS_TUPLE_SUBCLASS))
3929 {
3930 Py_INCREF(Py_True);
3931 PUSH(Py_True);
3932 DISPATCH();
3933 }
3934 // Bail on some possible Sequences that we intentionally exclude:
3935 if (PyType_FastSubclass(Py_TYPE(subject),
3936 Py_TPFLAGS_BYTES_SUBCLASS |
3937 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3938 PyByteArray_Check(subject))
3939 {
3940 Py_INCREF(Py_False);
3941 PUSH(Py_False);
3942 DISPATCH();
3943 }
3944 // Lazily import _collections_abc.Sequence, and keep it handy on the
3945 // PyInterpreterState struct (it gets cleaned up at exit):
3946 PyInterpreterState *interp = PyInterpreterState_Get();
3947 if (interp->seq_abc == NULL) {
3948 PyObject *abc = PyImport_ImportModule("_collections_abc");
3949 if (abc == NULL) {
3950 goto error;
3951 }
3952 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3953 if (interp->seq_abc == NULL) {
3954 goto error;
3955 }
3956 }
3957 int match = PyObject_IsInstance(subject, interp->seq_abc);
3958 if (match < 0) {
3959 goto error;
3960 }
3961 PUSH(PyBool_FromLong(match));
3962 DISPATCH();
3963 }
3964
3965 case TARGET(MATCH_KEYS): {
3966 // On successful match for all keys, PUSH(values) and PUSH(True).
3967 // Otherwise, PUSH(None) and PUSH(False).
3968 PyObject *keys = TOP();
3969 PyObject *subject = SECOND();
3970 PyObject *values_or_none = match_keys(tstate, subject, keys);
3971 if (values_or_none == NULL) {
3972 goto error;
3973 }
3974 PUSH(values_or_none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02003975 if (Py_IsNone(values_or_none)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08003976 Py_INCREF(Py_False);
3977 PUSH(Py_False);
3978 DISPATCH();
3979 }
3980 assert(PyTuple_CheckExact(values_or_none));
3981 Py_INCREF(Py_True);
3982 PUSH(Py_True);
3983 DISPATCH();
3984 }
3985
3986 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3987 // rest = dict(TOS1)
3988 // for key in TOS:
3989 // del rest[key]
3990 // SET_TOP(rest)
3991 PyObject *keys = TOP();
3992 PyObject *subject = SECOND();
3993 PyObject *rest = PyDict_New();
3994 if (rest == NULL || PyDict_Update(rest, subject)) {
3995 Py_XDECREF(rest);
3996 goto error;
3997 }
3998 // This may seem a bit inefficient, but keys is rarely big enough to
3999 // actually impact runtime.
4000 assert(PyTuple_CheckExact(keys));
4001 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
4002 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
4003 Py_DECREF(rest);
4004 goto error;
4005 }
4006 }
4007 Py_DECREF(keys);
4008 SET_TOP(rest);
4009 DISPATCH();
4010 }
4011
Benjamin Petersonddd19492018-09-16 22:38:02 -07004012 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004014 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004015 PyObject *iter = PyObject_GetIter(iterable);
4016 Py_DECREF(iterable);
4017 SET_TOP(iter);
4018 if (iter == NULL)
4019 goto error;
4020 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004021 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004022 DISPATCH();
4023 }
4024
Benjamin Petersonddd19492018-09-16 22:38:02 -07004025 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004026 /* before: [obj]; after [getiter(obj)] */
4027 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004028 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004029 if (PyCoro_CheckExact(iterable)) {
4030 /* `iterable` is a coroutine */
4031 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4032 /* and it is used in a 'yield from' expression of a
4033 regular generator. */
4034 Py_DECREF(iterable);
4035 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004036 _PyErr_SetString(tstate, PyExc_TypeError,
4037 "cannot 'yield from' a coroutine object "
4038 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004039 goto error;
4040 }
4041 }
4042 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004043 /* `iterable` is not a generator. */
4044 iter = PyObject_GetIter(iterable);
4045 Py_DECREF(iterable);
4046 SET_TOP(iter);
4047 if (iter == NULL)
4048 goto error;
4049 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004050 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004051 DISPATCH();
4052 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004053
Benjamin Petersonddd19492018-09-16 22:38:02 -07004054 case TARGET(FOR_ITER): {
4055 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004057 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004058 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004059 if (next != NULL) {
4060 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 PREDICT(STORE_FAST);
4062 PREDICT(UNPACK_SEQUENCE);
4063 DISPATCH();
4064 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004065 if (_PyErr_Occurred(tstate)) {
4066 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004067 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004068 }
4069 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004070 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004071 }
4072 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 }
4074 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004075 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004076 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 JUMPBY(oparg);
4078 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004079 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004080
Benjamin Petersonddd19492018-09-16 22:38:02 -07004081 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004082 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 STACK_LEVEL());
4084 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004085 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004086
Benjamin Petersonddd19492018-09-16 22:38:02 -07004087 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004088 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004089 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004090 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004091 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004092 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004093 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004094 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004095 }
4096 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4097 if (exit == NULL) {
4098 Py_DECREF(enter);
4099 goto error;
4100 }
Yury Selivanov75445082015-05-11 22:57:16 -04004101 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004102 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004103 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004104 Py_DECREF(enter);
4105 if (res == NULL)
4106 goto error;
4107 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004108 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004109 DISPATCH();
4110 }
4111
Benjamin Petersonddd19492018-09-16 22:38:02 -07004112 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004113 PyObject *res = POP();
4114 /* Setup the finally block before pushing the result
4115 of __aenter__ on the stack. */
4116 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4117 STACK_LEVEL());
4118 PUSH(res);
4119 DISPATCH();
4120 }
4121
Benjamin Petersonddd19492018-09-16 22:38:02 -07004122 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004123 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004124 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004125 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004126 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004127 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004128 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004129 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004130 }
4131 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004132 if (exit == NULL) {
4133 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004134 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004136 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004137 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004138 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004139 Py_DECREF(enter);
4140 if (res == NULL)
4141 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 /* Setup the finally block before pushing the result
4143 of __enter__ on the stack. */
4144 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4145 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004147 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 DISPATCH();
4149 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004150
Mark Shannonfee55262019-11-21 09:11:43 +00004151 case TARGET(WITH_EXCEPT_START): {
4152 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004154 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4155 - SEVENTH: the context.__exit__ bound method
4156 We call SEVENTH(TOP, SECOND, THIRD).
4157 Then we push again the TOP exception and the __exit__
4158 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004161 PyObject *exc, *val, *tb, *res;
4162
Victor Stinner842cfff2016-12-01 14:45:31 +01004163 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004164 val = SECOND();
4165 tb = THIRD();
Victor Stinner09bbebe2021-04-11 00:17:39 +02004166 assert(!Py_IsNone(exc));
Mark Shannonfee55262019-11-21 09:11:43 +00004167 assert(!PyLong_Check(exc));
4168 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004169 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004170 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004171 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004172 if (res == NULL)
4173 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004174
Yury Selivanov75445082015-05-11 22:57:16 -04004175 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004176 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004178
Benjamin Petersonddd19492018-09-16 22:38:02 -07004179 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004180 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004181 PyObject *name = GETITEM(names, oparg);
4182 PyObject *obj = TOP();
4183 PyObject *meth = NULL;
4184
4185 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4186
Yury Selivanovf2392132016-12-13 19:03:51 -05004187 if (meth == NULL) {
4188 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004189 goto error;
4190 }
4191
4192 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004193 /* We can bypass temporary bound method object.
4194 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004195
INADA Naoki015bce62017-01-16 17:23:30 +09004196 meth | self | arg1 | ... | argN
4197 */
4198 SET_TOP(meth);
4199 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004200 }
4201 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004202 /* meth is not an unbound method (but a regular attr, or
4203 something was returned by a descriptor protocol). Set
4204 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004205 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004206
4207 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004208 */
INADA Naoki015bce62017-01-16 17:23:30 +09004209 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004210 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004211 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004212 }
4213 DISPATCH();
4214 }
4215
Benjamin Petersonddd19492018-09-16 22:38:02 -07004216 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004217 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004218 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004219
4220 sp = stack_pointer;
4221
INADA Naoki015bce62017-01-16 17:23:30 +09004222 meth = PEEK(oparg + 2);
4223 if (meth == NULL) {
4224 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4225 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004226
4227 Stack layout:
4228
INADA Naoki015bce62017-01-16 17:23:30 +09004229 ... | NULL | callable | arg1 | ... | argN
4230 ^- TOP()
4231 ^- (-oparg)
4232 ^- (-oparg-1)
4233 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004234
Ville Skyttä49b27342017-08-03 09:00:59 +03004235 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004236 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004237 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004238 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004239 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004240 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004241 }
4242 else {
4243 /* This is a method call. Stack layout:
4244
INADA Naoki015bce62017-01-16 17:23:30 +09004245 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004246 ^- TOP()
4247 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004248 ^- (-oparg-1)
4249 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004250
INADA Naoki015bce62017-01-16 17:23:30 +09004251 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004252 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004253 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004254 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004255 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004256 stack_pointer = sp;
4257 }
4258
4259 PUSH(res);
4260 if (res == NULL)
4261 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004262 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004263 DISPATCH();
4264 }
4265
Benjamin Petersonddd19492018-09-16 22:38:02 -07004266 case TARGET(CALL_FUNCTION): {
4267 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004268 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004270 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004272 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004273 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004274 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004275 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004276 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004277 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004279
Benjamin Petersonddd19492018-09-16 22:38:02 -07004280 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004281 PyObject **sp, *res, *names;
4282
4283 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004284 assert(PyTuple_Check(names));
4285 assert(PyTuple_GET_SIZE(names) <= oparg);
4286 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004288 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004290 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004291 Py_DECREF(names);
4292
4293 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004294 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004295 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004296 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004297 DISPATCH();
4298 }
4299
Benjamin Petersonddd19492018-09-16 22:38:02 -07004300 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004301 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004302 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004303 if (oparg & 0x01) {
4304 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004305 if (!PyDict_CheckExact(kwargs)) {
4306 PyObject *d = PyDict_New();
4307 if (d == NULL)
4308 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004309 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004310 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004311 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004312 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004313 goto error;
4314 }
4315 Py_DECREF(kwargs);
4316 kwargs = d;
4317 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004318 assert(PyDict_CheckExact(kwargs));
4319 }
4320 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004321 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004322 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004323 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004324 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004325 goto error;
4326 }
4327 Py_SETREF(callargs, PySequence_Tuple(callargs));
4328 if (callargs == NULL) {
4329 goto error;
4330 }
4331 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004332 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004333
Mark Shannon8e1b4062021-03-05 14:45:50 +00004334 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004335 Py_DECREF(func);
4336 Py_DECREF(callargs);
4337 Py_XDECREF(kwargs);
4338
4339 SET_TOP(result);
4340 if (result == NULL) {
4341 goto error;
4342 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004343 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004344 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004346
Benjamin Petersonddd19492018-09-16 22:38:02 -07004347 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004348 PyObject *qualname = POP();
4349 PyObject *codeobj = POP();
4350 PyFunctionObject *func = (PyFunctionObject *)
4351 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004352
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004353 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004354 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004355 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004356 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004358
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004359 if (oparg & 0x08) {
4360 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004361 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004362 }
4363 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004364 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004365 func->func_annotations = POP();
4366 }
4367 if (oparg & 0x02) {
4368 assert(PyDict_CheckExact(TOP()));
4369 func->func_kwdefaults = POP();
4370 }
4371 if (oparg & 0x01) {
4372 assert(PyTuple_CheckExact(TOP()));
4373 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004375
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004376 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004377 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004379
Benjamin Petersonddd19492018-09-16 22:38:02 -07004380 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004381 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004383 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004385 step = NULL;
4386 stop = POP();
4387 start = TOP();
4388 slice = PySlice_New(start, stop, step);
4389 Py_DECREF(start);
4390 Py_DECREF(stop);
4391 Py_XDECREF(step);
4392 SET_TOP(slice);
4393 if (slice == NULL)
4394 goto error;
4395 DISPATCH();
4396 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004397
Benjamin Petersonddd19492018-09-16 22:38:02 -07004398 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004399 /* Handles f-string value formatting. */
4400 PyObject *result;
4401 PyObject *fmt_spec;
4402 PyObject *value;
4403 PyObject *(*conv_fn)(PyObject *);
4404 int which_conversion = oparg & FVC_MASK;
4405 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4406
4407 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004408 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004409
4410 /* See if any conversion is specified. */
4411 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004412 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004413 case FVC_STR: conv_fn = PyObject_Str; break;
4414 case FVC_REPR: conv_fn = PyObject_Repr; break;
4415 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004416 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004417 _PyErr_Format(tstate, PyExc_SystemError,
4418 "unexpected conversion flag %d",
4419 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004420 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004421 }
4422
4423 /* If there's a conversion function, call it and replace
4424 value with that result. Otherwise, just use value,
4425 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004426 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004427 result = conv_fn(value);
4428 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004429 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004430 Py_XDECREF(fmt_spec);
4431 goto error;
4432 }
4433 value = result;
4434 }
4435
4436 /* If value is a unicode object, and there's no fmt_spec,
4437 then we know the result of format(value) is value
4438 itself. In that case, skip calling format(). I plan to
4439 move this optimization in to PyObject_Format()
4440 itself. */
4441 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4442 /* Do nothing, just transfer ownership to result. */
4443 result = value;
4444 } else {
4445 /* Actually call format(). */
4446 result = PyObject_Format(value, fmt_spec);
4447 Py_DECREF(value);
4448 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004449 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004450 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004451 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004452 }
4453
Eric V. Smith135d5f42016-02-05 18:23:08 -05004454 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004455 DISPATCH();
4456 }
4457
Benjamin Petersonddd19492018-09-16 22:38:02 -07004458 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004459 int oldoparg = oparg;
4460 NEXTOPARG();
4461 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004463 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004464
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004465
Antoine Pitrou042b1282010-08-13 21:15:58 +00004466#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004468#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 default:
4470 fprintf(stderr,
4471 "XXX lineno: %d, opcode: %d\n",
4472 PyFrame_GetLineNumber(f),
4473 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004474 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004475 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004478
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004479 /* This should never be reached. Every opcode should end with DISPATCH()
4480 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004481 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004483error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004484 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004485#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004486 if (!_PyErr_Occurred(tstate)) {
4487 _PyErr_SetString(tstate, PyExc_SystemError,
4488 "error return without exception set");
4489 }
Victor Stinner365b6932013-07-12 00:11:58 +02004490#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004491 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004492#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004493
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004494 /* Log traceback info. */
4495 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004496
Mark Shannoncb9879b2020-07-17 11:44:23 +01004497 if (tstate->c_tracefunc != NULL) {
4498 /* Make sure state is set to FRAME_EXECUTING for tracing */
4499 assert(f->f_state == FRAME_EXECUTING);
4500 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004501 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004502 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004503 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004504exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004505 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004506 /* Unwind stacks if an exception occurred */
4507 while (f->f_iblock > 0) {
4508 /* Pop the current block. */
4509 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 if (b->b_type == EXCEPT_HANDLER) {
4512 UNWIND_EXCEPT_HANDLER(b);
4513 continue;
4514 }
4515 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004516 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 PyObject *exc, *val, *tb;
4518 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004519 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004521 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004522 PUSH(exc_info->exc_traceback);
4523 PUSH(exc_info->exc_value);
4524 if (exc_info->exc_type != NULL) {
4525 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 }
4527 else {
4528 Py_INCREF(Py_None);
4529 PUSH(Py_None);
4530 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004531 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 /* Make the raw exception data
4533 available to the handler,
4534 so a program can emulate the
4535 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004536 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004537 if (tb != NULL)
4538 PyException_SetTraceback(val, tb);
4539 else
4540 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004542 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004544 exc_info->exc_value = val;
4545 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 if (tb == NULL)
4547 tb = Py_None;
4548 Py_INCREF(tb);
4549 PUSH(tb);
4550 PUSH(val);
4551 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 JUMPTO(handler);
Mark Shannon9e7b2072021-04-13 11:08:14 +01004553 if (trace_info.cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004554 trace_info.instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004555 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004556 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004557 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004558 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 }
4560 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004561
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004562 /* End the loop as we still have an error */
4563 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004565
Pablo Galindof00828a2019-05-09 16:52:02 +01004566 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004567 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 /* Pop remaining stack entries. */
4570 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004571 PyObject *o = POP();
4572 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004574 f->f_stackdepth = 0;
4575 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004576exiting:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004577 if (trace_info.cframe.use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004578 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004579 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004580 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004581 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 }
4583 }
4584 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004585 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004586 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004587 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 }
4589 }
4590 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004593exit_eval_frame:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004594 /* Restore previous cframe */
4595 tstate->cframe = trace_info.cframe.previous;
4596 tstate->cframe->use_tracing = trace_info.cframe.use_tracing;
4597
Łukasz Langaa785c872016-09-09 17:37:37 -07004598 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4599 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004600 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004602
Victor Stinner0b72b232020-03-12 23:18:39 +01004603 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004604}
4605
Benjamin Petersonb204a422011-06-05 22:04:07 -05004606static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004607format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004608 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004609{
4610 int err;
4611 Py_ssize_t len = PyList_GET_SIZE(names);
4612 PyObject *name_str, *comma, *tail, *tmp;
4613
4614 assert(PyList_CheckExact(names));
4615 assert(len >= 1);
4616 /* Deal with the joys of natural language. */
4617 switch (len) {
4618 case 1:
4619 name_str = PyList_GET_ITEM(names, 0);
4620 Py_INCREF(name_str);
4621 break;
4622 case 2:
4623 name_str = PyUnicode_FromFormat("%U and %U",
4624 PyList_GET_ITEM(names, len - 2),
4625 PyList_GET_ITEM(names, len - 1));
4626 break;
4627 default:
4628 tail = PyUnicode_FromFormat(", %U, and %U",
4629 PyList_GET_ITEM(names, len - 2),
4630 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004631 if (tail == NULL)
4632 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004633 /* Chop off the last two objects in the list. This shouldn't actually
4634 fail, but we can't be too careful. */
4635 err = PyList_SetSlice(names, len - 2, len, NULL);
4636 if (err == -1) {
4637 Py_DECREF(tail);
4638 return;
4639 }
4640 /* Stitch everything up into a nice comma-separated list. */
4641 comma = PyUnicode_FromString(", ");
4642 if (comma == NULL) {
4643 Py_DECREF(tail);
4644 return;
4645 }
4646 tmp = PyUnicode_Join(comma, names);
4647 Py_DECREF(comma);
4648 if (tmp == NULL) {
4649 Py_DECREF(tail);
4650 return;
4651 }
4652 name_str = PyUnicode_Concat(tmp, tail);
4653 Py_DECREF(tmp);
4654 Py_DECREF(tail);
4655 break;
4656 }
4657 if (name_str == NULL)
4658 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004659 _PyErr_Format(tstate, PyExc_TypeError,
4660 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004661 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004662 len,
4663 kind,
4664 len == 1 ? "" : "s",
4665 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004666 Py_DECREF(name_str);
4667}
4668
4669static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004670missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4671 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004672 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004673{
Victor Stinner74319ae2016-08-25 00:04:09 +02004674 Py_ssize_t i, j = 0;
4675 Py_ssize_t start, end;
4676 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004677 const char *kind = positional ? "positional" : "keyword-only";
4678 PyObject *missing_names;
4679
4680 /* Compute the names of the arguments that are missing. */
4681 missing_names = PyList_New(missing);
4682 if (missing_names == NULL)
4683 return;
4684 if (positional) {
4685 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004686 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004687 }
4688 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004689 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004690 end = start + co->co_kwonlyargcount;
4691 }
4692 for (i = start; i < end; i++) {
4693 if (GETLOCAL(i) == NULL) {
4694 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4695 PyObject *name = PyObject_Repr(raw);
4696 if (name == NULL) {
4697 Py_DECREF(missing_names);
4698 return;
4699 }
4700 PyList_SET_ITEM(missing_names, j++, name);
4701 }
4702 }
4703 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004704 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004705 Py_DECREF(missing_names);
4706}
4707
4708static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004709too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004710 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004711 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004712{
4713 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004714 Py_ssize_t kwonly_given = 0;
4715 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004716 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004717 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004718
Benjamin Petersone109c702011-06-24 09:37:26 -05004719 assert((co->co_flags & CO_VARARGS) == 0);
4720 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004721 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004722 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004723 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004724 }
4725 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004726 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004727 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004728 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004729 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004730 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004731 }
4732 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004733 plural = (co_argcount != 1);
4734 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004735 }
4736 if (sig == NULL)
4737 return;
4738 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004739 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4740 kwonly_sig = PyUnicode_FromFormat(format,
4741 given != 1 ? "s" : "",
4742 kwonly_given,
4743 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004744 if (kwonly_sig == NULL) {
4745 Py_DECREF(sig);
4746 return;
4747 }
4748 }
4749 else {
4750 /* This will not fail. */
4751 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004752 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004753 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004754 _PyErr_Format(tstate, PyExc_TypeError,
4755 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004756 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004757 sig,
4758 plural ? "s" : "",
4759 given,
4760 kwonly_sig,
4761 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004762 Py_DECREF(sig);
4763 Py_DECREF(kwonly_sig);
4764}
4765
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004766static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004767positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004768 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004769 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004770{
4771 int posonly_conflicts = 0;
4772 PyObject* posonly_names = PyList_New(0);
4773
4774 for(int k=0; k < co->co_posonlyargcount; k++){
4775 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4776
4777 for (int k2=0; k2<kwcount; k2++){
4778 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004779 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004780 if (kwname == posonly_name){
4781 if(PyList_Append(posonly_names, kwname) != 0) {
4782 goto fail;
4783 }
4784 posonly_conflicts++;
4785 continue;
4786 }
4787
4788 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4789
4790 if ( cmp > 0) {
4791 if(PyList_Append(posonly_names, kwname) != 0) {
4792 goto fail;
4793 }
4794 posonly_conflicts++;
4795 } else if (cmp < 0) {
4796 goto fail;
4797 }
4798
4799 }
4800 }
4801 if (posonly_conflicts) {
4802 PyObject* comma = PyUnicode_FromString(", ");
4803 if (comma == NULL) {
4804 goto fail;
4805 }
4806 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4807 Py_DECREF(comma);
4808 if (error_names == NULL) {
4809 goto fail;
4810 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004811 _PyErr_Format(tstate, PyExc_TypeError,
4812 "%U() got some positional-only arguments passed"
4813 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004814 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004815 Py_DECREF(error_names);
4816 goto fail;
4817 }
4818
4819 Py_DECREF(posonly_names);
4820 return 0;
4821
4822fail:
4823 Py_XDECREF(posonly_names);
4824 return 1;
4825
4826}
4827
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004828
Mark Shannon0332e562021-02-01 10:42:03 +00004829PyFrameObject *
4830_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004831 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004832 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004833 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004834{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004835 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004836
Mark Shannond6c33fb2021-01-29 13:24:55 +00004837 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4838 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004839 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004840
Victor Stinnerc7020012016-08-16 23:40:29 +02004841 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004842 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004843 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004845 }
Victor Stinner232dda62020-06-04 15:19:02 +02004846 PyObject **fastlocals = f->f_localsplus;
4847 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004848
Victor Stinnerc7020012016-08-16 23:40:29 +02004849 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004850 PyObject *kwdict;
4851 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004852 if (co->co_flags & CO_VARKEYWORDS) {
4853 kwdict = PyDict_New();
4854 if (kwdict == NULL)
4855 goto fail;
4856 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004857 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004858 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004859 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004860 SETLOCAL(i, kwdict);
4861 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004862 else {
4863 kwdict = NULL;
4864 }
4865
Pablo Galindocd74e662019-06-01 18:08:04 +01004866 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004867 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004868 if (argcount > co->co_argcount) {
4869 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004870 }
4871 else {
4872 n = argcount;
4873 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004874 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004875 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004876 Py_INCREF(x);
4877 SETLOCAL(j, x);
4878 }
4879
Victor Stinnerc7020012016-08-16 23:40:29 +02004880 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004881 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004882 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004883 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004884 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004885 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004886 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004887 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004888
Mark Shannon0332e562021-02-01 10:42:03 +00004889 /* Handle keyword arguments */
4890 if (kwnames != NULL) {
4891 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4892 for (i = 0; i < kwcount; i++) {
4893 PyObject **co_varnames;
4894 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4895 PyObject *value = args[i+argcount];
4896 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004897
Mark Shannon0332e562021-02-01 10:42:03 +00004898 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4899 _PyErr_Format(tstate, PyExc_TypeError,
4900 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004901 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004903 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004904
Mark Shannon0332e562021-02-01 10:42:03 +00004905 /* Speed hack: do raw pointer compares. As names are
4906 normally interned this should almost always hit. */
4907 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4908 for (j = co->co_posonlyargcount; j < total_args; j++) {
4909 PyObject *varname = co_varnames[j];
4910 if (varname == keyword) {
4911 goto kw_found;
4912 }
4913 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004914
Mark Shannon0332e562021-02-01 10:42:03 +00004915 /* Slow fallback, just in case */
4916 for (j = co->co_posonlyargcount; j < total_args; j++) {
4917 PyObject *varname = co_varnames[j];
4918 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4919 if (cmp > 0) {
4920 goto kw_found;
4921 }
4922 else if (cmp < 0) {
4923 goto fail;
4924 }
4925 }
4926
4927 assert(j >= total_args);
4928 if (kwdict == NULL) {
4929
4930 if (co->co_posonlyargcount
4931 && positional_only_passed_as_keyword(tstate, co,
4932 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004933 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004934 {
4935 goto fail;
4936 }
4937
4938 _PyErr_Format(tstate, PyExc_TypeError,
4939 "%U() got an unexpected keyword argument '%S'",
4940 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004941 goto fail;
4942 }
4943
Mark Shannon0332e562021-02-01 10:42:03 +00004944 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4945 goto fail;
4946 }
4947 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004948
Mark Shannon0332e562021-02-01 10:42:03 +00004949 kw_found:
4950 if (GETLOCAL(j) != NULL) {
4951 _PyErr_Format(tstate, PyExc_TypeError,
4952 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004953 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004954 goto fail;
4955 }
4956 Py_INCREF(value);
4957 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004960
4961 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004962 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004963 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4964 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965 goto fail;
4966 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004967
4968 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004969 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004970 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004971 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004972 Py_ssize_t missing = 0;
4973 for (i = argcount; i < m; i++) {
4974 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004975 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004976 }
4977 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004978 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004979 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004980 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004981 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004982 }
4983 if (n > m)
4984 i = n - m;
4985 else
4986 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004987 if (defcount) {
4988 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4989 for (; i < defcount; i++) {
4990 if (GETLOCAL(m+i) == NULL) {
4991 PyObject *def = defs[i];
4992 Py_INCREF(def);
4993 SETLOCAL(m+i, def);
4994 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004995 }
4996 }
4997 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004998
4999 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05005000 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02005001 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01005002 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05005003 if (GETLOCAL(i) != NULL)
5004 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02005005 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005006 if (con->fc_kwdefaults != NULL) {
5007 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005008 if (def) {
5009 Py_INCREF(def);
5010 SETLOCAL(i, def);
5011 continue;
5012 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005013 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005014 goto fail;
5015 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05005016 }
Benjamin Petersone109c702011-06-24 09:37:26 -05005017 missing++;
5018 }
5019 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005020 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005021 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005022 goto fail;
5023 }
5024 }
5025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005027 vars into frame. */
5028 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005030 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005031 /* Possibly account for the cell variable being an argument. */
5032 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005033 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005034 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005035 /* Clear the local copy. */
5036 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005037 }
5038 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005039 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005040 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005041 if (c == NULL)
5042 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005043 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005045
5046 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005047 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005048 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005049 Py_INCREF(o);
5050 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005052
Mark Shannon0332e562021-02-01 10:42:03 +00005053 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005054
Thomas Woutersce272b62007-09-19 21:19:28 +00005055fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 /* decref'ing the frame can cause __del__ methods to get invoked,
5058 which can call back into Python. While we're done with the
5059 current Python frame (f), the associated C stack is still in use,
5060 so recursion_depth must be boosted for the duration.
5061 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005062 if (Py_REFCNT(f) > 1) {
5063 Py_DECREF(f);
5064 _PyObject_GC_TRACK(f);
5065 }
5066 else {
5067 ++tstate->recursion_depth;
5068 Py_DECREF(f);
5069 --tstate->recursion_depth;
5070 }
Mark Shannon0332e562021-02-01 10:42:03 +00005071 return NULL;
5072}
5073
5074static PyObject *
5075make_coro(PyFrameConstructor *con, PyFrameObject *f)
5076{
5077 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5078 PyObject *gen;
5079 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5080
5081 /* Don't need to keep the reference to f_back, it will be set
5082 * when the generator is resumed. */
5083 Py_CLEAR(f->f_back);
5084
5085 /* Create a new generator that owns the ready to run frame
5086 * and return that as the value. */
5087 if (is_coro) {
5088 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5089 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5090 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5091 } else {
5092 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5093 }
5094 if (gen == NULL) {
5095 return NULL;
5096 }
5097
5098 _PyObject_GC_TRACK(f);
5099
5100 return gen;
5101}
5102
5103PyObject *
5104_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5105 PyObject *locals,
5106 PyObject* const* args, size_t argcount,
5107 PyObject *kwnames)
5108{
5109 PyFrameObject *f = _PyEval_MakeFrameVector(
5110 tstate, con, locals, args, argcount, kwnames);
5111 if (f == NULL) {
5112 return NULL;
5113 }
5114 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5115 return make_coro(con, f);
5116 }
5117 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5118
5119 /* decref'ing the frame can cause __del__ methods to get invoked,
5120 which can call back into Python. While we're done with the
5121 current Python frame (f), the associated C stack is still in use,
5122 so recursion_depth must be boosted for the duration.
5123 */
5124 if (Py_REFCNT(f) > 1) {
5125 Py_DECREF(f);
5126 _PyObject_GC_TRACK(f);
5127 }
5128 else {
5129 ++tstate->recursion_depth;
5130 Py_DECREF(f);
5131 --tstate->recursion_depth;
5132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005134}
5135
Mark Shannond6c33fb2021-01-29 13:24:55 +00005136/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005137PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005138PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5139 PyObject *const *args, int argcount,
5140 PyObject *const *kws, int kwcount,
5141 PyObject *const *defs, int defcount,
5142 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005143{
Victor Stinner46496f92021-02-20 15:17:18 +01005144 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005145 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005146 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5147 if (defaults == NULL) {
5148 return NULL;
5149 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005150 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005151 if (builtins == NULL) {
5152 Py_DECREF(defaults);
5153 return NULL;
5154 }
Mark Shannon0332e562021-02-01 10:42:03 +00005155 if (locals == NULL) {
5156 locals = globals;
5157 }
5158 PyObject *kwnames;
5159 PyObject *const *allargs;
5160 PyObject **newargs;
5161 if (kwcount == 0) {
5162 allargs = args;
5163 kwnames = NULL;
5164 }
5165 else {
5166 kwnames = PyTuple_New(kwcount);
5167 if (kwnames == NULL) {
5168 res = NULL;
5169 goto fail;
5170 }
5171 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5172 if (newargs == NULL) {
5173 res = NULL;
5174 Py_DECREF(kwnames);
5175 goto fail;
5176 }
5177 for (int i = 0; i < argcount; i++) {
5178 newargs[i] = args[i];
5179 }
5180 for (int i = 0; i < kwcount; i++) {
5181 Py_INCREF(kws[2*i]);
5182 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5183 newargs[argcount+i] = kws[2*i+1];
5184 }
5185 allargs = newargs;
5186 }
5187 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5188 if (kwargs == NULL) {
5189 res = NULL;
5190 Py_DECREF(kwnames);
5191 goto fail;
5192 }
5193 for (int i = 0; i < kwcount; i++) {
5194 Py_INCREF(kws[2*i]);
5195 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5196 kwargs[i] = kws[2*i+1];
5197 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005198 PyFrameConstructor constr = {
5199 .fc_globals = globals,
5200 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005201 .fc_name = ((PyCodeObject *)_co)->co_name,
5202 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005203 .fc_code = _co,
5204 .fc_defaults = defaults,
5205 .fc_kwdefaults = kwdefs,
5206 .fc_closure = closure
5207 };
Mark Shannon0332e562021-02-01 10:42:03 +00005208 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005209 allargs, argcount,
5210 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005211 if (kwcount) {
5212 Py_DECREF(kwnames);
5213 PyMem_Free(newargs);
5214 }
5215fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005216 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005217 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005218}
5219
Tim Peters5ca576e2001-06-18 22:08:13 +00005220
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005221static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005222special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005225 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005226 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005227 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 return NULL;
5229 }
5230 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005231}
5232
5233
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005234/* Logic for the raise statement (too complicated for inlining).
5235 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005236static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005237do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 if (exc == NULL) {
5242 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005243 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005245 type = exc_info->exc_type;
5246 value = exc_info->exc_value;
5247 tb = exc_info->exc_traceback;
Victor Stinner09bbebe2021-04-11 00:17:39 +02005248 if (Py_IsNone(type) || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005249 _PyErr_SetString(tstate, PyExc_RuntimeError,
5250 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005251 return 0;
5252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 Py_XINCREF(type);
5254 Py_XINCREF(value);
5255 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005256 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005257 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 /* We support the following forms of raise:
5261 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005262 raise <instance>
5263 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 if (PyExceptionClass_Check(exc)) {
5266 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005267 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 if (value == NULL)
5269 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005270 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005271 _PyErr_Format(tstate, PyExc_TypeError,
5272 "calling %R should have returned an instance of "
5273 "BaseException, not %R",
5274 type, Py_TYPE(value));
5275 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 }
5278 else if (PyExceptionInstance_Check(exc)) {
5279 value = exc;
5280 type = PyExceptionInstance_Class(exc);
5281 Py_INCREF(type);
5282 }
5283 else {
5284 /* Not something you can raise. You get an exception
5285 anyway, just not what you specified :-) */
5286 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005287 _PyErr_SetString(tstate, PyExc_TypeError,
5288 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 goto raise_error;
5290 }
Collin Winter828f04a2007-08-31 00:04:24 +00005291
Serhiy Storchakac0191582016-09-27 11:37:10 +03005292 assert(type != NULL);
5293 assert(value != NULL);
5294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 if (cause) {
5296 PyObject *fixed_cause;
5297 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005298 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 if (fixed_cause == NULL)
5300 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005301 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005303 else if (PyExceptionInstance_Check(cause)) {
5304 fixed_cause = cause;
5305 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02005306 else if (Py_IsNone(cause)) {
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005307 Py_DECREF(cause);
5308 fixed_cause = NULL;
5309 }
5310 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005311 _PyErr_SetString(tstate, PyExc_TypeError,
5312 "exception causes must derive from "
5313 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 goto raise_error;
5315 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005316 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005317 }
Collin Winter828f04a2007-08-31 00:04:24 +00005318
Victor Stinner438a12d2019-05-24 17:01:38 +02005319 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005320 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005321 Py_DECREF(value);
5322 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005323 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005324
5325raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005326 Py_XDECREF(value);
5327 Py_XDECREF(type);
5328 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005329 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005330}
5331
Tim Petersd6d010b2001-06-21 02:49:55 +00005332/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005333 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005334
Guido van Rossum0368b722007-05-11 16:50:42 +00005335 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5336 with a variable target.
5337*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005338
Barry Warsawe42b18f1997-08-25 22:13:04 +00005339static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005340unpack_iterable(PyThreadState *tstate, PyObject *v,
5341 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 int i = 0, j = 0;
5344 Py_ssize_t ll = 0;
5345 PyObject *it; /* iter(v) */
5346 PyObject *w;
5347 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005349 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005352 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005353 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005354 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005355 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 _PyErr_Format(tstate, PyExc_TypeError,
5357 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005358 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005359 }
5360 return 0;
5361 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 for (; i < argcnt; i++) {
5364 w = PyIter_Next(it);
5365 if (w == NULL) {
5366 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005367 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005368 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005369 _PyErr_Format(tstate, PyExc_ValueError,
5370 "not enough values to unpack "
5371 "(expected %d, got %d)",
5372 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005373 }
5374 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005375 _PyErr_Format(tstate, PyExc_ValueError,
5376 "not enough values to unpack "
5377 "(expected at least %d, got %d)",
5378 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 }
5381 goto Error;
5382 }
5383 *--sp = w;
5384 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 if (argcntafter == -1) {
5387 /* We better have exhausted the iterator now. */
5388 w = PyIter_Next(it);
5389 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005390 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 goto Error;
5392 Py_DECREF(it);
5393 return 1;
5394 }
5395 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005396 _PyErr_Format(tstate, PyExc_ValueError,
5397 "too many values to unpack (expected %d)",
5398 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 goto Error;
5400 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 l = PySequence_List(it);
5403 if (l == NULL)
5404 goto Error;
5405 *--sp = l;
5406 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 ll = PyList_GET_SIZE(l);
5409 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005410 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005411 "not enough values to unpack (expected at least %d, got %zd)",
5412 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 goto Error;
5414 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005416 /* Pop the "after-variable" args off the list. */
5417 for (j = argcntafter; j > 0; j--, i++) {
5418 *--sp = PyList_GET_ITEM(l, ll - j);
5419 }
5420 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005421 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 Py_DECREF(it);
5423 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005424
Tim Petersd6d010b2001-06-21 02:49:55 +00005425Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 for (; i > 0; i--, sp++)
5427 Py_DECREF(*sp);
5428 Py_XDECREF(it);
5429 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005430}
5431
Guido van Rossum96a42c81992-01-12 02:29:51 +00005432#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005433static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005434prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005437 if (PyObject_Print(v, stdout, 0) != 0) {
5438 /* Don't know what else to do */
5439 _PyErr_Clear(tstate);
5440 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 printf("\n");
5442 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005443}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005444#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005445
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005446static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005447call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005448 PyThreadState *tstate,
5449 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005450 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005451{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005452 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005454 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 if (value == NULL) {
5456 value = Py_None;
5457 Py_INCREF(value);
5458 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005459 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005460 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 arg = PyTuple_Pack(3, type, value, traceback);
5462 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005463 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 return;
5465 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005466 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005467 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005468 if (err == 0) {
5469 _PyErr_Restore(tstate, type, value, orig_traceback);
5470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 else {
5472 Py_XDECREF(type);
5473 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005474 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005476}
5477
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005478static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005479call_trace_protected(Py_tracefunc func, PyObject *obj,
5480 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005481 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005484 PyObject *type, *value, *traceback;
5485 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005486 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005487 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005488 if (err == 0)
5489 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005490 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 return 0;
5492 }
5493 else {
5494 Py_XDECREF(type);
5495 Py_XDECREF(value);
5496 Py_XDECREF(traceback);
5497 return -1;
5498 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005499}
5500
Mark Shannon8e1b4062021-03-05 14:45:50 +00005501static void
5502initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5503{
5504 if (trace_info->code != frame->f_code) {
5505 trace_info->code = frame->f_code;
5506 trace_info->instr_prev = -1;
5507 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5508 }
5509}
5510
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005511static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005512call_trace(Py_tracefunc func, PyObject *obj,
5513 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005514 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005515 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005517 int result;
5518 if (tstate->tracing)
5519 return 0;
5520 tstate->tracing++;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005521 tstate->cframe->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005522 if (frame->f_lasti < 0) {
5523 frame->f_lineno = frame->f_code->co_firstlineno;
5524 }
5525 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005526 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005527 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005530 frame->f_lineno = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005531 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005532 || (tstate->c_profilefunc != NULL));
5533 tstate->tracing--;
5534 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005535}
5536
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005537PyObject *
5538_PyEval_CallTracing(PyObject *func, PyObject *args)
5539{
Victor Stinner50b48572018-11-01 01:51:40 +01005540 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 int save_tracing = tstate->tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005542 int save_use_tracing = tstate->cframe->use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005546 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005547 || (tstate->c_profilefunc != NULL));
5548 result = PyObject_Call(func, args, NULL);
5549 tstate->tracing = save_tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005550 tstate->cframe->use_tracing = save_use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005552}
5553
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005554/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005555static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005556maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005557 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005558 PyTraceInfo *trace_info)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005560 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005561
Nick Coghlan5a851672017-09-08 10:14:16 +10005562 /* If the last instruction falls at the start of a line or if it
5563 represents a jump backwards, update the frame's line number and
5564 then call the trace function if we're tracing source lines.
5565 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005566 initialize_trace_info(trace_info, frame);
5567 int lastline = trace_info->bounds.ar_line;
Mark Shannonfcb55c02021-04-01 16:00:31 +01005568 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005569 if (line != -1 && frame->f_trace_lines) {
5570 /* Trace backward edges or first instruction of a new line */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005571 if (frame->f_lasti < trace_info->instr_prev ||
Mark Shannonfcb55c02021-04-01 16:00:31 +01005572 (line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
Mark Shannonee9f98d2021-01-05 12:04:10 +00005573 {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005574 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 }
George King20faa682017-10-18 17:44:22 -07005577 /* Always emit an opcode event if we're tracing all opcodes. */
5578 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005579 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005580 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005581 trace_info->instr_prev = frame->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005582 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005583}
5584
Victor Stinner309d7cc2020-03-13 16:39:12 +01005585int
5586_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5587{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005588 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005589 /* The caller must hold the GIL */
5590 assert(PyGILState_Check());
5591
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005592 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005593 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005594 PyThreadState *current_tstate = _PyThreadState_GET();
5595 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005596 return -1;
5597 }
5598
5599 PyObject *profileobj = tstate->c_profileobj;
5600
5601 tstate->c_profilefunc = NULL;
5602 tstate->c_profileobj = NULL;
5603 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005604 tstate->cframe->use_tracing = tstate->c_tracefunc != NULL;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005605 Py_XDECREF(profileobj);
5606
5607 Py_XINCREF(arg);
5608 tstate->c_profileobj = arg;
5609 tstate->c_profilefunc = func;
5610
5611 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005612 tstate->cframe->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005613 return 0;
5614}
5615
Fred Drake5755ce62001-06-27 19:19:46 +00005616void
5617PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005618{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005619 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005620 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005621 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005622 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5623 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005624}
5625
5626int
5627_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5628{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005629 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005630 /* The caller must hold the GIL */
5631 assert(PyGILState_Check());
5632
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005633 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005634 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005635 PyThreadState *current_tstate = _PyThreadState_GET();
5636 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005637 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005638 }
5639
Victor Stinner309d7cc2020-03-13 16:39:12 +01005640 PyObject *traceobj = tstate->c_traceobj;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005641
5642 tstate->c_tracefunc = NULL;
5643 tstate->c_traceobj = NULL;
5644 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005645 tstate->cframe->use_tracing = (tstate->c_profilefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005646 Py_XDECREF(traceobj);
5647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005648 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005649 tstate->c_traceobj = arg;
5650 tstate->c_tracefunc = func;
5651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005652 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005653 tstate->cframe->use_tracing = ((func != NULL)
Victor Stinner309d7cc2020-03-13 16:39:12 +01005654 || (tstate->c_profilefunc != NULL));
5655
5656 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005657}
5658
5659void
5660PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5661{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005662 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005663 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005664 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005665 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5666 }
Fred Draked0838392001-06-16 21:02:31 +00005667}
5668
Victor Stinner309d7cc2020-03-13 16:39:12 +01005669
Yury Selivanov75445082015-05-11 22:57:16 -04005670void
Victor Stinner838f2642019-06-13 22:41:23 +02005671_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005672{
5673 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005674 tstate->coroutine_origin_tracking_depth = new_depth;
5675}
5676
5677int
5678_PyEval_GetCoroutineOriginTrackingDepth(void)
5679{
Victor Stinner50b48572018-11-01 01:51:40 +01005680 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005681 return tstate->coroutine_origin_tracking_depth;
5682}
5683
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005684int
Yury Selivanoveb636452016-09-08 22:01:51 -07005685_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5686{
Victor Stinner50b48572018-11-01 01:51:40 +01005687 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005688
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005689 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005690 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005691 }
5692
Yury Selivanoveb636452016-09-08 22:01:51 -07005693 Py_XINCREF(firstiter);
5694 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005695 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005696}
5697
5698PyObject *
5699_PyEval_GetAsyncGenFirstiter(void)
5700{
Victor Stinner50b48572018-11-01 01:51:40 +01005701 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005702 return tstate->async_gen_firstiter;
5703}
5704
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005705int
Yury Selivanoveb636452016-09-08 22:01:51 -07005706_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5707{
Victor Stinner50b48572018-11-01 01:51:40 +01005708 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005709
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005710 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005711 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005712 }
5713
Yury Selivanoveb636452016-09-08 22:01:51 -07005714 Py_XINCREF(finalizer);
5715 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005716 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005717}
5718
5719PyObject *
5720_PyEval_GetAsyncGenFinalizer(void)
5721{
Victor Stinner50b48572018-11-01 01:51:40 +01005722 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005723 return tstate->async_gen_finalizer;
5724}
5725
Victor Stinner438a12d2019-05-24 17:01:38 +02005726PyFrameObject *
5727PyEval_GetFrame(void)
5728{
5729 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005730 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005731}
5732
Guido van Rossumb209a111997-04-29 18:18:01 +00005733PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005734_PyEval_GetBuiltins(PyThreadState *tstate)
5735{
5736 PyFrameObject *frame = tstate->frame;
5737 if (frame != NULL) {
5738 return frame->f_builtins;
5739 }
5740 return tstate->interp->builtins;
5741}
5742
5743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005744PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005745{
Victor Stinner438a12d2019-05-24 17:01:38 +02005746 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005747 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005748}
5749
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005750/* Convenience function to get a builtin from its name */
5751PyObject *
5752_PyEval_GetBuiltinId(_Py_Identifier *name)
5753{
Victor Stinner438a12d2019-05-24 17:01:38 +02005754 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005755 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5756 if (attr) {
5757 Py_INCREF(attr);
5758 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005759 else if (!_PyErr_Occurred(tstate)) {
5760 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005761 }
5762 return attr;
5763}
5764
Guido van Rossumb209a111997-04-29 18:18:01 +00005765PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005766PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005767{
Victor Stinner438a12d2019-05-24 17:01:38 +02005768 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005769 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005770 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005771 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005772 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005773 }
5774
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005776 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005777 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005778
5779 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005780 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005781}
5782
Guido van Rossumb209a111997-04-29 18:18:01 +00005783PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005784PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005785{
Victor Stinner438a12d2019-05-24 17:01:38 +02005786 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005787 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005788 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005789 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005790 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005791
5792 assert(current_frame->f_globals != NULL);
5793 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005794}
5795
Guido van Rossum6135a871995-01-09 17:53:26 +00005796int
Tim Peters5ba58662001-07-16 02:29:45 +00005797PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005798{
Victor Stinner438a12d2019-05-24 17:01:38 +02005799 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005800 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005801 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005803 if (current_frame != NULL) {
5804 const int codeflags = current_frame->f_code->co_flags;
5805 const int compilerflags = codeflags & PyCF_MASK;
5806 if (compilerflags) {
5807 result = 1;
5808 cf->cf_flags |= compilerflags;
5809 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005810#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005811 if (codeflags & CO_GENERATOR_ALLOWED) {
5812 result = 1;
5813 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5814 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005815#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005816 }
5817 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005818}
5819
Guido van Rossum3f5da241990-12-20 15:06:42 +00005820
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005821const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005822PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 if (PyMethod_Check(func))
5825 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5826 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005827 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005828 else if (PyCFunction_Check(func))
5829 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5830 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005831 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005832}
5833
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005834const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005835PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005837 if (PyMethod_Check(func))
5838 return "()";
5839 else if (PyFunction_Check(func))
5840 return "()";
5841 else if (PyCFunction_Check(func))
5842 return "()";
5843 else
5844 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005845}
5846
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005847#define C_TRACE(x, call) \
Mark Shannon9e7b2072021-04-13 11:08:14 +01005848if (trace_info->cframe.use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005849 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005850 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005851 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 x = NULL; \
5853 } \
5854 else { \
5855 x = call; \
5856 if (tstate->c_profilefunc != NULL) { \
5857 if (x == NULL) { \
5858 call_trace_protected(tstate->c_profilefunc, \
5859 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005860 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005861 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005862 /* XXX should pass (type, value, tb) */ \
5863 } else { \
5864 if (call_trace(tstate->c_profilefunc, \
5865 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005866 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005867 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005868 Py_DECREF(x); \
5869 x = NULL; \
5870 } \
5871 } \
5872 } \
5873 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005874} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005875 x = call; \
5876 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005877
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005878
5879static PyObject *
5880trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005881 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005882 PyObject *func,
5883 PyObject **args, Py_ssize_t nargs,
5884 PyObject *kwnames)
5885{
5886 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005887 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005888 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005889 return x;
5890 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005891 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005892 /* We need to create a temporary bound method as argument
5893 for profiling.
5894
5895 If nargs == 0, then this cannot work because we have no
5896 "self". In any case, the call itself would raise
5897 TypeError (foo needs an argument), so we just skip
5898 profiling. */
5899 PyObject *self = args[0];
5900 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5901 if (func == NULL) {
5902 return NULL;
5903 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005904 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005905 args+1, nargs-1,
5906 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005907 Py_DECREF(func);
5908 return x;
5909 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005910 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005911}
5912
Victor Stinner415c5102017-01-11 00:54:57 +01005913/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5914 to reduce the stack consumption. */
5915Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005916call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005917 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005918 PyObject ***pp_stack,
5919 Py_ssize_t oparg,
5920 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005921{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005922 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923 PyObject *func = *pfunc;
5924 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005925 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5926 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005927 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005928
Mark Shannon9e7b2072021-04-13 11:08:14 +01005929 if (trace_info->cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005930 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005931 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005932 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005933 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005934 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005935
Victor Stinner438a12d2019-05-24 17:01:38 +02005936 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005937
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005938 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005939 while ((*pp_stack) > pfunc) {
5940 w = EXT_POP(*pp_stack);
5941 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005942 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005944 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005945}
5946
Jeremy Hylton52820442001-01-03 23:52:36 +00005947static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005948do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005949 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005950 PyObject *func,
5951 PyObject *callargs,
5952 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005953{
jdemeyere89de732018-09-19 12:06:20 +02005954 PyObject *result;
5955
scoder4c9ea092020-05-12 16:12:41 +02005956 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005957 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005958 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005959 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005960 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005961 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
Mark Shannon9e7b2072021-04-13 11:08:14 +01005962 if (nargs > 0 && trace_info->cframe.use_tracing) {
jdemeyere89de732018-09-19 12:06:20 +02005963 /* We need to create a temporary bound method as argument
5964 for profiling.
5965
5966 If nargs == 0, then this cannot work because we have no
5967 "self". In any case, the call itself would raise
5968 TypeError (foo needs an argument), so we just skip
5969 profiling. */
5970 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5971 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5972 if (func == NULL) {
5973 return NULL;
5974 }
5975
Victor Stinner4d231bc2019-11-14 13:36:21 +01005976 C_TRACE(result, _PyObject_FastCallDictTstate(
5977 tstate, func,
5978 &_PyTuple_ITEMS(callargs)[1],
5979 nargs - 1,
5980 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005981 Py_DECREF(func);
5982 return result;
5983 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005984 }
jdemeyere89de732018-09-19 12:06:20 +02005985 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005986}
5987
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005988/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005989 nb_index slot defined, and store in *pi.
5990 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005991 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005992 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005993*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005994int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005995_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005996{
Victor Stinner438a12d2019-05-24 17:01:38 +02005997 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner09bbebe2021-04-11 00:17:39 +02005998 if (!Py_IsNone(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005999 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02006000 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006001 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006002 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006003 return 0;
6004 }
6005 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006006 _PyErr_SetString(tstate, PyExc_TypeError,
6007 "slice indices must be integers or "
6008 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006009 return 0;
6010 }
6011 *pi = x;
6012 }
6013 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006014}
6015
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006016int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006017_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006018{
Victor Stinner438a12d2019-05-24 17:01:38 +02006019 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006020 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02006021 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006022 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006023 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006024 return 0;
6025 }
6026 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006027 _PyErr_SetString(tstate, PyExc_TypeError,
6028 "slice indices must be integers or "
6029 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006030 return 0;
6031 }
6032 *pi = x;
6033 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006034}
6035
Thomas Wouters52152252000-08-17 22:55:00 +00006036static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006037import_name(PyThreadState *tstate, PyFrameObject *f,
6038 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006039{
6040 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006041 PyObject *import_func, *res;
6042 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006043
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006044 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006045 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006046 if (!_PyErr_Occurred(tstate)) {
6047 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006048 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006049 return NULL;
6050 }
6051
6052 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006053 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006054 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006055 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006056 return NULL;
6057 }
6058 res = PyImport_ImportModuleLevelObject(
6059 name,
6060 f->f_globals,
6061 f->f_locals == NULL ? Py_None : f->f_locals,
6062 fromlist,
6063 ilevel);
6064 return res;
6065 }
6066
6067 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006068
6069 stack[0] = name;
6070 stack[1] = f->f_globals;
6071 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6072 stack[3] = fromlist;
6073 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006074 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006075 Py_DECREF(import_func);
6076 return res;
6077}
6078
6079static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006080import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006082 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006083 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006084
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006085 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006086 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006087 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006088 /* Issue #17636: in case this failed because of a circular relative
6089 import, try to fallback on reading the module directly from
6090 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006091 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006092 if (pkgname == NULL) {
6093 goto error;
6094 }
Oren Milman6db70332017-09-19 14:23:01 +03006095 if (!PyUnicode_Check(pkgname)) {
6096 Py_CLEAR(pkgname);
6097 goto error;
6098 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006099 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006100 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006101 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006102 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006103 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006104 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006105 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006106 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006107 goto error;
6108 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006109 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006110 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006111 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006112 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006113 if (pkgname == NULL) {
6114 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6115 if (pkgname_or_unknown == NULL) {
6116 Py_XDECREF(pkgpath);
6117 return NULL;
6118 }
6119 } else {
6120 pkgname_or_unknown = pkgname;
6121 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006122
6123 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006124 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006125 errmsg = PyUnicode_FromFormat(
6126 "cannot import name %R from %R (unknown location)",
6127 name, pkgname_or_unknown
6128 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006129 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006130 PyErr_SetImportError(errmsg, pkgname, NULL);
6131 }
6132 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006133 _Py_IDENTIFIER(__spec__);
6134 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006135 const char *fmt =
6136 _PyModuleSpec_IsInitializing(spec) ?
6137 "cannot import name %R from partially initialized module %R "
6138 "(most likely due to a circular import) (%S)" :
6139 "cannot import name %R from %R (%S)";
6140 Py_XDECREF(spec);
6141
6142 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006143 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006144 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006145 }
6146
Xiang Zhang4830f582017-03-21 11:13:42 +08006147 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006148 Py_XDECREF(pkgname_or_unknown);
6149 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006150 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006151}
Guido van Rossumac7be682001-01-17 15:42:30 +00006152
Thomas Wouters52152252000-08-17 22:55:00 +00006153static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006154import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006155{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006156 _Py_IDENTIFIER(__all__);
6157 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006158 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159 int skip_leading_underscores = 0;
6160 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006161
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006162 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6163 return -1; /* Unexpected error */
6164 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006165 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006166 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6167 return -1;
6168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006170 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006171 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006172 return -1;
6173 }
6174 all = PyMapping_Keys(dict);
6175 Py_DECREF(dict);
6176 if (all == NULL)
6177 return -1;
6178 skip_leading_underscores = 1;
6179 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006181 for (pos = 0, err = 0; ; pos++) {
6182 name = PySequence_GetItem(all, pos);
6183 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006184 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006185 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006186 }
6187 else {
6188 _PyErr_Clear(tstate);
6189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006190 break;
6191 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006192 if (!PyUnicode_Check(name)) {
6193 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6194 if (modname == NULL) {
6195 Py_DECREF(name);
6196 err = -1;
6197 break;
6198 }
6199 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006200 _PyErr_Format(tstate, PyExc_TypeError,
6201 "module __name__ must be a string, not %.100s",
6202 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006203 }
6204 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006205 _PyErr_Format(tstate, PyExc_TypeError,
6206 "%s in %U.%s must be str, not %.100s",
6207 skip_leading_underscores ? "Key" : "Item",
6208 modname,
6209 skip_leading_underscores ? "__dict__" : "__all__",
6210 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006211 }
6212 Py_DECREF(modname);
6213 Py_DECREF(name);
6214 err = -1;
6215 break;
6216 }
6217 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006218 if (PyUnicode_READY(name) == -1) {
6219 Py_DECREF(name);
6220 err = -1;
6221 break;
6222 }
6223 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6224 Py_DECREF(name);
6225 continue;
6226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006227 }
6228 value = PyObject_GetAttr(v, name);
6229 if (value == NULL)
6230 err = -1;
6231 else if (PyDict_CheckExact(locals))
6232 err = PyDict_SetItem(locals, name, value);
6233 else
6234 err = PyObject_SetItem(locals, name, value);
6235 Py_DECREF(name);
6236 Py_XDECREF(value);
6237 if (err != 0)
6238 break;
6239 }
6240 Py_DECREF(all);
6241 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006242}
6243
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006244static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006245check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006246{
Victor Stinnera102ed72020-02-07 02:24:48 +01006247 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006248 /* check_args_iterable() may be called with a live exception:
6249 * clear it to prevent calling _PyObject_FunctionStr() with an
6250 * exception set. */
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(tstate, PyExc_TypeError,
6255 "%U argument after * must be an iterable, not %.200s",
6256 funcstr, Py_TYPE(args)->tp_name);
6257 Py_DECREF(funcstr);
6258 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006259 return -1;
6260 }
6261 return 0;
6262}
6263
6264static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006265format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006266{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006267 /* _PyDict_MergeEx raises attribute
6268 * error (percolated from an attempt
6269 * to get 'keys' attribute) instead of
6270 * a type error if its second argument
6271 * is not a mapping.
6272 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006273 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006274 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006275 PyObject *funcstr = _PyObject_FunctionStr(func);
6276 if (funcstr != NULL) {
6277 _PyErr_Format(
6278 tstate, PyExc_TypeError,
6279 "%U argument after ** must be a mapping, not %.200s",
6280 funcstr, Py_TYPE(kwargs)->tp_name);
6281 Py_DECREF(funcstr);
6282 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006283 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006284 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006285 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006286 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006287 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006288 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006289 PyObject *funcstr = _PyObject_FunctionStr(func);
6290 if (funcstr != NULL) {
6291 PyObject *key = PyTuple_GET_ITEM(val, 0);
6292 _PyErr_Format(
6293 tstate, PyExc_TypeError,
6294 "%U got multiple values for keyword argument '%S'",
6295 funcstr, key);
6296 Py_DECREF(funcstr);
6297 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006298 Py_XDECREF(exc);
6299 Py_XDECREF(val);
6300 Py_XDECREF(tb);
6301 }
6302 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006303 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006304 }
6305 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006306}
6307
Guido van Rossumac7be682001-01-17 15:42:30 +00006308static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006309format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6310 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006312 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006314 if (!obj)
6315 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006316
Serhiy Storchaka06515832016-11-20 09:13:07 +02006317 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006318 if (!obj_str)
6319 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006320
Victor Stinner438a12d2019-05-24 17:01:38 +02006321 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006322}
Guido van Rossum950361c1997-01-24 13:49:28 +00006323
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006324static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006325format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006326{
6327 PyObject *name;
6328 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006329 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006330 return;
6331 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6332 name = PyTuple_GET_ITEM(co->co_cellvars,
6333 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006334 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006335 PyExc_UnboundLocalError,
6336 UNBOUNDLOCAL_ERROR_MSG,
6337 name);
6338 } else {
6339 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6340 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006341 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006342 UNBOUNDFREE_ERROR_MSG, name);
6343 }
6344}
6345
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006346static void
Mark Shannonfee55262019-11-21 09:11:43 +00006347format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006348{
6349 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6350 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006351 _PyErr_Format(tstate, PyExc_TypeError,
6352 "'async with' received an object from __aenter__ "
6353 "that does not implement __await__: %.100s",
6354 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006355 }
Mark Shannonfee55262019-11-21 09:11:43 +00006356 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006357 _PyErr_Format(tstate, PyExc_TypeError,
6358 "'async with' received an object from __aexit__ "
6359 "that does not implement __await__: %.100s",
6360 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006361 }
6362 }
6363}
6364
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006365static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006366unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006367 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006368{
6369 PyObject *res;
6370 if (Py_REFCNT(v) == 2) {
6371 /* In the common case, there are 2 references to the value
6372 * stored in 'variable' when the += is performed: one on the
6373 * value stack (in 'v') and one still stored in the
6374 * 'variable'. We try to delete the variable now to reduce
6375 * the refcnt to 1.
6376 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006377 int opcode, oparg;
6378 NEXTOPARG();
6379 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006380 case STORE_FAST:
6381 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006382 PyObject **fastlocals = f->f_localsplus;
6383 if (GETLOCAL(oparg) == v)
6384 SETLOCAL(oparg, NULL);
6385 break;
6386 }
6387 case STORE_DEREF:
6388 {
6389 PyObject **freevars = (f->f_localsplus +
6390 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006391 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006392 if (PyCell_GET(c) == v) {
6393 PyCell_SET(c, NULL);
6394 Py_DECREF(v);
6395 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006396 break;
6397 }
6398 case STORE_NAME:
6399 {
6400 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006401 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006402 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006403 if (locals && PyDict_CheckExact(locals)) {
6404 PyObject *w = PyDict_GetItemWithError(locals, name);
6405 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006406 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006407 {
6408 Py_DECREF(v);
6409 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006410 }
6411 }
6412 break;
6413 }
6414 }
6415 }
6416 res = v;
6417 PyUnicode_Append(&res, w);
6418 return res;
6419}
6420
Guido van Rossum950361c1997-01-24 13:49:28 +00006421#ifdef DYNAMIC_EXECUTION_PROFILE
6422
Skip Montanarof118cb12001-10-15 20:51:38 +00006423static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006424getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006426 int i;
6427 PyObject *l = PyList_New(256);
6428 if (l == NULL) return NULL;
6429 for (i = 0; i < 256; i++) {
6430 PyObject *x = PyLong_FromLong(a[i]);
6431 if (x == NULL) {
6432 Py_DECREF(l);
6433 return NULL;
6434 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006435 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006436 }
6437 for (i = 0; i < 256; i++)
6438 a[i] = 0;
6439 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006440}
6441
6442PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006443_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006444{
6445#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006446 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006447#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006448 int i;
6449 PyObject *l = PyList_New(257);
6450 if (l == NULL) return NULL;
6451 for (i = 0; i < 257; i++) {
6452 PyObject *x = getarray(dxpairs[i]);
6453 if (x == NULL) {
6454 Py_DECREF(l);
6455 return NULL;
6456 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006457 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006458 }
6459 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006460#endif
6461}
6462
6463#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006464
6465Py_ssize_t
6466_PyEval_RequestCodeExtraIndex(freefunc free)
6467{
Victor Stinner81a7be32020-04-14 15:14:01 +02006468 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006469 Py_ssize_t new_index;
6470
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006471 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006472 return -1;
6473 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006474 new_index = interp->co_extra_user_count++;
6475 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006476 return new_index;
6477}
Łukasz Langaa785c872016-09-09 17:37:37 -07006478
6479static void
6480dtrace_function_entry(PyFrameObject *f)
6481{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006482 const char *filename;
6483 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006484 int lineno;
6485
Victor Stinner6d86a232020-04-29 00:56:58 +02006486 PyCodeObject *code = f->f_code;
6487 filename = PyUnicode_AsUTF8(code->co_filename);
6488 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006489 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006490
Andy Lestere6be9b52020-02-11 20:28:35 -06006491 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006492}
6493
6494static void
6495dtrace_function_return(PyFrameObject *f)
6496{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006497 const char *filename;
6498 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006499 int lineno;
6500
Victor Stinner6d86a232020-04-29 00:56:58 +02006501 PyCodeObject *code = f->f_code;
6502 filename = PyUnicode_AsUTF8(code->co_filename);
6503 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006504 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006505
Andy Lestere6be9b52020-02-11 20:28:35 -06006506 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006507}
6508
6509/* DTrace equivalent of maybe_call_line_trace. */
6510static void
6511maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00006512 PyTraceInfo *trace_info)
Łukasz Langaa785c872016-09-09 17:37:37 -07006513{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006514 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006515
6516 /* If the last instruction executed isn't in the current
6517 instruction window, reset the window.
6518 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006519 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006520 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006521 /* If the last instruction falls at the start of a line or if
6522 it represents a jump backwards, update the frame's line
6523 number and call the trace function. */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006524 if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006525 if (line != -1) {
6526 frame->f_lineno = line;
6527 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6528 if (!co_filename)
6529 co_filename = "?";
6530 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6531 if (!co_name)
6532 co_name = "?";
6533 PyDTrace_LINE(co_filename, co_name, line);
6534 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006535 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00006536 trace_info->instr_prev = frame->f_lasti;
Łukasz Langaa785c872016-09-09 17:37:37 -07006537}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006538
6539
6540/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6541 for the limited API. */
6542
6543#undef Py_EnterRecursiveCall
6544
6545int Py_EnterRecursiveCall(const char *where)
6546{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006547 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006548}
6549
6550#undef Py_LeaveRecursiveCall
6551
6552void Py_LeaveRecursiveCall(void)
6553{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006554 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006555}