blob: 866c57afdbf9d06a9cdfba9db070f1a52fab2611 [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): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003892 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003893 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
3894 PyObject *res = match ? Py_True : Py_False;
3895 Py_INCREF(res);
3896 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003897 DISPATCH();
3898 }
3899
3900 case TARGET(MATCH_SEQUENCE): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003901 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003902 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
3903 PyObject *res = match ? Py_True : Py_False;
3904 Py_INCREF(res);
3905 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003906 DISPATCH();
3907 }
3908
3909 case TARGET(MATCH_KEYS): {
3910 // On successful match for all keys, PUSH(values) and PUSH(True).
3911 // Otherwise, PUSH(None) and PUSH(False).
3912 PyObject *keys = TOP();
3913 PyObject *subject = SECOND();
3914 PyObject *values_or_none = match_keys(tstate, subject, keys);
3915 if (values_or_none == NULL) {
3916 goto error;
3917 }
3918 PUSH(values_or_none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02003919 if (Py_IsNone(values_or_none)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08003920 Py_INCREF(Py_False);
3921 PUSH(Py_False);
3922 DISPATCH();
3923 }
3924 assert(PyTuple_CheckExact(values_or_none));
3925 Py_INCREF(Py_True);
3926 PUSH(Py_True);
3927 DISPATCH();
3928 }
3929
3930 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3931 // rest = dict(TOS1)
3932 // for key in TOS:
3933 // del rest[key]
3934 // SET_TOP(rest)
3935 PyObject *keys = TOP();
3936 PyObject *subject = SECOND();
3937 PyObject *rest = PyDict_New();
3938 if (rest == NULL || PyDict_Update(rest, subject)) {
3939 Py_XDECREF(rest);
3940 goto error;
3941 }
3942 // This may seem a bit inefficient, but keys is rarely big enough to
3943 // actually impact runtime.
3944 assert(PyTuple_CheckExact(keys));
3945 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3946 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3947 Py_DECREF(rest);
3948 goto error;
3949 }
3950 }
3951 Py_DECREF(keys);
3952 SET_TOP(rest);
3953 DISPATCH();
3954 }
3955
Benjamin Petersonddd19492018-09-16 22:38:02 -07003956 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003958 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003959 PyObject *iter = PyObject_GetIter(iterable);
3960 Py_DECREF(iterable);
3961 SET_TOP(iter);
3962 if (iter == NULL)
3963 goto error;
3964 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003965 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003966 DISPATCH();
3967 }
3968
Benjamin Petersonddd19492018-09-16 22:38:02 -07003969 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003970 /* before: [obj]; after [getiter(obj)] */
3971 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003972 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003973 if (PyCoro_CheckExact(iterable)) {
3974 /* `iterable` is a coroutine */
3975 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3976 /* and it is used in a 'yield from' expression of a
3977 regular generator. */
3978 Py_DECREF(iterable);
3979 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003980 _PyErr_SetString(tstate, PyExc_TypeError,
3981 "cannot 'yield from' a coroutine object "
3982 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003983 goto error;
3984 }
3985 }
3986 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003987 /* `iterable` is not a generator. */
3988 iter = PyObject_GetIter(iterable);
3989 Py_DECREF(iterable);
3990 SET_TOP(iter);
3991 if (iter == NULL)
3992 goto error;
3993 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003994 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003995 DISPATCH();
3996 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003997
Benjamin Petersonddd19492018-09-16 22:38:02 -07003998 case TARGET(FOR_ITER): {
3999 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004001 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004002 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004003 if (next != NULL) {
4004 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 PREDICT(STORE_FAST);
4006 PREDICT(UNPACK_SEQUENCE);
4007 DISPATCH();
4008 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004009 if (_PyErr_Occurred(tstate)) {
4010 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004011 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004012 }
4013 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004014 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004015 }
4016 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 }
4018 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004019 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004020 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 JUMPBY(oparg);
4022 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004023 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004024
Benjamin Petersonddd19492018-09-16 22:38:02 -07004025 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004026 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 STACK_LEVEL());
4028 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004029 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004030
Benjamin Petersonddd19492018-09-16 22:38:02 -07004031 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004032 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004033 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004034 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004035 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004036 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004037 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004038 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004039 }
4040 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4041 if (exit == NULL) {
4042 Py_DECREF(enter);
4043 goto error;
4044 }
Yury Selivanov75445082015-05-11 22:57:16 -04004045 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004046 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004047 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004048 Py_DECREF(enter);
4049 if (res == NULL)
4050 goto error;
4051 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004052 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004053 DISPATCH();
4054 }
4055
Benjamin Petersonddd19492018-09-16 22:38:02 -07004056 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004057 PyObject *res = POP();
4058 /* Setup the finally block before pushing the result
4059 of __aenter__ on the stack. */
4060 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4061 STACK_LEVEL());
4062 PUSH(res);
4063 DISPATCH();
4064 }
4065
Benjamin Petersonddd19492018-09-16 22:38:02 -07004066 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004067 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004068 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004069 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004070 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004071 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004072 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004073 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004074 }
4075 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004076 if (exit == NULL) {
4077 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004078 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004080 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004081 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004082 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004083 Py_DECREF(enter);
4084 if (res == NULL)
4085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 /* Setup the finally block before pushing the result
4087 of __enter__ on the stack. */
4088 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4089 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004091 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 DISPATCH();
4093 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004094
Mark Shannonfee55262019-11-21 09:11:43 +00004095 case TARGET(WITH_EXCEPT_START): {
4096 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004098 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4099 - SEVENTH: the context.__exit__ bound method
4100 We call SEVENTH(TOP, SECOND, THIRD).
4101 Then we push again the TOP exception and the __exit__
4102 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004105 PyObject *exc, *val, *tb, *res;
4106
Victor Stinner842cfff2016-12-01 14:45:31 +01004107 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004108 val = SECOND();
4109 tb = THIRD();
Victor Stinner09bbebe2021-04-11 00:17:39 +02004110 assert(!Py_IsNone(exc));
Mark Shannonfee55262019-11-21 09:11:43 +00004111 assert(!PyLong_Check(exc));
4112 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004113 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004114 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004115 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116 if (res == NULL)
4117 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004118
Yury Selivanov75445082015-05-11 22:57:16 -04004119 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004120 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004122
Benjamin Petersonddd19492018-09-16 22:38:02 -07004123 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004124 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004125 PyObject *name = GETITEM(names, oparg);
4126 PyObject *obj = TOP();
4127 PyObject *meth = NULL;
4128
4129 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4130
Yury Selivanovf2392132016-12-13 19:03:51 -05004131 if (meth == NULL) {
4132 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004133 goto error;
4134 }
4135
4136 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004137 /* We can bypass temporary bound method object.
4138 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004139
INADA Naoki015bce62017-01-16 17:23:30 +09004140 meth | self | arg1 | ... | argN
4141 */
4142 SET_TOP(meth);
4143 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004144 }
4145 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004146 /* meth is not an unbound method (but a regular attr, or
4147 something was returned by a descriptor protocol). Set
4148 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004149 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004150
4151 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004152 */
INADA Naoki015bce62017-01-16 17:23:30 +09004153 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004154 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004155 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004156 }
4157 DISPATCH();
4158 }
4159
Benjamin Petersonddd19492018-09-16 22:38:02 -07004160 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004161 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004162 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004163
4164 sp = stack_pointer;
4165
INADA Naoki015bce62017-01-16 17:23:30 +09004166 meth = PEEK(oparg + 2);
4167 if (meth == NULL) {
4168 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4169 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004170
4171 Stack layout:
4172
INADA Naoki015bce62017-01-16 17:23:30 +09004173 ... | NULL | callable | arg1 | ... | argN
4174 ^- TOP()
4175 ^- (-oparg)
4176 ^- (-oparg-1)
4177 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004178
Ville Skyttä49b27342017-08-03 09:00:59 +03004179 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004180 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004181 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004182 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004183 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004184 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004185 }
4186 else {
4187 /* This is a method call. Stack layout:
4188
INADA Naoki015bce62017-01-16 17:23:30 +09004189 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004190 ^- TOP()
4191 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004192 ^- (-oparg-1)
4193 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004194
INADA Naoki015bce62017-01-16 17:23:30 +09004195 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004196 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004197 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004198 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004199 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004200 stack_pointer = sp;
4201 }
4202
4203 PUSH(res);
4204 if (res == NULL)
4205 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004206 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004207 DISPATCH();
4208 }
4209
Benjamin Petersonddd19492018-09-16 22:38:02 -07004210 case TARGET(CALL_FUNCTION): {
4211 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004212 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004214 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004216 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004217 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004218 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004219 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004220 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004221 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004223
Benjamin Petersonddd19492018-09-16 22:38:02 -07004224 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004225 PyObject **sp, *res, *names;
4226
4227 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004228 assert(PyTuple_Check(names));
4229 assert(PyTuple_GET_SIZE(names) <= oparg);
4230 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004232 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004234 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004235 Py_DECREF(names);
4236
4237 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004238 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004239 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004240 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004241 DISPATCH();
4242 }
4243
Benjamin Petersonddd19492018-09-16 22:38:02 -07004244 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004245 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004246 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004247 if (oparg & 0x01) {
4248 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004249 if (!PyDict_CheckExact(kwargs)) {
4250 PyObject *d = PyDict_New();
4251 if (d == NULL)
4252 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004253 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004254 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004255 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004256 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004257 goto error;
4258 }
4259 Py_DECREF(kwargs);
4260 kwargs = d;
4261 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004262 assert(PyDict_CheckExact(kwargs));
4263 }
4264 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004265 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004266 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004267 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004268 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004269 goto error;
4270 }
4271 Py_SETREF(callargs, PySequence_Tuple(callargs));
4272 if (callargs == NULL) {
4273 goto error;
4274 }
4275 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004276 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004277
Mark Shannon8e1b4062021-03-05 14:45:50 +00004278 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004279 Py_DECREF(func);
4280 Py_DECREF(callargs);
4281 Py_XDECREF(kwargs);
4282
4283 SET_TOP(result);
4284 if (result == NULL) {
4285 goto error;
4286 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004287 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004288 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004290
Benjamin Petersonddd19492018-09-16 22:38:02 -07004291 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004292 PyObject *qualname = POP();
4293 PyObject *codeobj = POP();
4294 PyFunctionObject *func = (PyFunctionObject *)
4295 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004296
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004297 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004298 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004299 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004300 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004302
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004303 if (oparg & 0x08) {
4304 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004305 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004306 }
4307 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004308 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004309 func->func_annotations = POP();
4310 }
4311 if (oparg & 0x02) {
4312 assert(PyDict_CheckExact(TOP()));
4313 func->func_kwdefaults = POP();
4314 }
4315 if (oparg & 0x01) {
4316 assert(PyTuple_CheckExact(TOP()));
4317 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004319
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004320 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004321 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004323
Benjamin Petersonddd19492018-09-16 22:38:02 -07004324 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004325 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004327 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004329 step = NULL;
4330 stop = POP();
4331 start = TOP();
4332 slice = PySlice_New(start, stop, step);
4333 Py_DECREF(start);
4334 Py_DECREF(stop);
4335 Py_XDECREF(step);
4336 SET_TOP(slice);
4337 if (slice == NULL)
4338 goto error;
4339 DISPATCH();
4340 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004341
Benjamin Petersonddd19492018-09-16 22:38:02 -07004342 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004343 /* Handles f-string value formatting. */
4344 PyObject *result;
4345 PyObject *fmt_spec;
4346 PyObject *value;
4347 PyObject *(*conv_fn)(PyObject *);
4348 int which_conversion = oparg & FVC_MASK;
4349 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4350
4351 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004352 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004353
4354 /* See if any conversion is specified. */
4355 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004356 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004357 case FVC_STR: conv_fn = PyObject_Str; break;
4358 case FVC_REPR: conv_fn = PyObject_Repr; break;
4359 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004360 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004361 _PyErr_Format(tstate, PyExc_SystemError,
4362 "unexpected conversion flag %d",
4363 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004364 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004365 }
4366
4367 /* If there's a conversion function, call it and replace
4368 value with that result. Otherwise, just use value,
4369 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004370 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004371 result = conv_fn(value);
4372 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004373 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004374 Py_XDECREF(fmt_spec);
4375 goto error;
4376 }
4377 value = result;
4378 }
4379
4380 /* If value is a unicode object, and there's no fmt_spec,
4381 then we know the result of format(value) is value
4382 itself. In that case, skip calling format(). I plan to
4383 move this optimization in to PyObject_Format()
4384 itself. */
4385 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4386 /* Do nothing, just transfer ownership to result. */
4387 result = value;
4388 } else {
4389 /* Actually call format(). */
4390 result = PyObject_Format(value, fmt_spec);
4391 Py_DECREF(value);
4392 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004393 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004394 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004395 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004396 }
4397
Eric V. Smith135d5f42016-02-05 18:23:08 -05004398 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004399 DISPATCH();
4400 }
4401
Benjamin Petersonddd19492018-09-16 22:38:02 -07004402 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004403 int oldoparg = oparg;
4404 NEXTOPARG();
4405 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004407 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004408
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004409
Antoine Pitrou042b1282010-08-13 21:15:58 +00004410#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004412#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 default:
4414 fprintf(stderr,
4415 "XXX lineno: %d, opcode: %d\n",
4416 PyFrame_GetLineNumber(f),
4417 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004418 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004419 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004423 /* This should never be reached. Every opcode should end with DISPATCH()
4424 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004425 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004427error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004428 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004429#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004430 if (!_PyErr_Occurred(tstate)) {
4431 _PyErr_SetString(tstate, PyExc_SystemError,
4432 "error return without exception set");
4433 }
Victor Stinner365b6932013-07-12 00:11:58 +02004434#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004435 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004436#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004437
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004438 /* Log traceback info. */
4439 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004440
Mark Shannoncb9879b2020-07-17 11:44:23 +01004441 if (tstate->c_tracefunc != NULL) {
4442 /* Make sure state is set to FRAME_EXECUTING for tracing */
4443 assert(f->f_state == FRAME_EXECUTING);
4444 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004445 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004446 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004447 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004448exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004449 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004450 /* Unwind stacks if an exception occurred */
4451 while (f->f_iblock > 0) {
4452 /* Pop the current block. */
4453 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (b->b_type == EXCEPT_HANDLER) {
4456 UNWIND_EXCEPT_HANDLER(b);
4457 continue;
4458 }
4459 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004460 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 PyObject *exc, *val, *tb;
4462 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004463 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004465 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004466 PUSH(exc_info->exc_traceback);
4467 PUSH(exc_info->exc_value);
4468 if (exc_info->exc_type != NULL) {
4469 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 }
4471 else {
4472 Py_INCREF(Py_None);
4473 PUSH(Py_None);
4474 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 /* Make the raw exception data
4477 available to the handler,
4478 so a program can emulate the
4479 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004481 if (tb != NULL)
4482 PyException_SetTraceback(val, tb);
4483 else
4484 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004486 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004488 exc_info->exc_value = val;
4489 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 if (tb == NULL)
4491 tb = Py_None;
4492 Py_INCREF(tb);
4493 PUSH(tb);
4494 PUSH(val);
4495 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 JUMPTO(handler);
Mark Shannon9e7b2072021-04-13 11:08:14 +01004497 if (trace_info.cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004498 trace_info.instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004499 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004500 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004501 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004502 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
4504 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004505
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004506 /* End the loop as we still have an error */
4507 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004509
Pablo Galindof00828a2019-05-09 16:52:02 +01004510 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004511 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 /* Pop remaining stack entries. */
4514 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004515 PyObject *o = POP();
4516 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004518 f->f_stackdepth = 0;
4519 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004520exiting:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004521 if (trace_info.cframe.use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004522 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004523 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004524 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004525 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 }
4527 }
4528 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004529 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004530 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004531 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 }
4533 }
4534 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004537exit_eval_frame:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004538 /* Restore previous cframe */
4539 tstate->cframe = trace_info.cframe.previous;
4540 tstate->cframe->use_tracing = trace_info.cframe.use_tracing;
4541
Łukasz Langaa785c872016-09-09 17:37:37 -07004542 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4543 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004544 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004546
Victor Stinner0b72b232020-03-12 23:18:39 +01004547 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004548}
4549
Benjamin Petersonb204a422011-06-05 22:04:07 -05004550static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004551format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004552 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004553{
4554 int err;
4555 Py_ssize_t len = PyList_GET_SIZE(names);
4556 PyObject *name_str, *comma, *tail, *tmp;
4557
4558 assert(PyList_CheckExact(names));
4559 assert(len >= 1);
4560 /* Deal with the joys of natural language. */
4561 switch (len) {
4562 case 1:
4563 name_str = PyList_GET_ITEM(names, 0);
4564 Py_INCREF(name_str);
4565 break;
4566 case 2:
4567 name_str = PyUnicode_FromFormat("%U and %U",
4568 PyList_GET_ITEM(names, len - 2),
4569 PyList_GET_ITEM(names, len - 1));
4570 break;
4571 default:
4572 tail = PyUnicode_FromFormat(", %U, and %U",
4573 PyList_GET_ITEM(names, len - 2),
4574 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004575 if (tail == NULL)
4576 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004577 /* Chop off the last two objects in the list. This shouldn't actually
4578 fail, but we can't be too careful. */
4579 err = PyList_SetSlice(names, len - 2, len, NULL);
4580 if (err == -1) {
4581 Py_DECREF(tail);
4582 return;
4583 }
4584 /* Stitch everything up into a nice comma-separated list. */
4585 comma = PyUnicode_FromString(", ");
4586 if (comma == NULL) {
4587 Py_DECREF(tail);
4588 return;
4589 }
4590 tmp = PyUnicode_Join(comma, names);
4591 Py_DECREF(comma);
4592 if (tmp == NULL) {
4593 Py_DECREF(tail);
4594 return;
4595 }
4596 name_str = PyUnicode_Concat(tmp, tail);
4597 Py_DECREF(tmp);
4598 Py_DECREF(tail);
4599 break;
4600 }
4601 if (name_str == NULL)
4602 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004603 _PyErr_Format(tstate, PyExc_TypeError,
4604 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004605 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004606 len,
4607 kind,
4608 len == 1 ? "" : "s",
4609 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004610 Py_DECREF(name_str);
4611}
4612
4613static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004614missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4615 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004616 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004617{
Victor Stinner74319ae2016-08-25 00:04:09 +02004618 Py_ssize_t i, j = 0;
4619 Py_ssize_t start, end;
4620 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004621 const char *kind = positional ? "positional" : "keyword-only";
4622 PyObject *missing_names;
4623
4624 /* Compute the names of the arguments that are missing. */
4625 missing_names = PyList_New(missing);
4626 if (missing_names == NULL)
4627 return;
4628 if (positional) {
4629 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004630 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004631 }
4632 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004633 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004634 end = start + co->co_kwonlyargcount;
4635 }
4636 for (i = start; i < end; i++) {
4637 if (GETLOCAL(i) == NULL) {
4638 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4639 PyObject *name = PyObject_Repr(raw);
4640 if (name == NULL) {
4641 Py_DECREF(missing_names);
4642 return;
4643 }
4644 PyList_SET_ITEM(missing_names, j++, name);
4645 }
4646 }
4647 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004648 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004649 Py_DECREF(missing_names);
4650}
4651
4652static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004653too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004654 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004655 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004656{
4657 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004658 Py_ssize_t kwonly_given = 0;
4659 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004660 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004661 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004662
Benjamin Petersone109c702011-06-24 09:37:26 -05004663 assert((co->co_flags & CO_VARARGS) == 0);
4664 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004665 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004666 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004667 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004668 }
4669 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004670 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004671 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004672 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004673 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004674 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004675 }
4676 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004677 plural = (co_argcount != 1);
4678 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004679 }
4680 if (sig == NULL)
4681 return;
4682 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004683 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4684 kwonly_sig = PyUnicode_FromFormat(format,
4685 given != 1 ? "s" : "",
4686 kwonly_given,
4687 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004688 if (kwonly_sig == NULL) {
4689 Py_DECREF(sig);
4690 return;
4691 }
4692 }
4693 else {
4694 /* This will not fail. */
4695 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004696 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004697 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004698 _PyErr_Format(tstate, PyExc_TypeError,
4699 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004700 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004701 sig,
4702 plural ? "s" : "",
4703 given,
4704 kwonly_sig,
4705 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004706 Py_DECREF(sig);
4707 Py_DECREF(kwonly_sig);
4708}
4709
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004710static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004711positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004712 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004713 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004714{
4715 int posonly_conflicts = 0;
4716 PyObject* posonly_names = PyList_New(0);
4717
4718 for(int k=0; k < co->co_posonlyargcount; k++){
4719 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4720
4721 for (int k2=0; k2<kwcount; k2++){
4722 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004723 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004724 if (kwname == posonly_name){
4725 if(PyList_Append(posonly_names, kwname) != 0) {
4726 goto fail;
4727 }
4728 posonly_conflicts++;
4729 continue;
4730 }
4731
4732 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4733
4734 if ( cmp > 0) {
4735 if(PyList_Append(posonly_names, kwname) != 0) {
4736 goto fail;
4737 }
4738 posonly_conflicts++;
4739 } else if (cmp < 0) {
4740 goto fail;
4741 }
4742
4743 }
4744 }
4745 if (posonly_conflicts) {
4746 PyObject* comma = PyUnicode_FromString(", ");
4747 if (comma == NULL) {
4748 goto fail;
4749 }
4750 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4751 Py_DECREF(comma);
4752 if (error_names == NULL) {
4753 goto fail;
4754 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004755 _PyErr_Format(tstate, PyExc_TypeError,
4756 "%U() got some positional-only arguments passed"
4757 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004758 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004759 Py_DECREF(error_names);
4760 goto fail;
4761 }
4762
4763 Py_DECREF(posonly_names);
4764 return 0;
4765
4766fail:
4767 Py_XDECREF(posonly_names);
4768 return 1;
4769
4770}
4771
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004772
Mark Shannon0332e562021-02-01 10:42:03 +00004773PyFrameObject *
4774_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004775 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004776 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004777 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004778{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004779 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004780
Mark Shannond6c33fb2021-01-29 13:24:55 +00004781 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4782 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004783 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004784
Victor Stinnerc7020012016-08-16 23:40:29 +02004785 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004786 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004787 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004789 }
Victor Stinner232dda62020-06-04 15:19:02 +02004790 PyObject **fastlocals = f->f_localsplus;
4791 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004792
Victor Stinnerc7020012016-08-16 23:40:29 +02004793 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004794 PyObject *kwdict;
4795 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004796 if (co->co_flags & CO_VARKEYWORDS) {
4797 kwdict = PyDict_New();
4798 if (kwdict == NULL)
4799 goto fail;
4800 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004801 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004802 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004803 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004804 SETLOCAL(i, kwdict);
4805 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004806 else {
4807 kwdict = NULL;
4808 }
4809
Pablo Galindocd74e662019-06-01 18:08:04 +01004810 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004811 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004812 if (argcount > co->co_argcount) {
4813 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004814 }
4815 else {
4816 n = argcount;
4817 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004818 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004819 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004820 Py_INCREF(x);
4821 SETLOCAL(j, x);
4822 }
4823
Victor Stinnerc7020012016-08-16 23:40:29 +02004824 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004825 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004826 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004827 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004828 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004829 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004830 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004831 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004832
Mark Shannon0332e562021-02-01 10:42:03 +00004833 /* Handle keyword arguments */
4834 if (kwnames != NULL) {
4835 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4836 for (i = 0; i < kwcount; i++) {
4837 PyObject **co_varnames;
4838 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4839 PyObject *value = args[i+argcount];
4840 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004841
Mark Shannon0332e562021-02-01 10:42:03 +00004842 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4843 _PyErr_Format(tstate, PyExc_TypeError,
4844 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004845 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004848
Mark Shannon0332e562021-02-01 10:42:03 +00004849 /* Speed hack: do raw pointer compares. As names are
4850 normally interned this should almost always hit. */
4851 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4852 for (j = co->co_posonlyargcount; j < total_args; j++) {
4853 PyObject *varname = co_varnames[j];
4854 if (varname == keyword) {
4855 goto kw_found;
4856 }
4857 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004858
Mark Shannon0332e562021-02-01 10:42:03 +00004859 /* Slow fallback, just in case */
4860 for (j = co->co_posonlyargcount; j < total_args; j++) {
4861 PyObject *varname = co_varnames[j];
4862 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4863 if (cmp > 0) {
4864 goto kw_found;
4865 }
4866 else if (cmp < 0) {
4867 goto fail;
4868 }
4869 }
4870
4871 assert(j >= total_args);
4872 if (kwdict == NULL) {
4873
4874 if (co->co_posonlyargcount
4875 && positional_only_passed_as_keyword(tstate, co,
4876 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004877 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004878 {
4879 goto fail;
4880 }
4881
4882 _PyErr_Format(tstate, PyExc_TypeError,
4883 "%U() got an unexpected keyword argument '%S'",
4884 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004885 goto fail;
4886 }
4887
Mark Shannon0332e562021-02-01 10:42:03 +00004888 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4889 goto fail;
4890 }
4891 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004892
Mark Shannon0332e562021-02-01 10:42:03 +00004893 kw_found:
4894 if (GETLOCAL(j) != NULL) {
4895 _PyErr_Format(tstate, PyExc_TypeError,
4896 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004897 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004898 goto fail;
4899 }
4900 Py_INCREF(value);
4901 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004904
4905 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004906 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004907 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4908 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 goto fail;
4910 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004911
4912 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004913 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004914 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004915 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004916 Py_ssize_t missing = 0;
4917 for (i = argcount; i < m; i++) {
4918 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004919 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004920 }
4921 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004922 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004923 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004924 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004925 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004926 }
4927 if (n > m)
4928 i = n - m;
4929 else
4930 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004931 if (defcount) {
4932 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4933 for (; i < defcount; i++) {
4934 if (GETLOCAL(m+i) == NULL) {
4935 PyObject *def = defs[i];
4936 Py_INCREF(def);
4937 SETLOCAL(m+i, def);
4938 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004939 }
4940 }
4941 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004942
4943 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004944 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004945 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004946 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004947 if (GETLOCAL(i) != NULL)
4948 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004949 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004950 if (con->fc_kwdefaults != NULL) {
4951 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004952 if (def) {
4953 Py_INCREF(def);
4954 SETLOCAL(i, def);
4955 continue;
4956 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004957 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004958 goto fail;
4959 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004960 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004961 missing++;
4962 }
4963 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004964 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004965 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004966 goto fail;
4967 }
4968 }
4969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004971 vars into frame. */
4972 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004974 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004975 /* Possibly account for the cell variable being an argument. */
4976 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004977 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004978 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004979 /* Clear the local copy. */
4980 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004981 }
4982 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004983 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004984 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004985 if (c == NULL)
4986 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004987 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004989
4990 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004991 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004992 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05004993 Py_INCREF(o);
4994 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004996
Mark Shannon0332e562021-02-01 10:42:03 +00004997 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00004998
Thomas Woutersce272b62007-09-19 21:19:28 +00004999fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005001 /* decref'ing the frame can cause __del__ methods to get invoked,
5002 which can call back into Python. While we're done with the
5003 current Python frame (f), the associated C stack is still in use,
5004 so recursion_depth must be boosted for the duration.
5005 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005006 if (Py_REFCNT(f) > 1) {
5007 Py_DECREF(f);
5008 _PyObject_GC_TRACK(f);
5009 }
5010 else {
5011 ++tstate->recursion_depth;
5012 Py_DECREF(f);
5013 --tstate->recursion_depth;
5014 }
Mark Shannon0332e562021-02-01 10:42:03 +00005015 return NULL;
5016}
5017
5018static PyObject *
5019make_coro(PyFrameConstructor *con, PyFrameObject *f)
5020{
5021 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5022 PyObject *gen;
5023 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5024
5025 /* Don't need to keep the reference to f_back, it will be set
5026 * when the generator is resumed. */
5027 Py_CLEAR(f->f_back);
5028
5029 /* Create a new generator that owns the ready to run frame
5030 * and return that as the value. */
5031 if (is_coro) {
5032 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5033 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5034 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5035 } else {
5036 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5037 }
5038 if (gen == NULL) {
5039 return NULL;
5040 }
5041
5042 _PyObject_GC_TRACK(f);
5043
5044 return gen;
5045}
5046
5047PyObject *
5048_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5049 PyObject *locals,
5050 PyObject* const* args, size_t argcount,
5051 PyObject *kwnames)
5052{
5053 PyFrameObject *f = _PyEval_MakeFrameVector(
5054 tstate, con, locals, args, argcount, kwnames);
5055 if (f == NULL) {
5056 return NULL;
5057 }
5058 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5059 return make_coro(con, f);
5060 }
5061 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5062
5063 /* decref'ing the frame can cause __del__ methods to get invoked,
5064 which can call back into Python. While we're done with the
5065 current Python frame (f), the associated C stack is still in use,
5066 so recursion_depth must be boosted for the duration.
5067 */
5068 if (Py_REFCNT(f) > 1) {
5069 Py_DECREF(f);
5070 _PyObject_GC_TRACK(f);
5071 }
5072 else {
5073 ++tstate->recursion_depth;
5074 Py_DECREF(f);
5075 --tstate->recursion_depth;
5076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005078}
5079
Mark Shannond6c33fb2021-01-29 13:24:55 +00005080/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005081PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005082PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5083 PyObject *const *args, int argcount,
5084 PyObject *const *kws, int kwcount,
5085 PyObject *const *defs, int defcount,
5086 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005087{
Victor Stinner46496f92021-02-20 15:17:18 +01005088 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005089 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005090 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5091 if (defaults == NULL) {
5092 return NULL;
5093 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005094 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005095 if (builtins == NULL) {
5096 Py_DECREF(defaults);
5097 return NULL;
5098 }
Mark Shannon0332e562021-02-01 10:42:03 +00005099 if (locals == NULL) {
5100 locals = globals;
5101 }
5102 PyObject *kwnames;
5103 PyObject *const *allargs;
5104 PyObject **newargs;
5105 if (kwcount == 0) {
5106 allargs = args;
5107 kwnames = NULL;
5108 }
5109 else {
5110 kwnames = PyTuple_New(kwcount);
5111 if (kwnames == NULL) {
5112 res = NULL;
5113 goto fail;
5114 }
5115 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5116 if (newargs == NULL) {
5117 res = NULL;
5118 Py_DECREF(kwnames);
5119 goto fail;
5120 }
5121 for (int i = 0; i < argcount; i++) {
5122 newargs[i] = args[i];
5123 }
5124 for (int i = 0; i < kwcount; i++) {
5125 Py_INCREF(kws[2*i]);
5126 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5127 newargs[argcount+i] = kws[2*i+1];
5128 }
5129 allargs = newargs;
5130 }
5131 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5132 if (kwargs == NULL) {
5133 res = NULL;
5134 Py_DECREF(kwnames);
5135 goto fail;
5136 }
5137 for (int i = 0; i < kwcount; i++) {
5138 Py_INCREF(kws[2*i]);
5139 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5140 kwargs[i] = kws[2*i+1];
5141 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005142 PyFrameConstructor constr = {
5143 .fc_globals = globals,
5144 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005145 .fc_name = ((PyCodeObject *)_co)->co_name,
5146 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005147 .fc_code = _co,
5148 .fc_defaults = defaults,
5149 .fc_kwdefaults = kwdefs,
5150 .fc_closure = closure
5151 };
Mark Shannon0332e562021-02-01 10:42:03 +00005152 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005153 allargs, argcount,
5154 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005155 if (kwcount) {
5156 Py_DECREF(kwnames);
5157 PyMem_Free(newargs);
5158 }
5159fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005160 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005161 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005162}
5163
Tim Peters5ca576e2001-06-18 22:08:13 +00005164
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005165static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005166special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005169 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005170 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005171 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 return NULL;
5173 }
5174 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005175}
5176
5177
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005178/* Logic for the raise statement (too complicated for inlining).
5179 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005180static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005181do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 if (exc == NULL) {
5186 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005187 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005189 type = exc_info->exc_type;
5190 value = exc_info->exc_value;
5191 tb = exc_info->exc_traceback;
Victor Stinner09bbebe2021-04-11 00:17:39 +02005192 if (Py_IsNone(type) || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005193 _PyErr_SetString(tstate, PyExc_RuntimeError,
5194 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005195 return 0;
5196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 Py_XINCREF(type);
5198 Py_XINCREF(value);
5199 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005200 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005201 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 /* We support the following forms of raise:
5205 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005206 raise <instance>
5207 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 if (PyExceptionClass_Check(exc)) {
5210 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005211 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 if (value == NULL)
5213 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005214 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005215 _PyErr_Format(tstate, PyExc_TypeError,
5216 "calling %R should have returned an instance of "
5217 "BaseException, not %R",
5218 type, Py_TYPE(value));
5219 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005220 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 }
5222 else if (PyExceptionInstance_Check(exc)) {
5223 value = exc;
5224 type = PyExceptionInstance_Class(exc);
5225 Py_INCREF(type);
5226 }
5227 else {
5228 /* Not something you can raise. You get an exception
5229 anyway, just not what you specified :-) */
5230 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005231 _PyErr_SetString(tstate, PyExc_TypeError,
5232 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 goto raise_error;
5234 }
Collin Winter828f04a2007-08-31 00:04:24 +00005235
Serhiy Storchakac0191582016-09-27 11:37:10 +03005236 assert(type != NULL);
5237 assert(value != NULL);
5238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 if (cause) {
5240 PyObject *fixed_cause;
5241 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005242 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 if (fixed_cause == NULL)
5244 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005245 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005247 else if (PyExceptionInstance_Check(cause)) {
5248 fixed_cause = cause;
5249 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02005250 else if (Py_IsNone(cause)) {
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005251 Py_DECREF(cause);
5252 fixed_cause = NULL;
5253 }
5254 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005255 _PyErr_SetString(tstate, PyExc_TypeError,
5256 "exception causes must derive from "
5257 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 goto raise_error;
5259 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005260 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 }
Collin Winter828f04a2007-08-31 00:04:24 +00005262
Victor Stinner438a12d2019-05-24 17:01:38 +02005263 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005264 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005265 Py_DECREF(value);
5266 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005267 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005268
5269raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 Py_XDECREF(value);
5271 Py_XDECREF(type);
5272 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005273 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005274}
5275
Tim Petersd6d010b2001-06-21 02:49:55 +00005276/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005277 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005278
Guido van Rossum0368b722007-05-11 16:50:42 +00005279 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5280 with a variable target.
5281*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005282
Barry Warsawe42b18f1997-08-25 22:13:04 +00005283static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005284unpack_iterable(PyThreadState *tstate, PyObject *v,
5285 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 int i = 0, j = 0;
5288 Py_ssize_t ll = 0;
5289 PyObject *it; /* iter(v) */
5290 PyObject *w;
5291 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005296 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005298 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005299 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005300 _PyErr_Format(tstate, PyExc_TypeError,
5301 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005302 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005303 }
5304 return 0;
5305 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 for (; i < argcnt; i++) {
5308 w = PyIter_Next(it);
5309 if (w == NULL) {
5310 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005311 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005312 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005313 _PyErr_Format(tstate, PyExc_ValueError,
5314 "not enough values to unpack "
5315 "(expected %d, got %d)",
5316 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005317 }
5318 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005319 _PyErr_Format(tstate, PyExc_ValueError,
5320 "not enough values to unpack "
5321 "(expected at least %d, got %d)",
5322 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 }
5325 goto Error;
5326 }
5327 *--sp = w;
5328 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 if (argcntafter == -1) {
5331 /* We better have exhausted the iterator now. */
5332 w = PyIter_Next(it);
5333 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005334 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005335 goto Error;
5336 Py_DECREF(it);
5337 return 1;
5338 }
5339 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005340 _PyErr_Format(tstate, PyExc_ValueError,
5341 "too many values to unpack (expected %d)",
5342 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 goto Error;
5344 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 l = PySequence_List(it);
5347 if (l == NULL)
5348 goto Error;
5349 *--sp = l;
5350 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005352 ll = PyList_GET_SIZE(l);
5353 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005355 "not enough values to unpack (expected at least %d, got %zd)",
5356 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 goto Error;
5358 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 /* Pop the "after-variable" args off the list. */
5361 for (j = argcntafter; j > 0; j--, i++) {
5362 *--sp = PyList_GET_ITEM(l, ll - j);
5363 }
5364 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005365 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 Py_DECREF(it);
5367 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005368
Tim Petersd6d010b2001-06-21 02:49:55 +00005369Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 for (; i > 0; i--, sp++)
5371 Py_DECREF(*sp);
5372 Py_XDECREF(it);
5373 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005374}
5375
Guido van Rossum96a42c81992-01-12 02:29:51 +00005376#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005377static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005378prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005381 if (PyObject_Print(v, stdout, 0) != 0) {
5382 /* Don't know what else to do */
5383 _PyErr_Clear(tstate);
5384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 printf("\n");
5386 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005387}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005388#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005389
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005390static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005391call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005392 PyThreadState *tstate,
5393 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005394 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005395{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005396 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005398 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 if (value == NULL) {
5400 value = Py_None;
5401 Py_INCREF(value);
5402 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005403 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005404 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 arg = PyTuple_Pack(3, type, value, traceback);
5406 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005407 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 return;
5409 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005410 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005412 if (err == 0) {
5413 _PyErr_Restore(tstate, type, value, orig_traceback);
5414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 else {
5416 Py_XDECREF(type);
5417 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005418 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005420}
5421
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005422static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005423call_trace_protected(Py_tracefunc func, PyObject *obj,
5424 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005425 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 PyObject *type, *value, *traceback;
5429 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005430 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005431 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 if (err == 0)
5433 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005434 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 return 0;
5436 }
5437 else {
5438 Py_XDECREF(type);
5439 Py_XDECREF(value);
5440 Py_XDECREF(traceback);
5441 return -1;
5442 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005443}
5444
Mark Shannon8e1b4062021-03-05 14:45:50 +00005445static void
5446initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5447{
5448 if (trace_info->code != frame->f_code) {
5449 trace_info->code = frame->f_code;
5450 trace_info->instr_prev = -1;
5451 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5452 }
5453}
5454
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005455static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005456call_trace(Py_tracefunc func, PyObject *obj,
5457 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005458 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005459 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 int result;
5462 if (tstate->tracing)
5463 return 0;
5464 tstate->tracing++;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005465 tstate->cframe->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005466 if (frame->f_lasti < 0) {
5467 frame->f_lineno = frame->f_code->co_firstlineno;
5468 }
5469 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005470 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005471 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005472 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005474 frame->f_lineno = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005475 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 || (tstate->c_profilefunc != NULL));
5477 tstate->tracing--;
5478 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005479}
5480
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005481PyObject *
5482_PyEval_CallTracing(PyObject *func, PyObject *args)
5483{
Victor Stinner50b48572018-11-01 01:51:40 +01005484 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 int save_tracing = tstate->tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005486 int save_use_tracing = tstate->cframe->use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005489 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005490 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 || (tstate->c_profilefunc != NULL));
5492 result = PyObject_Call(func, args, NULL);
5493 tstate->tracing = save_tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005494 tstate->cframe->use_tracing = save_use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005495 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005496}
5497
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005498/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005499static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005500maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005501 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005502 PyTraceInfo *trace_info)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005505
Nick Coghlan5a851672017-09-08 10:14:16 +10005506 /* If the last instruction falls at the start of a line or if it
5507 represents a jump backwards, update the frame's line number and
5508 then call the trace function if we're tracing source lines.
5509 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005510 initialize_trace_info(trace_info, frame);
5511 int lastline = trace_info->bounds.ar_line;
Mark Shannonfcb55c02021-04-01 16:00:31 +01005512 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005513 if (line != -1 && frame->f_trace_lines) {
5514 /* Trace backward edges or first instruction of a new line */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005515 if (frame->f_lasti < trace_info->instr_prev ||
Mark Shannonfcb55c02021-04-01 16:00:31 +01005516 (line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
Mark Shannonee9f98d2021-01-05 12:04:10 +00005517 {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005518 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 }
George King20faa682017-10-18 17:44:22 -07005521 /* Always emit an opcode event if we're tracing all opcodes. */
5522 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005523 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005524 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005525 trace_info->instr_prev = frame->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005527}
5528
Victor Stinner309d7cc2020-03-13 16:39:12 +01005529int
5530_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5531{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005532 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005533 /* The caller must hold the GIL */
5534 assert(PyGILState_Check());
5535
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005536 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005537 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005538 PyThreadState *current_tstate = _PyThreadState_GET();
5539 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005540 return -1;
5541 }
5542
5543 PyObject *profileobj = tstate->c_profileobj;
5544
5545 tstate->c_profilefunc = NULL;
5546 tstate->c_profileobj = NULL;
5547 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005548 tstate->cframe->use_tracing = tstate->c_tracefunc != NULL;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005549 Py_XDECREF(profileobj);
5550
5551 Py_XINCREF(arg);
5552 tstate->c_profileobj = arg;
5553 tstate->c_profilefunc = func;
5554
5555 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005556 tstate->cframe->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005557 return 0;
5558}
5559
Fred Drake5755ce62001-06-27 19:19:46 +00005560void
5561PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005562{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005563 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005564 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005565 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005566 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5567 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005568}
5569
5570int
5571_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5572{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005573 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005574 /* The caller must hold the GIL */
5575 assert(PyGILState_Check());
5576
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005577 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005578 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005579 PyThreadState *current_tstate = _PyThreadState_GET();
5580 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005581 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005582 }
5583
Victor Stinner309d7cc2020-03-13 16:39:12 +01005584 PyObject *traceobj = tstate->c_traceobj;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005585
5586 tstate->c_tracefunc = NULL;
5587 tstate->c_traceobj = NULL;
5588 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005589 tstate->cframe->use_tracing = (tstate->c_profilefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005590 Py_XDECREF(traceobj);
5591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005592 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005593 tstate->c_traceobj = arg;
5594 tstate->c_tracefunc = func;
5595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005596 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005597 tstate->cframe->use_tracing = ((func != NULL)
Victor Stinner309d7cc2020-03-13 16:39:12 +01005598 || (tstate->c_profilefunc != NULL));
5599
5600 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005601}
5602
5603void
5604PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5605{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005606 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005607 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005608 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005609 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5610 }
Fred Draked0838392001-06-16 21:02:31 +00005611}
5612
Victor Stinner309d7cc2020-03-13 16:39:12 +01005613
Yury Selivanov75445082015-05-11 22:57:16 -04005614void
Victor Stinner838f2642019-06-13 22:41:23 +02005615_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005616{
5617 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005618 tstate->coroutine_origin_tracking_depth = new_depth;
5619}
5620
5621int
5622_PyEval_GetCoroutineOriginTrackingDepth(void)
5623{
Victor Stinner50b48572018-11-01 01:51:40 +01005624 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005625 return tstate->coroutine_origin_tracking_depth;
5626}
5627
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005628int
Yury Selivanoveb636452016-09-08 22:01:51 -07005629_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5630{
Victor Stinner50b48572018-11-01 01:51:40 +01005631 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005632
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005633 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005634 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005635 }
5636
Yury Selivanoveb636452016-09-08 22:01:51 -07005637 Py_XINCREF(firstiter);
5638 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005639 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005640}
5641
5642PyObject *
5643_PyEval_GetAsyncGenFirstiter(void)
5644{
Victor Stinner50b48572018-11-01 01:51:40 +01005645 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005646 return tstate->async_gen_firstiter;
5647}
5648
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005649int
Yury Selivanoveb636452016-09-08 22:01:51 -07005650_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5651{
Victor Stinner50b48572018-11-01 01:51:40 +01005652 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005653
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005654 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005655 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005656 }
5657
Yury Selivanoveb636452016-09-08 22:01:51 -07005658 Py_XINCREF(finalizer);
5659 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005660 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005661}
5662
5663PyObject *
5664_PyEval_GetAsyncGenFinalizer(void)
5665{
Victor Stinner50b48572018-11-01 01:51:40 +01005666 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005667 return tstate->async_gen_finalizer;
5668}
5669
Victor Stinner438a12d2019-05-24 17:01:38 +02005670PyFrameObject *
5671PyEval_GetFrame(void)
5672{
5673 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005674 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005675}
5676
Guido van Rossumb209a111997-04-29 18:18:01 +00005677PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005678_PyEval_GetBuiltins(PyThreadState *tstate)
5679{
5680 PyFrameObject *frame = tstate->frame;
5681 if (frame != NULL) {
5682 return frame->f_builtins;
5683 }
5684 return tstate->interp->builtins;
5685}
5686
5687PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005688PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005689{
Victor Stinner438a12d2019-05-24 17:01:38 +02005690 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005691 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005692}
5693
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005694/* Convenience function to get a builtin from its name */
5695PyObject *
5696_PyEval_GetBuiltinId(_Py_Identifier *name)
5697{
Victor Stinner438a12d2019-05-24 17:01:38 +02005698 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005699 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5700 if (attr) {
5701 Py_INCREF(attr);
5702 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005703 else if (!_PyErr_Occurred(tstate)) {
5704 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005705 }
5706 return attr;
5707}
5708
Guido van Rossumb209a111997-04-29 18:18:01 +00005709PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005710PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005711{
Victor Stinner438a12d2019-05-24 17:01:38 +02005712 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005713 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005714 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005715 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005716 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005717 }
5718
Victor Stinner438a12d2019-05-24 17:01:38 +02005719 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005720 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005721 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005722
5723 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005724 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005725}
5726
Guido van Rossumb209a111997-04-29 18:18:01 +00005727PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005728PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005729{
Victor Stinner438a12d2019-05-24 17:01:38 +02005730 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005731 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005732 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005733 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005734 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005735
5736 assert(current_frame->f_globals != NULL);
5737 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005738}
5739
Guido van Rossum6135a871995-01-09 17:53:26 +00005740int
Tim Peters5ba58662001-07-16 02:29:45 +00005741PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005742{
Victor Stinner438a12d2019-05-24 17:01:38 +02005743 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005744 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005745 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005747 if (current_frame != NULL) {
5748 const int codeflags = current_frame->f_code->co_flags;
5749 const int compilerflags = codeflags & PyCF_MASK;
5750 if (compilerflags) {
5751 result = 1;
5752 cf->cf_flags |= compilerflags;
5753 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005754#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005755 if (codeflags & CO_GENERATOR_ALLOWED) {
5756 result = 1;
5757 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5758 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005759#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 }
5761 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005762}
5763
Guido van Rossum3f5da241990-12-20 15:06:42 +00005764
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005765const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005766PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005768 if (PyMethod_Check(func))
5769 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5770 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005771 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005772 else if (PyCFunction_Check(func))
5773 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5774 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005775 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005776}
5777
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005778const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005779PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005781 if (PyMethod_Check(func))
5782 return "()";
5783 else if (PyFunction_Check(func))
5784 return "()";
5785 else if (PyCFunction_Check(func))
5786 return "()";
5787 else
5788 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005789}
5790
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005791#define C_TRACE(x, call) \
Mark Shannon9e7b2072021-04-13 11:08:14 +01005792if (trace_info->cframe.use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005793 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005794 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005795 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005796 x = NULL; \
5797 } \
5798 else { \
5799 x = call; \
5800 if (tstate->c_profilefunc != NULL) { \
5801 if (x == NULL) { \
5802 call_trace_protected(tstate->c_profilefunc, \
5803 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005804 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005805 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 /* XXX should pass (type, value, tb) */ \
5807 } else { \
5808 if (call_trace(tstate->c_profilefunc, \
5809 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005810 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005811 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005812 Py_DECREF(x); \
5813 x = NULL; \
5814 } \
5815 } \
5816 } \
5817 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005818} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005819 x = call; \
5820 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005821
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005822
5823static PyObject *
5824trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005825 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005826 PyObject *func,
5827 PyObject **args, Py_ssize_t nargs,
5828 PyObject *kwnames)
5829{
5830 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005831 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005832 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005833 return x;
5834 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005835 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005836 /* We need to create a temporary bound method as argument
5837 for profiling.
5838
5839 If nargs == 0, then this cannot work because we have no
5840 "self". In any case, the call itself would raise
5841 TypeError (foo needs an argument), so we just skip
5842 profiling. */
5843 PyObject *self = args[0];
5844 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5845 if (func == NULL) {
5846 return NULL;
5847 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005848 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005849 args+1, nargs-1,
5850 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005851 Py_DECREF(func);
5852 return x;
5853 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005854 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005855}
5856
Victor Stinner415c5102017-01-11 00:54:57 +01005857/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5858 to reduce the stack consumption. */
5859Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005860call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005861 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005862 PyObject ***pp_stack,
5863 Py_ssize_t oparg,
5864 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005865{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005866 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005867 PyObject *func = *pfunc;
5868 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005869 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5870 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005871 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005872
Mark Shannon9e7b2072021-04-13 11:08:14 +01005873 if (trace_info->cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005874 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005875 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005876 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005877 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005878 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005879
Victor Stinner438a12d2019-05-24 17:01:38 +02005880 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005881
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005882 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005883 while ((*pp_stack) > pfunc) {
5884 w = EXT_POP(*pp_stack);
5885 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005886 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005888 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005889}
5890
Jeremy Hylton52820442001-01-03 23:52:36 +00005891static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005892do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005893 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005894 PyObject *func,
5895 PyObject *callargs,
5896 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005897{
jdemeyere89de732018-09-19 12:06:20 +02005898 PyObject *result;
5899
scoder4c9ea092020-05-12 16:12:41 +02005900 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005901 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005902 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005903 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005904 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005905 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
Mark Shannon9e7b2072021-04-13 11:08:14 +01005906 if (nargs > 0 && trace_info->cframe.use_tracing) {
jdemeyere89de732018-09-19 12:06:20 +02005907 /* We need to create a temporary bound method as argument
5908 for profiling.
5909
5910 If nargs == 0, then this cannot work because we have no
5911 "self". In any case, the call itself would raise
5912 TypeError (foo needs an argument), so we just skip
5913 profiling. */
5914 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5915 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5916 if (func == NULL) {
5917 return NULL;
5918 }
5919
Victor Stinner4d231bc2019-11-14 13:36:21 +01005920 C_TRACE(result, _PyObject_FastCallDictTstate(
5921 tstate, func,
5922 &_PyTuple_ITEMS(callargs)[1],
5923 nargs - 1,
5924 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005925 Py_DECREF(func);
5926 return result;
5927 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005928 }
jdemeyere89de732018-09-19 12:06:20 +02005929 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005930}
5931
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005932/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005933 nb_index slot defined, and store in *pi.
5934 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005935 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005936 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005937*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005938int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005939_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005940{
Victor Stinner438a12d2019-05-24 17:01:38 +02005941 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner09bbebe2021-04-11 00:17:39 +02005942 if (!Py_IsNone(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005943 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005944 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005945 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005946 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005947 return 0;
5948 }
5949 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005950 _PyErr_SetString(tstate, PyExc_TypeError,
5951 "slice indices must be integers or "
5952 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005953 return 0;
5954 }
5955 *pi = x;
5956 }
5957 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005958}
5959
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005960int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005961_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005962{
Victor Stinner438a12d2019-05-24 17:01:38 +02005963 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005964 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005965 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005966 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005967 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005968 return 0;
5969 }
5970 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005971 _PyErr_SetString(tstate, PyExc_TypeError,
5972 "slice indices must be integers or "
5973 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005974 return 0;
5975 }
5976 *pi = x;
5977 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005978}
5979
Thomas Wouters52152252000-08-17 22:55:00 +00005980static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005981import_name(PyThreadState *tstate, PyFrameObject *f,
5982 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005983{
5984 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005985 PyObject *import_func, *res;
5986 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005987
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005988 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005989 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005990 if (!_PyErr_Occurred(tstate)) {
5991 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005992 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005993 return NULL;
5994 }
5995
5996 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005997 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005998 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005999 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006000 return NULL;
6001 }
6002 res = PyImport_ImportModuleLevelObject(
6003 name,
6004 f->f_globals,
6005 f->f_locals == NULL ? Py_None : f->f_locals,
6006 fromlist,
6007 ilevel);
6008 return res;
6009 }
6010
6011 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006012
6013 stack[0] = name;
6014 stack[1] = f->f_globals;
6015 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6016 stack[3] = fromlist;
6017 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006018 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006019 Py_DECREF(import_func);
6020 return res;
6021}
6022
6023static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006024import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006026 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006027 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006028
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006029 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006030 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006031 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006032 /* Issue #17636: in case this failed because of a circular relative
6033 import, try to fallback on reading the module directly from
6034 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006035 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006036 if (pkgname == NULL) {
6037 goto error;
6038 }
Oren Milman6db70332017-09-19 14:23:01 +03006039 if (!PyUnicode_Check(pkgname)) {
6040 Py_CLEAR(pkgname);
6041 goto error;
6042 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006043 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006044 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006045 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006046 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006047 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006048 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006049 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006050 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006051 goto error;
6052 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006053 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006054 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006055 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006056 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006057 if (pkgname == NULL) {
6058 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6059 if (pkgname_or_unknown == NULL) {
6060 Py_XDECREF(pkgpath);
6061 return NULL;
6062 }
6063 } else {
6064 pkgname_or_unknown = pkgname;
6065 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006066
6067 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006068 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006069 errmsg = PyUnicode_FromFormat(
6070 "cannot import name %R from %R (unknown location)",
6071 name, pkgname_or_unknown
6072 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006073 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006074 PyErr_SetImportError(errmsg, pkgname, NULL);
6075 }
6076 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006077 _Py_IDENTIFIER(__spec__);
6078 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006079 const char *fmt =
6080 _PyModuleSpec_IsInitializing(spec) ?
6081 "cannot import name %R from partially initialized module %R "
6082 "(most likely due to a circular import) (%S)" :
6083 "cannot import name %R from %R (%S)";
6084 Py_XDECREF(spec);
6085
6086 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006087 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006088 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006089 }
6090
Xiang Zhang4830f582017-03-21 11:13:42 +08006091 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006092 Py_XDECREF(pkgname_or_unknown);
6093 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006094 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006095}
Guido van Rossumac7be682001-01-17 15:42:30 +00006096
Thomas Wouters52152252000-08-17 22:55:00 +00006097static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006098import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006099{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006100 _Py_IDENTIFIER(__all__);
6101 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006102 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006103 int skip_leading_underscores = 0;
6104 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006105
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006106 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6107 return -1; /* Unexpected error */
6108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006109 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006110 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6111 return -1;
6112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006113 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006114 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006115 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006116 return -1;
6117 }
6118 all = PyMapping_Keys(dict);
6119 Py_DECREF(dict);
6120 if (all == NULL)
6121 return -1;
6122 skip_leading_underscores = 1;
6123 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006125 for (pos = 0, err = 0; ; pos++) {
6126 name = PySequence_GetItem(all, pos);
6127 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006128 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006129 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006130 }
6131 else {
6132 _PyErr_Clear(tstate);
6133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006134 break;
6135 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006136 if (!PyUnicode_Check(name)) {
6137 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6138 if (modname == NULL) {
6139 Py_DECREF(name);
6140 err = -1;
6141 break;
6142 }
6143 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006144 _PyErr_Format(tstate, PyExc_TypeError,
6145 "module __name__ must be a string, not %.100s",
6146 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006147 }
6148 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006149 _PyErr_Format(tstate, PyExc_TypeError,
6150 "%s in %U.%s must be str, not %.100s",
6151 skip_leading_underscores ? "Key" : "Item",
6152 modname,
6153 skip_leading_underscores ? "__dict__" : "__all__",
6154 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006155 }
6156 Py_DECREF(modname);
6157 Py_DECREF(name);
6158 err = -1;
6159 break;
6160 }
6161 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006162 if (PyUnicode_READY(name) == -1) {
6163 Py_DECREF(name);
6164 err = -1;
6165 break;
6166 }
6167 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6168 Py_DECREF(name);
6169 continue;
6170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006171 }
6172 value = PyObject_GetAttr(v, name);
6173 if (value == NULL)
6174 err = -1;
6175 else if (PyDict_CheckExact(locals))
6176 err = PyDict_SetItem(locals, name, value);
6177 else
6178 err = PyObject_SetItem(locals, name, value);
6179 Py_DECREF(name);
6180 Py_XDECREF(value);
6181 if (err != 0)
6182 break;
6183 }
6184 Py_DECREF(all);
6185 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006186}
6187
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006188static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006189check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006190{
Victor Stinnera102ed72020-02-07 02:24:48 +01006191 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006192 /* check_args_iterable() may be called with a live exception:
6193 * clear it to prevent calling _PyObject_FunctionStr() with an
6194 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006195 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006196 PyObject *funcstr = _PyObject_FunctionStr(func);
6197 if (funcstr != NULL) {
6198 _PyErr_Format(tstate, PyExc_TypeError,
6199 "%U argument after * must be an iterable, not %.200s",
6200 funcstr, Py_TYPE(args)->tp_name);
6201 Py_DECREF(funcstr);
6202 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006203 return -1;
6204 }
6205 return 0;
6206}
6207
6208static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006209format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006210{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006211 /* _PyDict_MergeEx raises attribute
6212 * error (percolated from an attempt
6213 * to get 'keys' attribute) instead of
6214 * a type error if its second argument
6215 * is not a mapping.
6216 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006217 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006218 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006219 PyObject *funcstr = _PyObject_FunctionStr(func);
6220 if (funcstr != NULL) {
6221 _PyErr_Format(
6222 tstate, PyExc_TypeError,
6223 "%U argument after ** must be a mapping, not %.200s",
6224 funcstr, Py_TYPE(kwargs)->tp_name);
6225 Py_DECREF(funcstr);
6226 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006227 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006228 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006229 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006230 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006231 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006232 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006233 PyObject *funcstr = _PyObject_FunctionStr(func);
6234 if (funcstr != NULL) {
6235 PyObject *key = PyTuple_GET_ITEM(val, 0);
6236 _PyErr_Format(
6237 tstate, PyExc_TypeError,
6238 "%U got multiple values for keyword argument '%S'",
6239 funcstr, key);
6240 Py_DECREF(funcstr);
6241 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006242 Py_XDECREF(exc);
6243 Py_XDECREF(val);
6244 Py_XDECREF(tb);
6245 }
6246 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006247 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006248 }
6249 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006250}
6251
Guido van Rossumac7be682001-01-17 15:42:30 +00006252static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006253format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6254 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006256 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258 if (!obj)
6259 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006260
Serhiy Storchaka06515832016-11-20 09:13:07 +02006261 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006262 if (!obj_str)
6263 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006264
Victor Stinner438a12d2019-05-24 17:01:38 +02006265 _PyErr_Format(tstate, exc, format_str, obj_str);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +01006266
6267 if (exc == PyExc_NameError) {
6268 // Include the name in the NameError exceptions to offer suggestions later.
6269 _Py_IDENTIFIER(name);
6270 PyObject *type, *value, *traceback;
6271 PyErr_Fetch(&type, &value, &traceback);
6272 PyErr_NormalizeException(&type, &value, &traceback);
6273 if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
6274 // We do not care if this fails because we are going to restore the
6275 // NameError anyway.
6276 (void)_PyObject_SetAttrId(value, &PyId_name, obj);
6277 }
6278 PyErr_Restore(type, value, traceback);
6279 }
Paul Prescode68140d2000-08-30 20:25:01 +00006280}
Guido van Rossum950361c1997-01-24 13:49:28 +00006281
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006282static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006283format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006284{
6285 PyObject *name;
6286 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006287 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006288 return;
6289 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6290 name = PyTuple_GET_ITEM(co->co_cellvars,
6291 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006292 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006293 PyExc_UnboundLocalError,
6294 UNBOUNDLOCAL_ERROR_MSG,
6295 name);
6296 } else {
6297 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6298 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006299 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006300 UNBOUNDFREE_ERROR_MSG, name);
6301 }
6302}
6303
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006304static void
Mark Shannonfee55262019-11-21 09:11:43 +00006305format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006306{
6307 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6308 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006309 _PyErr_Format(tstate, PyExc_TypeError,
6310 "'async with' received an object from __aenter__ "
6311 "that does not implement __await__: %.100s",
6312 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006313 }
Mark Shannonfee55262019-11-21 09:11:43 +00006314 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006315 _PyErr_Format(tstate, PyExc_TypeError,
6316 "'async with' received an object from __aexit__ "
6317 "that does not implement __await__: %.100s",
6318 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006319 }
6320 }
6321}
6322
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006323static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006324unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006325 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006326{
6327 PyObject *res;
6328 if (Py_REFCNT(v) == 2) {
6329 /* In the common case, there are 2 references to the value
6330 * stored in 'variable' when the += is performed: one on the
6331 * value stack (in 'v') and one still stored in the
6332 * 'variable'. We try to delete the variable now to reduce
6333 * the refcnt to 1.
6334 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006335 int opcode, oparg;
6336 NEXTOPARG();
6337 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006338 case STORE_FAST:
6339 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006340 PyObject **fastlocals = f->f_localsplus;
6341 if (GETLOCAL(oparg) == v)
6342 SETLOCAL(oparg, NULL);
6343 break;
6344 }
6345 case STORE_DEREF:
6346 {
6347 PyObject **freevars = (f->f_localsplus +
6348 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006349 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006350 if (PyCell_GET(c) == v) {
6351 PyCell_SET(c, NULL);
6352 Py_DECREF(v);
6353 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006354 break;
6355 }
6356 case STORE_NAME:
6357 {
6358 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006359 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006360 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006361 if (locals && PyDict_CheckExact(locals)) {
6362 PyObject *w = PyDict_GetItemWithError(locals, name);
6363 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006364 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006365 {
6366 Py_DECREF(v);
6367 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006368 }
6369 }
6370 break;
6371 }
6372 }
6373 }
6374 res = v;
6375 PyUnicode_Append(&res, w);
6376 return res;
6377}
6378
Guido van Rossum950361c1997-01-24 13:49:28 +00006379#ifdef DYNAMIC_EXECUTION_PROFILE
6380
Skip Montanarof118cb12001-10-15 20:51:38 +00006381static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006382getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006384 int i;
6385 PyObject *l = PyList_New(256);
6386 if (l == NULL) return NULL;
6387 for (i = 0; i < 256; i++) {
6388 PyObject *x = PyLong_FromLong(a[i]);
6389 if (x == NULL) {
6390 Py_DECREF(l);
6391 return NULL;
6392 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006393 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006394 }
6395 for (i = 0; i < 256; i++)
6396 a[i] = 0;
6397 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006398}
6399
6400PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006401_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006402{
6403#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006404 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006405#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 int i;
6407 PyObject *l = PyList_New(257);
6408 if (l == NULL) return NULL;
6409 for (i = 0; i < 257; i++) {
6410 PyObject *x = getarray(dxpairs[i]);
6411 if (x == NULL) {
6412 Py_DECREF(l);
6413 return NULL;
6414 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006415 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006416 }
6417 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006418#endif
6419}
6420
6421#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006422
6423Py_ssize_t
6424_PyEval_RequestCodeExtraIndex(freefunc free)
6425{
Victor Stinner81a7be32020-04-14 15:14:01 +02006426 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006427 Py_ssize_t new_index;
6428
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006429 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006430 return -1;
6431 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006432 new_index = interp->co_extra_user_count++;
6433 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006434 return new_index;
6435}
Łukasz Langaa785c872016-09-09 17:37:37 -07006436
6437static void
6438dtrace_function_entry(PyFrameObject *f)
6439{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006440 const char *filename;
6441 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006442 int lineno;
6443
Victor Stinner6d86a232020-04-29 00:56:58 +02006444 PyCodeObject *code = f->f_code;
6445 filename = PyUnicode_AsUTF8(code->co_filename);
6446 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006447 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006448
Andy Lestere6be9b52020-02-11 20:28:35 -06006449 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006450}
6451
6452static void
6453dtrace_function_return(PyFrameObject *f)
6454{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006455 const char *filename;
6456 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006457 int lineno;
6458
Victor Stinner6d86a232020-04-29 00:56:58 +02006459 PyCodeObject *code = f->f_code;
6460 filename = PyUnicode_AsUTF8(code->co_filename);
6461 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006462 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006463
Andy Lestere6be9b52020-02-11 20:28:35 -06006464 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006465}
6466
6467/* DTrace equivalent of maybe_call_line_trace. */
6468static void
6469maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00006470 PyTraceInfo *trace_info)
Łukasz Langaa785c872016-09-09 17:37:37 -07006471{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006472 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006473
6474 /* If the last instruction executed isn't in the current
6475 instruction window, reset the window.
6476 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006477 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006478 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006479 /* If the last instruction falls at the start of a line or if
6480 it represents a jump backwards, update the frame's line
6481 number and call the trace function. */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006482 if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006483 if (line != -1) {
6484 frame->f_lineno = line;
6485 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6486 if (!co_filename)
6487 co_filename = "?";
6488 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6489 if (!co_name)
6490 co_name = "?";
6491 PyDTrace_LINE(co_filename, co_name, line);
6492 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006493 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00006494 trace_info->instr_prev = frame->f_lasti;
Łukasz Langaa785c872016-09-09 17:37:37 -07006495}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006496
6497
6498/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6499 for the limited API. */
6500
6501#undef Py_EnterRecursiveCall
6502
6503int Py_EnterRecursiveCall(const char *where)
6504{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006505 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006506}
6507
6508#undef Py_LeaveRecursiveCall
6509
6510void Py_LeaveRecursiveCall(void)
6511{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006512 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006513}