blob: 8ad1713b78c017df7b3920c3cef33d224563c545 [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
Mark Shannon28d28e02021-04-08 11:22:55 +01001300#define DISPATCH() \
1301 { \
Mark Shannon9e7b2072021-04-13 11:08:14 +01001302 if (trace_info.cframe.use_tracing OR_DTRACE_LINE OR_LLTRACE) { \
Mark Shannon28d28e02021-04-08 11:22:55 +01001303 goto tracing_dispatch; \
1304 } \
1305 f->f_lasti = INSTR_OFFSET(); \
1306 NEXTOPARG(); \
Mark Shannon2be4c372021-09-29 13:16:13 +01001307 goto *opcode_targets[opcode]; \
Mark Shannon28d28e02021-04-08 11:22:55 +01001308 }
Mark Shannon2be4c372021-09-29 13:16:13 +01001309#else
1310#define TARGET(op) op
1311#define DISPATCH() goto predispatch;
1312#endif
1313
Mark Shannon28d28e02021-04-08 11:22:55 +01001314
Mark Shannon4958f5d2021-03-24 17:56:12 +00001315#define CHECK_EVAL_BREAKER() \
1316 if (_Py_atomic_load_relaxed(eval_breaker)) { \
1317 continue; \
1318 }
1319
Antoine Pitroub52ec782009-01-25 16:34:23 +00001320
Neal Norwitza81d2202002-07-14 00:27:26 +00001321/* Tuple access macros */
1322
1323#ifndef Py_DEBUG
1324#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1325#else
1326#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1327#endif
1328
Guido van Rossum374a9221991-04-04 10:40:29 +00001329/* Code access macros */
1330
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001331/* The integer overflow is checked by an assertion below. */
Mark Shannonfcb55c02021-04-01 16:00:31 +01001332#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001333#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001334 _Py_CODEUNIT word = *next_instr; \
1335 opcode = _Py_OPCODE(word); \
1336 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001337 next_instr++; \
1338 } while (0)
Mark Shannonfcb55c02021-04-01 16:00:31 +01001339#define JUMPTO(x) (next_instr = first_instr + (x))
1340#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001341
Raymond Hettingerf606f872003-03-16 03:11:04 +00001342/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 Some opcodes tend to come in pairs thus making it possible to
1344 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001345 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 Verifying the prediction costs a single high-speed test of a register
1348 variable against a constant. If the pairing was good, then the
1349 processor's own internal branch predication has a high likelihood of
1350 success, resulting in a nearly zero-overhead transition to the
1351 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001352 including its unpredictable switch-case branch. Combined with the
1353 processor's internal branch prediction, a successful PREDICT has the
1354 effect of making the two opcodes run as if they were a single new opcode
1355 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001356
Georg Brandl86b2fb92008-07-16 03:43:04 +00001357 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 predictions turned-on and interpret the results as if some opcodes
1359 had been combined or turn-off predictions so that the opcode frequency
1360 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001361
1362 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 the CPU to record separate branch prediction information for each
1364 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001365
Raymond Hettingerf606f872003-03-16 03:11:04 +00001366*/
1367
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001368#define PREDICT_ID(op) PRED_##op
1369
Antoine Pitrou042b1282010-08-13 21:15:58 +00001370#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001371#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001372#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001373#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001374 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001375 _Py_CODEUNIT word = *next_instr; \
1376 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001377 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001378 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001379 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001380 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001381 } \
1382 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001383#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001384#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001385
Raymond Hettingerf606f872003-03-16 03:11:04 +00001386
Guido van Rossum374a9221991-04-04 10:40:29 +00001387/* Stack manipulation macros */
1388
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389/* The stack can grow at most MAXINT deep, as co_nlocals and
1390 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001391#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1392#define EMPTY() (STACK_LEVEL() == 0)
1393#define TOP() (stack_pointer[-1])
1394#define SECOND() (stack_pointer[-2])
1395#define THIRD() (stack_pointer[-3])
1396#define FOURTH() (stack_pointer[-4])
1397#define PEEK(n) (stack_pointer[-(n)])
1398#define SET_TOP(v) (stack_pointer[-1] = (v))
1399#define SET_SECOND(v) (stack_pointer[-2] = (v))
1400#define SET_THIRD(v) (stack_pointer[-3] = (v))
1401#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001402#define BASIC_STACKADJ(n) (stack_pointer += n)
1403#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1404#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001405
Guido van Rossum96a42c81992-01-12 02:29:51 +00001406#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001408 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001409 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001410#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001411 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001412#define STACK_GROW(n) do { \
1413 assert(n >= 0); \
1414 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001415 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001416 assert(STACK_LEVEL() <= co->co_stacksize); \
1417 } while (0)
1418#define STACK_SHRINK(n) do { \
1419 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001420 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001421 (void)(BASIC_STACKADJ(-n)); \
1422 assert(STACK_LEVEL() <= co->co_stacksize); \
1423 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001424#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001425 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001426 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001427#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001428#define PUSH(v) BASIC_PUSH(v)
1429#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001430#define STACK_GROW(n) BASIC_STACKADJ(n)
1431#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001432#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001433#endif
1434
Guido van Rossum681d79a1995-07-18 14:51:37 +00001435/* Local variable macros */
1436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001438
1439/* The SETLOCAL() macro must not DECREF the local variable in-place and
1440 then store the new value; it must copy the old value to a temporary
1441 value, then store the new value, and then DECREF the temporary value.
1442 This is because it is possible that during the DECREF the frame is
1443 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1444 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001446 GETLOCAL(i) = value; \
1447 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001448
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001449
1450#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 while (STACK_LEVEL() > (b)->b_level) { \
1452 PyObject *v = POP(); \
1453 Py_XDECREF(v); \
1454 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001455
1456#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001457 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001459 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1461 while (STACK_LEVEL() > (b)->b_level + 3) { \
1462 value = POP(); \
1463 Py_XDECREF(value); \
1464 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001465 exc_info = tstate->exc_info; \
1466 type = exc_info->exc_type; \
1467 value = exc_info->exc_value; \
1468 traceback = exc_info->exc_traceback; \
1469 exc_info->exc_type = POP(); \
1470 exc_info->exc_value = POP(); \
1471 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_XDECREF(type); \
1473 Py_XDECREF(value); \
1474 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001475 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001476
Inada Naoki91234a12019-06-03 21:30:58 +09001477 /* macros for opcode cache */
1478#define OPCACHE_CHECK() \
1479 do { \
1480 co_opcache = NULL; \
1481 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001482 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001483 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001484 if (co_opcache_offset > 0) { \
1485 assert(co_opcache_offset <= co->co_opcache_size); \
1486 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001487 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001488 } \
1489 } \
1490 } while (0)
1491
Pablo Galindo109826c2020-10-20 06:22:44 +01001492#define OPCACHE_DEOPT() \
1493 do { \
1494 if (co_opcache != NULL) { \
1495 co_opcache->optimized = -1; \
1496 unsigned char co_opcache_offset = \
1497 co->co_opcache_map[next_instr - first_instr]; \
1498 assert(co_opcache_offset <= co->co_opcache_size); \
1499 co->co_opcache_map[co_opcache_offset] = 0; \
1500 co_opcache = NULL; \
1501 } \
1502 } while (0)
1503
1504#define OPCACHE_DEOPT_LOAD_ATTR() \
1505 do { \
1506 if (co_opcache != NULL) { \
1507 OPCACHE_STAT_ATTR_DEOPT(); \
1508 OPCACHE_DEOPT(); \
1509 } \
1510 } while (0)
1511
1512#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1513 do { \
1514 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1515 OPCACHE_DEOPT_LOAD_ATTR(); \
1516 } \
1517 } while (0)
1518
Inada Naoki91234a12019-06-03 21:30:58 +09001519#if OPCACHE_STATS
1520
1521#define OPCACHE_STAT_GLOBAL_HIT() \
1522 do { \
1523 if (co->co_opcache != NULL) opcache_global_hits++; \
1524 } while (0)
1525
1526#define OPCACHE_STAT_GLOBAL_MISS() \
1527 do { \
1528 if (co->co_opcache != NULL) opcache_global_misses++; \
1529 } while (0)
1530
1531#define OPCACHE_STAT_GLOBAL_OPT() \
1532 do { \
1533 if (co->co_opcache != NULL) opcache_global_opts++; \
1534 } while (0)
1535
Pablo Galindo109826c2020-10-20 06:22:44 +01001536#define OPCACHE_STAT_ATTR_HIT() \
1537 do { \
1538 if (co->co_opcache != NULL) opcache_attr_hits++; \
1539 } while (0)
1540
1541#define OPCACHE_STAT_ATTR_MISS() \
1542 do { \
1543 if (co->co_opcache != NULL) opcache_attr_misses++; \
1544 } while (0)
1545
1546#define OPCACHE_STAT_ATTR_OPT() \
1547 do { \
1548 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1549 } while (0)
1550
1551#define OPCACHE_STAT_ATTR_DEOPT() \
1552 do { \
1553 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1554 } while (0)
1555
1556#define OPCACHE_STAT_ATTR_TOTAL() \
1557 do { \
1558 if (co->co_opcache != NULL) opcache_attr_total++; \
1559 } while (0)
1560
Inada Naoki91234a12019-06-03 21:30:58 +09001561#else /* OPCACHE_STATS */
1562
1563#define OPCACHE_STAT_GLOBAL_HIT()
1564#define OPCACHE_STAT_GLOBAL_MISS()
1565#define OPCACHE_STAT_GLOBAL_OPT()
1566
Pablo Galindo109826c2020-10-20 06:22:44 +01001567#define OPCACHE_STAT_ATTR_HIT()
1568#define OPCACHE_STAT_ATTR_MISS()
1569#define OPCACHE_STAT_ATTR_OPT()
1570#define OPCACHE_STAT_ATTR_DEOPT()
1571#define OPCACHE_STAT_ATTR_TOTAL()
1572
Inada Naoki91234a12019-06-03 21:30:58 +09001573#endif
1574
Mark Shannond41bddd2021-03-25 12:00:30 +00001575
1576PyObject* _Py_HOT_FUNCTION
1577_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1578{
1579 _Py_EnsureTstateNotNULL(tstate);
1580
1581#if USE_COMPUTED_GOTOS
1582/* Import the static jump table */
1583#include "opcode_targets.h"
1584#endif
1585
1586#ifdef DXPAIRS
1587 int lastopcode = 0;
1588#endif
1589 PyObject **stack_pointer; /* Next free slot in value stack */
1590 const _Py_CODEUNIT *next_instr;
1591 int opcode; /* Current opcode */
1592 int oparg; /* Current opcode argument, if any */
1593 PyObject **fastlocals, **freevars;
1594 PyObject *retval = NULL; /* Return value */
Mark Shannon9e7b2072021-04-13 11:08:14 +01001595 _Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker;
Mark Shannond41bddd2021-03-25 12:00:30 +00001596 PyCodeObject *co;
1597
Mark Shannond41bddd2021-03-25 12:00:30 +00001598 const _Py_CODEUNIT *first_instr;
1599 PyObject *names;
1600 PyObject *consts;
1601 _PyOpcache *co_opcache;
1602
1603#ifdef LLTRACE
1604 _Py_IDENTIFIER(__ltrace__);
1605#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001606
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001607 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001609 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001610
Mark Shannon8e1b4062021-03-05 14:45:50 +00001611 PyTraceInfo trace_info;
Mark Shannon28d28e02021-04-08 11:22:55 +01001612 /* Mark trace_info as uninitialized */
Mark Shannon8e1b4062021-03-05 14:45:50 +00001613 trace_info.code = NULL;
1614
Mark Shannon9e7b2072021-04-13 11:08:14 +01001615 /* WARNING: Because the CFrame lives on the C stack,
1616 * but can be accessed from a heap allocated object (tstate)
1617 * strict stack discipline must be maintained.
1618 */
1619 CFrame *prev_cframe = tstate->cframe;
1620 trace_info.cframe.use_tracing = prev_cframe->use_tracing;
1621 trace_info.cframe.previous = prev_cframe;
1622 tstate->cframe = &trace_info.cframe;
1623
Mark Shannon8e1b4062021-03-05 14:45:50 +00001624 /* push frame */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001626 co = f->f_code;
Tim Peters5ca576e2001-06-18 22:08:13 +00001627
Mark Shannon9e7b2072021-04-13 11:08:14 +01001628 if (trace_info.cframe.use_tracing) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (tstate->c_tracefunc != NULL) {
1630 /* tstate->c_tracefunc, if defined, is a
1631 function that will be called on *every* entry
1632 to a code block. Its return value, if not
1633 None, is a function that will be called at
1634 the start of each executed line of code.
1635 (Actually, the function must return itself
1636 in order to continue tracing.) The trace
1637 functions are called with three arguments:
1638 a pointer to the current frame, a string
1639 indicating why the function is called, and
1640 an argument which depends on the situation.
1641 The global trace function is also called
1642 whenever an exception is detected. */
1643 if (call_trace_protected(tstate->c_tracefunc,
1644 tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001645 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001646 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 /* Trace function raised an error */
1648 goto exit_eval_frame;
1649 }
1650 }
1651 if (tstate->c_profilefunc != NULL) {
1652 /* Similar for c_profilefunc, except it needn't
1653 return itself and isn't called for "line" events */
1654 if (call_trace_protected(tstate->c_profilefunc,
1655 tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001656 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001657 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 /* Profile function raised an error */
1659 goto exit_eval_frame;
1660 }
1661 }
1662 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001663
Łukasz Langaa785c872016-09-09 17:37:37 -07001664 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1665 dtrace_function_entry(f);
1666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 names = co->co_names;
1668 consts = co->co_consts;
1669 fastlocals = f->f_localsplus;
1670 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001671 assert(PyBytes_Check(co->co_code));
1672 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001673 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1674 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1675 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001676 /*
1677 f->f_lasti refers to the index of the last instruction,
1678 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001679
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001680 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001681 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 When the PREDICT() macros are enabled, some opcode pairs follow in
1684 direct succession without updating f->f_lasti. A successful
1685 prediction effectively links the two codes together as if they
1686 were a single new opcode; accordingly,f->f_lasti will point to
1687 the first code in the pair (for instance, GET_ITER followed by
1688 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001689 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001691 assert(f->f_lasti >= -1);
Mark Shannonfcb55c02021-04-01 16:00:31 +01001692 next_instr = first_instr + f->f_lasti + 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01001693 stack_pointer = f->f_valuestack + f->f_stackdepth;
1694 /* Set f->f_stackdepth to -1.
1695 * Update when returning or calling trace function.
1696 Having f_stackdepth <= 0 ensures that invalid
1697 values are not visible to the cycle GC.
1698 We choose -1 rather than 0 to assist debugging.
1699 */
1700 f->f_stackdepth = -1;
1701 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001702
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001703 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001704 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001705 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001706 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001707 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001708 }
1709#if OPCACHE_STATS
1710 opcache_code_objects_extra_mem +=
1711 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1712 sizeof(_PyOpcache) * co->co_opcache_size;
1713 opcache_code_objects++;
1714#endif
1715 }
1716 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001717
Tim Peters5ca576e2001-06-18 22:08:13 +00001718#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001719 {
1720 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1721 if (r < 0) {
1722 goto exit_eval_frame;
1723 }
1724 lltrace = r;
1725 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001726#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001727
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001728 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001730 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001731
Victor Stinnerace47d72013-07-18 01:41:08 +02001732#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001733 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001734 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001735 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001736 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001737#endif
1738
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001739main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1742 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001743 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 /* Do periodic things. Doing this every time through
1746 the loop would add too much overhead, so we do it
1747 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001748 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 event needs attention (e.g. a signal handler or
1750 async I/O handler); see Py_AddPendingCall() and
1751 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Eric Snow7bda9de2019-03-08 17:25:54 -07001753 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001754 opcode = _Py_OPCODE(*next_instr);
Mark Shannon28d28e02021-04-08 11:22:55 +01001755 if (opcode != SETUP_FINALLY &&
1756 opcode != SETUP_WITH &&
1757 opcode != BEFORE_ASYNC_WITH &&
1758 opcode != YIELD_FROM) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001759 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001760 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001761 - If we're about to enter the 'with:'. It will prevent
1762 emitting a resource warning in the common idiom
1763 'with open(path) as file:'.
1764 - If we're about to enter the 'async with:'.
1765 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001766 *very* useful, but might help in some cases and it's
1767 traditional)
1768 - If we're resuming a chain of nested 'yield from' or
1769 'await' calls, then each frame is parked with YIELD_FROM
1770 as its next opcode. If the user hit control-C we want to
1771 wait until we've reached the innermost frame before
1772 running the signal handler and raising KeyboardInterrupt
1773 (see bpo-30039).
1774 */
Mark Shannon28d28e02021-04-08 11:22:55 +01001775 if (eval_frame_handle_pending(tstate) != 0) {
1776 goto error;
1777 }
1778 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001780
Mark Shannon28d28e02021-04-08 11:22:55 +01001781 tracing_dispatch:
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001782 {
1783 int instr_prev = f->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 f->f_lasti = INSTR_OFFSET();
Mark Shannon28d28e02021-04-08 11:22:55 +01001785 NEXTOPARG();
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Łukasz Langaa785c872016-09-09 17:37:37 -07001787 if (PyDTrace_LINE_ENABLED())
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001788 maybe_dtrace_line(f, &trace_info, instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001791
Mark Shannon9e7b2072021-04-13 11:08:14 +01001792 if (trace_info.cframe.use_tracing &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001793 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001795 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001797 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 err = maybe_call_line_trace(tstate->c_tracefunc,
1800 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001801 tstate, f,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001802 &trace_info, instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 /* Reload possibly changed frame fields */
1804 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001805 stack_pointer = f->f_valuestack+f->f_stackdepth;
1806 f->f_stackdepth = -1;
Mark Shannon28d28e02021-04-08 11:22:55 +01001807 if (err) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 goto error;
Mark Shannon28d28e02021-04-08 11:22:55 +01001810 }
1811 NEXTOPARG();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Mark Shannon9f2c63b2021-07-08 19:21:22 +01001813 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001814
Guido van Rossum96a42c81992-01-12 02:29:51 +00001815#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (lltrace) {
1819 if (HAS_ARG(opcode)) {
1820 printf("%d: %d, %d\n",
1821 f->f_lasti, opcode, oparg);
1822 }
1823 else {
1824 printf("%d: %d\n",
1825 f->f_lasti, opcode);
1826 }
1827 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001828#endif
Mark Shannon2be4c372021-09-29 13:16:13 +01001829#if USE_COMPUTED_GOTOS == 0
1830 goto dispatch_opcode;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001831
Mark Shannon2be4c372021-09-29 13:16:13 +01001832 predispatch:
1833 if (trace_info.cframe.use_tracing OR_DTRACE_LINE OR_LLTRACE) {
1834 goto tracing_dispatch;
1835 }
1836 f->f_lasti = INSTR_OFFSET();
1837 NEXTOPARG();
1838#endif
Mark Shannon28d28e02021-04-08 11:22:55 +01001839 dispatch_opcode:
1840#ifdef DYNAMIC_EXECUTION_PROFILE
1841#ifdef DXPAIRS
1842 dxpairs[lastopcode][opcode]++;
1843 lastopcode = opcode;
1844#endif
1845 dxp[opcode]++;
1846#endif
1847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001851 It is essential that any operation that fails must goto error
Mark Shannon28d28e02021-04-08 11:22:55 +01001852 and that all operation that succeed call DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Benjamin Petersonddd19492018-09-16 22:38:02 -07001854 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001855 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001857
Benjamin Petersonddd19492018-09-16 22:38:02 -07001858 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 PyObject *value = GETLOCAL(oparg);
1860 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001861 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 UNBOUNDLOCAL_ERROR_MSG,
1863 PyTuple_GetItem(co->co_varnames, oparg));
1864 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 Py_INCREF(value);
1867 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001868 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 }
1870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(LOAD_CONST): {
1872 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 PyObject *value = GETITEM(consts, oparg);
1874 Py_INCREF(value);
1875 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001876 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(STORE_FAST): {
1880 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 PyObject *value = POP();
1882 SETLOCAL(oparg, value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001883 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001885
Benjamin Petersonddd19492018-09-16 22:38:02 -07001886 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 PyObject *value = POP();
1888 Py_DECREF(value);
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_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *top = TOP();
1894 PyObject *second = SECOND();
1895 SET_TOP(second);
1896 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001897 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Benjamin Petersonddd19492018-09-16 22:38:02 -07001900 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 PyObject *top = TOP();
1902 PyObject *second = SECOND();
1903 PyObject *third = THIRD();
1904 SET_TOP(second);
1905 SET_SECOND(third);
1906 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001907 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Benjamin Petersonddd19492018-09-16 22:38:02 -07001910 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001911 PyObject *top = TOP();
1912 PyObject *second = SECOND();
1913 PyObject *third = THIRD();
1914 PyObject *fourth = FOURTH();
1915 SET_TOP(second);
1916 SET_SECOND(third);
1917 SET_THIRD(fourth);
1918 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001919 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001920 }
1921
Benjamin Petersonddd19492018-09-16 22:38:02 -07001922 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 PyObject *top = TOP();
1924 Py_INCREF(top);
1925 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001926 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Benjamin Petersonddd19492018-09-16 22:38:02 -07001929 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001932 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001934 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 SET_TOP(top);
1936 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001937 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001939
Benjamin Petersonddd19492018-09-16 22:38:02 -07001940 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *value = TOP();
1942 PyObject *res = PyNumber_Positive(value);
1943 Py_DECREF(value);
1944 SET_TOP(res);
1945 if (res == NULL)
1946 goto error;
1947 DISPATCH();
1948 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Benjamin Petersonddd19492018-09-16 22:38:02 -07001950 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 PyObject *value = TOP();
1952 PyObject *res = PyNumber_Negative(value);
1953 Py_DECREF(value);
1954 SET_TOP(res);
1955 if (res == NULL)
1956 goto error;
1957 DISPATCH();
1958 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PyObject *value = TOP();
1962 int err = PyObject_IsTrue(value);
1963 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 if (err == 0) {
1965 Py_INCREF(Py_True);
1966 SET_TOP(Py_True);
1967 DISPATCH();
1968 }
1969 else if (err > 0) {
1970 Py_INCREF(Py_False);
1971 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 DISPATCH();
1973 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001974 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 goto error;
1976 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Benjamin Petersonddd19492018-09-16 22:38:02 -07001978 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 PyObject *value = TOP();
1980 PyObject *res = PyNumber_Invert(value);
1981 Py_DECREF(value);
1982 SET_TOP(res);
1983 if (res == NULL)
1984 goto error;
1985 DISPATCH();
1986 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001987
Benjamin Petersonddd19492018-09-16 22:38:02 -07001988 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 PyObject *exp = POP();
1990 PyObject *base = TOP();
1991 PyObject *res = PyNumber_Power(base, exp, Py_None);
1992 Py_DECREF(base);
1993 Py_DECREF(exp);
1994 SET_TOP(res);
1995 if (res == NULL)
1996 goto error;
1997 DISPATCH();
1998 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Benjamin Petersonddd19492018-09-16 22:38:02 -07002000 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 PyObject *right = POP();
2002 PyObject *left = TOP();
2003 PyObject *res = PyNumber_Multiply(left, right);
2004 Py_DECREF(left);
2005 Py_DECREF(right);
2006 SET_TOP(res);
2007 if (res == NULL)
2008 goto error;
2009 DISPATCH();
2010 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Benjamin Petersonddd19492018-09-16 22:38:02 -07002012 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002013 PyObject *right = POP();
2014 PyObject *left = TOP();
2015 PyObject *res = PyNumber_MatrixMultiply(left, right);
2016 Py_DECREF(left);
2017 Py_DECREF(right);
2018 SET_TOP(res);
2019 if (res == NULL)
2020 goto error;
2021 DISPATCH();
2022 }
2023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 PyObject *divisor = POP();
2026 PyObject *dividend = TOP();
2027 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2028 Py_DECREF(dividend);
2029 Py_DECREF(divisor);
2030 SET_TOP(quotient);
2031 if (quotient == NULL)
2032 goto error;
2033 DISPATCH();
2034 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Benjamin Petersonddd19492018-09-16 22:38:02 -07002036 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 PyObject *divisor = POP();
2038 PyObject *dividend = TOP();
2039 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2040 Py_DECREF(dividend);
2041 Py_DECREF(divisor);
2042 SET_TOP(quotient);
2043 if (quotient == NULL)
2044 goto error;
2045 DISPATCH();
2046 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002047
Benjamin Petersonddd19492018-09-16 22:38:02 -07002048 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 PyObject *divisor = POP();
2050 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002051 PyObject *res;
2052 if (PyUnicode_CheckExact(dividend) && (
2053 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2054 // fast path; string formatting, but not if the RHS is a str subclass
2055 // (see issue28598)
2056 res = PyUnicode_Format(dividend, divisor);
2057 } else {
2058 res = PyNumber_Remainder(dividend, divisor);
2059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(divisor);
2061 Py_DECREF(dividend);
2062 SET_TOP(res);
2063 if (res == NULL)
2064 goto error;
2065 DISPATCH();
2066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Benjamin Petersonddd19492018-09-16 22:38:02 -07002068 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 PyObject *right = POP();
2070 PyObject *left = TOP();
2071 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002072 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002073 CPython using bytecode, it is simply worthless.
2074 See http://bugs.python.org/issue21955 and
2075 http://bugs.python.org/issue10044 for the discussion. In short,
2076 no patch shown any impact on a realistic benchmark, only a minor
2077 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 if (PyUnicode_CheckExact(left) &&
2079 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002080 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002081 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002082 }
2083 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 sum = PyNumber_Add(left, right);
2085 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 Py_DECREF(right);
2088 SET_TOP(sum);
2089 if (sum == NULL)
2090 goto error;
2091 DISPATCH();
2092 }
2093
Benjamin Petersonddd19492018-09-16 22:38:02 -07002094 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 PyObject *right = POP();
2096 PyObject *left = TOP();
2097 PyObject *diff = PyNumber_Subtract(left, right);
2098 Py_DECREF(right);
2099 Py_DECREF(left);
2100 SET_TOP(diff);
2101 if (diff == NULL)
2102 goto error;
2103 DISPATCH();
2104 }
2105
Benjamin Petersonddd19492018-09-16 22:38:02 -07002106 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 PyObject *sub = POP();
2108 PyObject *container = TOP();
2109 PyObject *res = PyObject_GetItem(container, sub);
2110 Py_DECREF(container);
2111 Py_DECREF(sub);
2112 SET_TOP(res);
2113 if (res == NULL)
2114 goto error;
2115 DISPATCH();
2116 }
2117
Benjamin Petersonddd19492018-09-16 22:38:02 -07002118 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 PyObject *right = POP();
2120 PyObject *left = TOP();
2121 PyObject *res = PyNumber_Lshift(left, right);
2122 Py_DECREF(left);
2123 Py_DECREF(right);
2124 SET_TOP(res);
2125 if (res == NULL)
2126 goto error;
2127 DISPATCH();
2128 }
2129
Benjamin Petersonddd19492018-09-16 22:38:02 -07002130 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 PyObject *right = POP();
2132 PyObject *left = TOP();
2133 PyObject *res = PyNumber_Rshift(left, right);
2134 Py_DECREF(left);
2135 Py_DECREF(right);
2136 SET_TOP(res);
2137 if (res == NULL)
2138 goto error;
2139 DISPATCH();
2140 }
2141
Benjamin Petersonddd19492018-09-16 22:38:02 -07002142 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 PyObject *right = POP();
2144 PyObject *left = TOP();
2145 PyObject *res = PyNumber_And(left, right);
2146 Py_DECREF(left);
2147 Py_DECREF(right);
2148 SET_TOP(res);
2149 if (res == NULL)
2150 goto error;
2151 DISPATCH();
2152 }
2153
Benjamin Petersonddd19492018-09-16 22:38:02 -07002154 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 PyObject *right = POP();
2156 PyObject *left = TOP();
2157 PyObject *res = PyNumber_Xor(left, right);
2158 Py_DECREF(left);
2159 Py_DECREF(right);
2160 SET_TOP(res);
2161 if (res == NULL)
2162 goto error;
2163 DISPATCH();
2164 }
2165
Benjamin Petersonddd19492018-09-16 22:38:02 -07002166 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 PyObject *right = POP();
2168 PyObject *left = TOP();
2169 PyObject *res = PyNumber_Or(left, right);
2170 Py_DECREF(left);
2171 Py_DECREF(right);
2172 SET_TOP(res);
2173 if (res == NULL)
2174 goto error;
2175 DISPATCH();
2176 }
2177
Benjamin Petersonddd19492018-09-16 22:38:02 -07002178 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 PyObject *v = POP();
2180 PyObject *list = PEEK(oparg);
2181 int err;
2182 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 if (err != 0)
2185 goto error;
2186 PREDICT(JUMP_ABSOLUTE);
2187 DISPATCH();
2188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002189
Benjamin Petersonddd19492018-09-16 22:38:02 -07002190 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002192 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 int err;
2194 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 if (err != 0)
2197 goto error;
2198 PREDICT(JUMP_ABSOLUTE);
2199 DISPATCH();
2200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Benjamin Petersonddd19492018-09-16 22:38:02 -07002202 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 PyObject *exp = POP();
2204 PyObject *base = TOP();
2205 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2206 Py_DECREF(base);
2207 Py_DECREF(exp);
2208 SET_TOP(res);
2209 if (res == NULL)
2210 goto error;
2211 DISPATCH();
2212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Benjamin Petersonddd19492018-09-16 22:38:02 -07002214 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 PyObject *right = POP();
2216 PyObject *left = TOP();
2217 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2218 Py_DECREF(left);
2219 Py_DECREF(right);
2220 SET_TOP(res);
2221 if (res == NULL)
2222 goto error;
2223 DISPATCH();
2224 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Benjamin Petersonddd19492018-09-16 22:38:02 -07002226 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002227 PyObject *right = POP();
2228 PyObject *left = TOP();
2229 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2230 Py_DECREF(left);
2231 Py_DECREF(right);
2232 SET_TOP(res);
2233 if (res == NULL)
2234 goto error;
2235 DISPATCH();
2236 }
2237
Benjamin Petersonddd19492018-09-16 22:38:02 -07002238 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 PyObject *divisor = POP();
2240 PyObject *dividend = TOP();
2241 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2242 Py_DECREF(dividend);
2243 Py_DECREF(divisor);
2244 SET_TOP(quotient);
2245 if (quotient == NULL)
2246 goto error;
2247 DISPATCH();
2248 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002249
Benjamin Petersonddd19492018-09-16 22:38:02 -07002250 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 PyObject *divisor = POP();
2252 PyObject *dividend = TOP();
2253 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2254 Py_DECREF(dividend);
2255 Py_DECREF(divisor);
2256 SET_TOP(quotient);
2257 if (quotient == NULL)
2258 goto error;
2259 DISPATCH();
2260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002261
Benjamin Petersonddd19492018-09-16 22:38:02 -07002262 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 PyObject *right = POP();
2264 PyObject *left = TOP();
2265 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2266 Py_DECREF(left);
2267 Py_DECREF(right);
2268 SET_TOP(mod);
2269 if (mod == NULL)
2270 goto error;
2271 DISPATCH();
2272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Benjamin Petersonddd19492018-09-16 22:38:02 -07002274 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 PyObject *right = POP();
2276 PyObject *left = TOP();
2277 PyObject *sum;
2278 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002279 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002280 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002281 }
2282 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 sum = PyNumber_InPlaceAdd(left, right);
2284 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002285 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 Py_DECREF(right);
2287 SET_TOP(sum);
2288 if (sum == NULL)
2289 goto error;
2290 DISPATCH();
2291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Benjamin Petersonddd19492018-09-16 22:38:02 -07002293 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 PyObject *right = POP();
2295 PyObject *left = TOP();
2296 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2297 Py_DECREF(left);
2298 Py_DECREF(right);
2299 SET_TOP(diff);
2300 if (diff == NULL)
2301 goto error;
2302 DISPATCH();
2303 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Benjamin Petersonddd19492018-09-16 22:38:02 -07002305 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 PyObject *right = POP();
2307 PyObject *left = TOP();
2308 PyObject *res = PyNumber_InPlaceLshift(left, right);
2309 Py_DECREF(left);
2310 Py_DECREF(right);
2311 SET_TOP(res);
2312 if (res == NULL)
2313 goto error;
2314 DISPATCH();
2315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Benjamin Petersonddd19492018-09-16 22:38:02 -07002317 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 PyObject *right = POP();
2319 PyObject *left = TOP();
2320 PyObject *res = PyNumber_InPlaceRshift(left, right);
2321 Py_DECREF(left);
2322 Py_DECREF(right);
2323 SET_TOP(res);
2324 if (res == NULL)
2325 goto error;
2326 DISPATCH();
2327 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Benjamin Petersonddd19492018-09-16 22:38:02 -07002329 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 PyObject *right = POP();
2331 PyObject *left = TOP();
2332 PyObject *res = PyNumber_InPlaceAnd(left, right);
2333 Py_DECREF(left);
2334 Py_DECREF(right);
2335 SET_TOP(res);
2336 if (res == NULL)
2337 goto error;
2338 DISPATCH();
2339 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Benjamin Petersonddd19492018-09-16 22:38:02 -07002341 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 PyObject *right = POP();
2343 PyObject *left = TOP();
2344 PyObject *res = PyNumber_InPlaceXor(left, right);
2345 Py_DECREF(left);
2346 Py_DECREF(right);
2347 SET_TOP(res);
2348 if (res == NULL)
2349 goto error;
2350 DISPATCH();
2351 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002352
Benjamin Petersonddd19492018-09-16 22:38:02 -07002353 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 PyObject *right = POP();
2355 PyObject *left = TOP();
2356 PyObject *res = PyNumber_InPlaceOr(left, right);
2357 Py_DECREF(left);
2358 Py_DECREF(right);
2359 SET_TOP(res);
2360 if (res == NULL)
2361 goto error;
2362 DISPATCH();
2363 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002364
Benjamin Petersonddd19492018-09-16 22:38:02 -07002365 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 PyObject *sub = TOP();
2367 PyObject *container = SECOND();
2368 PyObject *v = THIRD();
2369 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002370 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002371 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 Py_DECREF(container);
2375 Py_DECREF(sub);
2376 if (err != 0)
2377 goto error;
2378 DISPATCH();
2379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002380
Benjamin Petersonddd19492018-09-16 22:38:02 -07002381 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 PyObject *sub = TOP();
2383 PyObject *container = SECOND();
2384 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002385 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002386 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 err = PyObject_DelItem(container, sub);
2388 Py_DECREF(container);
2389 Py_DECREF(sub);
2390 if (err != 0)
2391 goto error;
2392 DISPATCH();
2393 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002394
Benjamin Petersonddd19492018-09-16 22:38:02 -07002395 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002396 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002398 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002399 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 _PyErr_SetString(tstate, PyExc_RuntimeError,
2402 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 Py_DECREF(value);
2404 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002406 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 Py_DECREF(value);
2408 if (res == NULL)
2409 goto error;
2410 Py_DECREF(res);
2411 DISPATCH();
2412 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002413
Benjamin Petersonddd19492018-09-16 22:38:02 -07002414 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 switch (oparg) {
2417 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002419 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002422 /* fall through */
2423 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002424 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002425 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 break;
2428 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 _PyErr_SetString(tstate, PyExc_SystemError,
2430 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 break;
2432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 goto error;
2434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Benjamin Petersonddd19492018-09-16 22:38:02 -07002436 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002438 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002439 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002440 f->f_state = FRAME_RETURNED;
2441 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002442 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002444
Benjamin Petersonddd19492018-09-16 22:38:02 -07002445 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002446 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002447 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002448 PyObject *obj = TOP();
2449 PyTypeObject *type = Py_TYPE(obj);
2450
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002451 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002452 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002453 }
Yury Selivanov75445082015-05-11 22:57:16 -04002454
2455 if (getter != NULL) {
2456 iter = (*getter)(obj);
2457 Py_DECREF(obj);
2458 if (iter == NULL) {
2459 SET_TOP(NULL);
2460 goto error;
2461 }
2462 }
2463 else {
2464 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 _PyErr_Format(tstate, PyExc_TypeError,
2466 "'async for' requires an object with "
2467 "__aiter__ method, got %.100s",
2468 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002469 Py_DECREF(obj);
2470 goto error;
2471 }
2472
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002473 if (Py_TYPE(iter)->tp_as_async == NULL ||
2474 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002475
Yury Selivanov398ff912017-03-02 22:20:00 -05002476 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002477 _PyErr_Format(tstate, PyExc_TypeError,
2478 "'async for' received an object from __aiter__ "
2479 "that does not implement __anext__: %.100s",
2480 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002481 Py_DECREF(iter);
2482 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002483 }
2484
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002485 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002486 DISPATCH();
2487 }
2488
Benjamin Petersonddd19492018-09-16 22:38:02 -07002489 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002490 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002491 PyObject *next_iter = NULL;
2492 PyObject *awaitable = NULL;
2493 PyObject *aiter = TOP();
2494 PyTypeObject *type = Py_TYPE(aiter);
2495
Yury Selivanoveb636452016-09-08 22:01:51 -07002496 if (PyAsyncGen_CheckExact(aiter)) {
2497 awaitable = type->tp_as_async->am_anext(aiter);
2498 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002499 goto error;
2500 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002501 } else {
2502 if (type->tp_as_async != NULL){
2503 getter = type->tp_as_async->am_anext;
2504 }
Yury Selivanov75445082015-05-11 22:57:16 -04002505
Yury Selivanoveb636452016-09-08 22:01:51 -07002506 if (getter != NULL) {
2507 next_iter = (*getter)(aiter);
2508 if (next_iter == NULL) {
2509 goto error;
2510 }
2511 }
2512 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002513 _PyErr_Format(tstate, PyExc_TypeError,
2514 "'async for' requires an iterator with "
2515 "__anext__ method, got %.100s",
2516 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002517 goto error;
2518 }
Yury Selivanov75445082015-05-11 22:57:16 -04002519
Yury Selivanoveb636452016-09-08 22:01:51 -07002520 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2521 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002522 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002523 PyExc_TypeError,
2524 "'async for' received an invalid object "
2525 "from __anext__: %.100s",
2526 Py_TYPE(next_iter)->tp_name);
2527
2528 Py_DECREF(next_iter);
2529 goto error;
2530 } else {
2531 Py_DECREF(next_iter);
2532 }
2533 }
Yury Selivanov75445082015-05-11 22:57:16 -04002534
2535 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002536 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002537 DISPATCH();
2538 }
2539
Benjamin Petersonddd19492018-09-16 22:38:02 -07002540 case TARGET(GET_AWAITABLE): {
2541 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002542 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002543 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002544
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002545 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002546 int opcode_at_minus_3 = 0;
2547 if ((next_instr - first_instr) > 2) {
2548 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2549 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002550 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002551 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002552 _Py_OPCODE(next_instr[-2]));
2553 }
2554
Yury Selivanov75445082015-05-11 22:57:16 -04002555 Py_DECREF(iterable);
2556
Yury Selivanovc724bae2016-03-02 11:30:46 -05002557 if (iter != NULL && PyCoro_CheckExact(iter)) {
2558 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2559 if (yf != NULL) {
2560 /* `iter` is a coroutine object that is being
2561 awaited, `yf` is a pointer to the current awaitable
2562 being awaited on. */
2563 Py_DECREF(yf);
2564 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002565 _PyErr_SetString(tstate, PyExc_RuntimeError,
2566 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002567 /* The code below jumps to `error` if `iter` is NULL. */
2568 }
2569 }
2570
Yury Selivanov75445082015-05-11 22:57:16 -04002571 SET_TOP(iter); /* Even if it's NULL */
2572
2573 if (iter == NULL) {
2574 goto error;
2575 }
2576
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002577 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002578 DISPATCH();
2579 }
2580
Benjamin Petersonddd19492018-09-16 22:38:02 -07002581 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002583 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002584 PySendResult gen_status;
2585 if (tstate->c_tracefunc == NULL) {
2586 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002587 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002588 _Py_IDENTIFIER(send);
Victor Stinner09bbebe2021-04-11 00:17:39 +02002589 if (Py_IsNone(v) && PyIter_Check(receiver)) {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002590 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002591 }
2592 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002593 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002594 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002595 if (retval == NULL) {
2596 if (tstate->c_tracefunc != NULL
2597 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002598 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002599 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2600 gen_status = PYGEN_RETURN;
2601 }
2602 else {
2603 gen_status = PYGEN_ERROR;
2604 }
2605 }
2606 else {
2607 gen_status = PYGEN_NEXT;
2608 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002611 if (gen_status == PYGEN_ERROR) {
2612 assert (retval == NULL);
2613 goto error;
2614 }
2615 if (gen_status == PYGEN_RETURN) {
2616 assert (retval != NULL);
2617
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002618 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002619 SET_TOP(retval);
2620 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002622 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002623 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002624 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002625 /* and repeat... */
Mark Shannonfcb55c02021-04-01 16:00:31 +01002626 assert(f->f_lasti > 0);
2627 f->f_lasti -= 1;
Mark Shannoncb9879b2020-07-17 11:44:23 +01002628 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002629 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002630 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002632
Benjamin Petersonddd19492018-09-16 22:38:02 -07002633 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002635
2636 if (co->co_flags & CO_ASYNC_GENERATOR) {
2637 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2638 Py_DECREF(retval);
2639 if (w == NULL) {
2640 retval = NULL;
2641 goto error;
2642 }
2643 retval = w;
2644 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002645 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002646 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002647 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002649
Mark Shannonb37181e2021-04-06 11:48:59 +01002650 case TARGET(GEN_START): {
2651 PyObject *none = POP();
Mark Shannon99c72322021-12-08 14:46:32 +00002652 assert(none == Py_None);
2653 assert(oparg < 3);
Mark Shannonb37181e2021-04-06 11:48:59 +01002654 Py_DECREF(none);
Mark Shannonb37181e2021-04-06 11:48:59 +01002655 DISPATCH();
2656 }
2657
Benjamin Petersonddd19492018-09-16 22:38:02 -07002658 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002659 PyObject *type, *value, *traceback;
2660 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 PyTryBlock *b = PyFrame_BlockPop(f);
2662 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002663 _PyErr_SetString(tstate, PyExc_SystemError,
2664 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002667 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2668 STACK_LEVEL() <= (b)->b_level + 4);
2669 exc_info = tstate->exc_info;
2670 type = exc_info->exc_type;
2671 value = exc_info->exc_value;
2672 traceback = exc_info->exc_traceback;
2673 exc_info->exc_type = POP();
2674 exc_info->exc_value = POP();
2675 exc_info->exc_traceback = POP();
2676 Py_XDECREF(type);
2677 Py_XDECREF(value);
2678 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002681
Benjamin Petersonddd19492018-09-16 22:38:02 -07002682 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002683 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002686
Mark Shannonfee55262019-11-21 09:11:43 +00002687 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002688 assert(f->f_iblock > 0);
2689 if (oparg) {
2690 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2691 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002692 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002693 PyObject *val = POP();
2694 PyObject *tb = POP();
2695 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002696 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002697 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002699
Benjamin Petersonddd19492018-09-16 22:38:02 -07002700 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002701 PyObject *exc = POP();
2702 assert(PyExceptionClass_Check(exc));
2703 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2704 PyTryBlock *b = PyFrame_BlockPop(f);
2705 assert(b->b_type == EXCEPT_HANDLER);
2706 Py_DECREF(exc);
2707 UNWIND_EXCEPT_HANDLER(b);
2708 Py_DECREF(POP());
2709 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002710 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002711 }
2712 else {
2713 PyObject *val = POP();
2714 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002715 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002716 goto exception_unwind;
2717 }
2718 }
2719
Zackery Spytzce6a0702019-08-25 03:44:09 -06002720 case TARGET(LOAD_ASSERTION_ERROR): {
2721 PyObject *value = PyExc_AssertionError;
2722 Py_INCREF(value);
2723 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002724 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002725 }
2726
Benjamin Petersonddd19492018-09-16 22:38:02 -07002727 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002728 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002729
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002731 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002732 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002734 if (!_PyErr_Occurred(tstate)) {
2735 _PyErr_SetString(tstate, PyExc_NameError,
2736 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002739 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002741 }
2742 else {
2743 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2744 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002745 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2747 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002748 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2749 _PyErr_SetString(tstate, PyExc_NameError,
2750 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002755 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002756 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002757
Benjamin Petersonddd19492018-09-16 22:38:02 -07002758 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 PyObject *name = GETITEM(names, oparg);
2760 PyObject *v = POP();
2761 PyObject *ns = f->f_locals;
2762 int err;
2763 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002764 _PyErr_Format(tstate, PyExc_SystemError,
2765 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 if (PyDict_CheckExact(ns))
2770 err = PyDict_SetItem(ns, name, v);
2771 else
2772 err = PyObject_SetItem(ns, name, v);
2773 Py_DECREF(v);
2774 if (err != 0)
2775 goto error;
2776 DISPATCH();
2777 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002778
Benjamin Petersonddd19492018-09-16 22:38:02 -07002779 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *name = GETITEM(names, oparg);
2781 PyObject *ns = f->f_locals;
2782 int err;
2783 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002784 _PyErr_Format(tstate, PyExc_SystemError,
2785 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 err = PyObject_DelItem(ns, name);
2789 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002790 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 NAME_ERROR_MSG,
2792 name);
2793 goto error;
2794 }
2795 DISPATCH();
2796 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002797
Benjamin Petersonddd19492018-09-16 22:38:02 -07002798 case TARGET(UNPACK_SEQUENCE): {
2799 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 PyObject *seq = POP(), *item, **items;
2801 if (PyTuple_CheckExact(seq) &&
2802 PyTuple_GET_SIZE(seq) == oparg) {
2803 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 item = items[oparg];
2806 Py_INCREF(item);
2807 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 } else if (PyList_CheckExact(seq) &&
2810 PyList_GET_SIZE(seq) == oparg) {
2811 items = ((PyListObject *)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 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002817 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002819 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 } else {
2821 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 Py_DECREF(seq);
2823 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002826 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002828
Benjamin Petersonddd19492018-09-16 22:38:02 -07002829 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2831 PyObject *seq = POP();
2832
Victor Stinner438a12d2019-05-24 17:01:38 +02002833 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 stack_pointer + totalargs)) {
2835 stack_pointer += totalargs;
2836 } else {
2837 Py_DECREF(seq);
2838 goto error;
2839 }
2840 Py_DECREF(seq);
2841 DISPATCH();
2842 }
2843
Benjamin Petersonddd19492018-09-16 22:38:02 -07002844 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 PyObject *name = GETITEM(names, oparg);
2846 PyObject *owner = TOP();
2847 PyObject *v = SECOND();
2848 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002849 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 Py_DECREF(owner);
2853 if (err != 0)
2854 goto error;
2855 DISPATCH();
2856 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002857
Benjamin Petersonddd19492018-09-16 22:38:02 -07002858 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *name = GETITEM(names, oparg);
2860 PyObject *owner = POP();
2861 int err;
2862 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2863 Py_DECREF(owner);
2864 if (err != 0)
2865 goto error;
2866 DISPATCH();
2867 }
2868
Benjamin Petersonddd19492018-09-16 22:38:02 -07002869 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 PyObject *name = GETITEM(names, oparg);
2871 PyObject *v = POP();
2872 int err;
2873 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 if (err != 0)
2876 goto error;
2877 DISPATCH();
2878 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002879
Benjamin Petersonddd19492018-09-16 22:38:02 -07002880 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 PyObject *name = GETITEM(names, oparg);
2882 int err;
2883 err = PyDict_DelItem(f->f_globals, name);
2884 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002885 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2886 format_exc_check_arg(tstate, PyExc_NameError,
2887 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002888 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002890 }
2891 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002893
Benjamin Petersonddd19492018-09-16 22:38:02 -07002894 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 PyObject *name = GETITEM(names, oparg);
2896 PyObject *locals = f->f_locals;
2897 PyObject *v;
2898 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002899 _PyErr_Format(tstate, PyExc_SystemError,
2900 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002904 v = PyDict_GetItemWithError(locals, name);
2905 if (v != NULL) {
2906 Py_INCREF(v);
2907 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002908 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002909 goto error;
2910 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 }
2912 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002914 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002915 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002916 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002917 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 }
2919 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002921 v = PyDict_GetItemWithError(f->f_globals, name);
2922 if (v != NULL) {
2923 Py_INCREF(v);
2924 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002925 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002926 goto error;
2927 }
2928 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002929 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002930 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002932 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002933 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002934 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002936 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002940 }
2941 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 v = PyObject_GetItem(f->f_builtins, name);
2943 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002944 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002945 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002946 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002948 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002950 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002957
Benjamin Petersonddd19492018-09-16 22:38:02 -07002958 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002959 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002961 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002962 && PyDict_CheckExact(f->f_builtins))
2963 {
Inada Naoki91234a12019-06-03 21:30:58 +09002964 OPCACHE_CHECK();
2965 if (co_opcache != NULL && co_opcache->optimized > 0) {
2966 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2967
2968 if (lg->globals_ver ==
2969 ((PyDictObject *)f->f_globals)->ma_version_tag
2970 && lg->builtins_ver ==
2971 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2972 {
2973 PyObject *ptr = lg->ptr;
2974 OPCACHE_STAT_GLOBAL_HIT();
2975 assert(ptr != NULL);
2976 Py_INCREF(ptr);
2977 PUSH(ptr);
2978 DISPATCH();
2979 }
2980 }
2981
2982 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002984 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 name);
2986 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002987 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002988 /* _PyDict_LoadGlobal() returns NULL without raising
2989 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002990 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002991 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002992 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 }
Inada Naoki91234a12019-06-03 21:30:58 +09002995
2996 if (co_opcache != NULL) {
2997 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2998
2999 if (co_opcache->optimized == 0) {
3000 /* Wasn't optimized before. */
3001 OPCACHE_STAT_GLOBAL_OPT();
3002 } else {
3003 OPCACHE_STAT_GLOBAL_MISS();
3004 }
3005
3006 co_opcache->optimized = 1;
3007 lg->globals_ver =
3008 ((PyDictObject *)f->f_globals)->ma_version_tag;
3009 lg->builtins_ver =
3010 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3011 lg->ptr = v; /* borrowed */
3012 }
3013
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003016 else {
3017 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003018
3019 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003020 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 v = PyObject_GetItem(f->f_globals, name);
3022 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003023 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003024 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003025 }
3026 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003027
Victor Stinnerb4efc962015-11-20 09:24:02 +01003028 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 v = PyObject_GetItem(f->f_builtins, name);
3030 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003031 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003032 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003033 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003034 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003035 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003037 }
3038 }
3039 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003043
Benjamin Petersonddd19492018-09-16 22:38:02 -07003044 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 PyObject *v = GETLOCAL(oparg);
3046 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 SETLOCAL(oparg, NULL);
3048 DISPATCH();
3049 }
3050 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003051 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 UNBOUNDLOCAL_ERROR_MSG,
3053 PyTuple_GetItem(co->co_varnames, oparg)
3054 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 goto error;
3056 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003057
Benjamin Petersonddd19492018-09-16 22:38:02 -07003058 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003060 PyObject *oldobj = PyCell_GET(cell);
3061 if (oldobj != NULL) {
3062 PyCell_SET(cell, NULL);
3063 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003064 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003065 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003066 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 goto error;
3068 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003069
Benjamin Petersonddd19492018-09-16 22:38:02 -07003070 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 PyObject *cell = freevars[oparg];
3072 Py_INCREF(cell);
3073 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003076
Benjamin Petersonddd19492018-09-16 22:38:02 -07003077 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003078 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003079 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003080 assert(locals);
3081 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3082 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3083 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3084 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3085 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003086 value = PyDict_GetItemWithError(locals, name);
3087 if (value != NULL) {
3088 Py_INCREF(value);
3089 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003090 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003091 goto error;
3092 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003093 }
3094 else {
3095 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003096 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003097 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003098 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003099 }
3100 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003101 }
3102 }
3103 if (!value) {
3104 PyObject *cell = freevars[oparg];
3105 value = PyCell_GET(cell);
3106 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003107 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003108 goto error;
3109 }
3110 Py_INCREF(value);
3111 }
3112 PUSH(value);
3113 DISPATCH();
3114 }
3115
Benjamin Petersonddd19492018-09-16 22:38:02 -07003116 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PyObject *cell = freevars[oparg];
3118 PyObject *value = PyCell_GET(cell);
3119 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003120 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 Py_INCREF(value);
3124 PUSH(value);
3125 DISPATCH();
3126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003127
Benjamin Petersonddd19492018-09-16 22:38:02 -07003128 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 PyObject *v = POP();
3130 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003131 PyObject *oldobj = PyCell_GET(cell);
3132 PyCell_SET(cell, v);
3133 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 DISPATCH();
3135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003136
Benjamin Petersonddd19492018-09-16 22:38:02 -07003137 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003138 PyObject *str;
3139 PyObject *empty = PyUnicode_New(0, 0);
3140 if (empty == NULL) {
3141 goto error;
3142 }
3143 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3144 Py_DECREF(empty);
3145 if (str == NULL)
3146 goto error;
3147 while (--oparg >= 0) {
3148 PyObject *item = POP();
3149 Py_DECREF(item);
3150 }
3151 PUSH(str);
3152 DISPATCH();
3153 }
3154
Benjamin Petersonddd19492018-09-16 22:38:02 -07003155 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 PyObject *tup = PyTuple_New(oparg);
3157 if (tup == NULL)
3158 goto error;
3159 while (--oparg >= 0) {
3160 PyObject *item = POP();
3161 PyTuple_SET_ITEM(tup, oparg, item);
3162 }
3163 PUSH(tup);
3164 DISPATCH();
3165 }
3166
Benjamin Petersonddd19492018-09-16 22:38:02 -07003167 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 PyObject *list = PyList_New(oparg);
3169 if (list == NULL)
3170 goto error;
3171 while (--oparg >= 0) {
3172 PyObject *item = POP();
3173 PyList_SET_ITEM(list, oparg, item);
3174 }
3175 PUSH(list);
3176 DISPATCH();
3177 }
3178
Mark Shannon13bc1392020-01-23 09:25:17 +00003179 case TARGET(LIST_TO_TUPLE): {
3180 PyObject *list = POP();
3181 PyObject *tuple = PyList_AsTuple(list);
3182 Py_DECREF(list);
3183 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003184 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003185 }
3186 PUSH(tuple);
3187 DISPATCH();
3188 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003189
Mark Shannon13bc1392020-01-23 09:25:17 +00003190 case TARGET(LIST_EXTEND): {
3191 PyObject *iterable = POP();
3192 PyObject *list = PEEK(oparg);
3193 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3194 if (none_val == NULL) {
3195 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003196 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003197 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003198 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003199 _PyErr_Format(tstate, PyExc_TypeError,
3200 "Value after * must be an iterable, not %.200s",
3201 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003202 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003203 Py_DECREF(iterable);
3204 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003205 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003206 Py_DECREF(none_val);
3207 Py_DECREF(iterable);
3208 DISPATCH();
3209 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003210
Mark Shannon13bc1392020-01-23 09:25:17 +00003211 case TARGET(SET_UPDATE): {
3212 PyObject *iterable = POP();
3213 PyObject *set = PEEK(oparg);
3214 int err = _PySet_Update(set, iterable);
3215 Py_DECREF(iterable);
3216 if (err < 0) {
3217 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003218 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003219 DISPATCH();
3220 }
3221
Benjamin Petersonddd19492018-09-16 22:38:02 -07003222 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003223 PyObject *set = PySet_New(NULL);
3224 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003225 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 if (set == NULL)
3227 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003228 for (i = oparg; i > 0; i--) {
3229 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 if (err == 0)
3231 err = PySet_Add(set, item);
3232 Py_DECREF(item);
3233 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003234 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 if (err != 0) {
3236 Py_DECREF(set);
3237 goto error;
3238 }
3239 PUSH(set);
3240 DISPATCH();
3241 }
3242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003244 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3246 if (map == NULL)
3247 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003248 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003249 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003250 PyObject *key = PEEK(2*i);
3251 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003252 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003253 if (err != 0) {
3254 Py_DECREF(map);
3255 goto error;
3256 }
3257 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003258
3259 while (oparg--) {
3260 Py_DECREF(POP());
3261 Py_DECREF(POP());
3262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 PUSH(map);
3264 DISPATCH();
3265 }
3266
Benjamin Petersonddd19492018-09-16 22:38:02 -07003267 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003268 _Py_IDENTIFIER(__annotations__);
3269 int err;
3270 PyObject *ann_dict;
3271 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003272 _PyErr_Format(tstate, PyExc_SystemError,
3273 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 goto error;
3275 }
3276 /* check if __annotations__ in locals()... */
3277 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003278 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003279 &PyId___annotations__);
3280 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003281 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003282 goto error;
3283 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003284 /* ...if not, create a new one */
3285 ann_dict = PyDict_New();
3286 if (ann_dict == NULL) {
3287 goto error;
3288 }
3289 err = _PyDict_SetItemId(f->f_locals,
3290 &PyId___annotations__, ann_dict);
3291 Py_DECREF(ann_dict);
3292 if (err != 0) {
3293 goto error;
3294 }
3295 }
3296 }
3297 else {
3298 /* do the same if locals() is not a dict */
3299 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3300 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003301 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003302 }
3303 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3304 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003305 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003306 goto error;
3307 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003308 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003309 ann_dict = PyDict_New();
3310 if (ann_dict == NULL) {
3311 goto error;
3312 }
3313 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3314 Py_DECREF(ann_dict);
3315 if (err != 0) {
3316 goto error;
3317 }
3318 }
3319 else {
3320 Py_DECREF(ann_dict);
3321 }
3322 }
3323 DISPATCH();
3324 }
3325
Benjamin Petersonddd19492018-09-16 22:38:02 -07003326 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003327 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003328 PyObject *map;
3329 PyObject *keys = TOP();
3330 if (!PyTuple_CheckExact(keys) ||
3331 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003332 _PyErr_SetString(tstate, PyExc_SystemError,
3333 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003334 goto error;
3335 }
3336 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3337 if (map == NULL) {
3338 goto error;
3339 }
3340 for (i = oparg; i > 0; i--) {
3341 int err;
3342 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3343 PyObject *value = PEEK(i + 1);
3344 err = PyDict_SetItem(map, key, value);
3345 if (err != 0) {
3346 Py_DECREF(map);
3347 goto error;
3348 }
3349 }
3350
3351 Py_DECREF(POP());
3352 while (oparg--) {
3353 Py_DECREF(POP());
3354 }
3355 PUSH(map);
3356 DISPATCH();
3357 }
3358
Mark Shannon8a4cd702020-01-27 09:57:45 +00003359 case TARGET(DICT_UPDATE): {
3360 PyObject *update = POP();
3361 PyObject *dict = PEEK(oparg);
3362 if (PyDict_Update(dict, update) < 0) {
3363 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3364 _PyErr_Format(tstate, PyExc_TypeError,
3365 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003366 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003367 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003368 Py_DECREF(update);
3369 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003370 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003371 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003372 DISPATCH();
3373 }
3374
Mark Shannon8a4cd702020-01-27 09:57:45 +00003375 case TARGET(DICT_MERGE): {
3376 PyObject *update = POP();
3377 PyObject *dict = PEEK(oparg);
3378
3379 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3380 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3381 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003382 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003383 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003384 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003385 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003386 DISPATCH();
3387 }
3388
Benjamin Petersonddd19492018-09-16 22:38:02 -07003389 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003390 PyObject *value = TOP();
3391 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 PyObject *map;
3393 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003394 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003395 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003397 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 Py_DECREF(value);
3399 Py_DECREF(key);
3400 if (err != 0)
3401 goto error;
3402 PREDICT(JUMP_ABSOLUTE);
3403 DISPATCH();
3404 }
3405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 PyObject *name = GETITEM(names, oparg);
3408 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003409
3410 PyTypeObject *type = Py_TYPE(owner);
3411 PyObject *res;
3412 PyObject **dictptr;
3413 PyObject *dict;
3414 _PyOpCodeOpt_LoadAttr *la;
3415
3416 OPCACHE_STAT_ATTR_TOTAL();
3417
3418 OPCACHE_CHECK();
3419 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3420 {
3421 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003422 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003423 la = &co_opcache->u.la;
3424 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3425 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003426 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3427 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3428 // so ~offset is strictly < -1 (assuming 2's complement).
3429 if (la->hint < -1) {
3430 // Even faster path -- slot hint.
3431 Py_ssize_t offset = ~la->hint;
3432 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3433 char *addr = (char *)owner + offset;
3434 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003435 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003436 Py_INCREF(res);
3437 SET_TOP(res);
3438 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003439 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003440 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003441 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3442 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003443 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003444 // Fast path for dict.
3445 assert(type->tp_dict != NULL);
3446 assert(type->tp_dictoffset > 0);
3447
3448 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3449 dict = *dictptr;
3450 if (dict != NULL && PyDict_CheckExact(dict)) {
3451 Py_ssize_t hint = la->hint;
3452 Py_INCREF(dict);
3453 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003454 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003455 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003456 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003457 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003458 if (la->hint == hint && hint >= 0) {
3459 // Our hint has helped -- cache hit.
3460 OPCACHE_STAT_ATTR_HIT();
3461 } else {
3462 // The hint we provided didn't work.
3463 // Maybe next time?
3464 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3465 }
3466
3467 Py_INCREF(res);
3468 SET_TOP(res);
3469 Py_DECREF(owner);
3470 Py_DECREF(dict);
3471 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003472 }
3473 else {
3474 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003475 // This attribute can be missing sometimes;
3476 // we don't want to optimize this lookup.
3477 OPCACHE_DEOPT_LOAD_ATTR();
3478 Py_DECREF(dict);
3479 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003480 }
3481 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003482 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3483 OPCACHE_DEOPT_LOAD_ATTR();
3484 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003485 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003486 }
3487 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003488 // The type of the object has either been updated,
3489 // or is different. Maybe it will stabilize?
3490 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3491 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003492 OPCACHE_STAT_ATTR_MISS();
3493 }
3494
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003495 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003496 type->tp_getattro == PyObject_GenericGetAttr)
3497 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003498 if (type->tp_dict == NULL) {
3499 if (PyType_Ready(type) < 0) {
3500 Py_DECREF(owner);
3501 SET_TOP(NULL);
3502 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003503 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003504 }
3505 PyObject *descr = _PyType_Lookup(type, name);
3506 if (descr != NULL) {
3507 // We found an attribute with a data-like descriptor.
3508 PyTypeObject *dtype = Py_TYPE(descr);
3509 if (dtype == &PyMemberDescr_Type) { // It's a slot
3510 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3511 struct PyMemberDef *dmem = member->d_member;
3512 if (dmem->type == T_OBJECT_EX) {
3513 Py_ssize_t offset = dmem->offset;
3514 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003515
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003516 if (co_opcache->optimized == 0) {
3517 // First time we optimize this opcode.
3518 OPCACHE_STAT_ATTR_OPT();
3519 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3520 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3521 }
3522
3523 la = &co_opcache->u.la;
3524 la->type = type;
3525 la->tp_version_tag = type->tp_version_tag;
3526 la->hint = ~offset;
3527
3528 char *addr = (char *)owner + offset;
3529 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003530 if (res != NULL) {
3531 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003532 Py_DECREF(owner);
3533 SET_TOP(res);
3534
Pablo Galindo109826c2020-10-20 06:22:44 +01003535 DISPATCH();
3536 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003537 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003538 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003539 // Else it's a slot of a different type. We don't handle those.
3540 }
3541 // Else it's some other kind of descriptor that we don't handle.
3542 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003543 }
3544 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003545 // We found an instance with a __dict__.
3546 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3547 dict = *dictptr;
3548
3549 if (dict != NULL && PyDict_CheckExact(dict)) {
3550 Py_INCREF(dict);
3551 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003552 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003553 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3554 if (res != NULL) {
3555 Py_INCREF(res);
3556 Py_DECREF(dict);
3557 Py_DECREF(owner);
3558 SET_TOP(res);
3559
3560 if (co_opcache->optimized == 0) {
3561 // First time we optimize this opcode.
3562 OPCACHE_STAT_ATTR_OPT();
3563 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3564 }
3565
3566 la = &co_opcache->u.la;
3567 la->type = type;
3568 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003569 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003570 la->hint = hint;
3571
3572 DISPATCH();
3573 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003574 else {
3575 _PyErr_Clear(tstate);
3576 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003577 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003578 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003579 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003580 OPCACHE_DEOPT_LOAD_ATTR();
3581 }
3582 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003583 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003584 OPCACHE_DEOPT_LOAD_ATTR();
3585 }
3586 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3587 OPCACHE_DEOPT_LOAD_ATTR();
3588 }
3589 }
3590
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003591 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003592 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 Py_DECREF(owner);
3594 SET_TOP(res);
3595 if (res == NULL)
3596 goto error;
3597 DISPATCH();
3598 }
3599
Benjamin Petersonddd19492018-09-16 22:38:02 -07003600 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003601 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 PyObject *right = POP();
3603 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003604 PyObject *res = PyObject_RichCompare(left, right, oparg);
3605 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606 Py_DECREF(left);
3607 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003608 if (res == NULL)
3609 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 PREDICT(POP_JUMP_IF_FALSE);
3611 PREDICT(POP_JUMP_IF_TRUE);
3612 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003614
Mark Shannon9af0e472020-01-14 10:12:45 +00003615 case TARGET(IS_OP): {
3616 PyObject *right = POP();
3617 PyObject *left = TOP();
Victor Stinner09bbebe2021-04-11 00:17:39 +02003618 int res = Py_Is(left, right) ^ oparg;
Mark Shannon9af0e472020-01-14 10:12:45 +00003619 PyObject *b = res ? Py_True : Py_False;
3620 Py_INCREF(b);
3621 SET_TOP(b);
3622 Py_DECREF(left);
3623 Py_DECREF(right);
3624 PREDICT(POP_JUMP_IF_FALSE);
3625 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003626 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003627 }
3628
3629 case TARGET(CONTAINS_OP): {
3630 PyObject *right = POP();
3631 PyObject *left = POP();
3632 int res = PySequence_Contains(right, left);
3633 Py_DECREF(left);
3634 Py_DECREF(right);
3635 if (res < 0) {
3636 goto error;
3637 }
3638 PyObject *b = (res^oparg) ? Py_True : Py_False;
3639 Py_INCREF(b);
3640 PUSH(b);
3641 PREDICT(POP_JUMP_IF_FALSE);
3642 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003643 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003644 }
3645
3646#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3647 "BaseException is not allowed"
3648
3649 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3650 PyObject *right = POP();
3651 PyObject *left = POP();
3652 if (PyTuple_Check(right)) {
3653 Py_ssize_t i, length;
3654 length = PyTuple_GET_SIZE(right);
3655 for (i = 0; i < length; i++) {
3656 PyObject *exc = PyTuple_GET_ITEM(right, i);
3657 if (!PyExceptionClass_Check(exc)) {
3658 _PyErr_SetString(tstate, PyExc_TypeError,
3659 CANNOT_CATCH_MSG);
3660 Py_DECREF(left);
3661 Py_DECREF(right);
3662 goto error;
3663 }
3664 }
3665 }
3666 else {
3667 if (!PyExceptionClass_Check(right)) {
3668 _PyErr_SetString(tstate, PyExc_TypeError,
3669 CANNOT_CATCH_MSG);
3670 Py_DECREF(left);
3671 Py_DECREF(right);
3672 goto error;
3673 }
3674 }
3675 int res = PyErr_GivenExceptionMatches(left, right);
3676 Py_DECREF(left);
3677 Py_DECREF(right);
3678 if (res > 0) {
3679 /* Exception matches -- Do nothing */;
3680 }
3681 else if (res == 0) {
3682 JUMPTO(oparg);
3683 }
3684 else {
3685 goto error;
3686 }
3687 DISPATCH();
3688 }
3689
Benjamin Petersonddd19492018-09-16 22:38:02 -07003690 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003691 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003692 PyObject *fromlist = POP();
3693 PyObject *level = TOP();
3694 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003695 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003696 Py_DECREF(level);
3697 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003698 SET_TOP(res);
3699 if (res == NULL)
3700 goto error;
3701 DISPATCH();
3702 }
3703
Benjamin Petersonddd19492018-09-16 22:38:02 -07003704 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705 PyObject *from = POP(), *locals;
3706 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003707 if (PyFrame_FastToLocalsWithError(f) < 0) {
3708 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003709 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003710 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003711
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003712 locals = f->f_locals;
3713 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003714 _PyErr_SetString(tstate, PyExc_SystemError,
3715 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003716 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003717 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003719 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003721 Py_DECREF(from);
3722 if (err != 0)
3723 goto error;
3724 DISPATCH();
3725 }
Guido van Rossum25831651993-05-19 14:50:45 +00003726
Benjamin Petersonddd19492018-09-16 22:38:02 -07003727 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003728 PyObject *name = GETITEM(names, oparg);
3729 PyObject *from = TOP();
3730 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003731 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003732 PUSH(res);
3733 if (res == NULL)
3734 goto error;
3735 DISPATCH();
3736 }
Thomas Wouters52152252000-08-17 22:55:00 +00003737
Benjamin Petersonddd19492018-09-16 22:38:02 -07003738 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003740 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003741 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003742
Benjamin Petersonddd19492018-09-16 22:38:02 -07003743 case TARGET(POP_JUMP_IF_FALSE): {
3744 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003745 PyObject *cond = POP();
3746 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003747 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003748 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003749 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003751 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003752 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003754 CHECK_EVAL_BREAKER();
Mark Shannon4958f5d2021-03-24 17:56:12 +00003755 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003757 err = PyObject_IsTrue(cond);
3758 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003760 ;
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003761 else if (err == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003763 CHECK_EVAL_BREAKER();
3764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003766 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003768 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003769
Benjamin Petersonddd19492018-09-16 22:38:02 -07003770 case TARGET(POP_JUMP_IF_TRUE): {
3771 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003772 PyObject *cond = POP();
3773 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003774 if (Py_IsFalse(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003775 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003776 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003778 if (Py_IsTrue(cond)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003779 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003781 CHECK_EVAL_BREAKER();
Mark Shannon4958f5d2021-03-24 17:56:12 +00003782 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003784 err = PyObject_IsTrue(cond);
3785 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 JUMPTO(oparg);
Miss Islington (bot)37bdd222021-07-19 04:15:58 -07003788 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
3790 else if (err == 0)
3791 ;
3792 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003793 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003795 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003796
Benjamin Petersonddd19492018-09-16 22:38:02 -07003797 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 PyObject *cond = TOP();
3799 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003800 if (Py_IsTrue(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003801 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003802 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003803 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003805 if (Py_IsFalse(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003807 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003809 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003811 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003812 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 }
3814 else if (err == 0)
3815 JUMPTO(oparg);
3816 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003819 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003820
Benjamin Petersonddd19492018-09-16 22:38:02 -07003821 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003822 PyObject *cond = TOP();
3823 int err;
Victor Stinner09bbebe2021-04-11 00:17:39 +02003824 if (Py_IsFalse(cond)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003825 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003826 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003827 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02003829 if (Py_IsTrue(cond)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003831 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003833 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 JUMPTO(oparg);
3836 }
3837 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003838 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003839 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 }
3841 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003845
Benjamin Petersonddd19492018-09-16 22:38:02 -07003846 case TARGET(JUMP_ABSOLUTE): {
3847 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003849 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003852
Brandt Bucher145bf262021-02-26 14:51:55 -08003853 case TARGET(GET_LEN): {
3854 // PUSH(len(TOS))
3855 Py_ssize_t len_i = PyObject_Length(TOP());
3856 if (len_i < 0) {
3857 goto error;
3858 }
3859 PyObject *len_o = PyLong_FromSsize_t(len_i);
3860 if (len_o == NULL) {
3861 goto error;
3862 }
3863 PUSH(len_o);
3864 DISPATCH();
3865 }
3866
3867 case TARGET(MATCH_CLASS): {
3868 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3869 // attributes. On failure, set TOS to False.
3870 PyObject *names = POP();
3871 PyObject *type = TOP();
3872 PyObject *subject = SECOND();
3873 assert(PyTuple_CheckExact(names));
3874 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3875 Py_DECREF(names);
3876 if (attrs) {
3877 // Success!
3878 assert(PyTuple_CheckExact(attrs));
3879 Py_DECREF(subject);
3880 SET_SECOND(attrs);
3881 }
3882 else if (_PyErr_Occurred(tstate)) {
3883 goto error;
3884 }
3885 Py_DECREF(type);
3886 SET_TOP(PyBool_FromLong(!!attrs));
3887 DISPATCH();
3888 }
3889
3890 case TARGET(MATCH_MAPPING): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003891 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003892 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
3893 PyObject *res = match ? Py_True : Py_False;
3894 Py_INCREF(res);
3895 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003896 DISPATCH();
3897 }
3898
3899 case TARGET(MATCH_SEQUENCE): {
Brandt Bucher145bf262021-02-26 14:51:55 -08003900 PyObject *subject = TOP();
Mark Shannon069e81a2021-04-30 09:50:28 +01003901 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
3902 PyObject *res = match ? Py_True : Py_False;
3903 Py_INCREF(res);
3904 PUSH(res);
Brandt Bucher145bf262021-02-26 14:51:55 -08003905 DISPATCH();
3906 }
3907
3908 case TARGET(MATCH_KEYS): {
3909 // On successful match for all keys, PUSH(values) and PUSH(True).
3910 // Otherwise, PUSH(None) and PUSH(False).
3911 PyObject *keys = TOP();
3912 PyObject *subject = SECOND();
3913 PyObject *values_or_none = match_keys(tstate, subject, keys);
3914 if (values_or_none == NULL) {
3915 goto error;
3916 }
3917 PUSH(values_or_none);
Victor Stinner09bbebe2021-04-11 00:17:39 +02003918 if (Py_IsNone(values_or_none)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08003919 Py_INCREF(Py_False);
3920 PUSH(Py_False);
3921 DISPATCH();
3922 }
3923 assert(PyTuple_CheckExact(values_or_none));
3924 Py_INCREF(Py_True);
3925 PUSH(Py_True);
3926 DISPATCH();
3927 }
3928
3929 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3930 // rest = dict(TOS1)
3931 // for key in TOS:
3932 // del rest[key]
3933 // SET_TOP(rest)
3934 PyObject *keys = TOP();
3935 PyObject *subject = SECOND();
3936 PyObject *rest = PyDict_New();
3937 if (rest == NULL || PyDict_Update(rest, subject)) {
3938 Py_XDECREF(rest);
3939 goto error;
3940 }
3941 // This may seem a bit inefficient, but keys is rarely big enough to
3942 // actually impact runtime.
3943 assert(PyTuple_CheckExact(keys));
3944 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3945 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3946 Py_DECREF(rest);
3947 goto error;
3948 }
3949 }
3950 Py_DECREF(keys);
3951 SET_TOP(rest);
3952 DISPATCH();
3953 }
3954
Benjamin Petersonddd19492018-09-16 22:38:02 -07003955 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003957 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003958 PyObject *iter = PyObject_GetIter(iterable);
3959 Py_DECREF(iterable);
3960 SET_TOP(iter);
3961 if (iter == NULL)
3962 goto error;
3963 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003964 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003965 DISPATCH();
3966 }
3967
Benjamin Petersonddd19492018-09-16 22:38:02 -07003968 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003969 /* before: [obj]; after [getiter(obj)] */
3970 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003971 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003972 if (PyCoro_CheckExact(iterable)) {
3973 /* `iterable` is a coroutine */
3974 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3975 /* and it is used in a 'yield from' expression of a
3976 regular generator. */
3977 Py_DECREF(iterable);
3978 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003979 _PyErr_SetString(tstate, PyExc_TypeError,
3980 "cannot 'yield from' a coroutine object "
3981 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003982 goto error;
3983 }
3984 }
3985 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003986 /* `iterable` is not a generator. */
3987 iter = PyObject_GetIter(iterable);
3988 Py_DECREF(iterable);
3989 SET_TOP(iter);
3990 if (iter == NULL)
3991 goto error;
3992 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003993 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003994 DISPATCH();
3995 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003996
Benjamin Petersonddd19492018-09-16 22:38:02 -07003997 case TARGET(FOR_ITER): {
3998 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004000 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004001 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004002 if (next != NULL) {
4003 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 PREDICT(STORE_FAST);
4005 PREDICT(UNPACK_SEQUENCE);
4006 DISPATCH();
4007 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004008 if (_PyErr_Occurred(tstate)) {
4009 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004010 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004011 }
4012 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004013 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004014 }
4015 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 }
4017 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004018 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004019 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 JUMPBY(oparg);
4021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004022 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004023
Benjamin Petersonddd19492018-09-16 22:38:02 -07004024 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004025 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 STACK_LEVEL());
4027 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004028 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004029
Benjamin Petersonddd19492018-09-16 22:38:02 -07004030 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004031 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004032 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004033 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004034 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004035 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004036 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004037 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004038 }
4039 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4040 if (exit == NULL) {
4041 Py_DECREF(enter);
4042 goto error;
4043 }
Yury Selivanov75445082015-05-11 22:57:16 -04004044 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004045 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004046 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004047 Py_DECREF(enter);
4048 if (res == NULL)
4049 goto error;
4050 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004051 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004052 DISPATCH();
4053 }
4054
Benjamin Petersonddd19492018-09-16 22:38:02 -07004055 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004056 PyObject *res = POP();
4057 /* Setup the finally block before pushing the result
4058 of __aenter__ on the stack. */
4059 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4060 STACK_LEVEL());
4061 PUSH(res);
4062 DISPATCH();
4063 }
4064
Benjamin Petersonddd19492018-09-16 22:38:02 -07004065 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004066 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004067 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004068 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004069 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004070 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004071 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004072 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004073 }
4074 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004075 if (exit == NULL) {
4076 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004077 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004079 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004080 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004081 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004082 Py_DECREF(enter);
4083 if (res == NULL)
4084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 /* Setup the finally block before pushing the result
4086 of __enter__ on the stack. */
4087 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4088 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004090 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 DISPATCH();
4092 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004093
Mark Shannonfee55262019-11-21 09:11:43 +00004094 case TARGET(WITH_EXCEPT_START): {
4095 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004097 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4098 - SEVENTH: the context.__exit__ bound method
4099 We call SEVENTH(TOP, SECOND, THIRD).
4100 Then we push again the TOP exception and the __exit__
4101 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004104 PyObject *exc, *val, *tb, *res;
4105
Victor Stinner842cfff2016-12-01 14:45:31 +01004106 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004107 val = SECOND();
4108 tb = THIRD();
Victor Stinner09bbebe2021-04-11 00:17:39 +02004109 assert(!Py_IsNone(exc));
Mark Shannonfee55262019-11-21 09:11:43 +00004110 assert(!PyLong_Check(exc));
4111 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004112 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004113 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004114 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004115 if (res == NULL)
4116 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004117
Yury Selivanov75445082015-05-11 22:57:16 -04004118 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004119 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004121
Benjamin Petersonddd19492018-09-16 22:38:02 -07004122 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004123 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004124 PyObject *name = GETITEM(names, oparg);
4125 PyObject *obj = TOP();
4126 PyObject *meth = NULL;
4127
4128 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4129
Yury Selivanovf2392132016-12-13 19:03:51 -05004130 if (meth == NULL) {
4131 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004132 goto error;
4133 }
4134
4135 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004136 /* We can bypass temporary bound method object.
4137 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004138
INADA Naoki015bce62017-01-16 17:23:30 +09004139 meth | self | arg1 | ... | argN
4140 */
4141 SET_TOP(meth);
4142 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004143 }
4144 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004145 /* meth is not an unbound method (but a regular attr, or
4146 something was returned by a descriptor protocol). Set
4147 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004148 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004149
4150 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004151 */
INADA Naoki015bce62017-01-16 17:23:30 +09004152 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004153 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004154 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004155 }
4156 DISPATCH();
4157 }
4158
Benjamin Petersonddd19492018-09-16 22:38:02 -07004159 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004160 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004161 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004162
4163 sp = stack_pointer;
4164
INADA Naoki015bce62017-01-16 17:23:30 +09004165 meth = PEEK(oparg + 2);
4166 if (meth == NULL) {
4167 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4168 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004169
4170 Stack layout:
4171
INADA Naoki015bce62017-01-16 17:23:30 +09004172 ... | NULL | callable | arg1 | ... | argN
4173 ^- TOP()
4174 ^- (-oparg)
4175 ^- (-oparg-1)
4176 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004177
Ville Skyttä49b27342017-08-03 09:00:59 +03004178 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004179 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004180 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004181 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004182 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004183 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004184 }
4185 else {
4186 /* This is a method call. Stack layout:
4187
INADA Naoki015bce62017-01-16 17:23:30 +09004188 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004189 ^- TOP()
4190 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004191 ^- (-oparg-1)
4192 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004193
INADA Naoki015bce62017-01-16 17:23:30 +09004194 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004195 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004196 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004197 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004198 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004199 stack_pointer = sp;
4200 }
4201
4202 PUSH(res);
4203 if (res == NULL)
4204 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004205 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004206 DISPATCH();
4207 }
4208
Benjamin Petersonddd19492018-09-16 22:38:02 -07004209 case TARGET(CALL_FUNCTION): {
4210 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004211 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004213 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004215 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004216 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004217 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004218 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004219 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004220 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004222
Benjamin Petersonddd19492018-09-16 22:38:02 -07004223 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004224 PyObject **sp, *res, *names;
4225
4226 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004227 assert(PyTuple_Check(names));
4228 assert(PyTuple_GET_SIZE(names) <= oparg);
4229 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004231 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004233 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004234 Py_DECREF(names);
4235
4236 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004237 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004238 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004239 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004240 DISPATCH();
4241 }
4242
Benjamin Petersonddd19492018-09-16 22:38:02 -07004243 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004244 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004245 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004246 if (oparg & 0x01) {
4247 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004248 if (!PyDict_CheckExact(kwargs)) {
4249 PyObject *d = PyDict_New();
4250 if (d == NULL)
4251 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004252 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004253 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004254 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004255 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004256 goto error;
4257 }
4258 Py_DECREF(kwargs);
4259 kwargs = d;
4260 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004261 assert(PyDict_CheckExact(kwargs));
4262 }
4263 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004264 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004265 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004266 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004267 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004268 goto error;
4269 }
4270 Py_SETREF(callargs, PySequence_Tuple(callargs));
4271 if (callargs == NULL) {
4272 goto error;
4273 }
4274 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004275 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004276
Mark Shannon8e1b4062021-03-05 14:45:50 +00004277 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004278 Py_DECREF(func);
4279 Py_DECREF(callargs);
4280 Py_XDECREF(kwargs);
4281
4282 SET_TOP(result);
4283 if (result == NULL) {
4284 goto error;
4285 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004286 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004287 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004289
Benjamin Petersonddd19492018-09-16 22:38:02 -07004290 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004291 PyObject *qualname = POP();
4292 PyObject *codeobj = POP();
4293 PyFunctionObject *func = (PyFunctionObject *)
4294 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004295
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004296 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004297 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004298 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004299 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004301
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004302 if (oparg & 0x08) {
4303 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004304 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004305 }
4306 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004307 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004308 func->func_annotations = POP();
4309 }
4310 if (oparg & 0x02) {
4311 assert(PyDict_CheckExact(TOP()));
4312 func->func_kwdefaults = POP();
4313 }
4314 if (oparg & 0x01) {
4315 assert(PyTuple_CheckExact(TOP()));
4316 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004318
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004319 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004320 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004322
Benjamin Petersonddd19492018-09-16 22:38:02 -07004323 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004324 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004326 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004328 step = NULL;
4329 stop = POP();
4330 start = TOP();
4331 slice = PySlice_New(start, stop, step);
4332 Py_DECREF(start);
4333 Py_DECREF(stop);
4334 Py_XDECREF(step);
4335 SET_TOP(slice);
4336 if (slice == NULL)
4337 goto error;
4338 DISPATCH();
4339 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004340
Benjamin Petersonddd19492018-09-16 22:38:02 -07004341 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004342 /* Handles f-string value formatting. */
4343 PyObject *result;
4344 PyObject *fmt_spec;
4345 PyObject *value;
4346 PyObject *(*conv_fn)(PyObject *);
4347 int which_conversion = oparg & FVC_MASK;
4348 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4349
4350 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004351 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004352
4353 /* See if any conversion is specified. */
4354 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004355 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004356 case FVC_STR: conv_fn = PyObject_Str; break;
4357 case FVC_REPR: conv_fn = PyObject_Repr; break;
4358 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004359 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004360 _PyErr_Format(tstate, PyExc_SystemError,
4361 "unexpected conversion flag %d",
4362 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004363 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004364 }
4365
4366 /* If there's a conversion function, call it and replace
4367 value with that result. Otherwise, just use value,
4368 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004369 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004370 result = conv_fn(value);
4371 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004372 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004373 Py_XDECREF(fmt_spec);
4374 goto error;
4375 }
4376 value = result;
4377 }
4378
4379 /* If value is a unicode object, and there's no fmt_spec,
4380 then we know the result of format(value) is value
4381 itself. In that case, skip calling format(). I plan to
4382 move this optimization in to PyObject_Format()
4383 itself. */
4384 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4385 /* Do nothing, just transfer ownership to result. */
4386 result = value;
4387 } else {
4388 /* Actually call format(). */
4389 result = PyObject_Format(value, fmt_spec);
4390 Py_DECREF(value);
4391 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004392 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004393 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004394 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004395 }
4396
Eric V. Smith135d5f42016-02-05 18:23:08 -05004397 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004398 DISPATCH();
4399 }
4400
Brandt Bucher0ad1e032021-05-02 13:02:10 -07004401 case TARGET(ROT_N): {
4402 PyObject *top = TOP();
4403 memmove(&PEEK(oparg - 1), &PEEK(oparg),
4404 sizeof(PyObject*) * (oparg - 1));
4405 PEEK(oparg) = top;
4406 DISPATCH();
4407 }
4408
Benjamin Petersonddd19492018-09-16 22:38:02 -07004409 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004410 int oldoparg = oparg;
4411 NEXTOPARG();
4412 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004414 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004415
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004416
Antoine Pitrou042b1282010-08-13 21:15:58 +00004417#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 default:
4421 fprintf(stderr,
4422 "XXX lineno: %d, opcode: %d\n",
4423 PyFrame_GetLineNumber(f),
4424 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004425 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004426 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004429
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004430 /* This should never be reached. Every opcode should end with DISPATCH()
4431 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004432 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004434error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004435 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004436#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004437 if (!_PyErr_Occurred(tstate)) {
4438 _PyErr_SetString(tstate, PyExc_SystemError,
4439 "error return without exception set");
4440 }
Victor Stinner365b6932013-07-12 00:11:58 +02004441#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004442 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004443#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004445 /* Log traceback info. */
4446 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004447
Mark Shannoncb9879b2020-07-17 11:44:23 +01004448 if (tstate->c_tracefunc != NULL) {
4449 /* Make sure state is set to FRAME_EXECUTING for tracing */
4450 assert(f->f_state == FRAME_EXECUTING);
4451 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004452 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004453 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004454 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004455exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004456 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004457 /* Unwind stacks if an exception occurred */
4458 while (f->f_iblock > 0) {
4459 /* Pop the current block. */
4460 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 if (b->b_type == EXCEPT_HANDLER) {
4463 UNWIND_EXCEPT_HANDLER(b);
4464 continue;
4465 }
4466 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004467 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 PyObject *exc, *val, *tb;
4469 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004470 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004472 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004473 PUSH(exc_info->exc_traceback);
4474 PUSH(exc_info->exc_value);
4475 if (exc_info->exc_type != NULL) {
4476 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 }
4478 else {
4479 Py_INCREF(Py_None);
4480 PUSH(Py_None);
4481 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 /* Make the raw exception data
4484 available to the handler,
4485 so a program can emulate the
4486 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004487 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004488 if (tb != NULL)
4489 PyException_SetTraceback(val, tb);
4490 else
4491 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004493 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004495 exc_info->exc_value = val;
4496 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 if (tb == NULL)
4498 tb = Py_None;
4499 Py_INCREF(tb);
4500 PUSH(tb);
4501 PUSH(val);
4502 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004504 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004505 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004506 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 }
4508 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004509
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004510 /* End the loop as we still have an error */
4511 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004513
Pablo Galindof00828a2019-05-09 16:52:02 +01004514 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004515 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 /* Pop remaining stack entries. */
4518 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004519 PyObject *o = POP();
4520 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004522 f->f_stackdepth = 0;
4523 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004524exiting:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004525 if (trace_info.cframe.use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004526 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004527 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004528 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004529 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 }
4531 }
4532 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004533 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004534 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004535 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 }
4537 }
4538 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004541exit_eval_frame:
Mark Shannon9e7b2072021-04-13 11:08:14 +01004542 /* Restore previous cframe */
4543 tstate->cframe = trace_info.cframe.previous;
4544 tstate->cframe->use_tracing = trace_info.cframe.use_tracing;
4545
Łukasz Langaa785c872016-09-09 17:37:37 -07004546 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4547 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004548 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004550
Victor Stinner0b72b232020-03-12 23:18:39 +01004551 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004552}
4553
Benjamin Petersonb204a422011-06-05 22:04:07 -05004554static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004555format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004556 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004557{
4558 int err;
4559 Py_ssize_t len = PyList_GET_SIZE(names);
4560 PyObject *name_str, *comma, *tail, *tmp;
4561
4562 assert(PyList_CheckExact(names));
4563 assert(len >= 1);
4564 /* Deal with the joys of natural language. */
4565 switch (len) {
4566 case 1:
4567 name_str = PyList_GET_ITEM(names, 0);
4568 Py_INCREF(name_str);
4569 break;
4570 case 2:
4571 name_str = PyUnicode_FromFormat("%U and %U",
4572 PyList_GET_ITEM(names, len - 2),
4573 PyList_GET_ITEM(names, len - 1));
4574 break;
4575 default:
4576 tail = PyUnicode_FromFormat(", %U, and %U",
4577 PyList_GET_ITEM(names, len - 2),
4578 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004579 if (tail == NULL)
4580 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004581 /* Chop off the last two objects in the list. This shouldn't actually
4582 fail, but we can't be too careful. */
4583 err = PyList_SetSlice(names, len - 2, len, NULL);
4584 if (err == -1) {
4585 Py_DECREF(tail);
4586 return;
4587 }
4588 /* Stitch everything up into a nice comma-separated list. */
4589 comma = PyUnicode_FromString(", ");
4590 if (comma == NULL) {
4591 Py_DECREF(tail);
4592 return;
4593 }
4594 tmp = PyUnicode_Join(comma, names);
4595 Py_DECREF(comma);
4596 if (tmp == NULL) {
4597 Py_DECREF(tail);
4598 return;
4599 }
4600 name_str = PyUnicode_Concat(tmp, tail);
4601 Py_DECREF(tmp);
4602 Py_DECREF(tail);
4603 break;
4604 }
4605 if (name_str == NULL)
4606 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004607 _PyErr_Format(tstate, PyExc_TypeError,
4608 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004609 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004610 len,
4611 kind,
4612 len == 1 ? "" : "s",
4613 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004614 Py_DECREF(name_str);
4615}
4616
4617static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004618missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4619 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004620 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004621{
Victor Stinner74319ae2016-08-25 00:04:09 +02004622 Py_ssize_t i, j = 0;
4623 Py_ssize_t start, end;
4624 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004625 const char *kind = positional ? "positional" : "keyword-only";
4626 PyObject *missing_names;
4627
4628 /* Compute the names of the arguments that are missing. */
4629 missing_names = PyList_New(missing);
4630 if (missing_names == NULL)
4631 return;
4632 if (positional) {
4633 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004634 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004635 }
4636 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004637 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004638 end = start + co->co_kwonlyargcount;
4639 }
4640 for (i = start; i < end; i++) {
4641 if (GETLOCAL(i) == NULL) {
4642 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4643 PyObject *name = PyObject_Repr(raw);
4644 if (name == NULL) {
4645 Py_DECREF(missing_names);
4646 return;
4647 }
4648 PyList_SET_ITEM(missing_names, j++, name);
4649 }
4650 }
4651 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004652 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004653 Py_DECREF(missing_names);
4654}
4655
4656static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004657too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004658 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004659 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004660{
4661 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004662 Py_ssize_t kwonly_given = 0;
4663 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004664 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004665 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004666
Benjamin Petersone109c702011-06-24 09:37:26 -05004667 assert((co->co_flags & CO_VARARGS) == 0);
4668 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004669 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004670 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004671 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004672 }
4673 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004674 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004675 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004676 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004677 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004678 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004679 }
4680 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004681 plural = (co_argcount != 1);
4682 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004683 }
4684 if (sig == NULL)
4685 return;
4686 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004687 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4688 kwonly_sig = PyUnicode_FromFormat(format,
4689 given != 1 ? "s" : "",
4690 kwonly_given,
4691 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004692 if (kwonly_sig == NULL) {
4693 Py_DECREF(sig);
4694 return;
4695 }
4696 }
4697 else {
4698 /* This will not fail. */
4699 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004700 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004701 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004702 _PyErr_Format(tstate, PyExc_TypeError,
4703 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004704 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004705 sig,
4706 plural ? "s" : "",
4707 given,
4708 kwonly_sig,
4709 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004710 Py_DECREF(sig);
4711 Py_DECREF(kwonly_sig);
4712}
4713
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004714static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004715positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004716 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004717 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004718{
4719 int posonly_conflicts = 0;
4720 PyObject* posonly_names = PyList_New(0);
4721
4722 for(int k=0; k < co->co_posonlyargcount; k++){
4723 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4724
4725 for (int k2=0; k2<kwcount; k2++){
4726 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004727 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004728 if (kwname == posonly_name){
4729 if(PyList_Append(posonly_names, kwname) != 0) {
4730 goto fail;
4731 }
4732 posonly_conflicts++;
4733 continue;
4734 }
4735
4736 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4737
4738 if ( cmp > 0) {
4739 if(PyList_Append(posonly_names, kwname) != 0) {
4740 goto fail;
4741 }
4742 posonly_conflicts++;
4743 } else if (cmp < 0) {
4744 goto fail;
4745 }
4746
4747 }
4748 }
4749 if (posonly_conflicts) {
4750 PyObject* comma = PyUnicode_FromString(", ");
4751 if (comma == NULL) {
4752 goto fail;
4753 }
4754 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4755 Py_DECREF(comma);
4756 if (error_names == NULL) {
4757 goto fail;
4758 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004759 _PyErr_Format(tstate, PyExc_TypeError,
4760 "%U() got some positional-only arguments passed"
4761 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004762 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004763 Py_DECREF(error_names);
4764 goto fail;
4765 }
4766
4767 Py_DECREF(posonly_names);
4768 return 0;
4769
4770fail:
4771 Py_XDECREF(posonly_names);
4772 return 1;
4773
4774}
4775
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004776
Mark Shannon0332e562021-02-01 10:42:03 +00004777PyFrameObject *
4778_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004779 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004780 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004781 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004782{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004783 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004784
Mark Shannond6c33fb2021-01-29 13:24:55 +00004785 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4786 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004787 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004788
Victor Stinnerc7020012016-08-16 23:40:29 +02004789 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004790 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004791 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004793 }
Victor Stinner232dda62020-06-04 15:19:02 +02004794 PyObject **fastlocals = f->f_localsplus;
4795 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004796
Victor Stinnerc7020012016-08-16 23:40:29 +02004797 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004798 PyObject *kwdict;
4799 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004800 if (co->co_flags & CO_VARKEYWORDS) {
4801 kwdict = PyDict_New();
4802 if (kwdict == NULL)
4803 goto fail;
4804 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004805 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004806 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004807 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004808 SETLOCAL(i, kwdict);
4809 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004810 else {
4811 kwdict = NULL;
4812 }
4813
Pablo Galindocd74e662019-06-01 18:08:04 +01004814 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004815 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004816 if (argcount > co->co_argcount) {
4817 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004818 }
4819 else {
4820 n = argcount;
4821 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004822 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004823 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004824 Py_INCREF(x);
4825 SETLOCAL(j, x);
4826 }
4827
Victor Stinnerc7020012016-08-16 23:40:29 +02004828 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004829 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004830 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004831 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004832 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004833 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004834 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004835 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004836
Mark Shannon0332e562021-02-01 10:42:03 +00004837 /* Handle keyword arguments */
4838 if (kwnames != NULL) {
4839 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4840 for (i = 0; i < kwcount; i++) {
4841 PyObject **co_varnames;
4842 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4843 PyObject *value = args[i+argcount];
4844 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004845
Mark Shannon0332e562021-02-01 10:42:03 +00004846 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4847 _PyErr_Format(tstate, PyExc_TypeError,
4848 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004849 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004850 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004851 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004852
Mark Shannon0332e562021-02-01 10:42:03 +00004853 /* Speed hack: do raw pointer compares. As names are
4854 normally interned this should almost always hit. */
4855 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4856 for (j = co->co_posonlyargcount; j < total_args; j++) {
4857 PyObject *varname = co_varnames[j];
4858 if (varname == keyword) {
4859 goto kw_found;
4860 }
4861 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004862
Mark Shannon0332e562021-02-01 10:42:03 +00004863 /* Slow fallback, just in case */
4864 for (j = co->co_posonlyargcount; j < total_args; j++) {
4865 PyObject *varname = co_varnames[j];
4866 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4867 if (cmp > 0) {
4868 goto kw_found;
4869 }
4870 else if (cmp < 0) {
4871 goto fail;
4872 }
4873 }
4874
4875 assert(j >= total_args);
4876 if (kwdict == NULL) {
4877
4878 if (co->co_posonlyargcount
4879 && positional_only_passed_as_keyword(tstate, co,
4880 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004881 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004882 {
4883 goto fail;
4884 }
4885
4886 _PyErr_Format(tstate, PyExc_TypeError,
4887 "%U() got an unexpected keyword argument '%S'",
4888 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004889 goto fail;
4890 }
4891
Mark Shannon0332e562021-02-01 10:42:03 +00004892 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4893 goto fail;
4894 }
4895 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004896
Mark Shannon0332e562021-02-01 10:42:03 +00004897 kw_found:
4898 if (GETLOCAL(j) != NULL) {
4899 _PyErr_Format(tstate, PyExc_TypeError,
4900 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004901 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004902 goto fail;
4903 }
4904 Py_INCREF(value);
4905 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004908
4909 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004910 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004911 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4912 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913 goto fail;
4914 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004915
4916 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004917 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004918 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004919 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004920 Py_ssize_t missing = 0;
4921 for (i = argcount; i < m; i++) {
4922 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004923 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004924 }
4925 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004926 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004927 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004928 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004929 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004930 }
4931 if (n > m)
4932 i = n - m;
4933 else
4934 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004935 if (defcount) {
4936 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4937 for (; i < defcount; i++) {
4938 if (GETLOCAL(m+i) == NULL) {
4939 PyObject *def = defs[i];
4940 Py_INCREF(def);
4941 SETLOCAL(m+i, def);
4942 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004943 }
4944 }
4945 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004946
4947 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004948 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004949 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004950 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004951 if (GETLOCAL(i) != NULL)
4952 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004953 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004954 if (con->fc_kwdefaults != NULL) {
4955 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004956 if (def) {
4957 Py_INCREF(def);
4958 SETLOCAL(i, def);
4959 continue;
4960 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004962 goto fail;
4963 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004964 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004965 missing++;
4966 }
4967 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004968 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004969 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004970 goto fail;
4971 }
4972 }
4973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004975 vars into frame. */
4976 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004978 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004979 /* Possibly account for the cell variable being an argument. */
4980 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004981 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004982 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004983 /* Clear the local copy. */
4984 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004985 }
4986 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004987 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004988 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004989 if (c == NULL)
4990 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004991 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004993
4994 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004995 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004996 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05004997 Py_INCREF(o);
4998 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005000
Mark Shannon0332e562021-02-01 10:42:03 +00005001 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005002
Thomas Woutersce272b62007-09-19 21:19:28 +00005003fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 /* decref'ing the frame can cause __del__ methods to get invoked,
5006 which can call back into Python. While we're done with the
5007 current Python frame (f), the associated C stack is still in use,
5008 so recursion_depth must be boosted for the duration.
5009 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005010 if (Py_REFCNT(f) > 1) {
5011 Py_DECREF(f);
5012 _PyObject_GC_TRACK(f);
5013 }
5014 else {
5015 ++tstate->recursion_depth;
5016 Py_DECREF(f);
5017 --tstate->recursion_depth;
5018 }
Mark Shannon0332e562021-02-01 10:42:03 +00005019 return NULL;
5020}
5021
5022static PyObject *
5023make_coro(PyFrameConstructor *con, PyFrameObject *f)
5024{
5025 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5026 PyObject *gen;
5027 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5028
5029 /* Don't need to keep the reference to f_back, it will be set
5030 * when the generator is resumed. */
5031 Py_CLEAR(f->f_back);
5032
5033 /* Create a new generator that owns the ready to run frame
5034 * and return that as the value. */
5035 if (is_coro) {
5036 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5037 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5038 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5039 } else {
5040 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5041 }
5042 if (gen == NULL) {
5043 return NULL;
5044 }
5045
5046 _PyObject_GC_TRACK(f);
5047
5048 return gen;
5049}
5050
5051PyObject *
5052_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5053 PyObject *locals,
5054 PyObject* const* args, size_t argcount,
5055 PyObject *kwnames)
5056{
5057 PyFrameObject *f = _PyEval_MakeFrameVector(
5058 tstate, con, locals, args, argcount, kwnames);
5059 if (f == NULL) {
5060 return NULL;
5061 }
5062 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5063 return make_coro(con, f);
5064 }
5065 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5066
5067 /* decref'ing the frame can cause __del__ methods to get invoked,
5068 which can call back into Python. While we're done with the
5069 current Python frame (f), the associated C stack is still in use,
5070 so recursion_depth must be boosted for the duration.
5071 */
5072 if (Py_REFCNT(f) > 1) {
5073 Py_DECREF(f);
5074 _PyObject_GC_TRACK(f);
5075 }
5076 else {
5077 ++tstate->recursion_depth;
5078 Py_DECREF(f);
5079 --tstate->recursion_depth;
5080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005082}
5083
Mark Shannond6c33fb2021-01-29 13:24:55 +00005084/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005085PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005086PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5087 PyObject *const *args, int argcount,
5088 PyObject *const *kws, int kwcount,
5089 PyObject *const *defs, int defcount,
5090 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005091{
Victor Stinner46496f92021-02-20 15:17:18 +01005092 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005093 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005094 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5095 if (defaults == NULL) {
5096 return NULL;
5097 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005098 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005099 if (builtins == NULL) {
5100 Py_DECREF(defaults);
5101 return NULL;
5102 }
Mark Shannon0332e562021-02-01 10:42:03 +00005103 if (locals == NULL) {
5104 locals = globals;
5105 }
5106 PyObject *kwnames;
5107 PyObject *const *allargs;
5108 PyObject **newargs;
5109 if (kwcount == 0) {
5110 allargs = args;
5111 kwnames = NULL;
5112 }
5113 else {
5114 kwnames = PyTuple_New(kwcount);
5115 if (kwnames == NULL) {
5116 res = NULL;
5117 goto fail;
5118 }
5119 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5120 if (newargs == NULL) {
5121 res = NULL;
5122 Py_DECREF(kwnames);
5123 goto fail;
5124 }
5125 for (int i = 0; i < argcount; i++) {
5126 newargs[i] = args[i];
5127 }
5128 for (int i = 0; i < kwcount; i++) {
5129 Py_INCREF(kws[2*i]);
5130 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5131 newargs[argcount+i] = kws[2*i+1];
5132 }
5133 allargs = newargs;
5134 }
5135 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5136 if (kwargs == NULL) {
5137 res = NULL;
5138 Py_DECREF(kwnames);
5139 goto fail;
5140 }
5141 for (int i = 0; i < kwcount; i++) {
5142 Py_INCREF(kws[2*i]);
5143 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5144 kwargs[i] = kws[2*i+1];
5145 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005146 PyFrameConstructor constr = {
5147 .fc_globals = globals,
5148 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005149 .fc_name = ((PyCodeObject *)_co)->co_name,
5150 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005151 .fc_code = _co,
5152 .fc_defaults = defaults,
5153 .fc_kwdefaults = kwdefs,
5154 .fc_closure = closure
5155 };
Mark Shannon0332e562021-02-01 10:42:03 +00005156 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005157 allargs, argcount,
5158 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005159 if (kwcount) {
5160 Py_DECREF(kwnames);
5161 PyMem_Free(newargs);
5162 }
5163fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005164 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005165 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005166}
5167
Tim Peters5ca576e2001-06-18 22:08:13 +00005168
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005169static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005170special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005173 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005174 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005175 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005176 return NULL;
5177 }
5178 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005179}
5180
5181
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005182/* Logic for the raise statement (too complicated for inlining).
5183 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005184static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005185do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 if (exc == NULL) {
5190 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005191 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005193 type = exc_info->exc_type;
5194 value = exc_info->exc_value;
5195 tb = exc_info->exc_traceback;
Victor Stinner09bbebe2021-04-11 00:17:39 +02005196 if (Py_IsNone(type) || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005197 _PyErr_SetString(tstate, PyExc_RuntimeError,
5198 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005199 return 0;
5200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 Py_XINCREF(type);
5202 Py_XINCREF(value);
5203 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005204 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005205 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 /* We support the following forms of raise:
5209 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005210 raise <instance>
5211 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 if (PyExceptionClass_Check(exc)) {
5214 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005215 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 if (value == NULL)
5217 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005218 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005219 _PyErr_Format(tstate, PyExc_TypeError,
5220 "calling %R should have returned an instance of "
5221 "BaseException, not %R",
5222 type, Py_TYPE(value));
5223 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 }
5226 else if (PyExceptionInstance_Check(exc)) {
5227 value = exc;
5228 type = PyExceptionInstance_Class(exc);
5229 Py_INCREF(type);
5230 }
5231 else {
5232 /* Not something you can raise. You get an exception
5233 anyway, just not what you specified :-) */
5234 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005235 _PyErr_SetString(tstate, PyExc_TypeError,
5236 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 goto raise_error;
5238 }
Collin Winter828f04a2007-08-31 00:04:24 +00005239
Serhiy Storchakac0191582016-09-27 11:37:10 +03005240 assert(type != NULL);
5241 assert(value != NULL);
5242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 if (cause) {
5244 PyObject *fixed_cause;
5245 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005246 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 if (fixed_cause == NULL)
5248 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005249 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005251 else if (PyExceptionInstance_Check(cause)) {
5252 fixed_cause = cause;
5253 }
Victor Stinner09bbebe2021-04-11 00:17:39 +02005254 else if (Py_IsNone(cause)) {
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005255 Py_DECREF(cause);
5256 fixed_cause = NULL;
5257 }
5258 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005259 _PyErr_SetString(tstate, PyExc_TypeError,
5260 "exception causes must derive from "
5261 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 goto raise_error;
5263 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005264 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 }
Collin Winter828f04a2007-08-31 00:04:24 +00005266
Victor Stinner438a12d2019-05-24 17:01:38 +02005267 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005268 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005269 Py_DECREF(value);
5270 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005271 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005272
5273raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 Py_XDECREF(value);
5275 Py_XDECREF(type);
5276 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005277 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005278}
5279
Tim Petersd6d010b2001-06-21 02:49:55 +00005280/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005281 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005282
Guido van Rossum0368b722007-05-11 16:50:42 +00005283 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5284 with a variable target.
5285*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005286
Barry Warsawe42b18f1997-08-25 22:13:04 +00005287static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005288unpack_iterable(PyThreadState *tstate, PyObject *v,
5289 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 int i = 0, j = 0;
5292 Py_ssize_t ll = 0;
5293 PyObject *it; /* iter(v) */
5294 PyObject *w;
5295 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005297 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005300 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005301 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005302 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005303 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 _PyErr_Format(tstate, PyExc_TypeError,
5305 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005306 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005307 }
5308 return 0;
5309 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 for (; i < argcnt; i++) {
5312 w = PyIter_Next(it);
5313 if (w == NULL) {
5314 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005315 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005316 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 _PyErr_Format(tstate, PyExc_ValueError,
5318 "not enough values to unpack "
5319 "(expected %d, got %d)",
5320 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005321 }
5322 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 _PyErr_Format(tstate, PyExc_ValueError,
5324 "not enough values to unpack "
5325 "(expected at least %d, got %d)",
5326 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 }
5329 goto Error;
5330 }
5331 *--sp = w;
5332 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 if (argcntafter == -1) {
5335 /* We better have exhausted the iterator now. */
5336 w = PyIter_Next(it);
5337 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005338 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 goto Error;
5340 Py_DECREF(it);
5341 return 1;
5342 }
5343 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005344 _PyErr_Format(tstate, PyExc_ValueError,
5345 "too many values to unpack (expected %d)",
5346 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 goto Error;
5348 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 l = PySequence_List(it);
5351 if (l == NULL)
5352 goto Error;
5353 *--sp = l;
5354 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 ll = PyList_GET_SIZE(l);
5357 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005358 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005359 "not enough values to unpack (expected at least %d, got %zd)",
5360 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 goto Error;
5362 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 /* Pop the "after-variable" args off the list. */
5365 for (j = argcntafter; j > 0; j--, i++) {
5366 *--sp = PyList_GET_ITEM(l, ll - j);
5367 }
5368 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005369 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 Py_DECREF(it);
5371 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005372
Tim Petersd6d010b2001-06-21 02:49:55 +00005373Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 for (; i > 0; i--, sp++)
5375 Py_DECREF(*sp);
5376 Py_XDECREF(it);
5377 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005378}
5379
Guido van Rossum96a42c81992-01-12 02:29:51 +00005380#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005381static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005382prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 printf("%s ", str);
Miss Islington (bot)4a0f1df2021-07-13 01:43:26 -07005385 PyObject *type, *value, *traceback;
5386 PyErr_Fetch(&type, &value, &traceback);
Victor Stinner438a12d2019-05-24 17:01:38 +02005387 if (PyObject_Print(v, stdout, 0) != 0) {
5388 /* Don't know what else to do */
5389 _PyErr_Clear(tstate);
5390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 printf("\n");
Miss Islington (bot)4a0f1df2021-07-13 01:43:26 -07005392 PyErr_Restore(type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005394}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005395#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005396
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005397static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005398call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005399 PyThreadState *tstate,
5400 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005401 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005402{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005403 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005405 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005406 if (value == NULL) {
5407 value = Py_None;
5408 Py_INCREF(value);
5409 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005410 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005411 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 arg = PyTuple_Pack(3, type, value, traceback);
5413 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005414 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 return;
5416 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005417 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005419 if (err == 0) {
5420 _PyErr_Restore(tstate, type, value, orig_traceback);
5421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 else {
5423 Py_XDECREF(type);
5424 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005425 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005427}
5428
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005429static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005430call_trace_protected(Py_tracefunc func, PyObject *obj,
5431 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005432 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 PyObject *type, *value, *traceback;
5436 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005437 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005438 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 if (err == 0)
5440 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005441 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 return 0;
5443 }
5444 else {
5445 Py_XDECREF(type);
5446 Py_XDECREF(value);
5447 Py_XDECREF(traceback);
5448 return -1;
5449 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005450}
5451
Mark Shannon8e1b4062021-03-05 14:45:50 +00005452static void
5453initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5454{
5455 if (trace_info->code != frame->f_code) {
5456 trace_info->code = frame->f_code;
Mark Shannon8e1b4062021-03-05 14:45:50 +00005457 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5458 }
5459}
5460
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005461static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005462call_trace(Py_tracefunc func, PyObject *obj,
5463 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005464 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005465 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005467 int result;
5468 if (tstate->tracing)
5469 return 0;
5470 tstate->tracing++;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005471 tstate->cframe->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005472 if (frame->f_lasti < 0) {
5473 frame->f_lineno = frame->f_code->co_firstlineno;
5474 }
5475 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005476 initialize_trace_info(trace_info, frame);
Serhiy Storchakab5499782021-10-04 15:01:11 +03005477 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005480 frame->f_lineno = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005481 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 || (tstate->c_profilefunc != NULL));
5483 tstate->tracing--;
5484 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005485}
5486
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005487PyObject *
5488_PyEval_CallTracing(PyObject *func, PyObject *args)
5489{
Victor Stinner50b48572018-11-01 01:51:40 +01005490 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 int save_tracing = tstate->tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005492 int save_use_tracing = tstate->cframe->use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005495 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005496 tstate->cframe->use_tracing = ((tstate->c_tracefunc != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005497 || (tstate->c_profilefunc != NULL));
5498 result = PyObject_Call(func, args, NULL);
5499 tstate->tracing = save_tracing;
Mark Shannon9e7b2072021-04-13 11:08:14 +01005500 tstate->cframe->use_tracing = save_use_tracing;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005501 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005502}
5503
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005504/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005505static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005506maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005507 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01005508 PyTraceInfo *trace_info, int instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005510 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005511
Nick Coghlan5a851672017-09-08 10:14:16 +10005512 /* If the last instruction falls at the start of a line or if it
5513 represents a jump backwards, update the frame's line number and
5514 then call the trace function if we're tracing source lines.
5515 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005516 initialize_trace_info(trace_info, frame);
Serhiy Storchakab5499782021-10-04 15:01:11 +03005517 int lastline = _PyCode_CheckLineNumber(instr_prev*sizeof(_Py_CODEUNIT), &trace_info->bounds);
5518 int line = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005519 if (line != -1 && frame->f_trace_lines) {
Mark Shannon9f2c63b2021-07-08 19:21:22 +01005520 /* Trace backward edges or if line number has changed */
5521 if (frame->f_lasti < instr_prev || line != lastline) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005522 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005523 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 }
George King20faa682017-10-18 17:44:22 -07005525 /* Always emit an opcode event if we're tracing all opcodes. */
5526 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005527 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005530}
5531
Victor Stinner309d7cc2020-03-13 16:39:12 +01005532int
5533_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5534{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005535 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005536 /* The caller must hold the GIL */
5537 assert(PyGILState_Check());
5538
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005539 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005540 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005541 PyThreadState *current_tstate = _PyThreadState_GET();
5542 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005543 return -1;
5544 }
5545
5546 PyObject *profileobj = tstate->c_profileobj;
5547
5548 tstate->c_profilefunc = NULL;
5549 tstate->c_profileobj = NULL;
5550 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005551 tstate->cframe->use_tracing = tstate->c_tracefunc != NULL;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005552 Py_XDECREF(profileobj);
5553
5554 Py_XINCREF(arg);
5555 tstate->c_profileobj = arg;
5556 tstate->c_profilefunc = func;
5557
5558 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005559 tstate->cframe->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005560 return 0;
5561}
5562
Fred Drake5755ce62001-06-27 19:19:46 +00005563void
5564PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005565{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005566 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005567 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005568 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005569 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5570 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005571}
5572
5573int
5574_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5575{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005576 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005577 /* The caller must hold the GIL */
5578 assert(PyGILState_Check());
5579
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005580 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005581 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005582 PyThreadState *current_tstate = _PyThreadState_GET();
5583 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005584 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005585 }
5586
Victor Stinner309d7cc2020-03-13 16:39:12 +01005587 PyObject *traceobj = tstate->c_traceobj;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005588
5589 tstate->c_tracefunc = NULL;
5590 tstate->c_traceobj = NULL;
5591 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005592 tstate->cframe->use_tracing = (tstate->c_profilefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005593 Py_XDECREF(traceobj);
5594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005596 tstate->c_traceobj = arg;
5597 tstate->c_tracefunc = func;
5598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 /* Flag that tracing or profiling is turned on */
Mark Shannon9e7b2072021-04-13 11:08:14 +01005600 tstate->cframe->use_tracing = ((func != NULL)
Victor Stinner309d7cc2020-03-13 16:39:12 +01005601 || (tstate->c_profilefunc != NULL));
5602
5603 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005604}
5605
5606void
5607PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5608{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005609 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005610 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005611 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005612 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5613 }
Fred Draked0838392001-06-16 21:02:31 +00005614}
5615
Victor Stinner309d7cc2020-03-13 16:39:12 +01005616
Yury Selivanov75445082015-05-11 22:57:16 -04005617void
Victor Stinner838f2642019-06-13 22:41:23 +02005618_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005619{
5620 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005621 tstate->coroutine_origin_tracking_depth = new_depth;
5622}
5623
5624int
5625_PyEval_GetCoroutineOriginTrackingDepth(void)
5626{
Victor Stinner50b48572018-11-01 01:51:40 +01005627 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005628 return tstate->coroutine_origin_tracking_depth;
5629}
5630
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005631int
Yury Selivanoveb636452016-09-08 22:01:51 -07005632_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5633{
Victor Stinner50b48572018-11-01 01:51:40 +01005634 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005635
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005636 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005637 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005638 }
5639
Yury Selivanoveb636452016-09-08 22:01:51 -07005640 Py_XINCREF(firstiter);
5641 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005642 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005643}
5644
5645PyObject *
5646_PyEval_GetAsyncGenFirstiter(void)
5647{
Victor Stinner50b48572018-11-01 01:51:40 +01005648 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005649 return tstate->async_gen_firstiter;
5650}
5651
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005652int
Yury Selivanoveb636452016-09-08 22:01:51 -07005653_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5654{
Victor Stinner50b48572018-11-01 01:51:40 +01005655 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005656
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005657 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005658 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005659 }
5660
Yury Selivanoveb636452016-09-08 22:01:51 -07005661 Py_XINCREF(finalizer);
5662 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005663 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005664}
5665
5666PyObject *
5667_PyEval_GetAsyncGenFinalizer(void)
5668{
Victor Stinner50b48572018-11-01 01:51:40 +01005669 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005670 return tstate->async_gen_finalizer;
5671}
5672
Victor Stinner438a12d2019-05-24 17:01:38 +02005673PyFrameObject *
5674PyEval_GetFrame(void)
5675{
5676 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005677 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005678}
5679
Guido van Rossumb209a111997-04-29 18:18:01 +00005680PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005681_PyEval_GetBuiltins(PyThreadState *tstate)
5682{
5683 PyFrameObject *frame = tstate->frame;
5684 if (frame != NULL) {
5685 return frame->f_builtins;
5686 }
5687 return tstate->interp->builtins;
5688}
5689
5690PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005691PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005692{
Victor Stinner438a12d2019-05-24 17:01:38 +02005693 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005694 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005695}
5696
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005697/* Convenience function to get a builtin from its name */
5698PyObject *
5699_PyEval_GetBuiltinId(_Py_Identifier *name)
5700{
Victor Stinner438a12d2019-05-24 17:01:38 +02005701 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005702 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5703 if (attr) {
5704 Py_INCREF(attr);
5705 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005706 else if (!_PyErr_Occurred(tstate)) {
5707 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005708 }
5709 return attr;
5710}
5711
Guido van Rossumb209a111997-04-29 18:18:01 +00005712PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005713PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005714{
Victor Stinner438a12d2019-05-24 17:01:38 +02005715 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005716 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005717 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005718 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005719 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005720 }
5721
Victor Stinner438a12d2019-05-24 17:01:38 +02005722 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005723 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005724 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005725
5726 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005727 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005728}
5729
Guido van Rossumb209a111997-04-29 18:18:01 +00005730PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005731PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005732{
Victor Stinner438a12d2019-05-24 17:01:38 +02005733 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005734 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005735 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005737 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005738
5739 assert(current_frame->f_globals != NULL);
5740 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005741}
5742
Guido van Rossum6135a871995-01-09 17:53:26 +00005743int
Tim Peters5ba58662001-07-16 02:29:45 +00005744PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005745{
Victor Stinner438a12d2019-05-24 17:01:38 +02005746 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005747 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005748 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 if (current_frame != NULL) {
5751 const int codeflags = current_frame->f_code->co_flags;
5752 const int compilerflags = codeflags & PyCF_MASK;
5753 if (compilerflags) {
5754 result = 1;
5755 cf->cf_flags |= compilerflags;
5756 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005757#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 if (codeflags & CO_GENERATOR_ALLOWED) {
5759 result = 1;
5760 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5761 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005762#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005763 }
5764 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005765}
5766
Guido van Rossum3f5da241990-12-20 15:06:42 +00005767
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005768const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005769PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005771 if (PyMethod_Check(func))
5772 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5773 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005774 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005775 else if (PyCFunction_Check(func))
5776 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5777 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005778 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005779}
5780
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005781const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005782PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005784 if (PyMethod_Check(func))
5785 return "()";
5786 else if (PyFunction_Check(func))
5787 return "()";
5788 else if (PyCFunction_Check(func))
5789 return "()";
5790 else
5791 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005792}
5793
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005794#define C_TRACE(x, call) \
Mark Shannon9e7b2072021-04-13 11:08:14 +01005795if (trace_info->cframe.use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005796 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005797 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005798 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005799 x = NULL; \
5800 } \
5801 else { \
5802 x = call; \
5803 if (tstate->c_profilefunc != NULL) { \
5804 if (x == NULL) { \
5805 call_trace_protected(tstate->c_profilefunc, \
5806 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005807 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005808 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005809 /* XXX should pass (type, value, tb) */ \
5810 } else { \
5811 if (call_trace(tstate->c_profilefunc, \
5812 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005813 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005814 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 Py_DECREF(x); \
5816 x = NULL; \
5817 } \
5818 } \
5819 } \
5820 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005821} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 x = call; \
5823 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005824
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005825
5826static PyObject *
5827trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005828 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005829 PyObject *func,
5830 PyObject **args, Py_ssize_t nargs,
5831 PyObject *kwnames)
5832{
5833 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005834 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005835 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005836 return x;
5837 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005838 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005839 /* We need to create a temporary bound method as argument
5840 for profiling.
5841
5842 If nargs == 0, then this cannot work because we have no
5843 "self". In any case, the call itself would raise
5844 TypeError (foo needs an argument), so we just skip
5845 profiling. */
5846 PyObject *self = args[0];
5847 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5848 if (func == NULL) {
5849 return NULL;
5850 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005851 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005852 args+1, nargs-1,
5853 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005854 Py_DECREF(func);
5855 return x;
5856 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005857 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005858}
5859
Victor Stinner415c5102017-01-11 00:54:57 +01005860/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5861 to reduce the stack consumption. */
5862Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005863call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005864 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005865 PyObject ***pp_stack,
5866 Py_ssize_t oparg,
5867 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005868{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005869 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005870 PyObject *func = *pfunc;
5871 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005872 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5873 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005874 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005875
Mark Shannon9e7b2072021-04-13 11:08:14 +01005876 if (trace_info->cframe.use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005877 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005878 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005879 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005880 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005881 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005882
Victor Stinner438a12d2019-05-24 17:01:38 +02005883 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005884
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005885 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005886 while ((*pp_stack) > pfunc) {
5887 w = EXT_POP(*pp_stack);
5888 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005889 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005892}
5893
Jeremy Hylton52820442001-01-03 23:52:36 +00005894static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005895do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005896 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005897 PyObject *func,
5898 PyObject *callargs,
5899 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005900{
jdemeyere89de732018-09-19 12:06:20 +02005901 PyObject *result;
5902
scoder4c9ea092020-05-12 16:12:41 +02005903 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005904 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005905 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005906 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005907 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005908 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
Mark Shannon9e7b2072021-04-13 11:08:14 +01005909 if (nargs > 0 && trace_info->cframe.use_tracing) {
jdemeyere89de732018-09-19 12:06:20 +02005910 /* We need to create a temporary bound method as argument
5911 for profiling.
5912
5913 If nargs == 0, then this cannot work because we have no
5914 "self". In any case, the call itself would raise
5915 TypeError (foo needs an argument), so we just skip
5916 profiling. */
5917 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5918 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5919 if (func == NULL) {
5920 return NULL;
5921 }
5922
Victor Stinner4d231bc2019-11-14 13:36:21 +01005923 C_TRACE(result, _PyObject_FastCallDictTstate(
5924 tstate, func,
5925 &_PyTuple_ITEMS(callargs)[1],
5926 nargs - 1,
5927 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005928 Py_DECREF(func);
5929 return result;
5930 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005931 }
jdemeyere89de732018-09-19 12:06:20 +02005932 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005933}
5934
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005935/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005936 nb_index slot defined, and store in *pi.
5937 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005938 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005939 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005940*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005941int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005942_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005943{
Victor Stinner438a12d2019-05-24 17:01:38 +02005944 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner09bbebe2021-04-11 00:17:39 +02005945 if (!Py_IsNone(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005946 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005947 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005948 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005949 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005950 return 0;
5951 }
5952 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005953 _PyErr_SetString(tstate, PyExc_TypeError,
5954 "slice indices must be integers or "
5955 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005956 return 0;
5957 }
5958 *pi = x;
5959 }
5960 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005961}
5962
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005963int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005964_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005965{
Victor Stinner438a12d2019-05-24 17:01:38 +02005966 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005967 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005968 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005969 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005970 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005971 return 0;
5972 }
5973 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005974 _PyErr_SetString(tstate, PyExc_TypeError,
5975 "slice indices must be integers or "
5976 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005977 return 0;
5978 }
5979 *pi = x;
5980 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005981}
5982
Thomas Wouters52152252000-08-17 22:55:00 +00005983static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005984import_name(PyThreadState *tstate, PyFrameObject *f,
5985 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005986{
5987 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005988 PyObject *import_func, *res;
5989 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005990
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005991 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005992 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005993 if (!_PyErr_Occurred(tstate)) {
5994 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005995 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005996 return NULL;
5997 }
5998
5999 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006000 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006001 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006002 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006003 return NULL;
6004 }
6005 res = PyImport_ImportModuleLevelObject(
6006 name,
6007 f->f_globals,
6008 f->f_locals == NULL ? Py_None : f->f_locals,
6009 fromlist,
6010 ilevel);
6011 return res;
6012 }
6013
6014 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006015
6016 stack[0] = name;
6017 stack[1] = f->f_globals;
6018 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6019 stack[3] = fromlist;
6020 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006021 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006022 Py_DECREF(import_func);
6023 return res;
6024}
6025
6026static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006027import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006029 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006030 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006031
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006032 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006033 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006034 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006035 /* Issue #17636: in case this failed because of a circular relative
6036 import, try to fallback on reading the module directly from
6037 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006038 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006039 if (pkgname == NULL) {
6040 goto error;
6041 }
Oren Milman6db70332017-09-19 14:23:01 +03006042 if (!PyUnicode_Check(pkgname)) {
6043 Py_CLEAR(pkgname);
6044 goto error;
6045 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006046 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006047 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006048 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006049 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006050 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006051 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006052 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006053 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006054 goto error;
6055 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006056 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006057 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006058 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006059 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006060 if (pkgname == NULL) {
6061 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6062 if (pkgname_or_unknown == NULL) {
6063 Py_XDECREF(pkgpath);
6064 return NULL;
6065 }
6066 } else {
6067 pkgname_or_unknown = pkgname;
6068 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006069
6070 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006071 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006072 errmsg = PyUnicode_FromFormat(
6073 "cannot import name %R from %R (unknown location)",
6074 name, pkgname_or_unknown
6075 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006076 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006077 PyErr_SetImportError(errmsg, pkgname, NULL);
6078 }
6079 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006080 _Py_IDENTIFIER(__spec__);
6081 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006082 const char *fmt =
6083 _PyModuleSpec_IsInitializing(spec) ?
6084 "cannot import name %R from partially initialized module %R "
6085 "(most likely due to a circular import) (%S)" :
6086 "cannot import name %R from %R (%S)";
6087 Py_XDECREF(spec);
6088
6089 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006090 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006091 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006092 }
6093
Xiang Zhang4830f582017-03-21 11:13:42 +08006094 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006095 Py_XDECREF(pkgname_or_unknown);
6096 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006097 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006098}
Guido van Rossumac7be682001-01-17 15:42:30 +00006099
Thomas Wouters52152252000-08-17 22:55:00 +00006100static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006101import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006102{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006103 _Py_IDENTIFIER(__all__);
6104 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006105 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006106 int skip_leading_underscores = 0;
6107 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006108
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006109 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6110 return -1; /* Unexpected error */
6111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006112 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006113 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6114 return -1;
6115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006116 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006117 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006118 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006119 return -1;
6120 }
6121 all = PyMapping_Keys(dict);
6122 Py_DECREF(dict);
6123 if (all == NULL)
6124 return -1;
6125 skip_leading_underscores = 1;
6126 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006128 for (pos = 0, err = 0; ; pos++) {
6129 name = PySequence_GetItem(all, pos);
6130 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006131 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006132 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006133 }
6134 else {
6135 _PyErr_Clear(tstate);
6136 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006137 break;
6138 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006139 if (!PyUnicode_Check(name)) {
6140 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6141 if (modname == NULL) {
6142 Py_DECREF(name);
6143 err = -1;
6144 break;
6145 }
6146 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006147 _PyErr_Format(tstate, PyExc_TypeError,
6148 "module __name__ must be a string, not %.100s",
6149 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006150 }
6151 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006152 _PyErr_Format(tstate, PyExc_TypeError,
6153 "%s in %U.%s must be str, not %.100s",
6154 skip_leading_underscores ? "Key" : "Item",
6155 modname,
6156 skip_leading_underscores ? "__dict__" : "__all__",
6157 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006158 }
6159 Py_DECREF(modname);
6160 Py_DECREF(name);
6161 err = -1;
6162 break;
6163 }
6164 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006165 if (PyUnicode_READY(name) == -1) {
6166 Py_DECREF(name);
6167 err = -1;
6168 break;
6169 }
6170 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6171 Py_DECREF(name);
6172 continue;
6173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006174 }
6175 value = PyObject_GetAttr(v, name);
6176 if (value == NULL)
6177 err = -1;
6178 else if (PyDict_CheckExact(locals))
6179 err = PyDict_SetItem(locals, name, value);
6180 else
6181 err = PyObject_SetItem(locals, name, value);
6182 Py_DECREF(name);
6183 Py_XDECREF(value);
6184 if (err != 0)
6185 break;
6186 }
6187 Py_DECREF(all);
6188 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006189}
6190
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006191static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006192check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006193{
Victor Stinnera102ed72020-02-07 02:24:48 +01006194 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006195 /* check_args_iterable() may be called with a live exception:
6196 * clear it to prevent calling _PyObject_FunctionStr() with an
6197 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006198 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006199 PyObject *funcstr = _PyObject_FunctionStr(func);
6200 if (funcstr != NULL) {
6201 _PyErr_Format(tstate, PyExc_TypeError,
6202 "%U argument after * must be an iterable, not %.200s",
6203 funcstr, Py_TYPE(args)->tp_name);
6204 Py_DECREF(funcstr);
6205 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006206 return -1;
6207 }
6208 return 0;
6209}
6210
6211static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006212format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006213{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006214 /* _PyDict_MergeEx raises attribute
6215 * error (percolated from an attempt
6216 * to get 'keys' attribute) instead of
6217 * a type error if its second argument
6218 * is not a mapping.
6219 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006220 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006221 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006222 PyObject *funcstr = _PyObject_FunctionStr(func);
6223 if (funcstr != NULL) {
6224 _PyErr_Format(
6225 tstate, PyExc_TypeError,
6226 "%U argument after ** must be a mapping, not %.200s",
6227 funcstr, Py_TYPE(kwargs)->tp_name);
6228 Py_DECREF(funcstr);
6229 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006230 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006231 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006232 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006233 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006234 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006235 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006236 PyObject *funcstr = _PyObject_FunctionStr(func);
6237 if (funcstr != NULL) {
6238 PyObject *key = PyTuple_GET_ITEM(val, 0);
6239 _PyErr_Format(
6240 tstate, PyExc_TypeError,
6241 "%U got multiple values for keyword argument '%S'",
6242 funcstr, key);
6243 Py_DECREF(funcstr);
6244 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006245 Py_XDECREF(exc);
6246 Py_XDECREF(val);
6247 Py_XDECREF(tb);
6248 }
6249 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006250 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006251 }
6252 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006253}
6254
Guido van Rossumac7be682001-01-17 15:42:30 +00006255static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006256format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6257 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006259 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006261 if (!obj)
6262 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006263
Serhiy Storchaka06515832016-11-20 09:13:07 +02006264 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006265 if (!obj_str)
6266 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006267
Victor Stinner438a12d2019-05-24 17:01:38 +02006268 _PyErr_Format(tstate, exc, format_str, obj_str);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +01006269
6270 if (exc == PyExc_NameError) {
6271 // Include the name in the NameError exceptions to offer suggestions later.
6272 _Py_IDENTIFIER(name);
6273 PyObject *type, *value, *traceback;
6274 PyErr_Fetch(&type, &value, &traceback);
6275 PyErr_NormalizeException(&type, &value, &traceback);
6276 if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
6277 // We do not care if this fails because we are going to restore the
6278 // NameError anyway.
6279 (void)_PyObject_SetAttrId(value, &PyId_name, obj);
6280 }
6281 PyErr_Restore(type, value, traceback);
6282 }
Paul Prescode68140d2000-08-30 20:25:01 +00006283}
Guido van Rossum950361c1997-01-24 13:49:28 +00006284
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006285static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006286format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006287{
6288 PyObject *name;
6289 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006290 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006291 return;
6292 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6293 name = PyTuple_GET_ITEM(co->co_cellvars,
6294 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006295 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006296 PyExc_UnboundLocalError,
6297 UNBOUNDLOCAL_ERROR_MSG,
6298 name);
6299 } else {
6300 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6301 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006302 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006303 UNBOUNDFREE_ERROR_MSG, name);
6304 }
6305}
6306
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006307static void
Mark Shannonfee55262019-11-21 09:11:43 +00006308format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006309{
6310 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6311 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006312 _PyErr_Format(tstate, PyExc_TypeError,
6313 "'async with' received an object from __aenter__ "
6314 "that does not implement __await__: %.100s",
6315 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006316 }
Mark Shannonfee55262019-11-21 09:11:43 +00006317 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006318 _PyErr_Format(tstate, PyExc_TypeError,
6319 "'async with' received an object from __aexit__ "
6320 "that does not implement __await__: %.100s",
6321 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006322 }
6323 }
6324}
6325
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006326static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006327unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006328 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006329{
6330 PyObject *res;
6331 if (Py_REFCNT(v) == 2) {
6332 /* In the common case, there are 2 references to the value
6333 * stored in 'variable' when the += is performed: one on the
6334 * value stack (in 'v') and one still stored in the
6335 * 'variable'. We try to delete the variable now to reduce
6336 * the refcnt to 1.
6337 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006338 int opcode, oparg;
6339 NEXTOPARG();
6340 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006341 case STORE_FAST:
6342 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006343 PyObject **fastlocals = f->f_localsplus;
6344 if (GETLOCAL(oparg) == v)
6345 SETLOCAL(oparg, NULL);
6346 break;
6347 }
6348 case STORE_DEREF:
6349 {
6350 PyObject **freevars = (f->f_localsplus +
6351 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006352 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006353 if (PyCell_GET(c) == v) {
6354 PyCell_SET(c, NULL);
6355 Py_DECREF(v);
6356 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006357 break;
6358 }
6359 case STORE_NAME:
6360 {
6361 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006362 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006363 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006364 if (locals && PyDict_CheckExact(locals)) {
6365 PyObject *w = PyDict_GetItemWithError(locals, name);
6366 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006367 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006368 {
6369 Py_DECREF(v);
6370 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006371 }
6372 }
6373 break;
6374 }
6375 }
6376 }
6377 res = v;
6378 PyUnicode_Append(&res, w);
6379 return res;
6380}
6381
Guido van Rossum950361c1997-01-24 13:49:28 +00006382#ifdef DYNAMIC_EXECUTION_PROFILE
6383
Skip Montanarof118cb12001-10-15 20:51:38 +00006384static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006385getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006387 int i;
6388 PyObject *l = PyList_New(256);
6389 if (l == NULL) return NULL;
6390 for (i = 0; i < 256; i++) {
6391 PyObject *x = PyLong_FromLong(a[i]);
6392 if (x == NULL) {
6393 Py_DECREF(l);
6394 return NULL;
6395 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006396 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006397 }
6398 for (i = 0; i < 256; i++)
6399 a[i] = 0;
6400 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006401}
6402
6403PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006404_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006405{
6406#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006407 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006408#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006409 int i;
6410 PyObject *l = PyList_New(257);
6411 if (l == NULL) return NULL;
6412 for (i = 0; i < 257; i++) {
6413 PyObject *x = getarray(dxpairs[i]);
6414 if (x == NULL) {
6415 Py_DECREF(l);
6416 return NULL;
6417 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006418 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006419 }
6420 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006421#endif
6422}
6423
6424#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006425
6426Py_ssize_t
6427_PyEval_RequestCodeExtraIndex(freefunc free)
6428{
Victor Stinner81a7be32020-04-14 15:14:01 +02006429 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006430 Py_ssize_t new_index;
6431
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006432 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006433 return -1;
6434 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006435 new_index = interp->co_extra_user_count++;
6436 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006437 return new_index;
6438}
Łukasz Langaa785c872016-09-09 17:37:37 -07006439
6440static void
6441dtrace_function_entry(PyFrameObject *f)
6442{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006443 const char *filename;
6444 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006445 int lineno;
6446
Victor Stinner6d86a232020-04-29 00:56:58 +02006447 PyCodeObject *code = f->f_code;
6448 filename = PyUnicode_AsUTF8(code->co_filename);
6449 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006450 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006451
Andy Lestere6be9b52020-02-11 20:28:35 -06006452 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006453}
6454
6455static void
6456dtrace_function_return(PyFrameObject *f)
6457{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006458 const char *filename;
6459 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006460 int lineno;
6461
Victor Stinner6d86a232020-04-29 00:56:58 +02006462 PyCodeObject *code = f->f_code;
6463 filename = PyUnicode_AsUTF8(code->co_filename);
6464 funcname = PyUnicode_AsUTF8(code->co_name);
Mark Shannonfcb55c02021-04-01 16:00:31 +01006465 lineno = PyFrame_GetLineNumber(f);
Łukasz Langaa785c872016-09-09 17:37:37 -07006466
Andy Lestere6be9b52020-02-11 20:28:35 -06006467 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006468}
6469
6470/* DTrace equivalent of maybe_call_line_trace. */
6471static void
6472maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon9f2c63b2021-07-08 19:21:22 +01006473 PyTraceInfo *trace_info, int instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006474{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006475 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006476
6477 /* If the last instruction executed isn't in the current
6478 instruction window, reset the window.
6479 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006480 initialize_trace_info(trace_info, frame);
Serhiy Storchakab5499782021-10-04 15:01:11 +03006481 int line = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006482 /* If the last instruction falls at the start of a line or if
6483 it represents a jump backwards, update the frame's line
6484 number and call the trace function. */
Mark Shannon9f2c63b2021-07-08 19:21:22 +01006485 if (line != frame->f_lineno || frame->f_lasti < instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006486 if (line != -1) {
6487 frame->f_lineno = line;
6488 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6489 if (!co_filename)
6490 co_filename = "?";
6491 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6492 if (!co_name)
6493 co_name = "?";
6494 PyDTrace_LINE(co_filename, co_name, line);
6495 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006496 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006497}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006498
6499
6500/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6501 for the limited API. */
6502
6503#undef Py_EnterRecursiveCall
6504
6505int Py_EnterRecursiveCall(const char *where)
6506{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006507 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006508}
6509
6510#undef Py_LeaveRecursiveCall
6511
6512void Py_LeaveRecursiveCall(void)
6513{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006514 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006515}