blob: 429ddb82bd66d48771f1f42c8d114711343600d2 [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.
Mark Shannon8e1b4062021-03-05 14:45:50 +000038 PyCodeAddressRange bounds; // Only valid if code != NULL.
Mark Shannon9e7b2072021-04-13 11:08:14 +010039 CFrame cframe;
Mark Shannon8e1b4062021-03-05 14:45:50 +000040} PyTraceInfo;
41
42
Guido van Rossum408027e1996-12-30 16:17:54 +000043#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000044/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045#define LLTRACE 1 /* Low-level trace feature */
46#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000047#endif
48
Victor Stinner5c75f372019-04-17 23:02:26 +020049#if !defined(Py_BUILD_CORE)
50# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
51#endif
52
Hai Shi46874c22020-01-30 17:20:25 -060053_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000054
Guido van Rossum374a9221991-04-04 10:40:29 +000055/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020056Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon8e1b4062021-03-05 14:45:50 +000057 PyThreadState *tstate, PyTraceInfo *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020058 Py_ssize_t oparg, PyObject *kwnames);
59static PyObject * do_call_core(
Mark Shannon8e1b4062021-03-05 14:45:50 +000060 PyThreadState *tstate, PyTraceInfo *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020061 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000062
Guido van Rossum0a066c01992-03-27 17:29:15 +000063#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000064static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020065static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000066#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010067static int call_trace(Py_tracefunc, PyObject *,
68 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000069 PyTraceInfo *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000071static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010072 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000073 PyTraceInfo *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010074 int, PyObject *);
75static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000076 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000077 PyTraceInfo *trace_info);
Tim Peters8a5c3c72004-04-05 19:36:21 +000078static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060079 PyThreadState *, PyFrameObject *,
Mark Shannon9f2c63b2021-07-08 19:21:22 +010080 PyTraceInfo *, int);
81static void maybe_dtrace_line(PyFrameObject *, PyTraceInfo *, int);
Łukasz Langaa785c872016-09-09 17:37:37 -070082static void dtrace_function_entry(PyFrameObject *);
83static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000084
Victor Stinner438a12d2019-05-24 17:01:38 +020085static PyObject * import_name(PyThreadState *, PyFrameObject *,
86 PyObject *, PyObject *, PyObject *);
87static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
88static int import_all_from(PyThreadState *, PyObject *, PyObject *);
89static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
90static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
91static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030092 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020093static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
94static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
95static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000096static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000097
Paul Prescode68140d2000-08-30 20:25:01 +000098#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000100#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000102#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 "free variable '%.200s' referenced before assignment" \
104 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000105
Guido van Rossum950361c1997-01-24 13:49:28 +0000106/* Dynamic execution profile */
107#ifdef DYNAMIC_EXECUTION_PROFILE
108#ifdef DXPAIRS
109static long dxpairs[257][256];
110#define dxp dxpairs[256]
111#else
112static long dxp[256];
113#endif
114#endif
115
Inada Naoki91234a12019-06-03 21:30:58 +0900116/* per opcode cache */
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000117static int opcache_min_runs = 1024; /* create opcache when code executed this many times */
Pablo Galindo109826c2020-10-20 06:22:44 +0100118#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900119#define OPCACHE_STATS 0 /* Enable stats */
120
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000121// This function allows to deactivate the opcode cache. As different cache mechanisms may hold
122// references, this can mess with the reference leak detector functionality so the cache needs
123// to be deactivated in such scenarios to avoid false positives. See bpo-3714 for more information.
124void
125_PyEval_DeactivateOpCache(void)
126{
127 opcache_min_runs = 0;
128}
129
Inada Naoki91234a12019-06-03 21:30:58 +0900130#if OPCACHE_STATS
131static size_t opcache_code_objects = 0;
132static size_t opcache_code_objects_extra_mem = 0;
133
134static size_t opcache_global_opts = 0;
135static size_t opcache_global_hits = 0;
136static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100137
138static size_t opcache_attr_opts = 0;
139static size_t opcache_attr_hits = 0;
140static size_t opcache_attr_misses = 0;
141static size_t opcache_attr_deopts = 0;
142static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900143#endif
144
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100145
Victor Stinnerda2914d2020-03-20 09:29:08 +0100146#ifndef NDEBUG
147/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
148 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
149 when a thread continues to run after Python finalization, especially
150 daemon threads. */
151static int
152is_tstate_valid(PyThreadState *tstate)
153{
154 assert(!_PyMem_IsPtrFreed(tstate));
155 assert(!_PyMem_IsPtrFreed(tstate->interp));
156 return 1;
157}
158#endif
159
160
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000161/* This can set eval_breaker to 0 even though gil_drop_request became
162 1. We believe this is all right because the eval loop will release
163 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100164static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200165COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200166 struct _ceval_runtime_state *ceval,
167 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168{
Victor Stinner299b8c62020-05-05 17:40:18 +0200169 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
170 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200171 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200172 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200173 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100174 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200175 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100176}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Victor Stinnerda2914d2020-03-20 09:29:08 +0100179static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200180SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181{
Victor Stinner299b8c62020-05-05 17:40:18 +0200182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
184 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000186
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000196
Eric Snowfdf282d2019-01-11 14:26:55 -0700197
Victor Stinnerda2914d2020-03-20 09:29:08 +0100198static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200199SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
203 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
204 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100205}
Eric Snowfdf282d2019-01-11 14:26:55 -0700206
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000207
Victor Stinnerda2914d2020-03-20 09:29:08 +0100208static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200209UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100210{
Victor Stinner299b8c62020-05-05 17:40:18 +0200211 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
212 struct _ceval_state *ceval2 = &interp->ceval;
213 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
214 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100215}
216
217
218static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100219SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100220{
Victor Stinner299b8c62020-05-05 17:40:18 +0200221 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
222 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200223 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100224 if (force) {
225 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
226 }
227 else {
228 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
229 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
230 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100231}
232
233
234static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200235UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100236{
Victor Stinner299b8c62020-05-05 17:40:18 +0200237 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
238 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200239 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200240 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100241}
242
243
244static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200245SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100246{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200247 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100248 ceval2->pending.async_exc = 1;
249 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
250}
251
252
253static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200254UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100255{
Victor Stinner299b8c62020-05-05 17:40:18 +0200256 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
257 struct _ceval_state *ceval2 = &interp->ceval;
258 ceval2->pending.async_exc = 0;
259 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100260}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261
262
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000265#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000266#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267
Victor Stinner3026cad2020-06-01 16:02:40 +0200268void _Py_NO_RETURN
269_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100270{
Victor Stinner3026cad2020-06-01 16:02:40 +0200271 _Py_FatalErrorFunc(func,
272 "the function must be called with the GIL held, "
273 "but the GIL is released "
274 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100275}
276
Victor Stinner7be4e352020-05-05 20:27:47 +0200277#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
278int
279_PyEval_ThreadsInitialized(PyInterpreterState *interp)
280{
281 return gil_created(&interp->ceval.gil);
282}
283
284int
285PyEval_ThreadsInitialized(void)
286{
287 // Fatal error if there is no current interpreter
288 PyInterpreterState *interp = PyInterpreterState_Get();
289 return _PyEval_ThreadsInitialized(interp);
290}
291#else
Tim Peters7f468f22004-10-11 02:40:51 +0000292int
Victor Stinner175a7042020-03-10 00:37:48 +0100293_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
294{
295 return gil_created(&runtime->ceval.gil);
296}
297
298int
Tim Peters7f468f22004-10-11 02:40:51 +0000299PyEval_ThreadsInitialized(void)
300{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100301 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100302 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000303}
Victor Stinner7be4e352020-05-05 20:27:47 +0200304#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000305
Victor Stinner111e4ee2020-03-09 21:24:14 +0100306PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200307_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100310 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200311 /* Currently, the GIL is shared by all interpreters,
312 and only the main interpreter is responsible to create
313 and destroy it. */
314 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100315 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200316#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100317
Victor Stinner7be4e352020-05-05 20:27:47 +0200318#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
319 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
320#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200321 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200322#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200323 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100324
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200325 PyThread_init_thread();
326 create_gil(gil);
327
328 take_gil(tstate);
329
330 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100331 return _PyStatus_OK();
332}
333
334void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100335_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200336{
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100338 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200339 /* Currently, the GIL is shared by all interpreters,
340 and only the main interpreter is responsible to create
341 and destroy it. */
342 return;
343 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200344#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200345
Victor Stinner7be4e352020-05-05 20:27:47 +0200346#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100347 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200348#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100349 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200350#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200351 if (!gil_created(gil)) {
352 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
353 yet: do nothing. */
354 return;
355 }
356
357 destroy_gil(gil);
358 assert(!gil_created(gil));
359}
360
361void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100362PyEval_InitThreads(void)
363{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100364 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000365}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000366
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000367void
Inada Naoki91234a12019-06-03 21:30:58 +0900368_PyEval_Fini(void)
369{
370#if OPCACHE_STATS
371 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
372 opcache_code_objects);
373
374 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
375 opcache_code_objects_extra_mem);
376
377 fprintf(stderr, "\n");
378
379 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
380 opcache_global_hits,
381 (int) (100.0 * opcache_global_hits /
382 (opcache_global_hits + opcache_global_misses)));
383
384 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
385 opcache_global_misses,
386 (int) (100.0 * opcache_global_misses /
387 (opcache_global_hits + opcache_global_misses)));
388
389 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
390 opcache_global_opts);
391
392 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100393
394 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
395 opcache_attr_hits,
396 (int) (100.0 * opcache_attr_hits /
397 opcache_attr_total));
398
399 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
400 opcache_attr_misses,
401 (int) (100.0 * opcache_attr_misses /
402 opcache_attr_total));
403
404 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
405 opcache_attr_opts);
406
407 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
408 opcache_attr_deopts);
409
410 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
411 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900412#endif
413}
414
415void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000417{
Victor Stinner09532fe2019-05-10 23:39:09 +0200418 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200419 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200420 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100421
Victor Stinner85f5a692020-03-09 22:12:04 +0100422 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423}
424
425void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427{
Victor Stinner09532fe2019-05-10 23:39:09 +0200428 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200429 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100431 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100432 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200433 struct _ceval_runtime_state *ceval = &runtime->ceval;
434 struct _ceval_state *ceval2 = &tstate->interp->ceval;
435 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000436}
437
438void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100439_PyEval_ReleaseLock(PyThreadState *tstate)
440{
441 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200442 struct _ceval_state *ceval2 = &tstate->interp->ceval;
443 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100444}
445
446void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000447PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000448{
Victor Stinner3026cad2020-06-01 16:02:40 +0200449 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100450
Victor Stinner85f5a692020-03-09 22:12:04 +0100451 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200452
Victor Stinner85f5a692020-03-09 22:12:04 +0100453 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200454#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
455 (void)_PyThreadState_Swap(gilstate, tstate);
456#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100457 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100458 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200459 }
Victor Stinnere838a932020-05-05 19:56:48 +0200460#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000461}
462
463void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000464PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000465{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100466 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200467
Victor Stinner01b1cc12019-11-20 02:27:56 +0100468 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200469 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
470 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100471 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200472 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200473 struct _ceval_runtime_state *ceval = &runtime->ceval;
474 struct _ceval_state *ceval2 = &tstate->interp->ceval;
475 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000476}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000477
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900478#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200479/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200480 which are not running in the child process, and clear internal locks
481 which might be held by those threads. */
482PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200483_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000484{
Victor Stinner317bab02020-06-02 18:44:54 +0200485 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200486
487#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
488 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
489#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100490 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200491#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100492 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200493 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200494 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100495 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100496
497 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700498
Victor Stinner50e6e992020-03-19 02:41:21 +0100499 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900500 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200501 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700502 }
Jesse Nollera8513972008-07-17 16:49:17 +0000503
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200504 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100505 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200506 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000507}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900508#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000509
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000510/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600511 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000512
513void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100514_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000515{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100516 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000517}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000518
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000519PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000520PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000521{
Victor Stinner09532fe2019-05-10 23:39:09 +0200522 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200523#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
524 PyThreadState *old_tstate = _PyThreadState_GET();
525 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
526#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200527 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200528#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200529 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100530
Victor Stinner0b1e3302020-05-05 16:14:31 +0200531 struct _ceval_runtime_state *ceval = &runtime->ceval;
532 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200533#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
534 assert(gil_created(&ceval2->gil));
535#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200536 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200537#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200538 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000540}
541
542void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000544{
Victor Stinner3026cad2020-06-01 16:02:40 +0200545 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100546
Victor Stinner85f5a692020-03-09 22:12:04 +0100547 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100548
Victor Stinner85f5a692020-03-09 22:12:04 +0100549 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
550 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000551}
552
553
Guido van Rossuma9672091994-09-14 13:31:22 +0000554/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
555 signal handlers or Mac I/O completion routines) can schedule calls
556 to a function to be called synchronously.
557 The synchronous function is called with one void* argument.
558 It should return 0 for success or -1 for failure -- failure should
559 be accompanied by an exception.
560
561 If registry succeeds, the registry function returns 0; if it fails
562 (e.g. due to too many pending calls) it returns -1 (without setting
563 an exception condition).
564
565 Note that because registry may occur from within signal handlers,
566 or other asynchronous events, calling malloc() is unsafe!
567
Guido van Rossuma9672091994-09-14 13:31:22 +0000568 Any thread can schedule pending calls, but only the main thread
569 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000570 There is no facility to schedule calls to a particular thread, but
571 that should be easy to change, should that ever be required. In
572 that case, the static variables here should go into the python
573 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000574*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000575
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200576void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200577_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200578{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100579#ifdef MS_WINDOWS
580 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
581 // handler which can run in a thread different than the Python thread, in
582 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
583 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
584 //
585 // The next eval_frame_handle_pending() call will call
586 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
587 int force = 1;
588#else
589 int force = 0;
590#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200591 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100592 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200593 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100594 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200595}
596
Eric Snow5be45a62019-03-08 22:47:07 -0700597/* Push one item onto the queue while holding the lock. */
598static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200599_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600600 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700601{
Eric Snow842a2f02019-03-15 15:47:51 -0600602 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700603 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600604 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700605 return -1; /* Queue full */
606 }
Eric Snow842a2f02019-03-15 15:47:51 -0600607 pending->calls[i].func = func;
608 pending->calls[i].arg = arg;
609 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700610 return 0;
611}
612
613/* Pop one item off the queue while holding the lock. */
614static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200615_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600616 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700617{
Eric Snow842a2f02019-03-15 15:47:51 -0600618 int i = pending->first;
619 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700620 return; /* Queue empty */
621 }
622
Eric Snow842a2f02019-03-15 15:47:51 -0600623 *func = pending->calls[i].func;
624 *arg = pending->calls[i].arg;
625 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700626}
627
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200628/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000629 scheduling to be made from any thread, and even from an executing
630 callback.
631 */
632
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000633int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200634_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200635 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000636{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200637 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600638
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200639 /* Ensure that _PyEval_InitPendingCalls() was called
640 and that _PyEval_FiniPendingCalls() is not called yet. */
641 assert(pending->lock != NULL);
642
Eric Snow842a2f02019-03-15 15:47:51 -0600643 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200644 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600645 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700646
Victor Stinnere225beb2019-06-03 18:14:24 +0200647 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200648 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000650}
651
Victor Stinner09532fe2019-05-10 23:39:09 +0200652int
653Py_AddPendingCall(int (*func)(void *), void *arg)
654{
Victor Stinner50e6e992020-03-19 02:41:21 +0100655 /* Best-effort to support subinterpreters and calls with the GIL released.
656
657 First attempt _PyThreadState_GET() since it supports subinterpreters.
658
659 If the GIL is released, _PyThreadState_GET() returns NULL . In this
660 case, use PyGILState_GetThisThreadState() which works even if the GIL
661 is released.
662
663 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
664 see bpo-10915 and bpo-15751.
665
Victor Stinner8849e592020-03-18 19:28:53 +0100666 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100667 PyThreadState *tstate = _PyThreadState_GET();
668 if (tstate == NULL) {
669 tstate = PyGILState_GetThisThreadState();
670 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200671
672 PyInterpreterState *interp;
673 if (tstate != NULL) {
674 interp = tstate->interp;
675 }
676 else {
677 /* Last resort: use the main interpreter */
678 interp = _PyRuntime.interpreters.main;
679 }
680 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200681}
682
Eric Snowfdf282d2019-01-11 14:26:55 -0700683static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100684handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700685{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200686 assert(is_tstate_valid(tstate));
687 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700688 return 0;
689 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700690
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200691 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100692 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
693 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100694 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700695 return -1;
696 }
697 return 0;
698}
699
700static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100701make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000702{
Victor Stinnerd8316882020-03-20 14:50:35 +0100703 /* only execute pending calls on main thread */
704 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200705 return 0;
706 }
707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100709 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700710 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700712 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200713 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100714
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200715 /* unsignal before starting to call callbacks, so that any callback
716 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100717 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700718 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100721 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700722 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700723 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 void *arg = NULL;
725
726 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600727 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200728 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600729 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700730
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100731 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700732 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100733 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700734 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700735 res = func(arg);
736 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200737 goto error;
738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200740
Charles-François Natalif23339a2011-07-23 18:15:43 +0200741 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700742 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200743
744error:
745 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100746 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700747 return res;
748}
749
Eric Snow842a2f02019-03-15 15:47:51 -0600750void
Victor Stinner2b1df452020-01-13 18:46:59 +0100751_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600752{
Eric Snow842a2f02019-03-15 15:47:51 -0600753 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100754 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600755
Victor Stinner50e6e992020-03-19 02:41:21 +0100756 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200757
Eric Snow842a2f02019-03-15 15:47:51 -0600758 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
759 return;
760 }
761
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100762 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200763 PyObject *exc, *val, *tb;
764 _PyErr_Fetch(tstate, &exc, &val, &tb);
765 PyErr_BadInternalCall();
766 _PyErr_ChainExceptions(exc, val, tb);
767 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600768 }
769}
770
Eric Snowfdf282d2019-01-11 14:26:55 -0700771/* Py_MakePendingCalls() is a simple wrapper for the sake
772 of backward-compatibility. */
773int
774Py_MakePendingCalls(void)
775{
776 assert(PyGILState_Check());
777
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100778 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100779 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100780
Eric Snowfdf282d2019-01-11 14:26:55 -0700781 /* Python signal handler doesn't really queue a callback: it only signals
782 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100783 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700784 if (res != 0) {
785 return res;
786 }
787
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100788 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600789 if (res != 0) {
790 return res;
791 }
792
793 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000794}
795
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000796/* The interpreter's recursion limit */
797
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000798#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200799# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000800#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600801
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600802void
Victor Stinnerdab84232020-03-17 18:56:44 +0100803_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600804{
Victor Stinner7be4e352020-05-05 20:27:47 +0200805#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100806 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200807#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100808}
809
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200810int
Victor Stinnerdab84232020-03-17 18:56:44 +0100811_PyEval_InitState(struct _ceval_state *ceval)
812{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200813 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
814
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200815 struct _pending_calls *pending = &ceval->pending;
816 assert(pending->lock == NULL);
817
818 pending->lock = PyThread_allocate_lock();
819 if (pending->lock == NULL) {
820 return -1;
821 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200822
823#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
824 _gil_initialize(&ceval->gil);
825#endif
826
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200827 return 0;
828}
829
830void
831_PyEval_FiniState(struct _ceval_state *ceval)
832{
833 struct _pending_calls *pending = &ceval->pending;
834 if (pending->lock != NULL) {
835 PyThread_free_lock(pending->lock);
836 pending->lock = NULL;
837 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600838}
839
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000840int
841Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000842{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200843 PyInterpreterState *interp = _PyInterpreterState_GET();
844 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000845}
846
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000847void
848Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000849{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200850 PyThreadState *tstate = _PyThreadState_GET();
851 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000852}
853
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100854/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200855 if the recursion_depth reaches recursion_limit.
856 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000857 to guarantee that _Py_CheckRecursiveCall() is regularly called.
858 Without USE_STACKCHECK, there is no need for this. */
859int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100860_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000861{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200862 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000863
864#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700865 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (PyOS_CheckStack()) {
867 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200868 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 return -1;
870 }
pdox18967932017-10-25 23:03:01 -0700871#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000872 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (tstate->recursion_depth > recursion_limit + 50) {
874 /* Overflowing while handling an overflow. Give up. */
875 Py_FatalError("Cannot recover from stack overflow.");
876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000878 else {
879 if (tstate->recursion_depth > recursion_limit) {
880 tstate->recursion_headroom++;
881 _PyErr_Format(tstate, PyExc_RecursionError,
882 "maximum recursion depth exceeded%s",
883 where);
884 tstate->recursion_headroom--;
885 --tstate->recursion_depth;
886 return -1;
887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 }
889 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000890}
891
Brandt Bucher145bf262021-02-26 14:51:55 -0800892
893// PEP 634: Structural Pattern Matching
894
895
896// Return a tuple of values corresponding to keys, with error checks for
897// duplicate/missing keys.
898static PyObject*
899match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
900{
901 assert(PyTuple_CheckExact(keys));
902 Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
903 if (!nkeys) {
904 // No keys means no items.
905 return PyTuple_New(0);
906 }
907 PyObject *seen = NULL;
908 PyObject *dummy = NULL;
909 PyObject *values = NULL;
910 // We use the two argument form of map.get(key, default) for two reasons:
911 // - Atomically check for a key and get its value without error handling.
912 // - Don't cause key creation or resizing in dict subclasses like
913 // collections.defaultdict that define __missing__ (or similar).
914 _Py_IDENTIFIER(get);
915 PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
916 if (get == NULL) {
917 goto fail;
918 }
919 seen = PySet_New(NULL);
920 if (seen == NULL) {
921 goto fail;
922 }
923 // dummy = object()
924 dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
925 if (dummy == NULL) {
926 goto fail;
927 }
928 values = PyList_New(0);
929 if (values == NULL) {
930 goto fail;
931 }
932 for (Py_ssize_t i = 0; i < nkeys; i++) {
933 PyObject *key = PyTuple_GET_ITEM(keys, i);
934 if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
935 if (!_PyErr_Occurred(tstate)) {
936 // Seen it before!
937 _PyErr_Format(tstate, PyExc_ValueError,
938 "mapping pattern checks duplicate key (%R)", key);
939 }
940 goto fail;
941 }
942 PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
943 if (value == NULL) {
944 goto fail;
945 }
946 if (value == dummy) {
947 // key not in map!
948 Py_DECREF(value);
949 Py_DECREF(values);
950 // Return None:
951 Py_INCREF(Py_None);
952 values = Py_None;
953 goto done;
954 }
955 PyList_Append(values, value);
956 Py_DECREF(value);
957 }
958 Py_SETREF(values, PyList_AsTuple(values));
959 // Success:
960done:
961 Py_DECREF(get);
962 Py_DECREF(seen);
963 Py_DECREF(dummy);
964 return values;
965fail:
966 Py_XDECREF(get);
967 Py_XDECREF(seen);
968 Py_XDECREF(dummy);
969 Py_XDECREF(values);
970 return NULL;
971}
972
973// Extract a named attribute from the subject, with additional bookkeeping to
974// raise TypeErrors for repeated lookups. On failure, return NULL (with no
975// error set). Use _PyErr_Occurred(tstate) to disambiguate.
976static PyObject*
977match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
978 PyObject *name, PyObject *seen)
979{
980 assert(PyUnicode_CheckExact(name));
981 assert(PySet_CheckExact(seen));
982 if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
983 if (!_PyErr_Occurred(tstate)) {
984 // Seen it before!
985 _PyErr_Format(tstate, PyExc_TypeError,
986 "%s() got multiple sub-patterns for attribute %R",
987 ((PyTypeObject*)type)->tp_name, name);
988 }
989 return NULL;
990 }
991 PyObject *attr = PyObject_GetAttr(subject, name);
992 if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
993 _PyErr_Clear(tstate);
994 }
995 return attr;
996}
997
998// On success (match), return a tuple of extracted attributes. On failure (no
999// match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
1000static PyObject*
1001match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
1002 Py_ssize_t nargs, PyObject *kwargs)
1003{
1004 if (!PyType_Check(type)) {
1005 const char *e = "called match pattern must be a type";
1006 _PyErr_Format(tstate, PyExc_TypeError, e);
1007 return NULL;
1008 }
1009 assert(PyTuple_CheckExact(kwargs));
1010 // First, an isinstance check:
1011 if (PyObject_IsInstance(subject, type) <= 0) {
1012 return NULL;
1013 }
1014 // So far so good:
1015 PyObject *seen = PySet_New(NULL);
1016 if (seen == NULL) {
1017 return NULL;
1018 }
1019 PyObject *attrs = PyList_New(0);
1020 if (attrs == NULL) {
1021 Py_DECREF(seen);
1022 return NULL;
1023 }
1024 // NOTE: From this point on, goto fail on failure:
1025 PyObject *match_args = NULL;
1026 // First, the positional subpatterns:
1027 if (nargs) {
1028 int match_self = 0;
1029 match_args = PyObject_GetAttrString(type, "__match_args__");
1030 if (match_args) {
Brandt Bucher145bf262021-02-26 14:51:55 -08001031 if (!PyTuple_CheckExact(match_args)) {
Brandt Bucherf84d5a12021-04-05 19:17:08 -07001032 const char *e = "%s.__match_args__ must be a tuple (got %s)";
Brandt Bucher145bf262021-02-26 14:51:55 -08001033 _PyErr_Format(tstate, PyExc_TypeError, e,
1034 ((PyTypeObject *)type)->tp_name,
1035 Py_TYPE(match_args)->tp_name);
1036 goto fail;
1037 }
1038 }
1039 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1040 _PyErr_Clear(tstate);
1041 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1042 // define __match_args__. This is natural behavior for subclasses:
1043 // it's as if __match_args__ is some "magic" value that is lost as
1044 // soon as they redefine it.
1045 match_args = PyTuple_New(0);
1046 match_self = PyType_HasFeature((PyTypeObject*)type,
1047 _Py_TPFLAGS_MATCH_SELF);
1048 }
1049 else {
1050 goto fail;
1051 }
1052 assert(PyTuple_CheckExact(match_args));
1053 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1054 if (allowed < nargs) {
1055 const char *plural = (allowed == 1) ? "" : "s";
1056 _PyErr_Format(tstate, PyExc_TypeError,
1057 "%s() accepts %d positional sub-pattern%s (%d given)",
1058 ((PyTypeObject*)type)->tp_name,
1059 allowed, plural, nargs);
1060 goto fail;
1061 }
1062 if (match_self) {
1063 // Easy. Copy the subject itself, and move on to kwargs.
1064 PyList_Append(attrs, subject);
1065 }
1066 else {
1067 for (Py_ssize_t i = 0; i < nargs; i++) {
1068 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1069 if (!PyUnicode_CheckExact(name)) {
1070 _PyErr_Format(tstate, PyExc_TypeError,
1071 "__match_args__ elements must be strings "
1072 "(got %s)", Py_TYPE(name)->tp_name);
1073 goto fail;
1074 }
1075 PyObject *attr = match_class_attr(tstate, subject, type, name,
1076 seen);
1077 if (attr == NULL) {
1078 goto fail;
1079 }
1080 PyList_Append(attrs, attr);
1081 Py_DECREF(attr);
1082 }
1083 }
1084 Py_CLEAR(match_args);
1085 }
1086 // Finally, the keyword subpatterns:
1087 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1088 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1089 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1090 if (attr == NULL) {
1091 goto fail;
1092 }
1093 PyList_Append(attrs, attr);
1094 Py_DECREF(attr);
1095 }
1096 Py_SETREF(attrs, PyList_AsTuple(attrs));
1097 Py_DECREF(seen);
1098 return attrs;
1099fail:
1100 // We really don't care whether an error was raised or not... that's our
1101 // caller's problem. All we know is that the match failed.
1102 Py_XDECREF(match_args);
1103 Py_DECREF(seen);
1104 Py_DECREF(attrs);
1105 return NULL;
1106}
1107
1108
Victor Stinner09532fe2019-05-10 23:39:09 +02001109static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001110static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001111
Guido van Rossum374a9221991-04-04 10:40:29 +00001112
Guido van Rossumb209a111997-04-29 18:18:01 +00001113PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001114PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001115{
Victor Stinner46496f92021-02-20 15:17:18 +01001116 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001117 if (locals == NULL) {
1118 locals = globals;
1119 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01001120 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannon0332e562021-02-01 10:42:03 +00001121 if (builtins == NULL) {
1122 return NULL;
1123 }
1124 PyFrameConstructor desc = {
1125 .fc_globals = globals,
1126 .fc_builtins = builtins,
1127 .fc_name = ((PyCodeObject *)co)->co_name,
1128 .fc_qualname = ((PyCodeObject *)co)->co_name,
1129 .fc_code = co,
1130 .fc_defaults = NULL,
1131 .fc_kwdefaults = NULL,
1132 .fc_closure = NULL
1133 };
Victor Stinner46496f92021-02-20 15:17:18 +01001134 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001135}
1136
1137
1138/* Interpreter main loop */
1139
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001140PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001141PyEval_EvalFrame(PyFrameObject *f)
1142{
Victor Stinner0b72b232020-03-12 23:18:39 +01001143 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001144 PyThreadState *tstate = _PyThreadState_GET();
1145 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001146}
1147
1148PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001150{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001151 PyThreadState *tstate = _PyThreadState_GET();
1152 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001153}
1154
Victor Stinnerda2914d2020-03-20 09:29:08 +01001155
1156/* Handle signals, pending calls, GIL drop request
1157 and asynchronous exception */
1158static int
1159eval_frame_handle_pending(PyThreadState *tstate)
1160{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001161 _PyRuntimeState * const runtime = &_PyRuntime;
1162 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001163
1164 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001165 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001166 if (handle_signals(tstate) != 0) {
1167 return -1;
1168 }
1169 }
1170
1171 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001172 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001173 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001174 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001175 return -1;
1176 }
1177 }
1178
1179 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001180 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001181 /* Give another thread a chance */
1182 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1183 Py_FatalError("tstate mix-up");
1184 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001185 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001186
1187 /* Other threads may run now */
1188
1189 take_gil(tstate);
1190
Victor Stinnere838a932020-05-05 19:56:48 +02001191#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1192 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1193#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001194 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1195 Py_FatalError("orphan tstate");
1196 }
Victor Stinnere838a932020-05-05 19:56:48 +02001197#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001198 }
1199
1200 /* Check for asynchronous exception. */
1201 if (tstate->async_exc != NULL) {
1202 PyObject *exc = tstate->async_exc;
1203 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001204 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001205 _PyErr_SetNone(tstate, exc);
1206 Py_DECREF(exc);
1207 return -1;
1208 }
1209
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001210#ifdef MS_WINDOWS
1211 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1212 // different thread than the Python thread, in which case
1213 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1214 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1215 // value. It prevents to interrupt the eval loop at every instruction if
1216 // the current Python thread cannot handle signals (if
1217 // _Py_ThreadCanHandleSignals() is false).
1218 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1219#endif
1220
Victor Stinnerda2914d2020-03-20 09:29:08 +01001221 return 0;
1222}
1223
Victor Stinner3c1e4812012-03-26 22:10:51 +02001224
Antoine Pitroub52ec782009-01-25 16:34:23 +00001225/* Computed GOTOs, or
1226 the-optimization-commonly-but-improperly-known-as-"threaded code"
1227 using gcc's labels-as-values extension
1228 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1229
1230 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001232 combined with a lookup table of jump addresses. However, since the
1233 indirect jump instruction is shared by all opcodes, the CPU will have a
1234 hard time making the right prediction for where to jump next (actually,
1235 it will be always wrong except in the uncommon case of a sequence of
1236 several identical opcodes).
1237
1238 "Threaded code" in contrast, uses an explicit jump table and an explicit
1239 indirect jump instruction at the end of each opcode. Since the jump
1240 instruction is at a different address for each opcode, the CPU will make a
1241 separate prediction for each of these instructions, which is equivalent to
1242 predicting the second opcode of each opcode pair. These predictions have
1243 a much better chance to turn out valid, especially in small bytecode loops.
1244
1245 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001247 and potentially many more instructions (depending on the pipeline width).
1248 A correctly predicted branch, however, is nearly free.
1249
1250 At the time of this writing, the "threaded code" version is up to 15-20%
1251 faster than the normal "switch" version, depending on the compiler and the
1252 CPU architecture.
1253
1254 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1255 because it would render the measurements invalid.
1256
1257
1258 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1259 indirect jumps by sharing them between all opcodes. Such optimizations
1260 can be disabled on gcc by using the -fno-gcse flag (or possibly
1261 -fno-crossjumping).
1262*/
1263
Mark Shannon28d28e02021-04-08 11:22:55 +01001264/* Use macros rather than inline functions, to make it as clear as possible
1265 * to the C compiler that the tracing check is a simple test then branch.
1266 * We want to be sure that the compiler knows this before it generates
1267 * the CFG.
1268 */
1269#ifdef LLTRACE
1270#define OR_LLTRACE || lltrace
1271#else
1272#define OR_LLTRACE
1273#endif
1274
1275#ifdef WITH_DTRACE
1276#define OR_DTRACE_LINE || PyDTrace_LINE_ENABLED()
1277#else
1278#define OR_DTRACE_LINE
1279#endif
1280
Antoine Pitrou042b1282010-08-13 21:15:58 +00001281#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001282#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001283#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001284#endif
1285
Antoine Pitrou042b1282010-08-13 21:15:58 +00001286#ifdef HAVE_COMPUTED_GOTOS
1287 #ifndef USE_COMPUTED_GOTOS
1288 #define USE_COMPUTED_GOTOS 1
1289 #endif
1290#else
1291 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1292 #error "Computed gotos are not supported on this compiler."
1293 #endif
1294 #undef USE_COMPUTED_GOTOS
1295 #define USE_COMPUTED_GOTOS 0
1296#endif
1297
1298#if USE_COMPUTED_GOTOS
Mark Shannon28d28e02021-04-08 11:22:55 +01001299#define TARGET(op) op: TARGET_##op
1300#define DISPATCH_GOTO() goto *opcode_targets[opcode]
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001302#define TARGET(op) op
Mark Shannon28d28e02021-04-08 11:22:55 +01001303#define DISPATCH_GOTO() goto dispatch_opcode
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304#endif
1305
Mark Shannon28d28e02021-04-08 11:22:55 +01001306#define DISPATCH() \
1307 { \
Mark Shannon9e7b2072021-04-13 11:08:14 +01001308 if (trace_info.cframe.use_tracing OR_DTRACE_LINE OR_LLTRACE) { \
Mark Shannon28d28e02021-04-08 11:22:55 +01001309 goto tracing_dispatch; \
1310 } \
1311 f->f_lasti = INSTR_OFFSET(); \
1312 NEXTOPARG(); \
1313 DISPATCH_GOTO(); \
1314 }
1315
Mark Shannon4958f5d2021-03-24 17:56:12 +00001316#define CHECK_EVAL_BREAKER() \
1317 if (_Py_atomic_load_relaxed(eval_breaker)) { \
1318 continue; \
1319 }
1320
Antoine Pitroub52ec782009-01-25 16:34:23 +00001321
Neal Norwitza81d2202002-07-14 00:27:26 +00001322/* Tuple access macros */
1323
1324#ifndef Py_DEBUG
1325#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1326#else
1327#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1328#endif
1329
Guido van Rossum374a9221991-04-04 10:40:29 +00001330/* Code access macros */
1331
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001332/* The integer overflow is checked by an assertion below. */
Mark Shannonfcb55c02021-04-01 16:00:31 +01001333#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001334#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001335 _Py_CODEUNIT word = *next_instr; \
1336 opcode = _Py_OPCODE(word); \
1337 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001338 next_instr++; \
1339 } while (0)
Mark Shannonfcb55c02021-04-01 16:00:31 +01001340#define JUMPTO(x) (next_instr = first_instr + (x))
1341#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001342
Raymond Hettingerf606f872003-03-16 03:11:04 +00001343/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 Some opcodes tend to come in pairs thus making it possible to
1345 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001346 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 Verifying the prediction costs a single high-speed test of a register
1349 variable against a constant. If the pairing was good, then the
1350 processor's own internal branch predication has a high likelihood of
1351 success, resulting in a nearly zero-overhead transition to the
1352 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001353 including its unpredictable switch-case branch. Combined with the
1354 processor's internal branch prediction, a successful PREDICT has the
1355 effect of making the two opcodes run as if they were a single new opcode
1356 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001357
Georg Brandl86b2fb92008-07-16 03:43:04 +00001358 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 predictions turned-on and interpret the results as if some opcodes
1360 had been combined or turn-off predictions so that the opcode frequency
1361 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001362
1363 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 the CPU to record separate branch prediction information for each
1365 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001366
Raymond Hettingerf606f872003-03-16 03:11:04 +00001367*/
1368
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001369#define PREDICT_ID(op) PRED_##op
1370
Antoine Pitrou042b1282010-08-13 21:15:58 +00001371#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001372#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001373#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001374#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001375 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001376 _Py_CODEUNIT word = *next_instr; \
1377 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001378 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001379 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001380 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001381 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001382 } \
1383 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001384#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001385#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001386
Raymond Hettingerf606f872003-03-16 03:11:04 +00001387
Guido van Rossum374a9221991-04-04 10:40:29 +00001388/* Stack manipulation macros */
1389
Martin v. Löwis18e16552006-02-15 17:27:45 +00001390/* The stack can grow at most MAXINT deep, as co_nlocals and
1391 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001392#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1393#define EMPTY() (STACK_LEVEL() == 0)
1394#define TOP() (stack_pointer[-1])
1395#define SECOND() (stack_pointer[-2])
1396#define THIRD() (stack_pointer[-3])
1397#define FOURTH() (stack_pointer[-4])
1398#define PEEK(n) (stack_pointer[-(n)])
1399#define SET_TOP(v) (stack_pointer[-1] = (v))
1400#define SET_SECOND(v) (stack_pointer[-2] = (v))
1401#define SET_THIRD(v) (stack_pointer[-3] = (v))
1402#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001403#define BASIC_STACKADJ(n) (stack_pointer += n)
1404#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1405#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001406
Guido van Rossum96a42c81992-01-12 02:29:51 +00001407#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001409 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001410 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001411#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001412 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001413#define STACK_GROW(n) do { \
1414 assert(n >= 0); \
1415 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001416 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001417 assert(STACK_LEVEL() <= co->co_stacksize); \
1418 } while (0)
1419#define STACK_SHRINK(n) do { \
1420 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001421 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001422 (void)(BASIC_STACKADJ(-n)); \
1423 assert(STACK_LEVEL() <= co->co_stacksize); \
1424 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001425#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001426 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001427 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001428#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001429#define PUSH(v) BASIC_PUSH(v)
1430#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001431#define STACK_GROW(n) BASIC_STACKADJ(n)
1432#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001433#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001434#endif
1435
Guido van Rossum681d79a1995-07-18 14:51:37 +00001436/* Local variable macros */
1437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001439
1440/* The SETLOCAL() macro must not DECREF the local variable in-place and
1441 then store the new value; it must copy the old value to a temporary
1442 value, then store the new value, and then DECREF the temporary value.
1443 This is because it is possible that during the DECREF the frame is
1444 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1445 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001447 GETLOCAL(i) = value; \
1448 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001449
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001450
1451#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 while (STACK_LEVEL() > (b)->b_level) { \
1453 PyObject *v = POP(); \
1454 Py_XDECREF(v); \
1455 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001456
1457#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001458 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001460 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1462 while (STACK_LEVEL() > (b)->b_level + 3) { \
1463 value = POP(); \
1464 Py_XDECREF(value); \
1465 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001466 exc_info = tstate->exc_info; \
1467 type = exc_info->exc_type; \
1468 value = exc_info->exc_value; \
1469 traceback = exc_info->exc_traceback; \
1470 exc_info->exc_type = POP(); \
1471 exc_info->exc_value = POP(); \
1472 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 Py_XDECREF(type); \
1474 Py_XDECREF(value); \
1475 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001476 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001477
Inada Naoki91234a12019-06-03 21:30:58 +09001478 /* macros for opcode cache */
1479#define OPCACHE_CHECK() \
1480 do { \
1481 co_opcache = NULL; \
1482 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001483 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001484 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001485 if (co_opcache_offset > 0) { \
1486 assert(co_opcache_offset <= co->co_opcache_size); \
1487 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001488 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001489 } \
1490 } \
1491 } while (0)
1492
Pablo Galindo109826c2020-10-20 06:22:44 +01001493#define OPCACHE_DEOPT() \
1494 do { \
1495 if (co_opcache != NULL) { \
1496 co_opcache->optimized = -1; \
1497 unsigned char co_opcache_offset = \
1498 co->co_opcache_map[next_instr - first_instr]; \
1499 assert(co_opcache_offset <= co->co_opcache_size); \
1500 co->co_opcache_map[co_opcache_offset] = 0; \
1501 co_opcache = NULL; \
1502 } \
1503 } while (0)
1504
1505#define OPCACHE_DEOPT_LOAD_ATTR() \
1506 do { \
1507 if (co_opcache != NULL) { \
1508 OPCACHE_STAT_ATTR_DEOPT(); \
1509 OPCACHE_DEOPT(); \
1510 } \
1511 } while (0)
1512
1513#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1514 do { \
1515 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1516 OPCACHE_DEOPT_LOAD_ATTR(); \
1517 } \
1518 } while (0)
1519
Inada Naoki91234a12019-06-03 21:30:58 +09001520#if OPCACHE_STATS
1521
1522#define OPCACHE_STAT_GLOBAL_HIT() \
1523 do { \
1524 if (co->co_opcache != NULL) opcache_global_hits++; \
1525 } while (0)
1526
1527#define OPCACHE_STAT_GLOBAL_MISS() \
1528 do { \
1529 if (co->co_opcache != NULL) opcache_global_misses++; \
1530 } while (0)
1531
1532#define OPCACHE_STAT_GLOBAL_OPT() \
1533 do { \
1534 if (co->co_opcache != NULL) opcache_global_opts++; \
1535 } while (0)
1536
Pablo Galindo109826c2020-10-20 06:22:44 +01001537#define OPCACHE_STAT_ATTR_HIT() \
1538 do { \
1539 if (co->co_opcache != NULL) opcache_attr_hits++; \
1540 } while (0)
1541
1542#define OPCACHE_STAT_ATTR_MISS() \
1543 do { \
1544 if (co->co_opcache != NULL) opcache_attr_misses++; \
1545 } while (0)
1546
1547#define OPCACHE_STAT_ATTR_OPT() \
1548 do { \
1549 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1550 } while (0)
1551
1552#define OPCACHE_STAT_ATTR_DEOPT() \
1553 do { \
1554 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1555 } while (0)
1556
1557#define OPCACHE_STAT_ATTR_TOTAL() \
1558 do { \
1559 if (co->co_opcache != NULL) opcache_attr_total++; \
1560 } while (0)
1561
Inada Naoki91234a12019-06-03 21:30:58 +09001562#else /* OPCACHE_STATS */
1563
1564#define OPCACHE_STAT_GLOBAL_HIT()
1565#define OPCACHE_STAT_GLOBAL_MISS()
1566#define OPCACHE_STAT_GLOBAL_OPT()
1567
Pablo Galindo109826c2020-10-20 06:22:44 +01001568#define OPCACHE_STAT_ATTR_HIT()
1569#define OPCACHE_STAT_ATTR_MISS()
1570#define OPCACHE_STAT_ATTR_OPT()
1571#define OPCACHE_STAT_ATTR_DEOPT()
1572#define OPCACHE_STAT_ATTR_TOTAL()
1573
Inada Naoki91234a12019-06-03 21:30:58 +09001574#endif
1575
Mark Shannond41bddd2021-03-25 12:00:30 +00001576
1577PyObject* _Py_HOT_FUNCTION
1578_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1579{
1580 _Py_EnsureTstateNotNULL(tstate);
1581
1582#if USE_COMPUTED_GOTOS
1583/* Import the static jump table */
1584#include "opcode_targets.h"
1585#endif
1586
1587#ifdef DXPAIRS
1588 int lastopcode = 0;
1589#endif
1590 PyObject **stack_pointer; /* Next free slot in value stack */
1591 const _Py_CODEUNIT *next_instr;
1592 int opcode; /* Current opcode */
1593 int oparg; /* Current opcode argument, if any */
1594 PyObject **fastlocals, **freevars;
1595 PyObject *retval = NULL; /* Return value */
Mark Shannon9e7b2072021-04-13 11:08:14 +01001596 _Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker;
Mark Shannond41bddd2021-03-25 12:00:30 +00001597 PyCodeObject *co;
1598
Mark Shannond41bddd2021-03-25 12:00:30 +00001599 const _Py_CODEUNIT *first_instr;
1600 PyObject *names;
1601 PyObject *consts;
1602 _PyOpcache *co_opcache;
1603
1604#ifdef LLTRACE
1605 _Py_IDENTIFIER(__ltrace__);
1606#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001607
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001608 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001610 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001611
Mark Shannon8e1b4062021-03-05 14:45:50 +00001612 PyTraceInfo trace_info;
Mark Shannon28d28e02021-04-08 11:22:55 +01001613 /* Mark trace_info as uninitialized */
Mark Shannon8e1b4062021-03-05 14:45:50 +00001614 trace_info.code = NULL;
1615
Mark Shannon9e7b2072021-04-13 11:08:14 +01001616 /* WARNING: Because the CFrame lives on the C stack,
1617 * but can be accessed from a heap allocated object (tstate)
1618 * strict stack discipline must be maintained.
1619 */
1620 CFrame *prev_cframe = tstate->cframe;
1621 trace_info.cframe.use_tracing = prev_cframe->use_tracing;
1622 trace_info.cframe.previous = prev_cframe;
1623 tstate->cframe = &trace_info.cframe;
1624
Mark Shannon8e1b4062021-03-05 14:45:50 +00001625 /* push frame */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001627 co = f->f_code;
Tim Peters5ca576e2001-06-18 22:08:13 +00001628
Mark Shannon9e7b2072021-04-13 11:08:14 +01001629 if (trace_info.cframe.use_tracing) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (tstate->c_tracefunc != NULL) {
1631 /* tstate->c_tracefunc, if defined, is a
1632 function that will be called on *every* entry
1633 to a code block. Its return value, if not
1634 None, is a function that will be called at
1635 the start of each executed line of code.
1636 (Actually, the function must return itself
1637 in order to continue tracing.) The trace
1638 functions are called with three arguments:
1639 a pointer to the current frame, a string
1640 indicating why the function is called, and
1641 an argument which depends on the situation.
1642 The global trace function is also called
1643 whenever an exception is detected. */
1644 if (call_trace_protected(tstate->c_tracefunc,
1645 tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001646 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001647 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 /* Trace function raised an error */
1649 goto exit_eval_frame;
1650 }
1651 }
1652 if (tstate->c_profilefunc != NULL) {
1653 /* Similar for c_profilefunc, except it needn't
1654 return itself and isn't called for "line" events */
1655 if (call_trace_protected(tstate->c_profilefunc,
1656 tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001657 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001658 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 /* Profile function raised an error */
1660 goto exit_eval_frame;
1661 }
1662 }
1663 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001664
Łukasz Langaa785c872016-09-09 17:37:37 -07001665 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1666 dtrace_function_entry(f);
1667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 names = co->co_names;
1669 consts = co->co_consts;
1670 fastlocals = f->f_localsplus;
1671 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001672 assert(PyBytes_Check(co->co_code));
1673 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001674 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1675 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1676 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001677 /*
1678 f->f_lasti refers to the index of the last instruction,
1679 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001680
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001681 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001682 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 When the PREDICT() macros are enabled, some opcode pairs follow in
1685 direct succession without updating f->f_lasti. A successful
1686 prediction effectively links the two codes together as if they
1687 were a single new opcode; accordingly,f->f_lasti will point to
1688 the first code in the pair (for instance, GET_ITER followed by
1689 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001690 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001692 assert(f->f_lasti >= -1);
Mark Shannonfcb55c02021-04-01 16:00:31 +01001693 next_instr = first_instr + f->f_lasti + 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01001694 stack_pointer = f->f_valuestack + f->f_stackdepth;
1695 /* Set f->f_stackdepth to -1.
1696 * Update when returning or calling trace function.
1697 Having f_stackdepth <= 0 ensures that invalid
1698 values are not visible to the cycle GC.
1699 We choose -1 rather than 0 to assist debugging.
1700 */
1701 f->f_stackdepth = -1;
1702 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001703
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001704 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001705 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001706 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001707 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001708 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001709 }
1710#if OPCACHE_STATS
1711 opcache_code_objects_extra_mem +=
1712 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1713 sizeof(_PyOpcache) * co->co_opcache_size;
1714 opcache_code_objects++;
1715#endif
1716 }
1717 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001718
Tim Peters5ca576e2001-06-18 22:08:13 +00001719#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001720 {
1721 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1722 if (r < 0) {
1723 goto exit_eval_frame;
1724 }
1725 lltrace = r;
1726 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001727#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001729 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001731 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001732
Victor Stinnerace47d72013-07-18 01:41:08 +02001733#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001734 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001735 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001736 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001737 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001738#endif
1739
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001740main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1743 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001744 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 /* Do periodic things. Doing this every time through
1747 the loop would add too much overhead, so we do it
1748 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001749 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 event needs attention (e.g. a signal handler or
1751 async I/O handler); see Py_AddPendingCall() and
1752 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Eric Snow7bda9de2019-03-08 17:25:54 -07001754 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001755 opcode = _Py_OPCODE(*next_instr);
Mark Shannon28d28e02021-04-08 11:22:55 +01001756 if (opcode != SETUP_FINALLY &&
1757 opcode != SETUP_WITH &&
1758 opcode != BEFORE_ASYNC_WITH &&
1759 opcode != YIELD_FROM) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001760 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001761 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001762 - If we're about to enter the 'with:'. It will prevent
1763 emitting a resource warning in the common idiom
1764 'with open(path) as file:'.
1765 - If we're about to enter the 'async with:'.
1766 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001767 *very* useful, but might help in some cases and it's
1768 traditional)
1769 - If we're resuming a chain of nested 'yield from' or
1770 'await' calls, then each frame is parked with YIELD_FROM
1771 as its next opcode. If the user hit control-C we want to
1772 wait until we've reached the innermost frame before
1773 running the signal handler and raising KeyboardInterrupt
1774 (see bpo-30039).
1775 */
Mark Shannon28d28e02021-04-08 11:22:55 +01001776 if (eval_frame_handle_pending(tstate) != 0) {
1777 goto error;
1778 }
1779 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001781
Mark Shannon28d28e02021-04-08 11:22:55 +01001782 tracing_dispatch:
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001783 {
1784 int instr_prev = f->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 f->f_lasti = INSTR_OFFSET();
Mark Shannon28d28e02021-04-08 11:22:55 +01001786 NEXTOPARG();
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Łukasz Langaa785c872016-09-09 17:37:37 -07001788 if (PyDTrace_LINE_ENABLED())
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001789 maybe_dtrace_line(f, &trace_info, instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001792
Mark Shannon9e7b2072021-04-13 11:08:14 +01001793 if (trace_info.cframe.use_tracing &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001794 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001796 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001798 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 err = maybe_call_line_trace(tstate->c_tracefunc,
1801 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001802 tstate, f,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001803 &trace_info, instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 /* Reload possibly changed frame fields */
1805 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001806 stack_pointer = f->f_valuestack+f->f_stackdepth;
1807 f->f_stackdepth = -1;
Mark Shannon28d28e02021-04-08 11:22:55 +01001808 if (err) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 goto error;
Mark Shannon28d28e02021-04-08 11:22:55 +01001811 }
1812 NEXTOPARG();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 }
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001814 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001815
Guido van Rossum96a42c81992-01-12 02:29:51 +00001816#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (lltrace) {
1820 if (HAS_ARG(opcode)) {
1821 printf("%d: %d, %d\n",
1822 f->f_lasti, opcode, oparg);
1823 }
1824 else {
1825 printf("%d: %d\n",
1826 f->f_lasti, opcode);
1827 }
1828 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001829#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001830
Mark Shannon28d28e02021-04-08 11:22:55 +01001831 dispatch_opcode:
1832#ifdef DYNAMIC_EXECUTION_PROFILE
1833#ifdef DXPAIRS
1834 dxpairs[lastopcode][opcode]++;
1835 lastopcode = opcode;
1836#endif
1837 dxp[opcode]++;
1838#endif
1839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001843 It is essential that any operation that fails must goto error
Mark Shannon28d28e02021-04-08 11:22:55 +01001844 and that all operation that succeed call DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Benjamin Petersonddd19492018-09-16 22:38:02 -07001846 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001847 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001848 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001849
Benjamin Petersonddd19492018-09-16 22:38:02 -07001850 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 PyObject *value = GETLOCAL(oparg);
1852 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001853 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 UNBOUNDLOCAL_ERROR_MSG,
1855 PyTuple_GetItem(co->co_varnames, oparg));
1856 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 Py_INCREF(value);
1859 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001860 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 }
1862
Benjamin Petersonddd19492018-09-16 22:38:02 -07001863 case TARGET(LOAD_CONST): {
1864 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 PyObject *value = GETITEM(consts, oparg);
1866 Py_INCREF(value);
1867 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001868 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(STORE_FAST): {
1872 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 PyObject *value = POP();
1874 SETLOCAL(oparg, value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001875 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *value = POP();
1880 Py_DECREF(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *top = TOP();
1886 PyObject *second = SECOND();
1887 SET_TOP(second);
1888 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001889 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *top = TOP();
1894 PyObject *second = SECOND();
1895 PyObject *third = THIRD();
1896 SET_TOP(second);
1897 SET_SECOND(third);
1898 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001899 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Benjamin Petersonddd19492018-09-16 22:38:02 -07001902 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001903 PyObject *top = TOP();
1904 PyObject *second = SECOND();
1905 PyObject *third = THIRD();
1906 PyObject *fourth = FOURTH();
1907 SET_TOP(second);
1908 SET_SECOND(third);
1909 SET_THIRD(fourth);
1910 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001911 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 }
1913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 PyObject *top = TOP();
1916 Py_INCREF(top);
1917 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001924 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001926 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 SET_TOP(top);
1928 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001929 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001931
Benjamin Petersonddd19492018-09-16 22:38:02 -07001932 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 PyObject *value = TOP();
1934 PyObject *res = PyNumber_Positive(value);
1935 Py_DECREF(value);
1936 SET_TOP(res);
1937 if (res == NULL)
1938 goto error;
1939 DISPATCH();
1940 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Benjamin Petersonddd19492018-09-16 22:38:02 -07001942 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 PyObject *value = TOP();
1944 PyObject *res = PyNumber_Negative(value);
1945 Py_DECREF(value);
1946 SET_TOP(res);
1947 if (res == NULL)
1948 goto error;
1949 DISPATCH();
1950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Benjamin Petersonddd19492018-09-16 22:38:02 -07001952 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *value = TOP();
1954 int err = PyObject_IsTrue(value);
1955 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (err == 0) {
1957 Py_INCREF(Py_True);
1958 SET_TOP(Py_True);
1959 DISPATCH();
1960 }
1961 else if (err > 0) {
1962 Py_INCREF(Py_False);
1963 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 DISPATCH();
1965 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001966 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 goto error;
1968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Benjamin Petersonddd19492018-09-16 22:38:02 -07001970 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 PyObject *value = TOP();
1972 PyObject *res = PyNumber_Invert(value);
1973 Py_DECREF(value);
1974 SET_TOP(res);
1975 if (res == NULL)
1976 goto error;
1977 DISPATCH();
1978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Benjamin Petersonddd19492018-09-16 22:38:02 -07001980 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 PyObject *exp = POP();
1982 PyObject *base = TOP();
1983 PyObject *res = PyNumber_Power(base, exp, Py_None);
1984 Py_DECREF(base);
1985 Py_DECREF(exp);
1986 SET_TOP(res);
1987 if (res == NULL)
1988 goto error;
1989 DISPATCH();
1990 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Benjamin Petersonddd19492018-09-16 22:38:02 -07001992 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *right = POP();
1994 PyObject *left = TOP();
1995 PyObject *res = PyNumber_Multiply(left, right);
1996 Py_DECREF(left);
1997 Py_DECREF(right);
1998 SET_TOP(res);
1999 if (res == NULL)
2000 goto error;
2001 DISPATCH();
2002 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002003
Benjamin Petersonddd19492018-09-16 22:38:02 -07002004 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002005 PyObject *right = POP();
2006 PyObject *left = TOP();
2007 PyObject *res = PyNumber_MatrixMultiply(left, right);
2008 Py_DECREF(left);
2009 Py_DECREF(right);
2010 SET_TOP(res);
2011 if (res == NULL)
2012 goto error;
2013 DISPATCH();
2014 }
2015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 PyObject *divisor = POP();
2018 PyObject *dividend = TOP();
2019 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2020 Py_DECREF(dividend);
2021 Py_DECREF(divisor);
2022 SET_TOP(quotient);
2023 if (quotient == NULL)
2024 goto error;
2025 DISPATCH();
2026 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 PyObject *divisor = POP();
2030 PyObject *dividend = TOP();
2031 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2032 Py_DECREF(dividend);
2033 Py_DECREF(divisor);
2034 SET_TOP(quotient);
2035 if (quotient == NULL)
2036 goto error;
2037 DISPATCH();
2038 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002039
Benjamin Petersonddd19492018-09-16 22:38:02 -07002040 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 PyObject *divisor = POP();
2042 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002043 PyObject *res;
2044 if (PyUnicode_CheckExact(dividend) && (
2045 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2046 // fast path; string formatting, but not if the RHS is a str subclass
2047 // (see issue28598)
2048 res = PyUnicode_Format(dividend, divisor);
2049 } else {
2050 res = PyNumber_Remainder(dividend, divisor);
2051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 Py_DECREF(divisor);
2053 Py_DECREF(dividend);
2054 SET_TOP(res);
2055 if (res == NULL)
2056 goto error;
2057 DISPATCH();
2058 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002059
Benjamin Petersonddd19492018-09-16 22:38:02 -07002060 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 PyObject *right = POP();
2062 PyObject *left = TOP();
2063 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002064 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002065 CPython using bytecode, it is simply worthless.
2066 See http://bugs.python.org/issue21955 and
2067 http://bugs.python.org/issue10044 for the discussion. In short,
2068 no patch shown any impact on a realistic benchmark, only a minor
2069 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 if (PyUnicode_CheckExact(left) &&
2071 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002072 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002073 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002074 }
2075 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 sum = PyNumber_Add(left, right);
2077 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 Py_DECREF(right);
2080 SET_TOP(sum);
2081 if (sum == NULL)
2082 goto error;
2083 DISPATCH();
2084 }
2085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 PyObject *right = POP();
2088 PyObject *left = TOP();
2089 PyObject *diff = PyNumber_Subtract(left, right);
2090 Py_DECREF(right);
2091 Py_DECREF(left);
2092 SET_TOP(diff);
2093 if (diff == NULL)
2094 goto error;
2095 DISPATCH();
2096 }
2097
Benjamin Petersonddd19492018-09-16 22:38:02 -07002098 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 PyObject *sub = POP();
2100 PyObject *container = TOP();
2101 PyObject *res = PyObject_GetItem(container, sub);
2102 Py_DECREF(container);
2103 Py_DECREF(sub);
2104 SET_TOP(res);
2105 if (res == NULL)
2106 goto error;
2107 DISPATCH();
2108 }
2109
Benjamin Petersonddd19492018-09-16 22:38:02 -07002110 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 PyObject *right = POP();
2112 PyObject *left = TOP();
2113 PyObject *res = PyNumber_Lshift(left, right);
2114 Py_DECREF(left);
2115 Py_DECREF(right);
2116 SET_TOP(res);
2117 if (res == NULL)
2118 goto error;
2119 DISPATCH();
2120 }
2121
Benjamin Petersonddd19492018-09-16 22:38:02 -07002122 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 PyObject *right = POP();
2124 PyObject *left = TOP();
2125 PyObject *res = PyNumber_Rshift(left, right);
2126 Py_DECREF(left);
2127 Py_DECREF(right);
2128 SET_TOP(res);
2129 if (res == NULL)
2130 goto error;
2131 DISPATCH();
2132 }
2133
Benjamin Petersonddd19492018-09-16 22:38:02 -07002134 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 PyObject *right = POP();
2136 PyObject *left = TOP();
2137 PyObject *res = PyNumber_And(left, right);
2138 Py_DECREF(left);
2139 Py_DECREF(right);
2140 SET_TOP(res);
2141 if (res == NULL)
2142 goto error;
2143 DISPATCH();
2144 }
2145
Benjamin Petersonddd19492018-09-16 22:38:02 -07002146 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 PyObject *right = POP();
2148 PyObject *left = TOP();
2149 PyObject *res = PyNumber_Xor(left, right);
2150 Py_DECREF(left);
2151 Py_DECREF(right);
2152 SET_TOP(res);
2153 if (res == NULL)
2154 goto error;
2155 DISPATCH();
2156 }
2157
Benjamin Petersonddd19492018-09-16 22:38:02 -07002158 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 PyObject *right = POP();
2160 PyObject *left = TOP();
2161 PyObject *res = PyNumber_Or(left, right);
2162 Py_DECREF(left);
2163 Py_DECREF(right);
2164 SET_TOP(res);
2165 if (res == NULL)
2166 goto error;
2167 DISPATCH();
2168 }
2169
Benjamin Petersonddd19492018-09-16 22:38:02 -07002170 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *v = POP();
2172 PyObject *list = PEEK(oparg);
2173 int err;
2174 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 if (err != 0)
2177 goto error;
2178 PREDICT(JUMP_ABSOLUTE);
2179 DISPATCH();
2180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002181
Benjamin Petersonddd19492018-09-16 22:38:02 -07002182 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002184 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 int err;
2186 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 if (err != 0)
2189 goto error;
2190 PREDICT(JUMP_ABSOLUTE);
2191 DISPATCH();
2192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PyObject *exp = POP();
2196 PyObject *base = TOP();
2197 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2198 Py_DECREF(base);
2199 Py_DECREF(exp);
2200 SET_TOP(res);
2201 if (res == NULL)
2202 goto error;
2203 DISPATCH();
2204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002205
Benjamin Petersonddd19492018-09-16 22:38:02 -07002206 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 PyObject *right = POP();
2208 PyObject *left = TOP();
2209 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2210 Py_DECREF(left);
2211 Py_DECREF(right);
2212 SET_TOP(res);
2213 if (res == NULL)
2214 goto error;
2215 DISPATCH();
2216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002219 PyObject *right = POP();
2220 PyObject *left = TOP();
2221 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2222 Py_DECREF(left);
2223 Py_DECREF(right);
2224 SET_TOP(res);
2225 if (res == NULL)
2226 goto error;
2227 DISPATCH();
2228 }
2229
Benjamin Petersonddd19492018-09-16 22:38:02 -07002230 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 PyObject *divisor = POP();
2232 PyObject *dividend = TOP();
2233 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2234 Py_DECREF(dividend);
2235 Py_DECREF(divisor);
2236 SET_TOP(quotient);
2237 if (quotient == NULL)
2238 goto error;
2239 DISPATCH();
2240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 PyObject *divisor = POP();
2244 PyObject *dividend = TOP();
2245 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2246 Py_DECREF(dividend);
2247 Py_DECREF(divisor);
2248 SET_TOP(quotient);
2249 if (quotient == NULL)
2250 goto error;
2251 DISPATCH();
2252 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Benjamin Petersonddd19492018-09-16 22:38:02 -07002254 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 PyObject *right = POP();
2256 PyObject *left = TOP();
2257 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2258 Py_DECREF(left);
2259 Py_DECREF(right);
2260 SET_TOP(mod);
2261 if (mod == NULL)
2262 goto error;
2263 DISPATCH();
2264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Benjamin Petersonddd19492018-09-16 22:38:02 -07002266 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 PyObject *right = POP();
2268 PyObject *left = TOP();
2269 PyObject *sum;
2270 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002271 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002272 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002273 }
2274 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 sum = PyNumber_InPlaceAdd(left, right);
2276 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002277 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 Py_DECREF(right);
2279 SET_TOP(sum);
2280 if (sum == NULL)
2281 goto error;
2282 DISPATCH();
2283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Benjamin Petersonddd19492018-09-16 22:38:02 -07002285 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 PyObject *right = POP();
2287 PyObject *left = TOP();
2288 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2289 Py_DECREF(left);
2290 Py_DECREF(right);
2291 SET_TOP(diff);
2292 if (diff == NULL)
2293 goto error;
2294 DISPATCH();
2295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Benjamin Petersonddd19492018-09-16 22:38:02 -07002297 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PyObject *right = POP();
2299 PyObject *left = TOP();
2300 PyObject *res = PyNumber_InPlaceLshift(left, right);
2301 Py_DECREF(left);
2302 Py_DECREF(right);
2303 SET_TOP(res);
2304 if (res == NULL)
2305 goto error;
2306 DISPATCH();
2307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Benjamin Petersonddd19492018-09-16 22:38:02 -07002309 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 PyObject *right = POP();
2311 PyObject *left = TOP();
2312 PyObject *res = PyNumber_InPlaceRshift(left, right);
2313 Py_DECREF(left);
2314 Py_DECREF(right);
2315 SET_TOP(res);
2316 if (res == NULL)
2317 goto error;
2318 DISPATCH();
2319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Benjamin Petersonddd19492018-09-16 22:38:02 -07002321 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PyObject *right = POP();
2323 PyObject *left = TOP();
2324 PyObject *res = PyNumber_InPlaceAnd(left, right);
2325 Py_DECREF(left);
2326 Py_DECREF(right);
2327 SET_TOP(res);
2328 if (res == NULL)
2329 goto error;
2330 DISPATCH();
2331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Benjamin Petersonddd19492018-09-16 22:38:02 -07002333 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 PyObject *right = POP();
2335 PyObject *left = TOP();
2336 PyObject *res = PyNumber_InPlaceXor(left, right);
2337 Py_DECREF(left);
2338 Py_DECREF(right);
2339 SET_TOP(res);
2340 if (res == NULL)
2341 goto error;
2342 DISPATCH();
2343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002344
Benjamin Petersonddd19492018-09-16 22:38:02 -07002345 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 PyObject *right = POP();
2347 PyObject *left = TOP();
2348 PyObject *res = PyNumber_InPlaceOr(left, right);
2349 Py_DECREF(left);
2350 Py_DECREF(right);
2351 SET_TOP(res);
2352 if (res == NULL)
2353 goto error;
2354 DISPATCH();
2355 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002356
Benjamin Petersonddd19492018-09-16 22:38:02 -07002357 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 PyObject *sub = TOP();
2359 PyObject *container = SECOND();
2360 PyObject *v = THIRD();
2361 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002362 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002363 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 Py_DECREF(container);
2367 Py_DECREF(sub);
2368 if (err != 0)
2369 goto error;
2370 DISPATCH();
2371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 PyObject *sub = TOP();
2375 PyObject *container = SECOND();
2376 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002377 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002378 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 err = PyObject_DelItem(container, sub);
2380 Py_DECREF(container);
2381 Py_DECREF(sub);
2382 if (err != 0)
2383 goto error;
2384 DISPATCH();
2385 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002386
Benjamin Petersonddd19492018-09-16 22:38:02 -07002387 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002388 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002390 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002391 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002393 _PyErr_SetString(tstate, PyExc_RuntimeError,
2394 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 Py_DECREF(value);
2396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002398 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 Py_DECREF(value);
2400 if (res == NULL)
2401 goto error;
2402 Py_DECREF(res);
2403 DISPATCH();
2404 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 switch (oparg) {
2409 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002411 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002414 /* fall through */
2415 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002416 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002417 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 break;
2420 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002421 _PyErr_SetString(tstate, PyExc_SystemError,
2422 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 break;
2424 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 goto error;
2426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002427
Benjamin Petersonddd19492018-09-16 22:38:02 -07002428 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002430 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002431 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002432 f->f_state = FRAME_RETURNED;
2433 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002434 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002438 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002439 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002440 PyObject *obj = TOP();
2441 PyTypeObject *type = Py_TYPE(obj);
2442
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002443 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002444 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002445 }
Yury Selivanov75445082015-05-11 22:57:16 -04002446
2447 if (getter != NULL) {
2448 iter = (*getter)(obj);
2449 Py_DECREF(obj);
2450 if (iter == NULL) {
2451 SET_TOP(NULL);
2452 goto error;
2453 }
2454 }
2455 else {
2456 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002457 _PyErr_Format(tstate, PyExc_TypeError,
2458 "'async for' requires an object with "
2459 "__aiter__ method, got %.100s",
2460 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002461 Py_DECREF(obj);
2462 goto error;
2463 }
2464
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002465 if (Py_TYPE(iter)->tp_as_async == NULL ||
2466 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002467
Yury Selivanov398ff912017-03-02 22:20:00 -05002468 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002469 _PyErr_Format(tstate, PyExc_TypeError,
2470 "'async for' received an object from __aiter__ "
2471 "that does not implement __anext__: %.100s",
2472 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002473 Py_DECREF(iter);
2474 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002475 }
2476
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002477 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002478 DISPATCH();
2479 }
2480
Benjamin Petersonddd19492018-09-16 22:38:02 -07002481 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002482 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002483 PyObject *next_iter = NULL;
2484 PyObject *awaitable = NULL;
2485 PyObject *aiter = TOP();
2486 PyTypeObject *type = Py_TYPE(aiter);
2487
Yury Selivanoveb636452016-09-08 22:01:51 -07002488 if (PyAsyncGen_CheckExact(aiter)) {
2489 awaitable = type->tp_as_async->am_anext(aiter);
2490 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002491 goto error;
2492 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002493 } else {
2494 if (type->tp_as_async != NULL){
2495 getter = type->tp_as_async->am_anext;
2496 }
Yury Selivanov75445082015-05-11 22:57:16 -04002497
Yury Selivanoveb636452016-09-08 22:01:51 -07002498 if (getter != NULL) {
2499 next_iter = (*getter)(aiter);
2500 if (next_iter == NULL) {
2501 goto error;
2502 }
2503 }
2504 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002505 _PyErr_Format(tstate, PyExc_TypeError,
2506 "'async for' requires an iterator with "
2507 "__anext__ method, got %.100s",
2508 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002509 goto error;
2510 }
Yury Selivanov75445082015-05-11 22:57:16 -04002511
Yury Selivanoveb636452016-09-08 22:01:51 -07002512 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2513 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002514 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002515 PyExc_TypeError,
2516 "'async for' received an invalid object "
2517 "from __anext__: %.100s",
2518 Py_TYPE(next_iter)->tp_name);
2519
2520 Py_DECREF(next_iter);
2521 goto error;
2522 } else {
2523 Py_DECREF(next_iter);
2524 }
2525 }
Yury Selivanov75445082015-05-11 22:57:16 -04002526
2527 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002528 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002529 DISPATCH();
2530 }
2531
Benjamin Petersonddd19492018-09-16 22:38:02 -07002532 case TARGET(GET_AWAITABLE): {
2533 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002534 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002535 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002536
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002537 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002538 int opcode_at_minus_3 = 0;
2539 if ((next_instr - first_instr) > 2) {
2540 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2541 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002543 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002544 _Py_OPCODE(next_instr[-2]));
2545 }
2546
Yury Selivanov75445082015-05-11 22:57:16 -04002547 Py_DECREF(iterable);
2548
Yury Selivanovc724bae2016-03-02 11:30:46 -05002549 if (iter != NULL && PyCoro_CheckExact(iter)) {
2550 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2551 if (yf != NULL) {
2552 /* `iter` is a coroutine object that is being
2553 awaited, `yf` is a pointer to the current awaitable
2554 being awaited on. */
2555 Py_DECREF(yf);
2556 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002557 _PyErr_SetString(tstate, PyExc_RuntimeError,
2558 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002559 /* The code below jumps to `error` if `iter` is NULL. */
2560 }
2561 }
2562
Yury Selivanov75445082015-05-11 22:57:16 -04002563 SET_TOP(iter); /* Even if it's NULL */
2564
2565 if (iter == NULL) {
2566 goto error;
2567 }
2568
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002569 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002570 DISPATCH();
2571 }
2572
Benjamin Petersonddd19492018-09-16 22:38:02 -07002573 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002575 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002576 PySendResult gen_status;
2577 if (tstate->c_tracefunc == NULL) {
2578 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002579 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002580 _Py_IDENTIFIER(send);
Victor Stinner09bbebe2021-04-11 00:17:39 +02002581 if (Py_IsNone(v) && PyIter_Check(receiver)) {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002582 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002583 }
2584 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002585 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002586 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002587 if (retval == NULL) {
2588 if (tstate->c_tracefunc != NULL
2589 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002590 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002591 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2592 gen_status = PYGEN_RETURN;
2593 }
2594 else {
2595 gen_status = PYGEN_ERROR;
2596 }
2597 }
2598 else {
2599 gen_status = PYGEN_NEXT;
2600 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002601 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002603 if (gen_status == PYGEN_ERROR) {
2604 assert (retval == NULL);
2605 goto error;
2606 }
2607 if (gen_status == PYGEN_RETURN) {
2608 assert (retval != NULL);
2609
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002610 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002611 SET_TOP(retval);
2612 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002614 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002615 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002616 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002617 /* and repeat... */
Mark Shannonfcb55c02021-04-01 16:00:31 +01002618 assert(f->f_lasti > 0);
2619 f->f_lasti -= 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01002620 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002621 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002622 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002627
2628 if (co->co_flags & CO_ASYNC_GENERATOR) {
2629 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2630 Py_DECREF(retval);
2631 if (w == NULL) {
2632 retval = NULL;
2633 goto error;
2634 }
2635 retval = w;
2636 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002637 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002638 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002639 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002641
Mark Shannonb37181e2021-04-06 11:48:59 +01002642 case TARGET(GEN_START): {
2643 PyObject *none = POP();
2644 Py_DECREF(none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02002645 if (!Py_IsNone(none)) {
Mark Shannonb37181e2021-04-06 11:48:59 +01002646 if (oparg > 2) {
2647 _PyErr_SetString(tstate, PyExc_SystemError,
2648 "Illegal kind for GEN_START");
2649 }
2650 else {
2651 static const char *gen_kind[3] = {
2652 "generator",
2653 "coroutine",
2654 "async generator"
2655 };
2656 _PyErr_Format(tstate, PyExc_TypeError,
2657 "can't send non-None value to a "
2658 "just-started %s",
2659 gen_kind[oparg]);
2660 }
2661 goto error;
2662 }
2663 DISPATCH();
2664 }
2665
Benjamin Petersonddd19492018-09-16 22:38:02 -07002666 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002667 PyObject *type, *value, *traceback;
2668 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 PyTryBlock *b = PyFrame_BlockPop(f);
2670 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002671 _PyErr_SetString(tstate, PyExc_SystemError,
2672 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002675 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2676 STACK_LEVEL() <= (b)->b_level + 4);
2677 exc_info = tstate->exc_info;
2678 type = exc_info->exc_type;
2679 value = exc_info->exc_value;
2680 traceback = exc_info->exc_traceback;
2681 exc_info->exc_type = POP();
2682 exc_info->exc_value = POP();
2683 exc_info->exc_traceback = POP();
2684 Py_XDECREF(type);
2685 Py_XDECREF(value);
2686 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002689
Benjamin Petersonddd19492018-09-16 22:38:02 -07002690 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002691 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002694
Mark Shannonfee55262019-11-21 09:11:43 +00002695 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002696 assert(f->f_iblock > 0);
2697 if (oparg) {
2698 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2699 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002700 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002701 PyObject *val = POP();
2702 PyObject *tb = POP();
2703 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002704 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002705 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002707
Benjamin Petersonddd19492018-09-16 22:38:02 -07002708 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002709 PyObject *exc = POP();
2710 assert(PyExceptionClass_Check(exc));
2711 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2712 PyTryBlock *b = PyFrame_BlockPop(f);
2713 assert(b->b_type == EXCEPT_HANDLER);
2714 Py_DECREF(exc);
2715 UNWIND_EXCEPT_HANDLER(b);
2716 Py_DECREF(POP());
2717 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002718 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002719 }
2720 else {
2721 PyObject *val = POP();
2722 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002723 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002724 goto exception_unwind;
2725 }
2726 }
2727
Zackery Spytzce6a0702019-08-25 03:44:09 -06002728 case TARGET(LOAD_ASSERTION_ERROR): {
2729 PyObject *value = PyExc_AssertionError;
2730 Py_INCREF(value);
2731 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002732 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002733 }
2734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002736 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002737
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002739 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002740 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002742 if (!_PyErr_Occurred(tstate)) {
2743 _PyErr_SetString(tstate, PyExc_NameError,
2744 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002747 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002749 }
2750 else {
2751 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2752 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002753 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2755 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002756 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2757 _PyErr_SetString(tstate, PyExc_NameError,
2758 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002760 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002763 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002764 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002765
Benjamin Petersonddd19492018-09-16 22:38:02 -07002766 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 PyObject *name = GETITEM(names, oparg);
2768 PyObject *v = POP();
2769 PyObject *ns = f->f_locals;
2770 int err;
2771 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002772 _PyErr_Format(tstate, PyExc_SystemError,
2773 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 if (PyDict_CheckExact(ns))
2778 err = PyDict_SetItem(ns, name, v);
2779 else
2780 err = PyObject_SetItem(ns, name, v);
2781 Py_DECREF(v);
2782 if (err != 0)
2783 goto error;
2784 DISPATCH();
2785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002786
Benjamin Petersonddd19492018-09-16 22:38:02 -07002787 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 PyObject *name = GETITEM(names, oparg);
2789 PyObject *ns = f->f_locals;
2790 int err;
2791 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002792 _PyErr_Format(tstate, PyExc_SystemError,
2793 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 err = PyObject_DelItem(ns, name);
2797 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002798 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 NAME_ERROR_MSG,
2800 name);
2801 goto error;
2802 }
2803 DISPATCH();
2804 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002805
Benjamin Petersonddd19492018-09-16 22:38:02 -07002806 case TARGET(UNPACK_SEQUENCE): {
2807 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *seq = POP(), *item, **items;
2809 if (PyTuple_CheckExact(seq) &&
2810 PyTuple_GET_SIZE(seq) == oparg) {
2811 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 item = items[oparg];
2814 Py_INCREF(item);
2815 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 } else if (PyList_CheckExact(seq) &&
2818 PyList_GET_SIZE(seq) == oparg) {
2819 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 item = items[oparg];
2822 Py_INCREF(item);
2823 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002825 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002827 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 } else {
2829 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 Py_DECREF(seq);
2831 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002834 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002836
Benjamin Petersonddd19492018-09-16 22:38:02 -07002837 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2839 PyObject *seq = POP();
2840
Victor Stinner438a12d2019-05-24 17:01:38 +02002841 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 stack_pointer + totalargs)) {
2843 stack_pointer += totalargs;
2844 } else {
2845 Py_DECREF(seq);
2846 goto error;
2847 }
2848 Py_DECREF(seq);
2849 DISPATCH();
2850 }
2851
Benjamin Petersonddd19492018-09-16 22:38:02 -07002852 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 PyObject *name = GETITEM(names, oparg);
2854 PyObject *owner = TOP();
2855 PyObject *v = SECOND();
2856 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002857 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 Py_DECREF(owner);
2861 if (err != 0)
2862 goto error;
2863 DISPATCH();
2864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002865
Benjamin Petersonddd19492018-09-16 22:38:02 -07002866 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 PyObject *name = GETITEM(names, oparg);
2868 PyObject *owner = POP();
2869 int err;
2870 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2871 Py_DECREF(owner);
2872 if (err != 0)
2873 goto error;
2874 DISPATCH();
2875 }
2876
Benjamin Petersonddd19492018-09-16 22:38:02 -07002877 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 PyObject *name = GETITEM(names, oparg);
2879 PyObject *v = POP();
2880 int err;
2881 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 if (err != 0)
2884 goto error;
2885 DISPATCH();
2886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002887
Benjamin Petersonddd19492018-09-16 22:38:02 -07002888 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 PyObject *name = GETITEM(names, oparg);
2890 int err;
2891 err = PyDict_DelItem(f->f_globals, name);
2892 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002893 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2894 format_exc_check_arg(tstate, PyExc_NameError,
2895 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002896 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002898 }
2899 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002901
Benjamin Petersonddd19492018-09-16 22:38:02 -07002902 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 PyObject *name = GETITEM(names, oparg);
2904 PyObject *locals = f->f_locals;
2905 PyObject *v;
2906 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002907 _PyErr_Format(tstate, PyExc_SystemError,
2908 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002912 v = PyDict_GetItemWithError(locals, name);
2913 if (v != NULL) {
2914 Py_INCREF(v);
2915 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002916 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002917 goto error;
2918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 }
2920 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002922 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002923 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002924 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002925 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 }
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002929 v = PyDict_GetItemWithError(f->f_globals, name);
2930 if (v != NULL) {
2931 Py_INCREF(v);
2932 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002933 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002934 goto error;
2935 }
2936 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002937 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002938 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002940 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002941 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002942 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002946 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002948 }
2949 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 v = PyObject_GetItem(f->f_builtins, name);
2951 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002952 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002953 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002954 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002956 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002958 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002965
Benjamin Petersonddd19492018-09-16 22:38:02 -07002966 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002967 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002969 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002970 && PyDict_CheckExact(f->f_builtins))
2971 {
Inada Naoki91234a12019-06-03 21:30:58 +09002972 OPCACHE_CHECK();
2973 if (co_opcache != NULL && co_opcache->optimized > 0) {
2974 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2975
2976 if (lg->globals_ver ==
2977 ((PyDictObject *)f->f_globals)->ma_version_tag
2978 && lg->builtins_ver ==
2979 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2980 {
2981 PyObject *ptr = lg->ptr;
2982 OPCACHE_STAT_GLOBAL_HIT();
2983 assert(ptr != NULL);
2984 Py_INCREF(ptr);
2985 PUSH(ptr);
2986 DISPATCH();
2987 }
2988 }
2989
2990 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002992 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 name);
2994 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002995 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002996 /* _PyDict_LoadGlobal() returns NULL without raising
2997 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002998 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002999 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01003000 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
Inada Naoki91234a12019-06-03 21:30:58 +09003003
3004 if (co_opcache != NULL) {
3005 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
3006
3007 if (co_opcache->optimized == 0) {
3008 /* Wasn't optimized before. */
3009 OPCACHE_STAT_GLOBAL_OPT();
3010 } else {
3011 OPCACHE_STAT_GLOBAL_MISS();
3012 }
3013
3014 co_opcache->optimized = 1;
3015 lg->globals_ver =
3016 ((PyDictObject *)f->f_globals)->ma_version_tag;
3017 lg->builtins_ver =
3018 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3019 lg->ptr = v; /* borrowed */
3020 }
3021
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003024 else {
3025 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003026
3027 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003028 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 v = PyObject_GetItem(f->f_globals, name);
3030 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003031 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003032 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003033 }
3034 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003035
Victor Stinnerb4efc962015-11-20 09:24:02 +01003036 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 v = PyObject_GetItem(f->f_builtins, name);
3038 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003039 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003040 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003041 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003042 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003043 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003045 }
3046 }
3047 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003051
Benjamin Petersonddd19492018-09-16 22:38:02 -07003052 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 PyObject *v = GETLOCAL(oparg);
3054 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 SETLOCAL(oparg, NULL);
3056 DISPATCH();
3057 }
3058 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003059 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 UNBOUNDLOCAL_ERROR_MSG,
3061 PyTuple_GetItem(co->co_varnames, oparg)
3062 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 goto error;
3064 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003065
Benjamin Petersonddd19492018-09-16 22:38:02 -07003066 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003068 PyObject *oldobj = PyCell_GET(cell);
3069 if (oldobj != NULL) {
3070 PyCell_SET(cell, NULL);
3071 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003072 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003073 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003074 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 goto error;
3076 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003077
Benjamin Petersonddd19492018-09-16 22:38:02 -07003078 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 PyObject *cell = freevars[oparg];
3080 Py_INCREF(cell);
3081 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003084
Benjamin Petersonddd19492018-09-16 22:38:02 -07003085 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003086 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003087 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003088 assert(locals);
3089 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3090 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3091 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3092 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3093 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003094 value = PyDict_GetItemWithError(locals, name);
3095 if (value != NULL) {
3096 Py_INCREF(value);
3097 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003098 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003099 goto error;
3100 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003101 }
3102 else {
3103 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003104 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003105 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003106 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003107 }
3108 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003109 }
3110 }
3111 if (!value) {
3112 PyObject *cell = freevars[oparg];
3113 value = PyCell_GET(cell);
3114 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003115 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003116 goto error;
3117 }
3118 Py_INCREF(value);
3119 }
3120 PUSH(value);
3121 DISPATCH();
3122 }
3123
Benjamin Petersonddd19492018-09-16 22:38:02 -07003124 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 PyObject *cell = freevars[oparg];
3126 PyObject *value = PyCell_GET(cell);
3127 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003128 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 Py_INCREF(value);
3132 PUSH(value);
3133 DISPATCH();
3134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003135
Benjamin Petersonddd19492018-09-16 22:38:02 -07003136 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 PyObject *v = POP();
3138 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003139 PyObject *oldobj = PyCell_GET(cell);
3140 PyCell_SET(cell, v);
3141 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 DISPATCH();
3143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003144
Benjamin Petersonddd19492018-09-16 22:38:02 -07003145 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003146 PyObject *str;
3147 PyObject *empty = PyUnicode_New(0, 0);
3148 if (empty == NULL) {
3149 goto error;
3150 }
3151 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3152 Py_DECREF(empty);
3153 if (str == NULL)
3154 goto error;
3155 while (--oparg >= 0) {
3156 PyObject *item = POP();
3157 Py_DECREF(item);
3158 }
3159 PUSH(str);
3160 DISPATCH();
3161 }
3162
Benjamin Petersonddd19492018-09-16 22:38:02 -07003163 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 PyObject *tup = PyTuple_New(oparg);
3165 if (tup == NULL)
3166 goto error;
3167 while (--oparg >= 0) {
3168 PyObject *item = POP();
3169 PyTuple_SET_ITEM(tup, oparg, item);
3170 }
3171 PUSH(tup);
3172 DISPATCH();
3173 }
3174
Benjamin Petersonddd19492018-09-16 22:38:02 -07003175 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 PyObject *list = PyList_New(oparg);
3177 if (list == NULL)
3178 goto error;
3179 while (--oparg >= 0) {
3180 PyObject *item = POP();
3181 PyList_SET_ITEM(list, oparg, item);
3182 }
3183 PUSH(list);
3184 DISPATCH();
3185 }
3186
Mark Shannon13bc1392020-01-23 09:25:17 +00003187 case TARGET(LIST_TO_TUPLE): {
3188 PyObject *list = POP();
3189 PyObject *tuple = PyList_AsTuple(list);
3190 Py_DECREF(list);
3191 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003192 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003193 }
3194 PUSH(tuple);
3195 DISPATCH();
3196 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003197
Mark Shannon13bc1392020-01-23 09:25:17 +00003198 case TARGET(LIST_EXTEND): {
3199 PyObject *iterable = POP();
3200 PyObject *list = PEEK(oparg);
3201 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3202 if (none_val == NULL) {
3203 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003204 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003205 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003206 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003207 _PyErr_Format(tstate, PyExc_TypeError,
3208 "Value after * must be an iterable, not %.200s",
3209 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003210 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003211 Py_DECREF(iterable);
3212 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003213 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003214 Py_DECREF(none_val);
3215 Py_DECREF(iterable);
3216 DISPATCH();
3217 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003218
Mark Shannon13bc1392020-01-23 09:25:17 +00003219 case TARGET(SET_UPDATE): {
3220 PyObject *iterable = POP();
3221 PyObject *set = PEEK(oparg);
3222 int err = _PySet_Update(set, iterable);
3223 Py_DECREF(iterable);
3224 if (err < 0) {
3225 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003226 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003227 DISPATCH();
3228 }
3229
Benjamin Petersonddd19492018-09-16 22:38:02 -07003230 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 PyObject *set = PySet_New(NULL);
3232 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003233 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 if (set == NULL)
3235 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003236 for (i = oparg; i > 0; i--) {
3237 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 if (err == 0)
3239 err = PySet_Add(set, item);
3240 Py_DECREF(item);
3241 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003242 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 if (err != 0) {
3244 Py_DECREF(set);
3245 goto error;
3246 }
3247 PUSH(set);
3248 DISPATCH();
3249 }
3250
Benjamin Petersonddd19492018-09-16 22:38:02 -07003251 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003252 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3254 if (map == NULL)
3255 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003256 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003257 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003258 PyObject *key = PEEK(2*i);
3259 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003260 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003261 if (err != 0) {
3262 Py_DECREF(map);
3263 goto error;
3264 }
3265 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003266
3267 while (oparg--) {
3268 Py_DECREF(POP());
3269 Py_DECREF(POP());
3270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 PUSH(map);
3272 DISPATCH();
3273 }
3274
Benjamin Petersonddd19492018-09-16 22:38:02 -07003275 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003276 _Py_IDENTIFIER(__annotations__);
3277 int err;
3278 PyObject *ann_dict;
3279 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003280 _PyErr_Format(tstate, PyExc_SystemError,
3281 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003282 goto error;
3283 }
3284 /* check if __annotations__ in locals()... */
3285 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003286 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003287 &PyId___annotations__);
3288 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003289 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003290 goto error;
3291 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003292 /* ...if not, create a new one */
3293 ann_dict = PyDict_New();
3294 if (ann_dict == NULL) {
3295 goto error;
3296 }
3297 err = _PyDict_SetItemId(f->f_locals,
3298 &PyId___annotations__, ann_dict);
3299 Py_DECREF(ann_dict);
3300 if (err != 0) {
3301 goto error;
3302 }
3303 }
3304 }
3305 else {
3306 /* do the same if locals() is not a dict */
3307 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3308 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003309 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003310 }
3311 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3312 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003313 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003314 goto error;
3315 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003316 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003317 ann_dict = PyDict_New();
3318 if (ann_dict == NULL) {
3319 goto error;
3320 }
3321 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3322 Py_DECREF(ann_dict);
3323 if (err != 0) {
3324 goto error;
3325 }
3326 }
3327 else {
3328 Py_DECREF(ann_dict);
3329 }
3330 }
3331 DISPATCH();
3332 }
3333
Benjamin Petersonddd19492018-09-16 22:38:02 -07003334 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003335 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003336 PyObject *map;
3337 PyObject *keys = TOP();
3338 if (!PyTuple_CheckExact(keys) ||
3339 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003340 _PyErr_SetString(tstate, PyExc_SystemError,
3341 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003342 goto error;
3343 }
3344 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3345 if (map == NULL) {
3346 goto error;
3347 }
3348 for (i = oparg; i > 0; i--) {
3349 int err;
3350 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3351 PyObject *value = PEEK(i + 1);
3352 err = PyDict_SetItem(map, key, value);
3353 if (err != 0) {
3354 Py_DECREF(map);
3355 goto error;
3356 }
3357 }
3358
3359 Py_DECREF(POP());
3360 while (oparg--) {
3361 Py_DECREF(POP());
3362 }
3363 PUSH(map);
3364 DISPATCH();
3365 }
3366
Mark Shannon8a4cd702020-01-27 09:57:45 +00003367 case TARGET(DICT_UPDATE): {
3368 PyObject *update = POP();
3369 PyObject *dict = PEEK(oparg);
3370 if (PyDict_Update(dict, update) < 0) {
3371 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3372 _PyErr_Format(tstate, PyExc_TypeError,
3373 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003374 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003375 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003376 Py_DECREF(update);
3377 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003378 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003379 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003380 DISPATCH();
3381 }
3382
Mark Shannon8a4cd702020-01-27 09:57:45 +00003383 case TARGET(DICT_MERGE): {
3384 PyObject *update = POP();
3385 PyObject *dict = PEEK(oparg);
3386
3387 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3388 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3389 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003390 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003391 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003392 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003393 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003394 DISPATCH();
3395 }
3396
Benjamin Petersonddd19492018-09-16 22:38:02 -07003397 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003398 PyObject *value = TOP();
3399 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 PyObject *map;
3401 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003402 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003403 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003405 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 Py_DECREF(value);
3407 Py_DECREF(key);
3408 if (err != 0)
3409 goto error;
3410 PREDICT(JUMP_ABSOLUTE);
3411 DISPATCH();
3412 }
3413
Benjamin Petersonddd19492018-09-16 22:38:02 -07003414 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 PyObject *name = GETITEM(names, oparg);
3416 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003417
3418 PyTypeObject *type = Py_TYPE(owner);
3419 PyObject *res;
3420 PyObject **dictptr;
3421 PyObject *dict;
3422 _PyOpCodeOpt_LoadAttr *la;
3423
3424 OPCACHE_STAT_ATTR_TOTAL();
3425
3426 OPCACHE_CHECK();
3427 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3428 {
3429 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003430 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003431 la = &co_opcache->u.la;
3432 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3433 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003434 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3435 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3436 // so ~offset is strictly < -1 (assuming 2's complement).
3437 if (la->hint < -1) {
3438 // Even faster path -- slot hint.
3439 Py_ssize_t offset = ~la->hint;
3440 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3441 char *addr = (char *)owner + offset;
3442 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003443 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003444 Py_INCREF(res);
3445 SET_TOP(res);
3446 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003447 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003448 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003449 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3450 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003451 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003452 // Fast path for dict.
3453 assert(type->tp_dict != NULL);
3454 assert(type->tp_dictoffset > 0);
3455
3456 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3457 dict = *dictptr;
3458 if (dict != NULL && PyDict_CheckExact(dict)) {
3459 Py_ssize_t hint = la->hint;
3460 Py_INCREF(dict);
3461 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003462 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003463 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003464 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003465 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003466 if (la->hint == hint && hint >= 0) {
3467 // Our hint has helped -- cache hit.
3468 OPCACHE_STAT_ATTR_HIT();
3469 } else {
3470 // The hint we provided didn't work.
3471 // Maybe next time?
3472 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3473 }
3474
3475 Py_INCREF(res);
3476 SET_TOP(res);
3477 Py_DECREF(owner);
3478 Py_DECREF(dict);
3479 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003480 }
3481 else {
3482 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003483 // This attribute can be missing sometimes;
3484 // we don't want to optimize this lookup.
3485 OPCACHE_DEOPT_LOAD_ATTR();
3486 Py_DECREF(dict);
3487 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003488 }
3489 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003490 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3491 OPCACHE_DEOPT_LOAD_ATTR();
3492 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003493 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003494 }
3495 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003496 // The type of the object has either been updated,
3497 // or is different. Maybe it will stabilize?
3498 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3499 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003500 OPCACHE_STAT_ATTR_MISS();
3501 }
3502
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003503 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003504 type->tp_getattro == PyObject_GenericGetAttr)
3505 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003506 if (type->tp_dict == NULL) {
3507 if (PyType_Ready(type) < 0) {
3508 Py_DECREF(owner);
3509 SET_TOP(NULL);
3510 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003511 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003512 }
3513 PyObject *descr = _PyType_Lookup(type, name);
3514 if (descr != NULL) {
3515 // We found an attribute with a data-like descriptor.
3516 PyTypeObject *dtype = Py_TYPE(descr);
3517 if (dtype == &PyMemberDescr_Type) { // It's a slot
3518 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3519 struct PyMemberDef *dmem = member->d_member;
3520 if (dmem->type == T_OBJECT_EX) {
3521 Py_ssize_t offset = dmem->offset;
3522 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003523
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003524 if (co_opcache->optimized == 0) {
3525 // First time we optimize this opcode.
3526 OPCACHE_STAT_ATTR_OPT();
3527 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3528 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3529 }
3530
3531 la = &co_opcache->u.la;
3532 la->type = type;
3533 la->tp_version_tag = type->tp_version_tag;
3534 la->hint = ~offset;
3535
3536 char *addr = (char *)owner + offset;
3537 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003538 if (res != NULL) {
3539 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003540 Py_DECREF(owner);
3541 SET_TOP(res);
3542
Pablo Galindo109826c2020-10-20 06:22:44 +01003543 DISPATCH();
3544 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003545 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003546 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003547 // Else it's a slot of a different type. We don't handle those.
3548 }
3549 // Else it's some other kind of descriptor that we don't handle.
3550 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003551 }
3552 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003553 // We found an instance with a __dict__.
3554 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3555 dict = *dictptr;
3556
3557 if (dict != NULL && PyDict_CheckExact(dict)) {
3558 Py_INCREF(dict);
3559 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003560 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003561 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3562 if (res != NULL) {
3563 Py_INCREF(res);
3564 Py_DECREF(dict);
3565 Py_DECREF(owner);
3566 SET_TOP(res);
3567
3568 if (co_opcache->optimized == 0) {
3569 // First time we optimize this opcode.
3570 OPCACHE_STAT_ATTR_OPT();
3571 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3572 }
3573
3574 la = &co_opcache->u.la;
3575 la->type = type;
3576 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003577 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003578 la->hint = hint;
3579
3580 DISPATCH();
3581 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003582 else {
3583 _PyErr_Clear(tstate);
3584 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003585 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003586 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003587 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003588 OPCACHE_DEOPT_LOAD_ATTR();
3589 }
3590 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003591 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003592 OPCACHE_DEOPT_LOAD_ATTR();
3593 }
3594 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3595 OPCACHE_DEOPT_LOAD_ATTR();
3596 }
3597 }
3598
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003599 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003600 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003601 Py_DECREF(owner);
3602 SET_TOP(res);
3603 if (res == NULL)
3604 goto error;
3605 DISPATCH();
3606 }
3607
Benjamin Petersonddd19492018-09-16 22:38:02 -07003608 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003609 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610 PyObject *right = POP();
3611 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003612 PyObject *res = PyObject_RichCompare(left, right, oparg);
3613 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 Py_DECREF(left);
3615 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003616 if (res == NULL)
3617 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 PREDICT(POP_JUMP_IF_FALSE);
3619 PREDICT(POP_JUMP_IF_TRUE);
3620 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003621 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003622
Mark Shannon9af0e472020-01-14 10:12:45 +00003623 case TARGET(IS_OP): {
3624 PyObject *right = POP();
3625 PyObject *left = TOP();
Victor Stinner09bbebe2021-04-11 00:17:39 +02003626 int res = Py_Is(left, right) ^ oparg;
Mark Shannon9af0e472020-01-14 10:12:45 +00003627 PyObject *b = res ? Py_True : Py_False;
3628 Py_INCREF(b);
3629 SET_TOP(b);
3630 Py_DECREF(left);
3631 Py_DECREF(right);
3632 PREDICT(POP_JUMP_IF_FALSE);
3633 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003634 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003635 }
3636
3637 case TARGET(CONTAINS_OP): {
3638 PyObject *right = POP();
3639 PyObject *left = POP();
3640 int res = PySequence_Contains(right, left);
3641 Py_DECREF(left);
3642 Py_DECREF(right);
3643 if (res < 0) {
3644 goto error;
3645 }
3646 PyObject *b = (res^oparg) ? Py_True : Py_False;
3647 Py_INCREF(b);
3648 PUSH(b);
3649 PREDICT(POP_JUMP_IF_FALSE);
3650 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003651 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003652 }
3653
3654#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3655 "BaseException is not allowed"
3656
3657 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3658 PyObject *right = POP();
3659 PyObject *left = POP();
3660 if (PyTuple_Check(right)) {
3661 Py_ssize_t i, length;
3662 length = PyTuple_GET_SIZE(right);
3663 for (i = 0; i < length; i++) {
3664 PyObject *exc = PyTuple_GET_ITEM(right, i);
3665 if (!PyExceptionClass_Check(exc)) {
3666 _PyErr_SetString(tstate, PyExc_TypeError,
3667 CANNOT_CATCH_MSG);
3668 Py_DECREF(left);
3669 Py_DECREF(right);
3670 goto error;
3671 }
3672 }
3673 }
3674 else {
3675 if (!PyExceptionClass_Check(right)) {
3676 _PyErr_SetString(tstate, PyExc_TypeError,
3677 CANNOT_CATCH_MSG);
3678 Py_DECREF(left);
3679 Py_DECREF(right);
3680 goto error;
3681 }
3682 }
3683 int res = PyErr_GivenExceptionMatches(left, right);
3684 Py_DECREF(left);
3685 Py_DECREF(right);
3686 if (res > 0) {
3687 /* Exception matches -- Do nothing */;
3688 }
3689 else if (res == 0) {
3690 JUMPTO(oparg);
3691 }
3692 else {
3693 goto error;
3694 }
3695 DISPATCH();
3696 }
3697
Benjamin Petersonddd19492018-09-16 22:38:02 -07003698 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003699 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003700 PyObject *fromlist = POP();
3701 PyObject *level = TOP();
3702 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003703 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003704 Py_DECREF(level);
3705 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 SET_TOP(res);
3707 if (res == NULL)
3708 goto error;
3709 DISPATCH();
3710 }
3711
Benjamin Petersonddd19492018-09-16 22:38:02 -07003712 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003713 PyObject *from = POP(), *locals;
3714 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003715 if (PyFrame_FastToLocalsWithError(f) < 0) {
3716 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003717 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003718 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003719
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003720 locals = f->f_locals;
3721 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003722 _PyErr_SetString(tstate, PyExc_SystemError,
3723 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003724 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003725 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003727 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003729 Py_DECREF(from);
3730 if (err != 0)
3731 goto error;
3732 DISPATCH();
3733 }
Guido van Rossum25831651993-05-19 14:50:45 +00003734
Benjamin Petersonddd19492018-09-16 22:38:02 -07003735 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003736 PyObject *name = GETITEM(names, oparg);
3737 PyObject *from = TOP();
3738 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003739 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003740 PUSH(res);
3741 if (res == NULL)
3742 goto error;
3743 DISPATCH();
3744 }
Thomas Wouters52152252000-08-17 22:55:00 +00003745
Benjamin Petersonddd19492018-09-16 22:38:02 -07003746 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003748 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003750
Benjamin Petersonddd19492018-09-16 22:38:02 -07003751 case TARGET(POP_JUMP_IF_FALSE): {
3752 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003753 PyObject *cond = POP();
3754 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003755 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003756 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003757 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003759 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003760 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003762 CHECK_EVAL_BREAKER();
Mark Shannon4958f5d2021-03-24 17:56:12 +00003763 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003765 err = PyObject_IsTrue(cond);
3766 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003768 ;
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003769 else if (err == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003771 CHECK_EVAL_BREAKER();
3772 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003774 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003777
Benjamin Petersonddd19492018-09-16 22:38:02 -07003778 case TARGET(POP_JUMP_IF_TRUE): {
3779 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 PyObject *cond = POP();
3781 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003782 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003783 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003784 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003786 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003789 CHECK_EVAL_BREAKER();
Mark Shannon4958f5d2021-03-24 17:56:12 +00003790 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003792 err = PyObject_IsTrue(cond);
3793 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003796 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 else if (err == 0)
3799 ;
3800 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003803 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003804
Benjamin Petersonddd19492018-09-16 22:38:02 -07003805 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003806 PyObject *cond = TOP();
3807 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003808 if (Py_IsTrue(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003809 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003810 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003811 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003813 if (Py_IsFalse(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003815 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003817 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003819 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003820 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 }
3822 else if (err == 0)
3823 JUMPTO(oparg);
3824 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003825 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003827 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003828
Benjamin Petersonddd19492018-09-16 22:38:02 -07003829 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003830 PyObject *cond = TOP();
3831 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003832 if (Py_IsFalse(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003833 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003834 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003835 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003837 if (Py_IsTrue(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003839 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003841 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 JUMPTO(oparg);
3844 }
3845 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003846 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003847 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 }
3849 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003850 goto error;
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
Benjamin Petersonddd19492018-09-16 22:38:02 -07003854 case TARGET(JUMP_ABSOLUTE): {
3855 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003857 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003859 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003860
Brandt Bucher145bf262021-02-26 14:51:55 -08003861 case TARGET(GET_LEN): {
3862 // PUSH(len(TOS))
3863 Py_ssize_t len_i = PyObject_Length(TOP());
3864 if (len_i < 0) {
3865 goto error;
3866 }
3867 PyObject *len_o = PyLong_FromSsize_t(len_i);
3868 if (len_o == NULL) {
3869 goto error;
3870 }
3871 PUSH(len_o);
3872 DISPATCH();
3873 }
3874
3875 case TARGET(MATCH_CLASS): {
3876 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3877 // attributes. On failure, set TOS to False.
3878 PyObject *names = POP();
3879 PyObject *type = TOP();
3880 PyObject *subject = SECOND();
3881 assert(PyTuple_CheckExact(names));
3882 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3883 Py_DECREF(names);
3884 if (attrs) {
3885 // Success!
3886 assert(PyTuple_CheckExact(attrs));
3887 Py_DECREF(subject);
3888 SET_SECOND(attrs);
3889 }
3890 else if (_PyErr_Occurred(tstate)) {
3891 goto error;
3892 }
3893 Py_DECREF(type);
3894 SET_TOP(PyBool_FromLong(!!attrs));
3895 DISPATCH();
3896 }
3897
3898 case TARGET(MATCH_MAPPING): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003899 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003900 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
3901 PyObject *res = match ? Py_True : Py_False;
3902 Py_INCREF(res);
3903 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003904 DISPATCH();
3905 }
3906
3907 case TARGET(MATCH_SEQUENCE): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003908 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003909 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
3910 PyObject *res = match ? Py_True : Py_False;
3911 Py_INCREF(res);
3912 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003913 DISPATCH();
3914 }
3915
3916 case TARGET(MATCH_KEYS): {
3917 // On successful match for all keys, PUSH(values) and PUSH(True).
3918 // Otherwise, PUSH(None) and PUSH(False).
3919 PyObject *keys = TOP();
3920 PyObject *subject = SECOND();
3921 PyObject *values_or_none = match_keys(tstate, subject, keys);
3922 if (values_or_none == NULL) {
3923 goto error;
3924 }
3925 PUSH(values_or_none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02003926 if (Py_IsNone(values_or_none)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08003927 Py_INCREF(Py_False);
3928 PUSH(Py_False);
3929 DISPATCH();
3930 }
3931 assert(PyTuple_CheckExact(values_or_none));
3932 Py_INCREF(Py_True);
3933 PUSH(Py_True);
3934 DISPATCH();
3935 }
3936
3937 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3938 // rest = dict(TOS1)
3939 // for key in TOS:
3940 // del rest[key]
3941 // SET_TOP(rest)
3942 PyObject *keys = TOP();
3943 PyObject *subject = SECOND();
3944 PyObject *rest = PyDict_New();
3945 if (rest == NULL || PyDict_Update(rest, subject)) {
3946 Py_XDECREF(rest);
3947 goto error;
3948 }
3949 // This may seem a bit inefficient, but keys is rarely big enough to
3950 // actually impact runtime.
3951 assert(PyTuple_CheckExact(keys));
3952 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3953 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3954 Py_DECREF(rest);
3955 goto error;
3956 }
3957 }
3958 Py_DECREF(keys);
3959 SET_TOP(rest);
3960 DISPATCH();
3961 }
3962
Benjamin Petersonddd19492018-09-16 22:38:02 -07003963 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003965 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003966 PyObject *iter = PyObject_GetIter(iterable);
3967 Py_DECREF(iterable);
3968 SET_TOP(iter);
3969 if (iter == NULL)
3970 goto error;
3971 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003972 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003973 DISPATCH();
3974 }
3975
Benjamin Petersonddd19492018-09-16 22:38:02 -07003976 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003977 /* before: [obj]; after [getiter(obj)] */
3978 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003979 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003980 if (PyCoro_CheckExact(iterable)) {
3981 /* `iterable` is a coroutine */
3982 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3983 /* and it is used in a 'yield from' expression of a
3984 regular generator. */
3985 Py_DECREF(iterable);
3986 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003987 _PyErr_SetString(tstate, PyExc_TypeError,
3988 "cannot 'yield from' a coroutine object "
3989 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003990 goto error;
3991 }
3992 }
3993 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003994 /* `iterable` is not a generator. */
3995 iter = PyObject_GetIter(iterable);
3996 Py_DECREF(iterable);
3997 SET_TOP(iter);
3998 if (iter == NULL)
3999 goto error;
4000 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004001 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004002 DISPATCH();
4003 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004004
Benjamin Petersonddd19492018-09-16 22:38:02 -07004005 case TARGET(FOR_ITER): {
4006 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004008 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004009 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004010 if (next != NULL) {
4011 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 PREDICT(STORE_FAST);
4013 PREDICT(UNPACK_SEQUENCE);
4014 DISPATCH();
4015 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004016 if (_PyErr_Occurred(tstate)) {
4017 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004018 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004019 }
4020 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004021 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004022 }
4023 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 }
4025 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004026 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004027 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 JUMPBY(oparg);
4029 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004030 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004031
Benjamin Petersonddd19492018-09-16 22:38:02 -07004032 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004033 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 STACK_LEVEL());
4035 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004037
Benjamin Petersonddd19492018-09-16 22:38:02 -07004038 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004039 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004040 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004041 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004042 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004043 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004044 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004045 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004046 }
4047 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4048 if (exit == NULL) {
4049 Py_DECREF(enter);
4050 goto error;
4051 }
Yury Selivanov75445082015-05-11 22:57:16 -04004052 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004053 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004054 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004055 Py_DECREF(enter);
4056 if (res == NULL)
4057 goto error;
4058 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004059 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004060 DISPATCH();
4061 }
4062
Benjamin Petersonddd19492018-09-16 22:38:02 -07004063 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004064 PyObject *res = POP();
4065 /* Setup the finally block before pushing the result
4066 of __aenter__ on the stack. */
4067 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4068 STACK_LEVEL());
4069 PUSH(res);
4070 DISPATCH();
4071 }
4072
Benjamin Petersonddd19492018-09-16 22:38:02 -07004073 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004074 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004075 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004076 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004077 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004078 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004079 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004080 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004081 }
4082 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004083 if (exit == NULL) {
4084 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004085 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004087 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004088 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004089 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004090 Py_DECREF(enter);
4091 if (res == NULL)
4092 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 /* Setup the finally block before pushing the result
4094 of __enter__ on the stack. */
4095 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4096 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004097
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004098 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 DISPATCH();
4100 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004101
Mark Shannonfee55262019-11-21 09:11:43 +00004102 case TARGET(WITH_EXCEPT_START): {
4103 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004105 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4106 - SEVENTH: the context.__exit__ bound method
4107 We call SEVENTH(TOP, SECOND, THIRD).
4108 Then we push again the TOP exception and the __exit__
4109 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004112 PyObject *exc, *val, *tb, *res;
4113
Victor Stinner842cfff2016-12-01 14:45:31 +01004114 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004115 val = SECOND();
4116 tb = THIRD();
Victor Stinner09bbebe2021-04-11 00:17:39 +02004117 assert(!Py_IsNone(exc));
Mark Shannonfee55262019-11-21 09:11:43 +00004118 assert(!PyLong_Check(exc));
4119 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004120 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004121 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004122 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004123 if (res == NULL)
4124 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004125
Yury Selivanov75445082015-05-11 22:57:16 -04004126 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004127 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004129
Benjamin Petersonddd19492018-09-16 22:38:02 -07004130 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004131 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004132 PyObject *name = GETITEM(names, oparg);
4133 PyObject *obj = TOP();
4134 PyObject *meth = NULL;
4135
4136 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4137
Yury Selivanovf2392132016-12-13 19:03:51 -05004138 if (meth == NULL) {
4139 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004140 goto error;
4141 }
4142
4143 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004144 /* We can bypass temporary bound method object.
4145 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004146
INADA Naoki015bce62017-01-16 17:23:30 +09004147 meth | self | arg1 | ... | argN
4148 */
4149 SET_TOP(meth);
4150 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004151 }
4152 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004153 /* meth is not an unbound method (but a regular attr, or
4154 something was returned by a descriptor protocol). Set
4155 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004156 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004157
4158 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004159 */
INADA Naoki015bce62017-01-16 17:23:30 +09004160 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004161 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004162 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004163 }
4164 DISPATCH();
4165 }
4166
Benjamin Petersonddd19492018-09-16 22:38:02 -07004167 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004168 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004169 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004170
4171 sp = stack_pointer;
4172
INADA Naoki015bce62017-01-16 17:23:30 +09004173 meth = PEEK(oparg + 2);
4174 if (meth == NULL) {
4175 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4176 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004177
4178 Stack layout:
4179
INADA Naoki015bce62017-01-16 17:23:30 +09004180 ... | NULL | callable | arg1 | ... | argN
4181 ^- TOP()
4182 ^- (-oparg)
4183 ^- (-oparg-1)
4184 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004185
Ville Skyttä49b27342017-08-03 09:00:59 +03004186 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004187 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004188 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004189 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004190 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004191 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004192 }
4193 else {
4194 /* This is a method call. Stack layout:
4195
INADA Naoki015bce62017-01-16 17:23:30 +09004196 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004197 ^- TOP()
4198 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004199 ^- (-oparg-1)
4200 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004201
INADA Naoki015bce62017-01-16 17:23:30 +09004202 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004203 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004204 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004205 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004206 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004207 stack_pointer = sp;
4208 }
4209
4210 PUSH(res);
4211 if (res == NULL)
4212 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004213 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004214 DISPATCH();
4215 }
4216
Benjamin Petersonddd19492018-09-16 22:38:02 -07004217 case TARGET(CALL_FUNCTION): {
4218 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004219 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004221 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004223 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004224 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004225 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004226 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004227 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004228 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004230
Benjamin Petersonddd19492018-09-16 22:38:02 -07004231 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004232 PyObject **sp, *res, *names;
4233
4234 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004235 assert(PyTuple_Check(names));
4236 assert(PyTuple_GET_SIZE(names) <= oparg);
4237 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004239 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004241 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004242 Py_DECREF(names);
4243
4244 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004245 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004246 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004247 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004248 DISPATCH();
4249 }
4250
Benjamin Petersonddd19492018-09-16 22:38:02 -07004251 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004252 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004253 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004254 if (oparg & 0x01) {
4255 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004256 if (!PyDict_CheckExact(kwargs)) {
4257 PyObject *d = PyDict_New();
4258 if (d == NULL)
4259 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004260 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004261 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004262 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004263 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004264 goto error;
4265 }
4266 Py_DECREF(kwargs);
4267 kwargs = d;
4268 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004269 assert(PyDict_CheckExact(kwargs));
4270 }
4271 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004272 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004273 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004274 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004275 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004276 goto error;
4277 }
4278 Py_SETREF(callargs, PySequence_Tuple(callargs));
4279 if (callargs == NULL) {
4280 goto error;
4281 }
4282 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004283 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004284
Mark Shannon8e1b4062021-03-05 14:45:50 +00004285 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004286 Py_DECREF(func);
4287 Py_DECREF(callargs);
4288 Py_XDECREF(kwargs);
4289
4290 SET_TOP(result);
4291 if (result == NULL) {
4292 goto error;
4293 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004294 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004295 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004297
Benjamin Petersonddd19492018-09-16 22:38:02 -07004298 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004299 PyObject *qualname = POP();
4300 PyObject *codeobj = POP();
4301 PyFunctionObject *func = (PyFunctionObject *)
4302 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004303
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004304 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004305 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004306 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004309
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004310 if (oparg & 0x08) {
4311 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004312 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004313 }
4314 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004315 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004316 func->func_annotations = POP();
4317 }
4318 if (oparg & 0x02) {
4319 assert(PyDict_CheckExact(TOP()));
4320 func->func_kwdefaults = POP();
4321 }
4322 if (oparg & 0x01) {
4323 assert(PyTuple_CheckExact(TOP()));
4324 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004326
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004327 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004328 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004330
Benjamin Petersonddd19492018-09-16 22:38:02 -07004331 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004332 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004334 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004336 step = NULL;
4337 stop = POP();
4338 start = TOP();
4339 slice = PySlice_New(start, stop, step);
4340 Py_DECREF(start);
4341 Py_DECREF(stop);
4342 Py_XDECREF(step);
4343 SET_TOP(slice);
4344 if (slice == NULL)
4345 goto error;
4346 DISPATCH();
4347 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004348
Benjamin Petersonddd19492018-09-16 22:38:02 -07004349 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004350 /* Handles f-string value formatting. */
4351 PyObject *result;
4352 PyObject *fmt_spec;
4353 PyObject *value;
4354 PyObject *(*conv_fn)(PyObject *);
4355 int which_conversion = oparg & FVC_MASK;
4356 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4357
4358 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004359 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004360
4361 /* See if any conversion is specified. */
4362 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004363 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004364 case FVC_STR: conv_fn = PyObject_Str; break;
4365 case FVC_REPR: conv_fn = PyObject_Repr; break;
4366 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004367 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004368 _PyErr_Format(tstate, PyExc_SystemError,
4369 "unexpected conversion flag %d",
4370 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004371 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004372 }
4373
4374 /* If there's a conversion function, call it and replace
4375 value with that result. Otherwise, just use value,
4376 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004377 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004378 result = conv_fn(value);
4379 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004380 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004381 Py_XDECREF(fmt_spec);
4382 goto error;
4383 }
4384 value = result;
4385 }
4386
4387 /* If value is a unicode object, and there's no fmt_spec,
4388 then we know the result of format(value) is value
4389 itself. In that case, skip calling format(). I plan to
4390 move this optimization in to PyObject_Format()
4391 itself. */
4392 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4393 /* Do nothing, just transfer ownership to result. */
4394 result = value;
4395 } else {
4396 /* Actually call format(). */
4397 result = PyObject_Format(value, fmt_spec);
4398 Py_DECREF(value);
4399 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004400 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004401 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004402 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004403 }
4404
Eric V. Smith135d5f42016-02-05 18:23:08 -05004405 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004406 DISPATCH();
4407 }
4408
Brandt Bucher0ad1e032021-05-02 13:02:10 -07004409 case TARGET(ROT_N): {
4410 PyObject *top = TOP();
4411 memmove(&PEEK(oparg - 1), &PEEK(oparg),
4412 sizeof(PyObject*) * (oparg - 1));
4413 PEEK(oparg) = top;
4414 DISPATCH();
4415 }
4416
Benjamin Petersonddd19492018-09-16 22:38:02 -07004417 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004418 int oldoparg = oparg;
4419 NEXTOPARG();
4420 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004422 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004423
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004424
Antoine Pitrou042b1282010-08-13 21:15:58 +00004425#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004427#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 default:
4429 fprintf(stderr,
4430 "XXX lineno: %d, opcode: %d\n",
4431 PyFrame_GetLineNumber(f),
4432 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004433 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004434 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004437
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004438 /* This should never be reached. Every opcode should end with DISPATCH()
4439 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004440 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004442error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004443 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004444#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004445 if (!_PyErr_Occurred(tstate)) {
4446 _PyErr_SetString(tstate, PyExc_SystemError,
4447 "error return without exception set");
4448 }
Victor Stinner365b6932013-07-12 00:11:58 +02004449#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004450 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004451#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004453 /* Log traceback info. */
4454 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004455
Mark Shannoncb9879b2020-07-17 11:44:23 +01004456 if (tstate->c_tracefunc != NULL) {
4457 /* Make sure state is set to FRAME_EXECUTING for tracing */
4458 assert(f->f_state == FRAME_EXECUTING);
4459 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004460 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004461 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004462 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004463exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004464 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004465 /* Unwind stacks if an exception occurred */
4466 while (f->f_iblock > 0) {
4467 /* Pop the current block. */
4468 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 if (b->b_type == EXCEPT_HANDLER) {
4471 UNWIND_EXCEPT_HANDLER(b);
4472 continue;
4473 }
4474 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004475 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 PyObject *exc, *val, *tb;
4477 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004478 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004480 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004481 PUSH(exc_info->exc_traceback);
4482 PUSH(exc_info->exc_value);
4483 if (exc_info->exc_type != NULL) {
4484 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 }
4486 else {
4487 Py_INCREF(Py_None);
4488 PUSH(Py_None);
4489 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004490 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 /* Make the raw exception data
4492 available to the handler,
4493 so a program can emulate the
4494 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004495 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004496 if (tb != NULL)
4497 PyException_SetTraceback(val, tb);
4498 else
4499 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004501 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004503 exc_info->exc_value = val;
4504 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (tb == NULL)
4506 tb = Py_None;
4507 Py_INCREF(tb);
4508 PUSH(tb);
4509 PUSH(val);
4510 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004512 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004513 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004514 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 }
4516 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004517
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004518 /* End the loop as we still have an error */
4519 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004521
Pablo Galindof00828a2019-05-09 16:52:02 +01004522 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 /* Pop remaining stack entries. */
4526 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004527 PyObject *o = POP();
4528 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004530 f->f_stackdepth = 0;
4531 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004532exiting:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004533 if (trace_info.cframe.use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004534 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004535 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004536 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004537 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 }
4539 }
4540 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004541 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004542 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004543 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 }
4545 }
4546 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004549exit_eval_frame:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004550 /* Restore previous cframe */
4551 tstate->cframe = trace_info.cframe.previous;
4552 tstate->cframe->use_tracing = trace_info.cframe.use_tracing;
4553
Łukasz Langaa785c872016-09-09 17:37:37 -07004554 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4555 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004556 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004558
Victor Stinner0b72b232020-03-12 23:18:39 +01004559 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004560}
4561
Benjamin Petersonb204a422011-06-05 22:04:07 -05004562static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004563format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004564 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004565{
4566 int err;
4567 Py_ssize_t len = PyList_GET_SIZE(names);
4568 PyObject *name_str, *comma, *tail, *tmp;
4569
4570 assert(PyList_CheckExact(names));
4571 assert(len >= 1);
4572 /* Deal with the joys of natural language. */
4573 switch (len) {
4574 case 1:
4575 name_str = PyList_GET_ITEM(names, 0);
4576 Py_INCREF(name_str);
4577 break;
4578 case 2:
4579 name_str = PyUnicode_FromFormat("%U and %U",
4580 PyList_GET_ITEM(names, len - 2),
4581 PyList_GET_ITEM(names, len - 1));
4582 break;
4583 default:
4584 tail = PyUnicode_FromFormat(", %U, and %U",
4585 PyList_GET_ITEM(names, len - 2),
4586 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004587 if (tail == NULL)
4588 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004589 /* Chop off the last two objects in the list. This shouldn't actually
4590 fail, but we can't be too careful. */
4591 err = PyList_SetSlice(names, len - 2, len, NULL);
4592 if (err == -1) {
4593 Py_DECREF(tail);
4594 return;
4595 }
4596 /* Stitch everything up into a nice comma-separated list. */
4597 comma = PyUnicode_FromString(", ");
4598 if (comma == NULL) {
4599 Py_DECREF(tail);
4600 return;
4601 }
4602 tmp = PyUnicode_Join(comma, names);
4603 Py_DECREF(comma);
4604 if (tmp == NULL) {
4605 Py_DECREF(tail);
4606 return;
4607 }
4608 name_str = PyUnicode_Concat(tmp, tail);
4609 Py_DECREF(tmp);
4610 Py_DECREF(tail);
4611 break;
4612 }
4613 if (name_str == NULL)
4614 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004615 _PyErr_Format(tstate, PyExc_TypeError,
4616 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004617 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004618 len,
4619 kind,
4620 len == 1 ? "" : "s",
4621 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004622 Py_DECREF(name_str);
4623}
4624
4625static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004626missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4627 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004628 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004629{
Victor Stinner74319ae2016-08-25 00:04:09 +02004630 Py_ssize_t i, j = 0;
4631 Py_ssize_t start, end;
4632 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004633 const char *kind = positional ? "positional" : "keyword-only";
4634 PyObject *missing_names;
4635
4636 /* Compute the names of the arguments that are missing. */
4637 missing_names = PyList_New(missing);
4638 if (missing_names == NULL)
4639 return;
4640 if (positional) {
4641 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004642 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004643 }
4644 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004645 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004646 end = start + co->co_kwonlyargcount;
4647 }
4648 for (i = start; i < end; i++) {
4649 if (GETLOCAL(i) == NULL) {
4650 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4651 PyObject *name = PyObject_Repr(raw);
4652 if (name == NULL) {
4653 Py_DECREF(missing_names);
4654 return;
4655 }
4656 PyList_SET_ITEM(missing_names, j++, name);
4657 }
4658 }
4659 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004660 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004661 Py_DECREF(missing_names);
4662}
4663
4664static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004665too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004666 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004667 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004668{
4669 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004670 Py_ssize_t kwonly_given = 0;
4671 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004672 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004673 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004674
Benjamin Petersone109c702011-06-24 09:37:26 -05004675 assert((co->co_flags & CO_VARARGS) == 0);
4676 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004677 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004678 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004679 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004680 }
4681 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004682 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004683 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004684 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004685 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004686 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004687 }
4688 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004689 plural = (co_argcount != 1);
4690 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004691 }
4692 if (sig == NULL)
4693 return;
4694 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004695 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4696 kwonly_sig = PyUnicode_FromFormat(format,
4697 given != 1 ? "s" : "",
4698 kwonly_given,
4699 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004700 if (kwonly_sig == NULL) {
4701 Py_DECREF(sig);
4702 return;
4703 }
4704 }
4705 else {
4706 /* This will not fail. */
4707 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004708 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004709 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004710 _PyErr_Format(tstate, PyExc_TypeError,
4711 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004712 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004713 sig,
4714 plural ? "s" : "",
4715 given,
4716 kwonly_sig,
4717 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004718 Py_DECREF(sig);
4719 Py_DECREF(kwonly_sig);
4720}
4721
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004722static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004723positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004724 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004725 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004726{
4727 int posonly_conflicts = 0;
4728 PyObject* posonly_names = PyList_New(0);
4729
4730 for(int k=0; k < co->co_posonlyargcount; k++){
4731 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4732
4733 for (int k2=0; k2<kwcount; k2++){
4734 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004735 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004736 if (kwname == posonly_name){
4737 if(PyList_Append(posonly_names, kwname) != 0) {
4738 goto fail;
4739 }
4740 posonly_conflicts++;
4741 continue;
4742 }
4743
4744 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4745
4746 if ( cmp > 0) {
4747 if(PyList_Append(posonly_names, kwname) != 0) {
4748 goto fail;
4749 }
4750 posonly_conflicts++;
4751 } else if (cmp < 0) {
4752 goto fail;
4753 }
4754
4755 }
4756 }
4757 if (posonly_conflicts) {
4758 PyObject* comma = PyUnicode_FromString(", ");
4759 if (comma == NULL) {
4760 goto fail;
4761 }
4762 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4763 Py_DECREF(comma);
4764 if (error_names == NULL) {
4765 goto fail;
4766 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004767 _PyErr_Format(tstate, PyExc_TypeError,
4768 "%U() got some positional-only arguments passed"
4769 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004770 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004771 Py_DECREF(error_names);
4772 goto fail;
4773 }
4774
4775 Py_DECREF(posonly_names);
4776 return 0;
4777
4778fail:
4779 Py_XDECREF(posonly_names);
4780 return 1;
4781
4782}
4783
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004784
Mark Shannon0332e562021-02-01 10:42:03 +00004785PyFrameObject *
4786_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004787 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004788 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004789 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004790{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004791 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004792
Mark Shannond6c33fb2021-01-29 13:24:55 +00004793 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4794 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004795 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004796
Victor Stinnerc7020012016-08-16 23:40:29 +02004797 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004798 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004799 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004801 }
Victor Stinner232dda62020-06-04 15:19:02 +02004802 PyObject **fastlocals = f->f_localsplus;
4803 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004804
Victor Stinnerc7020012016-08-16 23:40:29 +02004805 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004806 PyObject *kwdict;
4807 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004808 if (co->co_flags & CO_VARKEYWORDS) {
4809 kwdict = PyDict_New();
4810 if (kwdict == NULL)
4811 goto fail;
4812 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004813 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004814 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004815 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004816 SETLOCAL(i, kwdict);
4817 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004818 else {
4819 kwdict = NULL;
4820 }
4821
Pablo Galindocd74e662019-06-01 18:08:04 +01004822 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004823 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004824 if (argcount > co->co_argcount) {
4825 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004826 }
4827 else {
4828 n = argcount;
4829 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004830 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004831 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004832 Py_INCREF(x);
4833 SETLOCAL(j, x);
4834 }
4835
Victor Stinnerc7020012016-08-16 23:40:29 +02004836 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004837 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004838 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004839 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004840 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004841 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004842 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004843 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004844
Mark Shannon0332e562021-02-01 10:42:03 +00004845 /* Handle keyword arguments */
4846 if (kwnames != NULL) {
4847 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4848 for (i = 0; i < kwcount; i++) {
4849 PyObject **co_varnames;
4850 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4851 PyObject *value = args[i+argcount];
4852 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004853
Mark Shannon0332e562021-02-01 10:42:03 +00004854 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4855 _PyErr_Format(tstate, PyExc_TypeError,
4856 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004857 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004859 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004860
Mark Shannon0332e562021-02-01 10:42:03 +00004861 /* Speed hack: do raw pointer compares. As names are
4862 normally interned this should almost always hit. */
4863 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4864 for (j = co->co_posonlyargcount; j < total_args; j++) {
4865 PyObject *varname = co_varnames[j];
4866 if (varname == keyword) {
4867 goto kw_found;
4868 }
4869 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004870
Mark Shannon0332e562021-02-01 10:42:03 +00004871 /* Slow fallback, just in case */
4872 for (j = co->co_posonlyargcount; j < total_args; j++) {
4873 PyObject *varname = co_varnames[j];
4874 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4875 if (cmp > 0) {
4876 goto kw_found;
4877 }
4878 else if (cmp < 0) {
4879 goto fail;
4880 }
4881 }
4882
4883 assert(j >= total_args);
4884 if (kwdict == NULL) {
4885
4886 if (co->co_posonlyargcount
4887 && positional_only_passed_as_keyword(tstate, co,
4888 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004889 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004890 {
4891 goto fail;
4892 }
4893
4894 _PyErr_Format(tstate, PyExc_TypeError,
4895 "%U() got an unexpected keyword argument '%S'",
4896 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004897 goto fail;
4898 }
4899
Mark Shannon0332e562021-02-01 10:42:03 +00004900 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4901 goto fail;
4902 }
4903 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004904
Mark Shannon0332e562021-02-01 10:42:03 +00004905 kw_found:
4906 if (GETLOCAL(j) != NULL) {
4907 _PyErr_Format(tstate, PyExc_TypeError,
4908 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004909 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004910 goto fail;
4911 }
4912 Py_INCREF(value);
4913 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004916
4917 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004918 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004919 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4920 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 goto fail;
4922 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004923
4924 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004925 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004926 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004927 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004928 Py_ssize_t missing = 0;
4929 for (i = argcount; i < m; i++) {
4930 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004931 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004932 }
4933 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004934 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004935 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004936 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004937 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004938 }
4939 if (n > m)
4940 i = n - m;
4941 else
4942 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004943 if (defcount) {
4944 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4945 for (; i < defcount; i++) {
4946 if (GETLOCAL(m+i) == NULL) {
4947 PyObject *def = defs[i];
4948 Py_INCREF(def);
4949 SETLOCAL(m+i, def);
4950 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004951 }
4952 }
4953 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004954
4955 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004956 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004957 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004958 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004959 if (GETLOCAL(i) != NULL)
4960 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004961 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004962 if (con->fc_kwdefaults != NULL) {
4963 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004964 if (def) {
4965 Py_INCREF(def);
4966 SETLOCAL(i, def);
4967 continue;
4968 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004969 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004970 goto fail;
4971 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004972 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004973 missing++;
4974 }
4975 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004976 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004977 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004978 goto fail;
4979 }
4980 }
4981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004983 vars into frame. */
4984 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004986 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004987 /* Possibly account for the cell variable being an argument. */
4988 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004989 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004990 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004991 /* Clear the local copy. */
4992 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004993 }
4994 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004995 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004996 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004997 if (c == NULL)
4998 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004999 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005001
5002 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005003 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005004 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005005 Py_INCREF(o);
5006 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005008
Mark Shannon0332e562021-02-01 10:42:03 +00005009 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005010
Thomas Woutersce272b62007-09-19 21:19:28 +00005011fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 /* decref'ing the frame can cause __del__ methods to get invoked,
5014 which can call back into Python. While we're done with the
5015 current Python frame (f), the associated C stack is still in use,
5016 so recursion_depth must be boosted for the duration.
5017 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005018 if (Py_REFCNT(f) > 1) {
5019 Py_DECREF(f);
5020 _PyObject_GC_TRACK(f);
5021 }
5022 else {
5023 ++tstate->recursion_depth;
5024 Py_DECREF(f);
5025 --tstate->recursion_depth;
5026 }
Mark Shannon0332e562021-02-01 10:42:03 +00005027 return NULL;
5028}
5029
5030static PyObject *
5031make_coro(PyFrameConstructor *con, PyFrameObject *f)
5032{
5033 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5034 PyObject *gen;
5035 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5036
5037 /* Don't need to keep the reference to f_back, it will be set
5038 * when the generator is resumed. */
5039 Py_CLEAR(f->f_back);
5040
5041 /* Create a new generator that owns the ready to run frame
5042 * and return that as the value. */
5043 if (is_coro) {
5044 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5045 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5046 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5047 } else {
5048 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5049 }
5050 if (gen == NULL) {
5051 return NULL;
5052 }
5053
5054 _PyObject_GC_TRACK(f);
5055
5056 return gen;
5057}
5058
5059PyObject *
5060_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5061 PyObject *locals,
5062 PyObject* const* args, size_t argcount,
5063 PyObject *kwnames)
5064{
5065 PyFrameObject *f = _PyEval_MakeFrameVector(
5066 tstate, con, locals, args, argcount, kwnames);
5067 if (f == NULL) {
5068 return NULL;
5069 }
5070 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5071 return make_coro(con, f);
5072 }
5073 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5074
5075 /* decref'ing the frame can cause __del__ methods to get invoked,
5076 which can call back into Python. While we're done with the
5077 current Python frame (f), the associated C stack is still in use,
5078 so recursion_depth must be boosted for the duration.
5079 */
5080 if (Py_REFCNT(f) > 1) {
5081 Py_DECREF(f);
5082 _PyObject_GC_TRACK(f);
5083 }
5084 else {
5085 ++tstate->recursion_depth;
5086 Py_DECREF(f);
5087 --tstate->recursion_depth;
5088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005090}
5091
Mark Shannond6c33fb2021-01-29 13:24:55 +00005092/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005093PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005094PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5095 PyObject *const *args, int argcount,
5096 PyObject *const *kws, int kwcount,
5097 PyObject *const *defs, int defcount,
5098 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005099{
Victor Stinner46496f92021-02-20 15:17:18 +01005100 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005101 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005102 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5103 if (defaults == NULL) {
5104 return NULL;
5105 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005106 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005107 if (builtins == NULL) {
5108 Py_DECREF(defaults);
5109 return NULL;
5110 }
Mark Shannon0332e562021-02-01 10:42:03 +00005111 if (locals == NULL) {
5112 locals = globals;
5113 }
5114 PyObject *kwnames;
5115 PyObject *const *allargs;
5116 PyObject **newargs;
5117 if (kwcount == 0) {
5118 allargs = args;
5119 kwnames = NULL;
5120 }
5121 else {
5122 kwnames = PyTuple_New(kwcount);
5123 if (kwnames == NULL) {
5124 res = NULL;
5125 goto fail;
5126 }
5127 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5128 if (newargs == NULL) {
5129 res = NULL;
5130 Py_DECREF(kwnames);
5131 goto fail;
5132 }
5133 for (int i = 0; i < argcount; i++) {
5134 newargs[i] = args[i];
5135 }
5136 for (int i = 0; i < kwcount; i++) {
5137 Py_INCREF(kws[2*i]);
5138 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5139 newargs[argcount+i] = kws[2*i+1];
5140 }
5141 allargs = newargs;
5142 }
5143 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5144 if (kwargs == NULL) {
5145 res = NULL;
5146 Py_DECREF(kwnames);
5147 goto fail;
5148 }
5149 for (int i = 0; i < kwcount; i++) {
5150 Py_INCREF(kws[2*i]);
5151 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5152 kwargs[i] = kws[2*i+1];
5153 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005154 PyFrameConstructor constr = {
5155 .fc_globals = globals,
5156 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005157 .fc_name = ((PyCodeObject *)_co)->co_name,
5158 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005159 .fc_code = _co,
5160 .fc_defaults = defaults,
5161 .fc_kwdefaults = kwdefs,
5162 .fc_closure = closure
5163 };
Mark Shannon0332e562021-02-01 10:42:03 +00005164 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005165 allargs, argcount,
5166 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005167 if (kwcount) {
5168 Py_DECREF(kwnames);
5169 PyMem_Free(newargs);
5170 }
5171fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005172 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005173 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005174}
5175
Tim Peters5ca576e2001-06-18 22:08:13 +00005176
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005177static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005178special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005181 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005182 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005183 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 return NULL;
5185 }
5186 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005187}
5188
5189
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005190/* Logic for the raise statement (too complicated for inlining).
5191 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005192static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005193do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 if (exc == NULL) {
5198 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005199 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005201 type = exc_info->exc_type;
5202 value = exc_info->exc_value;
5203 tb = exc_info->exc_traceback;
Victor Stinner09bbebe2021-04-11 00:17:39 +02005204 if (Py_IsNone(type) || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005205 _PyErr_SetString(tstate, PyExc_RuntimeError,
5206 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005207 return 0;
5208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 Py_XINCREF(type);
5210 Py_XINCREF(value);
5211 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005212 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005213 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 /* We support the following forms of raise:
5217 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005218 raise <instance>
5219 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 if (PyExceptionClass_Check(exc)) {
5222 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005223 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 if (value == NULL)
5225 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005226 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005227 _PyErr_Format(tstate, PyExc_TypeError,
5228 "calling %R should have returned an instance of "
5229 "BaseException, not %R",
5230 type, Py_TYPE(value));
5231 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 }
5234 else if (PyExceptionInstance_Check(exc)) {
5235 value = exc;
5236 type = PyExceptionInstance_Class(exc);
5237 Py_INCREF(type);
5238 }
5239 else {
5240 /* Not something you can raise. You get an exception
5241 anyway, just not what you specified :-) */
5242 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005243 _PyErr_SetString(tstate, PyExc_TypeError,
5244 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 goto raise_error;
5246 }
Collin Winter828f04a2007-08-31 00:04:24 +00005247
Serhiy Storchakac0191582016-09-27 11:37:10 +03005248 assert(type != NULL);
5249 assert(value != NULL);
5250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 if (cause) {
5252 PyObject *fixed_cause;
5253 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005254 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 if (fixed_cause == NULL)
5256 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005257 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005259 else if (PyExceptionInstance_Check(cause)) {
5260 fixed_cause = cause;
5261 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02005262 else if (Py_IsNone(cause)) {
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005263 Py_DECREF(cause);
5264 fixed_cause = NULL;
5265 }
5266 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005267 _PyErr_SetString(tstate, PyExc_TypeError,
5268 "exception causes must derive from "
5269 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 goto raise_error;
5271 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005272 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 }
Collin Winter828f04a2007-08-31 00:04:24 +00005274
Victor Stinner438a12d2019-05-24 17:01:38 +02005275 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005276 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005277 Py_DECREF(value);
5278 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005279 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005280
5281raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 Py_XDECREF(value);
5283 Py_XDECREF(type);
5284 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005285 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005286}
5287
Tim Petersd6d010b2001-06-21 02:49:55 +00005288/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005289 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005290
Guido van Rossum0368b722007-05-11 16:50:42 +00005291 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5292 with a variable target.
5293*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005294
Barry Warsawe42b18f1997-08-25 22:13:04 +00005295static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005296unpack_iterable(PyThreadState *tstate, PyObject *v,
5297 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 int i = 0, j = 0;
5300 Py_ssize_t ll = 0;
5301 PyObject *it; /* iter(v) */
5302 PyObject *w;
5303 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005305 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005308 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005309 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005310 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005311 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005312 _PyErr_Format(tstate, PyExc_TypeError,
5313 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005314 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005315 }
5316 return 0;
5317 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 for (; i < argcnt; i++) {
5320 w = PyIter_Next(it);
5321 if (w == NULL) {
5322 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005324 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005325 _PyErr_Format(tstate, PyExc_ValueError,
5326 "not enough values to unpack "
5327 "(expected %d, got %d)",
5328 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005329 }
5330 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005331 _PyErr_Format(tstate, PyExc_ValueError,
5332 "not enough values to unpack "
5333 "(expected at least %d, got %d)",
5334 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 }
5337 goto Error;
5338 }
5339 *--sp = w;
5340 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 if (argcntafter == -1) {
5343 /* We better have exhausted the iterator now. */
5344 w = PyIter_Next(it);
5345 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005346 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 goto Error;
5348 Py_DECREF(it);
5349 return 1;
5350 }
5351 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005352 _PyErr_Format(tstate, PyExc_ValueError,
5353 "too many values to unpack (expected %d)",
5354 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005355 goto Error;
5356 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 l = PySequence_List(it);
5359 if (l == NULL)
5360 goto Error;
5361 *--sp = l;
5362 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 ll = PyList_GET_SIZE(l);
5365 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005367 "not enough values to unpack (expected at least %d, got %zd)",
5368 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 goto Error;
5370 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 /* Pop the "after-variable" args off the list. */
5373 for (j = argcntafter; j > 0; j--, i++) {
5374 *--sp = PyList_GET_ITEM(l, ll - j);
5375 }
5376 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005377 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 Py_DECREF(it);
5379 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005380
Tim Petersd6d010b2001-06-21 02:49:55 +00005381Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 for (; i > 0; i--, sp++)
5383 Py_DECREF(*sp);
5384 Py_XDECREF(it);
5385 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005386}
5387
Guido van Rossum96a42c81992-01-12 02:29:51 +00005388#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005389static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005390prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 printf("%s ", str);
Miss Islington (bot)4a0f1df2021-07-13 01:43:26 -07005393 PyObject *type, *value, *traceback;
5394 PyErr_Fetch(&type, &value, &traceback);
Victor Stinner438a12d2019-05-24 17:01:38 +02005395 if (PyObject_Print(v, stdout, 0) != 0) {
5396 /* Don't know what else to do */
5397 _PyErr_Clear(tstate);
5398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 printf("\n");
Miss Islington (bot)4a0f1df2021-07-13 01:43:26 -07005400 PyErr_Restore(type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005402}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005403#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005404
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005405static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005406call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005407 PyThreadState *tstate,
5408 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005409 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005410{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005411 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005413 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 if (value == NULL) {
5415 value = Py_None;
5416 Py_INCREF(value);
5417 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005418 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005419 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005420 arg = PyTuple_Pack(3, type, value, traceback);
5421 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005422 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 return;
5424 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005425 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005427 if (err == 0) {
5428 _PyErr_Restore(tstate, type, value, orig_traceback);
5429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 else {
5431 Py_XDECREF(type);
5432 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005433 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005435}
5436
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005437static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005438call_trace_protected(Py_tracefunc func, PyObject *obj,
5439 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005440 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 PyObject *type, *value, *traceback;
5444 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005446 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 if (err == 0)
5448 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005449 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 return 0;
5451 }
5452 else {
5453 Py_XDECREF(type);
5454 Py_XDECREF(value);
5455 Py_XDECREF(traceback);
5456 return -1;
5457 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005458}
5459
Mark Shannon8e1b4062021-03-05 14:45:50 +00005460static void
5461initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5462{
5463 if (trace_info->code != frame->f_code) {
5464 trace_info->code = frame->f_code;
Mark Shannon8e1b4062021-03-05 14:45:50 +00005465 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5466 }
5467}
5468
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005469static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005470call_trace(Py_tracefunc func, PyObject *obj,
5471 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005472 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 int result;
5476 if (tstate->tracing)
5477 return 0;
5478 tstate->tracing++;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005479 tstate->cframe->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005480 if (frame->f_lasti < 0) {
5481 frame->f_lineno = frame->f_code->co_firstlineno;
5482 }
5483 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005484 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005485 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005486 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005488 frame->f_lineno = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005489 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 || (tstate->c_profilefunc != NULL));
5491 tstate->tracing--;
5492 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005493}
5494
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005495PyObject *
5496_PyEval_CallTracing(PyObject *func, PyObject *args)
5497{
Victor Stinner50b48572018-11-01 01:51:40 +01005498 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005499 int save_tracing = tstate->tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005500 int save_use_tracing = tstate->cframe->use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005501 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005503 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005504 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005505 || (tstate->c_profilefunc != NULL));
5506 result = PyObject_Call(func, args, NULL);
5507 tstate->tracing = save_tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005508 tstate->cframe->use_tracing = save_use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005509 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005510}
5511
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005512/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005513static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005514maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005515 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01005516 PyTraceInfo *trace_info, int instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005519
Nick Coghlan5a851672017-09-08 10:14:16 +10005520 /* If the last instruction falls at the start of a line or if it
5521 represents a jump backwards, update the frame's line number and
5522 then call the trace function if we're tracing source lines.
5523 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005524 initialize_trace_info(trace_info, frame);
Mark Shannon9f2c63b2021-07-08 19:21:22 +01005525 int lastline = _PyCode_CheckLineNumber(instr_prev*2, &trace_info->bounds);
Mark Shannonfcb55c02021-04-01 16:00:31 +01005526 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005527 if (line != -1 && frame->f_trace_lines) {
Mark Shannon9f2c63b2021-07-08 19:21:22 +01005528 /* Trace backward edges or if line number has changed */
5529 if (frame->f_lasti < instr_prev || line != lastline) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005530 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005532 }
George King20faa682017-10-18 17:44:22 -07005533 /* Always emit an opcode event if we're tracing all opcodes. */
5534 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005535 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005536 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005537 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005538}
5539
Victor Stinner309d7cc2020-03-13 16:39:12 +01005540int
5541_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5542{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005543 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005544 /* The caller must hold the GIL */
5545 assert(PyGILState_Check());
5546
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005547 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005548 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005549 PyThreadState *current_tstate = _PyThreadState_GET();
5550 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005551 return -1;
5552 }
5553
5554 PyObject *profileobj = tstate->c_profileobj;
5555
5556 tstate->c_profilefunc = NULL;
5557 tstate->c_profileobj = NULL;
5558 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005559 tstate->cframe->use_tracing = tstate->c_tracefunc != NULL;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005560 Py_XDECREF(profileobj);
5561
5562 Py_XINCREF(arg);
5563 tstate->c_profileobj = arg;
5564 tstate->c_profilefunc = func;
5565
5566 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005567 tstate->cframe->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005568 return 0;
5569}
5570
Fred Drake5755ce62001-06-27 19:19:46 +00005571void
5572PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005573{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005574 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005575 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005576 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005577 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5578 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005579}
5580
5581int
5582_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5583{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005584 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005585 /* The caller must hold the GIL */
5586 assert(PyGILState_Check());
5587
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005588 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005589 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005590 PyThreadState *current_tstate = _PyThreadState_GET();
5591 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005592 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005593 }
5594
Victor Stinner309d7cc2020-03-13 16:39:12 +01005595 PyObject *traceobj = tstate->c_traceobj;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005596
5597 tstate->c_tracefunc = NULL;
5598 tstate->c_traceobj = NULL;
5599 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005600 tstate->cframe->use_tracing = (tstate->c_profilefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005601 Py_XDECREF(traceobj);
5602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005604 tstate->c_traceobj = arg;
5605 tstate->c_tracefunc = func;
5606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005607 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005608 tstate->cframe->use_tracing = ((func != NULL)
Victor Stinner309d7cc2020-03-13 16:39:12 +01005609 || (tstate->c_profilefunc != NULL));
5610
5611 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005612}
5613
5614void
5615PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5616{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005617 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005618 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005619 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005620 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5621 }
Fred Draked0838392001-06-16 21:02:31 +00005622}
5623
Victor Stinner309d7cc2020-03-13 16:39:12 +01005624
Yury Selivanov75445082015-05-11 22:57:16 -04005625void
Victor Stinner838f2642019-06-13 22:41:23 +02005626_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005627{
5628 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005629 tstate->coroutine_origin_tracking_depth = new_depth;
5630}
5631
5632int
5633_PyEval_GetCoroutineOriginTrackingDepth(void)
5634{
Victor Stinner50b48572018-11-01 01:51:40 +01005635 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005636 return tstate->coroutine_origin_tracking_depth;
5637}
5638
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005639int
Yury Selivanoveb636452016-09-08 22:01:51 -07005640_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5641{
Victor Stinner50b48572018-11-01 01:51:40 +01005642 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005643
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005644 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005645 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005646 }
5647
Yury Selivanoveb636452016-09-08 22:01:51 -07005648 Py_XINCREF(firstiter);
5649 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005650 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005651}
5652
5653PyObject *
5654_PyEval_GetAsyncGenFirstiter(void)
5655{
Victor Stinner50b48572018-11-01 01:51:40 +01005656 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005657 return tstate->async_gen_firstiter;
5658}
5659
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005660int
Yury Selivanoveb636452016-09-08 22:01:51 -07005661_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5662{
Victor Stinner50b48572018-11-01 01:51:40 +01005663 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005664
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005665 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005666 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005667 }
5668
Yury Selivanoveb636452016-09-08 22:01:51 -07005669 Py_XINCREF(finalizer);
5670 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005671 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005672}
5673
5674PyObject *
5675_PyEval_GetAsyncGenFinalizer(void)
5676{
Victor Stinner50b48572018-11-01 01:51:40 +01005677 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005678 return tstate->async_gen_finalizer;
5679}
5680
Victor Stinner438a12d2019-05-24 17:01:38 +02005681PyFrameObject *
5682PyEval_GetFrame(void)
5683{
5684 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005685 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005686}
5687
Guido van Rossumb209a111997-04-29 18:18:01 +00005688PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005689_PyEval_GetBuiltins(PyThreadState *tstate)
5690{
5691 PyFrameObject *frame = tstate->frame;
5692 if (frame != NULL) {
5693 return frame->f_builtins;
5694 }
5695 return tstate->interp->builtins;
5696}
5697
5698PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005699PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005700{
Victor Stinner438a12d2019-05-24 17:01:38 +02005701 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005702 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005703}
5704
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005705/* Convenience function to get a builtin from its name */
5706PyObject *
5707_PyEval_GetBuiltinId(_Py_Identifier *name)
5708{
Victor Stinner438a12d2019-05-24 17:01:38 +02005709 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005710 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5711 if (attr) {
5712 Py_INCREF(attr);
5713 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005714 else if (!_PyErr_Occurred(tstate)) {
5715 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005716 }
5717 return attr;
5718}
5719
Guido van Rossumb209a111997-04-29 18:18:01 +00005720PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005721PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005722{
Victor Stinner438a12d2019-05-24 17:01:38 +02005723 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005724 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005725 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005726 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005727 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005728 }
5729
Victor Stinner438a12d2019-05-24 17:01:38 +02005730 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005731 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005732 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005733
5734 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005735 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005736}
5737
Guido van Rossumb209a111997-04-29 18:18:01 +00005738PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005739PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005740{
Victor Stinner438a12d2019-05-24 17:01:38 +02005741 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005742 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005743 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005744 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005745 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005746
5747 assert(current_frame->f_globals != NULL);
5748 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005749}
5750
Guido van Rossum6135a871995-01-09 17:53:26 +00005751int
Tim Peters5ba58662001-07-16 02:29:45 +00005752PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005753{
Victor Stinner438a12d2019-05-24 17:01:38 +02005754 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005755 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005756 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 if (current_frame != NULL) {
5759 const int codeflags = current_frame->f_code->co_flags;
5760 const int compilerflags = codeflags & PyCF_MASK;
5761 if (compilerflags) {
5762 result = 1;
5763 cf->cf_flags |= compilerflags;
5764 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005765#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005766 if (codeflags & CO_GENERATOR_ALLOWED) {
5767 result = 1;
5768 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5769 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005771 }
5772 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005773}
5774
Guido van Rossum3f5da241990-12-20 15:06:42 +00005775
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005776const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005777PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005779 if (PyMethod_Check(func))
5780 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5781 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005782 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005783 else if (PyCFunction_Check(func))
5784 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5785 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005786 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005787}
5788
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005789const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005790PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005792 if (PyMethod_Check(func))
5793 return "()";
5794 else if (PyFunction_Check(func))
5795 return "()";
5796 else if (PyCFunction_Check(func))
5797 return "()";
5798 else
5799 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005800}
5801
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005802#define C_TRACE(x, call) \
Mark Shannon9e7b2072021-04-13 11:08:14 +01005803if (trace_info->cframe.use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005804 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005805 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005806 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005807 x = NULL; \
5808 } \
5809 else { \
5810 x = call; \
5811 if (tstate->c_profilefunc != NULL) { \
5812 if (x == NULL) { \
5813 call_trace_protected(tstate->c_profilefunc, \
5814 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005815 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005816 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005817 /* XXX should pass (type, value, tb) */ \
5818 } else { \
5819 if (call_trace(tstate->c_profilefunc, \
5820 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005821 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005822 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005823 Py_DECREF(x); \
5824 x = NULL; \
5825 } \
5826 } \
5827 } \
5828 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005829} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005830 x = call; \
5831 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005832
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005833
5834static PyObject *
5835trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005836 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005837 PyObject *func,
5838 PyObject **args, Py_ssize_t nargs,
5839 PyObject *kwnames)
5840{
5841 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005842 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005843 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005844 return x;
5845 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005846 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005847 /* We need to create a temporary bound method as argument
5848 for profiling.
5849
5850 If nargs == 0, then this cannot work because we have no
5851 "self". In any case, the call itself would raise
5852 TypeError (foo needs an argument), so we just skip
5853 profiling. */
5854 PyObject *self = args[0];
5855 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5856 if (func == NULL) {
5857 return NULL;
5858 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005859 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005860 args+1, nargs-1,
5861 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005862 Py_DECREF(func);
5863 return x;
5864 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005865 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005866}
5867
Victor Stinner415c5102017-01-11 00:54:57 +01005868/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5869 to reduce the stack consumption. */
5870Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005871call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005872 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005873 PyObject ***pp_stack,
5874 Py_ssize_t oparg,
5875 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005876{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005877 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005878 PyObject *func = *pfunc;
5879 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005880 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5881 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005882 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005883
Mark Shannon9e7b2072021-04-13 11:08:14 +01005884 if (trace_info->cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005885 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005886 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005887 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005888 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005889 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005890
Victor Stinner438a12d2019-05-24 17:01:38 +02005891 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005892
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005893 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005894 while ((*pp_stack) > pfunc) {
5895 w = EXT_POP(*pp_stack);
5896 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005897 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005899 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005900}
5901
Jeremy Hylton52820442001-01-03 23:52:36 +00005902static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005903do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005904 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005905 PyObject *func,
5906 PyObject *callargs,
5907 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005908{
jdemeyere89de732018-09-19 12:06:20 +02005909 PyObject *result;
5910
scoder4c9ea092020-05-12 16:12:41 +02005911 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005912 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005913 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005914 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005915 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005916 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
Mark Shannon9e7b2072021-04-13 11:08:14 +01005917 if (nargs > 0 && trace_info->cframe.use_tracing) {
jdemeyere89de732018-09-19 12:06:20 +02005918 /* We need to create a temporary bound method as argument
5919 for profiling.
5920
5921 If nargs == 0, then this cannot work because we have no
5922 "self". In any case, the call itself would raise
5923 TypeError (foo needs an argument), so we just skip
5924 profiling. */
5925 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5926 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5927 if (func == NULL) {
5928 return NULL;
5929 }
5930
Victor Stinner4d231bc2019-11-14 13:36:21 +01005931 C_TRACE(result, _PyObject_FastCallDictTstate(
5932 tstate, func,
5933 &_PyTuple_ITEMS(callargs)[1],
5934 nargs - 1,
5935 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005936 Py_DECREF(func);
5937 return result;
5938 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005939 }
jdemeyere89de732018-09-19 12:06:20 +02005940 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005941}
5942
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005943/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005944 nb_index slot defined, and store in *pi.
5945 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005946 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005947 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005948*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005949int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005950_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005951{
Victor Stinner438a12d2019-05-24 17:01:38 +02005952 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner09bbebe2021-04-11 00:17:39 +02005953 if (!Py_IsNone(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005954 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005955 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005956 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005957 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005958 return 0;
5959 }
5960 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005961 _PyErr_SetString(tstate, PyExc_TypeError,
5962 "slice indices must be integers or "
5963 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005964 return 0;
5965 }
5966 *pi = x;
5967 }
5968 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005969}
5970
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005971int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005972_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005973{
Victor Stinner438a12d2019-05-24 17:01:38 +02005974 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005975 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005976 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005977 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005978 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005979 return 0;
5980 }
5981 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005982 _PyErr_SetString(tstate, PyExc_TypeError,
5983 "slice indices must be integers or "
5984 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005985 return 0;
5986 }
5987 *pi = x;
5988 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005989}
5990
Thomas Wouters52152252000-08-17 22:55:00 +00005991static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005992import_name(PyThreadState *tstate, PyFrameObject *f,
5993 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005994{
5995 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005996 PyObject *import_func, *res;
5997 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005998
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005999 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006000 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006001 if (!_PyErr_Occurred(tstate)) {
6002 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006003 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006004 return NULL;
6005 }
6006
6007 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006008 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006009 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006010 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006011 return NULL;
6012 }
6013 res = PyImport_ImportModuleLevelObject(
6014 name,
6015 f->f_globals,
6016 f->f_locals == NULL ? Py_None : f->f_locals,
6017 fromlist,
6018 ilevel);
6019 return res;
6020 }
6021
6022 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006023
6024 stack[0] = name;
6025 stack[1] = f->f_globals;
6026 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6027 stack[3] = fromlist;
6028 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006029 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006030 Py_DECREF(import_func);
6031 return res;
6032}
6033
6034static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006035import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006037 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006038 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006039
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006040 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006041 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006042 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006043 /* Issue #17636: in case this failed because of a circular relative
6044 import, try to fallback on reading the module directly from
6045 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006046 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006047 if (pkgname == NULL) {
6048 goto error;
6049 }
Oren Milman6db70332017-09-19 14:23:01 +03006050 if (!PyUnicode_Check(pkgname)) {
6051 Py_CLEAR(pkgname);
6052 goto error;
6053 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006054 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006055 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006056 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006057 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006058 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006059 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006060 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006061 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006062 goto error;
6063 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006064 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006065 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006066 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006067 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006068 if (pkgname == NULL) {
6069 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6070 if (pkgname_or_unknown == NULL) {
6071 Py_XDECREF(pkgpath);
6072 return NULL;
6073 }
6074 } else {
6075 pkgname_or_unknown = pkgname;
6076 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006077
6078 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006079 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006080 errmsg = PyUnicode_FromFormat(
6081 "cannot import name %R from %R (unknown location)",
6082 name, pkgname_or_unknown
6083 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006084 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006085 PyErr_SetImportError(errmsg, pkgname, NULL);
6086 }
6087 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006088 _Py_IDENTIFIER(__spec__);
6089 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006090 const char *fmt =
6091 _PyModuleSpec_IsInitializing(spec) ?
6092 "cannot import name %R from partially initialized module %R "
6093 "(most likely due to a circular import) (%S)" :
6094 "cannot import name %R from %R (%S)";
6095 Py_XDECREF(spec);
6096
6097 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006098 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006099 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006100 }
6101
Xiang Zhang4830f582017-03-21 11:13:42 +08006102 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006103 Py_XDECREF(pkgname_or_unknown);
6104 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006105 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006106}
Guido van Rossumac7be682001-01-17 15:42:30 +00006107
Thomas Wouters52152252000-08-17 22:55:00 +00006108static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006109import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006110{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006111 _Py_IDENTIFIER(__all__);
6112 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006113 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006114 int skip_leading_underscores = 0;
6115 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006116
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006117 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6118 return -1; /* Unexpected error */
6119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006120 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006121 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6122 return -1;
6123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006124 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006125 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006126 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006127 return -1;
6128 }
6129 all = PyMapping_Keys(dict);
6130 Py_DECREF(dict);
6131 if (all == NULL)
6132 return -1;
6133 skip_leading_underscores = 1;
6134 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006136 for (pos = 0, err = 0; ; pos++) {
6137 name = PySequence_GetItem(all, pos);
6138 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006139 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006140 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006141 }
6142 else {
6143 _PyErr_Clear(tstate);
6144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006145 break;
6146 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006147 if (!PyUnicode_Check(name)) {
6148 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6149 if (modname == NULL) {
6150 Py_DECREF(name);
6151 err = -1;
6152 break;
6153 }
6154 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006155 _PyErr_Format(tstate, PyExc_TypeError,
6156 "module __name__ must be a string, not %.100s",
6157 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006158 }
6159 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006160 _PyErr_Format(tstate, PyExc_TypeError,
6161 "%s in %U.%s must be str, not %.100s",
6162 skip_leading_underscores ? "Key" : "Item",
6163 modname,
6164 skip_leading_underscores ? "__dict__" : "__all__",
6165 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006166 }
6167 Py_DECREF(modname);
6168 Py_DECREF(name);
6169 err = -1;
6170 break;
6171 }
6172 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006173 if (PyUnicode_READY(name) == -1) {
6174 Py_DECREF(name);
6175 err = -1;
6176 break;
6177 }
6178 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6179 Py_DECREF(name);
6180 continue;
6181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006182 }
6183 value = PyObject_GetAttr(v, name);
6184 if (value == NULL)
6185 err = -1;
6186 else if (PyDict_CheckExact(locals))
6187 err = PyDict_SetItem(locals, name, value);
6188 else
6189 err = PyObject_SetItem(locals, name, value);
6190 Py_DECREF(name);
6191 Py_XDECREF(value);
6192 if (err != 0)
6193 break;
6194 }
6195 Py_DECREF(all);
6196 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006197}
6198
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006199static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006200check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006201{
Victor Stinnera102ed72020-02-07 02:24:48 +01006202 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006203 /* check_args_iterable() may be called with a live exception:
6204 * clear it to prevent calling _PyObject_FunctionStr() with an
6205 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006206 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006207 PyObject *funcstr = _PyObject_FunctionStr(func);
6208 if (funcstr != NULL) {
6209 _PyErr_Format(tstate, PyExc_TypeError,
6210 "%U argument after * must be an iterable, not %.200s",
6211 funcstr, Py_TYPE(args)->tp_name);
6212 Py_DECREF(funcstr);
6213 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006214 return -1;
6215 }
6216 return 0;
6217}
6218
6219static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006220format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006221{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006222 /* _PyDict_MergeEx raises attribute
6223 * error (percolated from an attempt
6224 * to get 'keys' attribute) instead of
6225 * a type error if its second argument
6226 * is not a mapping.
6227 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006228 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006229 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006230 PyObject *funcstr = _PyObject_FunctionStr(func);
6231 if (funcstr != NULL) {
6232 _PyErr_Format(
6233 tstate, PyExc_TypeError,
6234 "%U argument after ** must be a mapping, not %.200s",
6235 funcstr, Py_TYPE(kwargs)->tp_name);
6236 Py_DECREF(funcstr);
6237 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006238 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006239 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006240 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006241 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006242 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006243 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006244 PyObject *funcstr = _PyObject_FunctionStr(func);
6245 if (funcstr != NULL) {
6246 PyObject *key = PyTuple_GET_ITEM(val, 0);
6247 _PyErr_Format(
6248 tstate, PyExc_TypeError,
6249 "%U got multiple values for keyword argument '%S'",
6250 funcstr, key);
6251 Py_DECREF(funcstr);
6252 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006253 Py_XDECREF(exc);
6254 Py_XDECREF(val);
6255 Py_XDECREF(tb);
6256 }
6257 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006258 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006259 }
6260 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006261}
6262
Guido van Rossumac7be682001-01-17 15:42:30 +00006263static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006264format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6265 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006267 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006269 if (!obj)
6270 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006271
Serhiy Storchaka06515832016-11-20 09:13:07 +02006272 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006273 if (!obj_str)
6274 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006275
Victor Stinner438a12d2019-05-24 17:01:38 +02006276 _PyErr_Format(tstate, exc, format_str, obj_str);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +01006277
6278 if (exc == PyExc_NameError) {
6279 // Include the name in the NameError exceptions to offer suggestions later.
6280 _Py_IDENTIFIER(name);
6281 PyObject *type, *value, *traceback;
6282 PyErr_Fetch(&type, &value, &traceback);
6283 PyErr_NormalizeException(&type, &value, &traceback);
6284 if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
6285 // We do not care if this fails because we are going to restore the
6286 // NameError anyway.
6287 (void)_PyObject_SetAttrId(value, &PyId_name, obj);
6288 }
6289 PyErr_Restore(type, value, traceback);
6290 }
Paul Prescode68140d2000-08-30 20:25:01 +00006291}
Guido van Rossum950361c1997-01-24 13:49:28 +00006292
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006293static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006294format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006295{
6296 PyObject *name;
6297 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006298 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006299 return;
6300 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6301 name = PyTuple_GET_ITEM(co->co_cellvars,
6302 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006303 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006304 PyExc_UnboundLocalError,
6305 UNBOUNDLOCAL_ERROR_MSG,
6306 name);
6307 } else {
6308 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6309 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006310 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006311 UNBOUNDFREE_ERROR_MSG, name);
6312 }
6313}
6314
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006315static void
Mark Shannonfee55262019-11-21 09:11:43 +00006316format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006317{
6318 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6319 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006320 _PyErr_Format(tstate, PyExc_TypeError,
6321 "'async with' received an object from __aenter__ "
6322 "that does not implement __await__: %.100s",
6323 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006324 }
Mark Shannonfee55262019-11-21 09:11:43 +00006325 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006326 _PyErr_Format(tstate, PyExc_TypeError,
6327 "'async with' received an object from __aexit__ "
6328 "that does not implement __await__: %.100s",
6329 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006330 }
6331 }
6332}
6333
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006334static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006335unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006336 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006337{
6338 PyObject *res;
6339 if (Py_REFCNT(v) == 2) {
6340 /* In the common case, there are 2 references to the value
6341 * stored in 'variable' when the += is performed: one on the
6342 * value stack (in 'v') and one still stored in the
6343 * 'variable'. We try to delete the variable now to reduce
6344 * the refcnt to 1.
6345 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006346 int opcode, oparg;
6347 NEXTOPARG();
6348 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006349 case STORE_FAST:
6350 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006351 PyObject **fastlocals = f->f_localsplus;
6352 if (GETLOCAL(oparg) == v)
6353 SETLOCAL(oparg, NULL);
6354 break;
6355 }
6356 case STORE_DEREF:
6357 {
6358 PyObject **freevars = (f->f_localsplus +
6359 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006360 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006361 if (PyCell_GET(c) == v) {
6362 PyCell_SET(c, NULL);
6363 Py_DECREF(v);
6364 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006365 break;
6366 }
6367 case STORE_NAME:
6368 {
6369 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006370 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006371 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006372 if (locals && PyDict_CheckExact(locals)) {
6373 PyObject *w = PyDict_GetItemWithError(locals, name);
6374 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006375 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006376 {
6377 Py_DECREF(v);
6378 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006379 }
6380 }
6381 break;
6382 }
6383 }
6384 }
6385 res = v;
6386 PyUnicode_Append(&res, w);
6387 return res;
6388}
6389
Guido van Rossum950361c1997-01-24 13:49:28 +00006390#ifdef DYNAMIC_EXECUTION_PROFILE
6391
Skip Montanarof118cb12001-10-15 20:51:38 +00006392static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006393getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395 int i;
6396 PyObject *l = PyList_New(256);
6397 if (l == NULL) return NULL;
6398 for (i = 0; i < 256; i++) {
6399 PyObject *x = PyLong_FromLong(a[i]);
6400 if (x == NULL) {
6401 Py_DECREF(l);
6402 return NULL;
6403 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006404 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405 }
6406 for (i = 0; i < 256; i++)
6407 a[i] = 0;
6408 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006409}
6410
6411PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006412_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006413{
6414#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006415 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006416#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006417 int i;
6418 PyObject *l = PyList_New(257);
6419 if (l == NULL) return NULL;
6420 for (i = 0; i < 257; i++) {
6421 PyObject *x = getarray(dxpairs[i]);
6422 if (x == NULL) {
6423 Py_DECREF(l);
6424 return NULL;
6425 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006426 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 }
6428 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006429#endif
6430}
6431
6432#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006433
6434Py_ssize_t
6435_PyEval_RequestCodeExtraIndex(freefunc free)
6436{
Victor Stinner81a7be32020-04-14 15:14:01 +02006437 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006438 Py_ssize_t new_index;
6439
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006440 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006441 return -1;
6442 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006443 new_index = interp->co_extra_user_count++;
6444 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006445 return new_index;
6446}
Łukasz Langaa785c872016-09-09 17:37:37 -07006447
6448static void
6449dtrace_function_entry(PyFrameObject *f)
6450{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006451 const char *filename;
6452 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006453 int lineno;
6454
Victor Stinner6d86a232020-04-29 00:56:58 +02006455 PyCodeObject *code = f->f_code;
6456 filename = PyUnicode_AsUTF8(code->co_filename);
6457 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006458 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006459
Andy Lestere6be9b52020-02-11 20:28:35 -06006460 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006461}
6462
6463static void
6464dtrace_function_return(PyFrameObject *f)
6465{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006466 const char *filename;
6467 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006468 int lineno;
6469
Victor Stinner6d86a232020-04-29 00:56:58 +02006470 PyCodeObject *code = f->f_code;
6471 filename = PyUnicode_AsUTF8(code->co_filename);
6472 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006473 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006474
Andy Lestere6be9b52020-02-11 20:28:35 -06006475 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006476}
6477
6478/* DTrace equivalent of maybe_call_line_trace. */
6479static void
6480maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01006481 PyTraceInfo *trace_info, int instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006482{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006483 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006484
6485 /* If the last instruction executed isn't in the current
6486 instruction window, reset the window.
6487 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006488 initialize_trace_info(trace_info, frame);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006489 int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006490 /* If the last instruction falls at the start of a line or if
6491 it represents a jump backwards, update the frame's line
6492 number and call the trace function. */
Mark Shannon9f2c63b2021-07-08 19:21:22 +01006493 if (line != frame->f_lineno || frame->f_lasti < instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006494 if (line != -1) {
6495 frame->f_lineno = line;
6496 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6497 if (!co_filename)
6498 co_filename = "?";
6499 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6500 if (!co_name)
6501 co_name = "?";
6502 PyDTrace_LINE(co_filename, co_name, line);
6503 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006504 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006505}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006506
6507
6508/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6509 for the limited API. */
6510
6511#undef Py_EnterRecursiveCall
6512
6513int Py_EnterRecursiveCall(const char *where)
6514{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006515 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006516}
6517
6518#undef Py_LeaveRecursiveCall
6519
6520void Py_LeaveRecursiveCall(void)
6521{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006522 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006523}