blob: 7862643983e0864264ff878926b448ae517841ba [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#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
Guido van Rossum408027e1996-12-30 16:17:54 +000036#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000037/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038#define LLTRACE 1 /* Low-level trace feature */
39#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#endif
41
Victor Stinner5c75f372019-04-17 23:02:26 +020042#if !defined(Py_BUILD_CORE)
43# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
44#endif
45
Hai Shi46874c22020-01-30 17:20:25 -060046_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000047
Guido van Rossum374a9221991-04-04 10:40:29 +000048/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020049Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon86433452021-01-07 16:49:02 +000050 PyThreadState *tstate, PyCodeAddressRange *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020051 Py_ssize_t oparg, PyObject *kwnames);
52static PyObject * do_call_core(
Mark Shannon86433452021-01-07 16:49:02 +000053 PyThreadState *tstate, PyCodeAddressRange *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020054 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000055
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000057static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020058static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000059#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060static int call_trace(Py_tracefunc, PyObject *,
61 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000062 PyCodeAddressRange *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000064static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010065 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000066 PyCodeAddressRange *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010067 int, PyObject *);
68static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000069 PyThreadState *, PyFrameObject *,
70 PyCodeAddressRange *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000071static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060072 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000073 PyCodeAddressRange *, int *);
74static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070075static void dtrace_function_entry(PyFrameObject *);
76static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000077
Victor Stinner438a12d2019-05-24 17:01:38 +020078static PyObject * import_name(PyThreadState *, PyFrameObject *,
79 PyObject *, PyObject *, PyObject *);
80static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
81static int import_all_from(PyThreadState *, PyObject *, PyObject *);
82static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
83static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
84static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030085 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020086static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
87static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
88static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000089static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Paul Prescode68140d2000-08-30 20:25:01 +000091#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000093#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000095#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 "free variable '%.200s' referenced before assignment" \
97 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
Inada Naoki91234a12019-06-03 21:30:58 +0900109/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900110#ifdef Py_DEBUG
111// --with-pydebug is used to find memory leak. opcache makes it harder.
112// So we disable opcache when Py_DEBUG is defined.
113// See bpo-37146
114#define OPCACHE_MIN_RUNS 0 /* disable opcache */
115#else
Inada Naoki91234a12019-06-03 21:30:58 +0900116#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900117#endif
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
121#if OPCACHE_STATS
122static size_t opcache_code_objects = 0;
123static size_t opcache_code_objects_extra_mem = 0;
124
125static size_t opcache_global_opts = 0;
126static size_t opcache_global_hits = 0;
127static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100128
129static size_t opcache_attr_opts = 0;
130static size_t opcache_attr_hits = 0;
131static size_t opcache_attr_misses = 0;
132static size_t opcache_attr_deopts = 0;
133static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900134#endif
135
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100136
Victor Stinnerda2914d2020-03-20 09:29:08 +0100137#ifndef NDEBUG
138/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
139 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
140 when a thread continues to run after Python finalization, especially
141 daemon threads. */
142static int
143is_tstate_valid(PyThreadState *tstate)
144{
145 assert(!_PyMem_IsPtrFreed(tstate));
146 assert(!_PyMem_IsPtrFreed(tstate->interp));
147 return 1;
148}
149#endif
150
151
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000152/* This can set eval_breaker to 0 even though gil_drop_request became
153 1. We believe this is all right because the eval loop will release
154 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200156COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200157 struct _ceval_runtime_state *ceval,
158 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159{
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
161 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200162 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200163 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200164 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100165 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200166 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100167}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000169
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200171SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172{
Victor Stinner299b8c62020-05-05 17:40:18 +0200173 struct _ceval_state *ceval2 = &interp->ceval;
174 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
175 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
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 +0200180RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181{
Victor Stinner299b8c62020-05-05 17:40:18 +0200182 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
183 struct _ceval_state *ceval2 = &interp->ceval;
184 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
185 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100186}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Eric Snowfdf282d2019-01-11 14:26:55 -0700188
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200190SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191{
Victor Stinner299b8c62020-05-05 17:40:18 +0200192 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
193 struct _ceval_state *ceval2 = &interp->ceval;
194 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
195 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100196}
Eric Snowfdf282d2019-01-11 14:26:55 -0700197
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000198
Victor Stinnerda2914d2020-03-20 09:29:08 +0100199static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200200UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100201{
Victor Stinner299b8c62020-05-05 17:40:18 +0200202 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
203 struct _ceval_state *ceval2 = &interp->ceval;
204 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
207
208
209static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100210SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200214 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100215 if (force) {
216 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
217 }
218 else {
219 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
220 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
221 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100222}
223
224
225static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200226UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100227{
Victor Stinner299b8c62020-05-05 17:40:18 +0200228 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
229 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200230 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100232}
233
234
235static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200236SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100237{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200238 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100239 ceval2->pending.async_exc = 1;
240 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
241}
242
243
244static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200245UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100246{
Victor Stinner299b8c62020-05-05 17:40:18 +0200247 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
248 struct _ceval_state *ceval2 = &interp->ceval;
249 ceval2->pending.async_exc = 0;
250 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100251}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
253
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000255#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000256#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000257#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000258
Victor Stinner3026cad2020-06-01 16:02:40 +0200259void _Py_NO_RETURN
260_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100261{
Victor Stinner3026cad2020-06-01 16:02:40 +0200262 _Py_FatalErrorFunc(func,
263 "the function must be called with the GIL held, "
264 "but the GIL is released "
265 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100266}
267
Victor Stinner7be4e352020-05-05 20:27:47 +0200268#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
269int
270_PyEval_ThreadsInitialized(PyInterpreterState *interp)
271{
272 return gil_created(&interp->ceval.gil);
273}
274
275int
276PyEval_ThreadsInitialized(void)
277{
278 // Fatal error if there is no current interpreter
279 PyInterpreterState *interp = PyInterpreterState_Get();
280 return _PyEval_ThreadsInitialized(interp);
281}
282#else
Tim Peters7f468f22004-10-11 02:40:51 +0000283int
Victor Stinner175a7042020-03-10 00:37:48 +0100284_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
285{
286 return gil_created(&runtime->ceval.gil);
287}
288
289int
Tim Peters7f468f22004-10-11 02:40:51 +0000290PyEval_ThreadsInitialized(void)
291{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100292 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100293 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000294}
Victor Stinner7be4e352020-05-05 20:27:47 +0200295#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000296
Victor Stinner111e4ee2020-03-09 21:24:14 +0100297PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200298_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299{
Victor Stinner7be4e352020-05-05 20:27:47 +0200300#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100301 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200302 /* Currently, the GIL is shared by all interpreters,
303 and only the main interpreter is responsible to create
304 and destroy it. */
305 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100306 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200307#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100308
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
310 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
311#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200312 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200313#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200314 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100315
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200316 PyThread_init_thread();
317 create_gil(gil);
318
319 take_gil(tstate);
320
321 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100322 return _PyStatus_OK();
323}
324
325void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100326_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200327{
Victor Stinner7be4e352020-05-05 20:27:47 +0200328#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100329 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200330 /* Currently, the GIL is shared by all interpreters,
331 and only the main interpreter is responsible to create
332 and destroy it. */
333 return;
334 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200335#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200336
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100338 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200339#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100340 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200341#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200342 if (!gil_created(gil)) {
343 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
344 yet: do nothing. */
345 return;
346 }
347
348 destroy_gil(gil);
349 assert(!gil_created(gil));
350}
351
352void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100353PyEval_InitThreads(void)
354{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100355 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000357
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358void
Inada Naoki91234a12019-06-03 21:30:58 +0900359_PyEval_Fini(void)
360{
361#if OPCACHE_STATS
362 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
363 opcache_code_objects);
364
365 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
366 opcache_code_objects_extra_mem);
367
368 fprintf(stderr, "\n");
369
370 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
371 opcache_global_hits,
372 (int) (100.0 * opcache_global_hits /
373 (opcache_global_hits + opcache_global_misses)));
374
375 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
376 opcache_global_misses,
377 (int) (100.0 * opcache_global_misses /
378 (opcache_global_hits + opcache_global_misses)));
379
380 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
381 opcache_global_opts);
382
383 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100384
385 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
386 opcache_attr_hits,
387 (int) (100.0 * opcache_attr_hits /
388 opcache_attr_total));
389
390 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
391 opcache_attr_misses,
392 (int) (100.0 * opcache_attr_misses /
393 opcache_attr_total));
394
395 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
396 opcache_attr_opts);
397
398 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
399 opcache_attr_deopts);
400
401 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
402 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900403#endif
404}
405
406void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000408{
Victor Stinner09532fe2019-05-10 23:39:09 +0200409 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200410 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200411 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100412
Victor Stinner85f5a692020-03-09 22:12:04 +0100413 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414}
415
416void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418{
Victor Stinner09532fe2019-05-10 23:39:09 +0200419 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200420 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100422 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100423 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200424 struct _ceval_runtime_state *ceval = &runtime->ceval;
425 struct _ceval_state *ceval2 = &tstate->interp->ceval;
426 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427}
428
429void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100430_PyEval_ReleaseLock(PyThreadState *tstate)
431{
432 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200433 struct _ceval_state *ceval2 = &tstate->interp->ceval;
434 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100435}
436
437void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000438PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000439{
Victor Stinner3026cad2020-06-01 16:02:40 +0200440 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100441
Victor Stinner85f5a692020-03-09 22:12:04 +0100442 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200443
Victor Stinner85f5a692020-03-09 22:12:04 +0100444 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200445#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
446 (void)_PyThreadState_Swap(gilstate, tstate);
447#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100448 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100449 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200450 }
Victor Stinnere838a932020-05-05 19:56:48 +0200451#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000452}
453
454void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000456{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100457 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200458
Victor Stinner01b1cc12019-11-20 02:27:56 +0100459 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200460 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
461 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100462 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200463 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200464 struct _ceval_runtime_state *ceval = &runtime->ceval;
465 struct _ceval_state *ceval2 = &tstate->interp->ceval;
466 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000467}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000468
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900469#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200470/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200471 which are not running in the child process, and clear internal locks
472 which might be held by those threads. */
473PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200474_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000475{
Victor Stinner317bab02020-06-02 18:44:54 +0200476 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200477
478#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
479 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
480#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100481 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200482#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100483 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200484 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200485 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100486 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100487
488 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700489
Victor Stinner50e6e992020-03-19 02:41:21 +0100490 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900491 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200492 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700493 }
Jesse Nollera8513972008-07-17 16:49:17 +0000494
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200495 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100496 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200497 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000498}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900499#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000500
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000501/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600502 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503
504void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100505_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000506{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100507 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000508}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000510PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512{
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200514#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
515 PyThreadState *old_tstate = _PyThreadState_GET();
516 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
517#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200518 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200519#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200520 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100521
Victor Stinner0b1e3302020-05-05 16:14:31 +0200522 struct _ceval_runtime_state *ceval = &runtime->ceval;
523 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200524#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
525 assert(gil_created(&ceval2->gil));
526#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200528#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200529 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000531}
532
533void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535{
Victor Stinner3026cad2020-06-01 16:02:40 +0200536 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100537
Victor Stinner85f5a692020-03-09 22:12:04 +0100538 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100539
Victor Stinner85f5a692020-03-09 22:12:04 +0100540 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
541 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000542}
543
544
Guido van Rossuma9672091994-09-14 13:31:22 +0000545/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
546 signal handlers or Mac I/O completion routines) can schedule calls
547 to a function to be called synchronously.
548 The synchronous function is called with one void* argument.
549 It should return 0 for success or -1 for failure -- failure should
550 be accompanied by an exception.
551
552 If registry succeeds, the registry function returns 0; if it fails
553 (e.g. due to too many pending calls) it returns -1 (without setting
554 an exception condition).
555
556 Note that because registry may occur from within signal handlers,
557 or other asynchronous events, calling malloc() is unsafe!
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559 Any thread can schedule pending calls, but only the main thread
560 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000561 There is no facility to schedule calls to a particular thread, but
562 that should be easy to change, should that ever be required. In
563 that case, the static variables here should go into the python
564 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000566
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200567void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200568_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200569{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100570#ifdef MS_WINDOWS
571 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
572 // handler which can run in a thread different than the Python thread, in
573 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
574 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
575 //
576 // The next eval_frame_handle_pending() call will call
577 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
578 int force = 1;
579#else
580 int force = 0;
581#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200582 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100583 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200584 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100585 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200586}
587
Eric Snow5be45a62019-03-08 22:47:07 -0700588/* Push one item onto the queue while holding the lock. */
589static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200590_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600591 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700592{
Eric Snow842a2f02019-03-15 15:47:51 -0600593 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700594 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600595 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700596 return -1; /* Queue full */
597 }
Eric Snow842a2f02019-03-15 15:47:51 -0600598 pending->calls[i].func = func;
599 pending->calls[i].arg = arg;
600 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700601 return 0;
602}
603
604/* Pop one item off the queue while holding the lock. */
605static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200606_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600607 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700608{
Eric Snow842a2f02019-03-15 15:47:51 -0600609 int i = pending->first;
610 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700611 return; /* Queue empty */
612 }
613
Eric Snow842a2f02019-03-15 15:47:51 -0600614 *func = pending->calls[i].func;
615 *arg = pending->calls[i].arg;
616 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700617}
618
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200619/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000620 scheduling to be made from any thread, and even from an executing
621 callback.
622 */
623
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000624int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200625_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200626 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000627{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200628 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600629
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200630 /* Ensure that _PyEval_InitPendingCalls() was called
631 and that _PyEval_FiniPendingCalls() is not called yet. */
632 assert(pending->lock != NULL);
633
Eric Snow842a2f02019-03-15 15:47:51 -0600634 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600636 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700637
Victor Stinnere225beb2019-06-03 18:14:24 +0200638 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000641}
642
Victor Stinner09532fe2019-05-10 23:39:09 +0200643int
644Py_AddPendingCall(int (*func)(void *), void *arg)
645{
Victor Stinner50e6e992020-03-19 02:41:21 +0100646 /* Best-effort to support subinterpreters and calls with the GIL released.
647
648 First attempt _PyThreadState_GET() since it supports subinterpreters.
649
650 If the GIL is released, _PyThreadState_GET() returns NULL . In this
651 case, use PyGILState_GetThisThreadState() which works even if the GIL
652 is released.
653
654 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
655 see bpo-10915 and bpo-15751.
656
Victor Stinner8849e592020-03-18 19:28:53 +0100657 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100658 PyThreadState *tstate = _PyThreadState_GET();
659 if (tstate == NULL) {
660 tstate = PyGILState_GetThisThreadState();
661 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200662
663 PyInterpreterState *interp;
664 if (tstate != NULL) {
665 interp = tstate->interp;
666 }
667 else {
668 /* Last resort: use the main interpreter */
669 interp = _PyRuntime.interpreters.main;
670 }
671 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200672}
673
Eric Snowfdf282d2019-01-11 14:26:55 -0700674static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100675handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700676{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200677 assert(is_tstate_valid(tstate));
678 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700679 return 0;
680 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700681
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200682 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100683 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
684 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100685 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700686 return -1;
687 }
688 return 0;
689}
690
691static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100692make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000693{
Victor Stinnerd8316882020-03-20 14:50:35 +0100694 /* only execute pending calls on main thread */
695 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200696 return 0;
697 }
698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100700 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700701 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700703 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200704 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100705
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200706 /* unsignal before starting to call callbacks, so that any callback
707 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100708 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700709 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100712 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700713 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700714 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 void *arg = NULL;
716
717 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600718 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200719 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600720 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700721
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100722 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700723 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100724 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700725 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700726 res = func(arg);
727 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200728 goto error;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200731
Charles-François Natalif23339a2011-07-23 18:15:43 +0200732 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700733 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200734
735error:
736 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100737 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700738 return res;
739}
740
Eric Snow842a2f02019-03-15 15:47:51 -0600741void
Victor Stinner2b1df452020-01-13 18:46:59 +0100742_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600743{
Eric Snow842a2f02019-03-15 15:47:51 -0600744 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100745 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600746
Victor Stinner50e6e992020-03-19 02:41:21 +0100747 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200748
Eric Snow842a2f02019-03-15 15:47:51 -0600749 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
750 return;
751 }
752
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100753 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200754 PyObject *exc, *val, *tb;
755 _PyErr_Fetch(tstate, &exc, &val, &tb);
756 PyErr_BadInternalCall();
757 _PyErr_ChainExceptions(exc, val, tb);
758 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600759 }
760}
761
Eric Snowfdf282d2019-01-11 14:26:55 -0700762/* Py_MakePendingCalls() is a simple wrapper for the sake
763 of backward-compatibility. */
764int
765Py_MakePendingCalls(void)
766{
767 assert(PyGILState_Check());
768
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100769 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100770 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771
Eric Snowfdf282d2019-01-11 14:26:55 -0700772 /* Python signal handler doesn't really queue a callback: it only signals
773 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100774 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700775 if (res != 0) {
776 return res;
777 }
778
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100779 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600780 if (res != 0) {
781 return res;
782 }
783
784 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000785}
786
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000787/* The interpreter's recursion limit */
788
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000789#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200790# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000791#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600792
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600793void
Victor Stinnerdab84232020-03-17 18:56:44 +0100794_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600795{
Victor Stinner7be4e352020-05-05 20:27:47 +0200796#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100797 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200798#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100799}
800
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200801int
Victor Stinnerdab84232020-03-17 18:56:44 +0100802_PyEval_InitState(struct _ceval_state *ceval)
803{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200804 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
805
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200806 struct _pending_calls *pending = &ceval->pending;
807 assert(pending->lock == NULL);
808
809 pending->lock = PyThread_allocate_lock();
810 if (pending->lock == NULL) {
811 return -1;
812 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200813
814#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
815 _gil_initialize(&ceval->gil);
816#endif
817
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200818 return 0;
819}
820
821void
822_PyEval_FiniState(struct _ceval_state *ceval)
823{
824 struct _pending_calls *pending = &ceval->pending;
825 if (pending->lock != NULL) {
826 PyThread_free_lock(pending->lock);
827 pending->lock = NULL;
828 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600829}
830
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000831int
832Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000833{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200834 PyInterpreterState *interp = _PyInterpreterState_GET();
835 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000836}
837
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000838void
839Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000840{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200841 PyThreadState *tstate = _PyThreadState_GET();
842 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000843}
844
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100845/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200846 if the recursion_depth reaches recursion_limit.
847 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000848 to guarantee that _Py_CheckRecursiveCall() is regularly called.
849 Without USE_STACKCHECK, there is no need for this. */
850int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100851_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000852{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200853 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000854
855#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700856 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (PyOS_CheckStack()) {
858 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200859 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return -1;
861 }
pdox18967932017-10-25 23:03:01 -0700862#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000863 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (tstate->recursion_depth > recursion_limit + 50) {
865 /* Overflowing while handling an overflow. Give up. */
866 Py_FatalError("Cannot recover from stack overflow.");
867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000869 else {
870 if (tstate->recursion_depth > recursion_limit) {
871 tstate->recursion_headroom++;
872 _PyErr_Format(tstate, PyExc_RecursionError,
873 "maximum recursion depth exceeded%s",
874 where);
875 tstate->recursion_headroom--;
876 --tstate->recursion_depth;
877 return -1;
878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 }
880 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000881}
882
Brandt Bucher145bf262021-02-26 14:51:55 -0800883
884// PEP 634: Structural Pattern Matching
885
886
887// Return a tuple of values corresponding to keys, with error checks for
888// duplicate/missing keys.
889static PyObject*
890match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
891{
892 assert(PyTuple_CheckExact(keys));
893 Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
894 if (!nkeys) {
895 // No keys means no items.
896 return PyTuple_New(0);
897 }
898 PyObject *seen = NULL;
899 PyObject *dummy = NULL;
900 PyObject *values = NULL;
901 // We use the two argument form of map.get(key, default) for two reasons:
902 // - Atomically check for a key and get its value without error handling.
903 // - Don't cause key creation or resizing in dict subclasses like
904 // collections.defaultdict that define __missing__ (or similar).
905 _Py_IDENTIFIER(get);
906 PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
907 if (get == NULL) {
908 goto fail;
909 }
910 seen = PySet_New(NULL);
911 if (seen == NULL) {
912 goto fail;
913 }
914 // dummy = object()
915 dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
916 if (dummy == NULL) {
917 goto fail;
918 }
919 values = PyList_New(0);
920 if (values == NULL) {
921 goto fail;
922 }
923 for (Py_ssize_t i = 0; i < nkeys; i++) {
924 PyObject *key = PyTuple_GET_ITEM(keys, i);
925 if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
926 if (!_PyErr_Occurred(tstate)) {
927 // Seen it before!
928 _PyErr_Format(tstate, PyExc_ValueError,
929 "mapping pattern checks duplicate key (%R)", key);
930 }
931 goto fail;
932 }
933 PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
934 if (value == NULL) {
935 goto fail;
936 }
937 if (value == dummy) {
938 // key not in map!
939 Py_DECREF(value);
940 Py_DECREF(values);
941 // Return None:
942 Py_INCREF(Py_None);
943 values = Py_None;
944 goto done;
945 }
946 PyList_Append(values, value);
947 Py_DECREF(value);
948 }
949 Py_SETREF(values, PyList_AsTuple(values));
950 // Success:
951done:
952 Py_DECREF(get);
953 Py_DECREF(seen);
954 Py_DECREF(dummy);
955 return values;
956fail:
957 Py_XDECREF(get);
958 Py_XDECREF(seen);
959 Py_XDECREF(dummy);
960 Py_XDECREF(values);
961 return NULL;
962}
963
964// Extract a named attribute from the subject, with additional bookkeeping to
965// raise TypeErrors for repeated lookups. On failure, return NULL (with no
966// error set). Use _PyErr_Occurred(tstate) to disambiguate.
967static PyObject*
968match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
969 PyObject *name, PyObject *seen)
970{
971 assert(PyUnicode_CheckExact(name));
972 assert(PySet_CheckExact(seen));
973 if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
974 if (!_PyErr_Occurred(tstate)) {
975 // Seen it before!
976 _PyErr_Format(tstate, PyExc_TypeError,
977 "%s() got multiple sub-patterns for attribute %R",
978 ((PyTypeObject*)type)->tp_name, name);
979 }
980 return NULL;
981 }
982 PyObject *attr = PyObject_GetAttr(subject, name);
983 if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
984 _PyErr_Clear(tstate);
985 }
986 return attr;
987}
988
989// On success (match), return a tuple of extracted attributes. On failure (no
990// match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
991static PyObject*
992match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
993 Py_ssize_t nargs, PyObject *kwargs)
994{
995 if (!PyType_Check(type)) {
996 const char *e = "called match pattern must be a type";
997 _PyErr_Format(tstate, PyExc_TypeError, e);
998 return NULL;
999 }
1000 assert(PyTuple_CheckExact(kwargs));
1001 // First, an isinstance check:
1002 if (PyObject_IsInstance(subject, type) <= 0) {
1003 return NULL;
1004 }
1005 // So far so good:
1006 PyObject *seen = PySet_New(NULL);
1007 if (seen == NULL) {
1008 return NULL;
1009 }
1010 PyObject *attrs = PyList_New(0);
1011 if (attrs == NULL) {
1012 Py_DECREF(seen);
1013 return NULL;
1014 }
1015 // NOTE: From this point on, goto fail on failure:
1016 PyObject *match_args = NULL;
1017 // First, the positional subpatterns:
1018 if (nargs) {
1019 int match_self = 0;
1020 match_args = PyObject_GetAttrString(type, "__match_args__");
1021 if (match_args) {
1022 if (PyList_CheckExact(match_args)) {
1023 Py_SETREF(match_args, PyList_AsTuple(match_args));
1024 }
1025 if (match_args == NULL) {
1026 goto fail;
1027 }
1028 if (!PyTuple_CheckExact(match_args)) {
1029 const char *e = "%s.__match_args__ must be a list or tuple "
1030 "(got %s)";
1031 _PyErr_Format(tstate, PyExc_TypeError, e,
1032 ((PyTypeObject *)type)->tp_name,
1033 Py_TYPE(match_args)->tp_name);
1034 goto fail;
1035 }
1036 }
1037 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1038 _PyErr_Clear(tstate);
1039 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1040 // define __match_args__. This is natural behavior for subclasses:
1041 // it's as if __match_args__ is some "magic" value that is lost as
1042 // soon as they redefine it.
1043 match_args = PyTuple_New(0);
1044 match_self = PyType_HasFeature((PyTypeObject*)type,
1045 _Py_TPFLAGS_MATCH_SELF);
1046 }
1047 else {
1048 goto fail;
1049 }
1050 assert(PyTuple_CheckExact(match_args));
1051 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1052 if (allowed < nargs) {
1053 const char *plural = (allowed == 1) ? "" : "s";
1054 _PyErr_Format(tstate, PyExc_TypeError,
1055 "%s() accepts %d positional sub-pattern%s (%d given)",
1056 ((PyTypeObject*)type)->tp_name,
1057 allowed, plural, nargs);
1058 goto fail;
1059 }
1060 if (match_self) {
1061 // Easy. Copy the subject itself, and move on to kwargs.
1062 PyList_Append(attrs, subject);
1063 }
1064 else {
1065 for (Py_ssize_t i = 0; i < nargs; i++) {
1066 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1067 if (!PyUnicode_CheckExact(name)) {
1068 _PyErr_Format(tstate, PyExc_TypeError,
1069 "__match_args__ elements must be strings "
1070 "(got %s)", Py_TYPE(name)->tp_name);
1071 goto fail;
1072 }
1073 PyObject *attr = match_class_attr(tstate, subject, type, name,
1074 seen);
1075 if (attr == NULL) {
1076 goto fail;
1077 }
1078 PyList_Append(attrs, attr);
1079 Py_DECREF(attr);
1080 }
1081 }
1082 Py_CLEAR(match_args);
1083 }
1084 // Finally, the keyword subpatterns:
1085 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1086 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1087 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1088 if (attr == NULL) {
1089 goto fail;
1090 }
1091 PyList_Append(attrs, attr);
1092 Py_DECREF(attr);
1093 }
1094 Py_SETREF(attrs, PyList_AsTuple(attrs));
1095 Py_DECREF(seen);
1096 return attrs;
1097fail:
1098 // We really don't care whether an error was raised or not... that's our
1099 // caller's problem. All we know is that the match failed.
1100 Py_XDECREF(match_args);
1101 Py_DECREF(seen);
1102 Py_DECREF(attrs);
1103 return NULL;
1104}
1105
1106
Victor Stinner09532fe2019-05-10 23:39:09 +02001107static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001108static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001109
Victor Stinnere225beb2019-06-03 18:14:24 +02001110#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +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 Stinner46496f92021-02-20 15:17:18 +01001120 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
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 Stinnerc6944e72016-11-11 02:13:35 +01001224PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001225_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001226{
Victor Stinner3026cad2020-06-01 16:02:40 +02001227 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001228
Guido van Rossum950361c1997-01-24 13:49:28 +00001229#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001231#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001232 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001233 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001234 int opcode; /* Current opcode */
1235 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001236 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001238 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001239 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 is true when the line being executed has changed. The
1247 initial values are such as to make this false the first
1248 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001249
Serhiy Storchakaab874002016-09-11 13:48:15 +03001250 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 PyObject *names;
1252 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001253 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001254
Brett Cannon368b4b72012-04-02 12:17:59 -04001255#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001256 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001257#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001258
Antoine Pitroub52ec782009-01-25 16:34:23 +00001259/* Computed GOTOs, or
1260 the-optimization-commonly-but-improperly-known-as-"threaded code"
1261 using gcc's labels-as-values extension
1262 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1263
1264 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001266 combined with a lookup table of jump addresses. However, since the
1267 indirect jump instruction is shared by all opcodes, the CPU will have a
1268 hard time making the right prediction for where to jump next (actually,
1269 it will be always wrong except in the uncommon case of a sequence of
1270 several identical opcodes).
1271
1272 "Threaded code" in contrast, uses an explicit jump table and an explicit
1273 indirect jump instruction at the end of each opcode. Since the jump
1274 instruction is at a different address for each opcode, the CPU will make a
1275 separate prediction for each of these instructions, which is equivalent to
1276 predicting the second opcode of each opcode pair. These predictions have
1277 a much better chance to turn out valid, especially in small bytecode loops.
1278
1279 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001281 and potentially many more instructions (depending on the pipeline width).
1282 A correctly predicted branch, however, is nearly free.
1283
1284 At the time of this writing, the "threaded code" version is up to 15-20%
1285 faster than the normal "switch" version, depending on the compiler and the
1286 CPU architecture.
1287
1288 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1289 because it would render the measurements invalid.
1290
1291
1292 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1293 indirect jumps by sharing them between all opcodes. Such optimizations
1294 can be disabled on gcc by using the -fno-gcse flag (or possibly
1295 -fno-crossjumping).
1296*/
1297
Antoine Pitrou042b1282010-08-13 21:15:58 +00001298#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001300#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301#endif
1302
Antoine Pitrou042b1282010-08-13 21:15:58 +00001303#ifdef HAVE_COMPUTED_GOTOS
1304 #ifndef USE_COMPUTED_GOTOS
1305 #define USE_COMPUTED_GOTOS 1
1306 #endif
1307#else
1308 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1309 #error "Computed gotos are not supported on this compiler."
1310 #endif
1311 #undef USE_COMPUTED_GOTOS
1312 #define USE_COMPUTED_GOTOS 0
1313#endif
1314
1315#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001316/* Import the static jump table */
1317#include "opcode_targets.h"
1318
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001320 op: \
1321 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322
Antoine Pitroub52ec782009-01-25 16:34:23 +00001323#ifdef LLTRACE
1324#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001326 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001328 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001329 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 } \
1331 goto fast_next_opcode; \
1332 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001333#else
1334#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001336 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001338 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001339 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 } \
1341 goto fast_next_opcode; \
1342 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001343#endif
1344
Victor Stinner09532fe2019-05-10 23:39:09 +02001345#define DISPATCH() \
1346 { \
1347 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1348 FAST_DISPATCH(); \
1349 } \
1350 continue; \
1351 }
1352
Antoine Pitroub52ec782009-01-25 16:34:23 +00001353#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001354#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001355#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001356#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357#endif
1358
1359
Neal Norwitza81d2202002-07-14 00:27:26 +00001360/* Tuple access macros */
1361
1362#ifndef Py_DEBUG
1363#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1364#else
1365#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1366#endif
1367
Guido van Rossum374a9221991-04-04 10:40:29 +00001368/* Code access macros */
1369
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001370/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001371#define INSTR_OFFSET() \
1372 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001373#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001374 _Py_CODEUNIT word = *next_instr; \
1375 opcode = _Py_OPCODE(word); \
1376 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001377 next_instr++; \
1378 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001379#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1380#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001381
Raymond Hettingerf606f872003-03-16 03:11:04 +00001382/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 Some opcodes tend to come in pairs thus making it possible to
1384 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001385 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Verifying the prediction costs a single high-speed test of a register
1388 variable against a constant. If the pairing was good, then the
1389 processor's own internal branch predication has a high likelihood of
1390 success, resulting in a nearly zero-overhead transition to the
1391 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001392 including its unpredictable switch-case branch. Combined with the
1393 processor's internal branch prediction, a successful PREDICT has the
1394 effect of making the two opcodes run as if they were a single new opcode
1395 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001396
Georg Brandl86b2fb92008-07-16 03:43:04 +00001397 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 predictions turned-on and interpret the results as if some opcodes
1399 had been combined or turn-off predictions so that the opcode frequency
1400 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001401
1402 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 the CPU to record separate branch prediction information for each
1404 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001405
Raymond Hettingerf606f872003-03-16 03:11:04 +00001406*/
1407
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001408#define PREDICT_ID(op) PRED_##op
1409
Antoine Pitrou042b1282010-08-13 21:15:58 +00001410#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001411#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001412#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001413#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001414 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001415 _Py_CODEUNIT word = *next_instr; \
1416 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001417 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001418 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001419 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001420 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001421 } \
1422 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001423#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001424#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001425
Raymond Hettingerf606f872003-03-16 03:11:04 +00001426
Guido van Rossum374a9221991-04-04 10:40:29 +00001427/* Stack manipulation macros */
1428
Martin v. Löwis18e16552006-02-15 17:27:45 +00001429/* The stack can grow at most MAXINT deep, as co_nlocals and
1430 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001431#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1432#define EMPTY() (STACK_LEVEL() == 0)
1433#define TOP() (stack_pointer[-1])
1434#define SECOND() (stack_pointer[-2])
1435#define THIRD() (stack_pointer[-3])
1436#define FOURTH() (stack_pointer[-4])
1437#define PEEK(n) (stack_pointer[-(n)])
1438#define SET_TOP(v) (stack_pointer[-1] = (v))
1439#define SET_SECOND(v) (stack_pointer[-2] = (v))
1440#define SET_THIRD(v) (stack_pointer[-3] = (v))
1441#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001442#define BASIC_STACKADJ(n) (stack_pointer += n)
1443#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1444#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001445
Guido van Rossum96a42c81992-01-12 02:29:51 +00001446#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001448 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001449 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001450#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001451 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001452#define STACK_GROW(n) do { \
1453 assert(n >= 0); \
1454 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001455 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001456 assert(STACK_LEVEL() <= co->co_stacksize); \
1457 } while (0)
1458#define STACK_SHRINK(n) do { \
1459 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001460 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001461 (void)(BASIC_STACKADJ(-n)); \
1462 assert(STACK_LEVEL() <= co->co_stacksize); \
1463 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001464#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001465 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001466 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001467#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001468#define PUSH(v) BASIC_PUSH(v)
1469#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001470#define STACK_GROW(n) BASIC_STACKADJ(n)
1471#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001472#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001473#endif
1474
Guido van Rossum681d79a1995-07-18 14:51:37 +00001475/* Local variable macros */
1476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001478
1479/* The SETLOCAL() macro must not DECREF the local variable in-place and
1480 then store the new value; it must copy the old value to a temporary
1481 value, then store the new value, and then DECREF the temporary value.
1482 This is because it is possible that during the DECREF the frame is
1483 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1484 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001486 GETLOCAL(i) = value; \
1487 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001488
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001489
1490#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 while (STACK_LEVEL() > (b)->b_level) { \
1492 PyObject *v = POP(); \
1493 Py_XDECREF(v); \
1494 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001495
1496#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001497 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001499 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1501 while (STACK_LEVEL() > (b)->b_level + 3) { \
1502 value = POP(); \
1503 Py_XDECREF(value); \
1504 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001505 exc_info = tstate->exc_info; \
1506 type = exc_info->exc_type; \
1507 value = exc_info->exc_value; \
1508 traceback = exc_info->exc_traceback; \
1509 exc_info->exc_type = POP(); \
1510 exc_info->exc_value = POP(); \
1511 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 Py_XDECREF(type); \
1513 Py_XDECREF(value); \
1514 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001515 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001516
Inada Naoki91234a12019-06-03 21:30:58 +09001517 /* macros for opcode cache */
1518#define OPCACHE_CHECK() \
1519 do { \
1520 co_opcache = NULL; \
1521 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001522 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001523 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001524 if (co_opcache_offset > 0) { \
1525 assert(co_opcache_offset <= co->co_opcache_size); \
1526 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001527 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001528 } \
1529 } \
1530 } while (0)
1531
Pablo Galindo109826c2020-10-20 06:22:44 +01001532#define OPCACHE_DEOPT() \
1533 do { \
1534 if (co_opcache != NULL) { \
1535 co_opcache->optimized = -1; \
1536 unsigned char co_opcache_offset = \
1537 co->co_opcache_map[next_instr - first_instr]; \
1538 assert(co_opcache_offset <= co->co_opcache_size); \
1539 co->co_opcache_map[co_opcache_offset] = 0; \
1540 co_opcache = NULL; \
1541 } \
1542 } while (0)
1543
1544#define OPCACHE_DEOPT_LOAD_ATTR() \
1545 do { \
1546 if (co_opcache != NULL) { \
1547 OPCACHE_STAT_ATTR_DEOPT(); \
1548 OPCACHE_DEOPT(); \
1549 } \
1550 } while (0)
1551
1552#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1553 do { \
1554 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1555 OPCACHE_DEOPT_LOAD_ATTR(); \
1556 } \
1557 } while (0)
1558
Inada Naoki91234a12019-06-03 21:30:58 +09001559#if OPCACHE_STATS
1560
1561#define OPCACHE_STAT_GLOBAL_HIT() \
1562 do { \
1563 if (co->co_opcache != NULL) opcache_global_hits++; \
1564 } while (0)
1565
1566#define OPCACHE_STAT_GLOBAL_MISS() \
1567 do { \
1568 if (co->co_opcache != NULL) opcache_global_misses++; \
1569 } while (0)
1570
1571#define OPCACHE_STAT_GLOBAL_OPT() \
1572 do { \
1573 if (co->co_opcache != NULL) opcache_global_opts++; \
1574 } while (0)
1575
Pablo Galindo109826c2020-10-20 06:22:44 +01001576#define OPCACHE_STAT_ATTR_HIT() \
1577 do { \
1578 if (co->co_opcache != NULL) opcache_attr_hits++; \
1579 } while (0)
1580
1581#define OPCACHE_STAT_ATTR_MISS() \
1582 do { \
1583 if (co->co_opcache != NULL) opcache_attr_misses++; \
1584 } while (0)
1585
1586#define OPCACHE_STAT_ATTR_OPT() \
1587 do { \
1588 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1589 } while (0)
1590
1591#define OPCACHE_STAT_ATTR_DEOPT() \
1592 do { \
1593 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1594 } while (0)
1595
1596#define OPCACHE_STAT_ATTR_TOTAL() \
1597 do { \
1598 if (co->co_opcache != NULL) opcache_attr_total++; \
1599 } while (0)
1600
Inada Naoki91234a12019-06-03 21:30:58 +09001601#else /* OPCACHE_STATS */
1602
1603#define OPCACHE_STAT_GLOBAL_HIT()
1604#define OPCACHE_STAT_GLOBAL_MISS()
1605#define OPCACHE_STAT_GLOBAL_OPT()
1606
Pablo Galindo109826c2020-10-20 06:22:44 +01001607#define OPCACHE_STAT_ATTR_HIT()
1608#define OPCACHE_STAT_ATTR_MISS()
1609#define OPCACHE_STAT_ATTR_OPT()
1610#define OPCACHE_STAT_ATTR_DEOPT()
1611#define OPCACHE_STAT_ATTR_TOTAL()
1612
Inada Naoki91234a12019-06-03 21:30:58 +09001613#endif
1614
Guido van Rossuma027efa1997-05-05 20:56:21 +00001615/* Start of code */
1616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001618 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001620 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001623 co = f->f_code;
1624 PyCodeAddressRange bounds;
1625 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (tstate->use_tracing) {
1628 if (tstate->c_tracefunc != NULL) {
1629 /* tstate->c_tracefunc, if defined, is a
1630 function that will be called on *every* entry
1631 to a code block. Its return value, if not
1632 None, is a function that will be called at
1633 the start of each executed line of code.
1634 (Actually, the function must return itself
1635 in order to continue tracing.) The trace
1636 functions are called with three arguments:
1637 a pointer to the current frame, a string
1638 indicating why the function is called, and
1639 an argument which depends on the situation.
1640 The global trace function is also called
1641 whenever an exception is detected. */
1642 if (call_trace_protected(tstate->c_tracefunc,
1643 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001644 tstate, f, &bounds,
1645 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 /* Trace function raised an error */
1647 goto exit_eval_frame;
1648 }
1649 }
1650 if (tstate->c_profilefunc != NULL) {
1651 /* Similar for c_profilefunc, except it needn't
1652 return itself and isn't called for "line" events */
1653 if (call_trace_protected(tstate->c_profilefunc,
1654 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001655 tstate, f, &bounds,
1656 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* Profile function raised an error */
1658 goto exit_eval_frame;
1659 }
1660 }
1661 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001662
Łukasz Langaa785c872016-09-09 17:37:37 -07001663 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1664 dtrace_function_entry(f);
1665
Mark Shannon877df852020-11-12 09:43:29 +00001666 int instr_prev = -1;
1667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 names = co->co_names;
1669 consts = co->co_consts;
1670 fastlocals = f->f_localsplus;
1671 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001672 assert(PyBytes_Check(co->co_code));
1673 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001674 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1675 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1676 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001677 /*
1678 f->f_lasti refers to the index of the last instruction,
1679 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001680
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001681 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001682 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 When the PREDICT() macros are enabled, some opcode pairs follow in
1685 direct succession without updating f->f_lasti. A successful
1686 prediction effectively links the two codes together as if they
1687 were a single new opcode; accordingly,f->f_lasti will point to
1688 the first code in the pair (for instance, GET_ITER followed by
1689 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001690 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001692 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001693 next_instr = first_instr;
1694 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001695 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1696 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001697 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001698 stack_pointer = f->f_valuestack + f->f_stackdepth;
1699 /* Set f->f_stackdepth to -1.
1700 * Update when returning or calling trace function.
1701 Having f_stackdepth <= 0 ensures that invalid
1702 values are not visible to the cycle GC.
1703 We choose -1 rather than 0 to assist debugging.
1704 */
1705 f->f_stackdepth = -1;
1706 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001707
Inada Naoki91234a12019-06-03 21:30:58 +09001708 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1709 co->co_opcache_flag++;
1710 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1711 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001712 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001713 }
1714#if OPCACHE_STATS
1715 opcache_code_objects_extra_mem +=
1716 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1717 sizeof(_PyOpcache) * co->co_opcache_size;
1718 opcache_code_objects++;
1719#endif
1720 }
1721 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001722
Tim Peters5ca576e2001-06-18 22:08:13 +00001723#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001724 {
1725 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1726 if (r < 0) {
1727 goto exit_eval_frame;
1728 }
1729 lltrace = r;
1730 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001731#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001733 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001735 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001736
Victor Stinnerace47d72013-07-18 01:41:08 +02001737#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001738 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001739 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001740 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001741 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001742#endif
1743
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001744main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1747 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001748 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 /* Do periodic things. Doing this every time through
1751 the loop would add too much overhead, so we do it
1752 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001753 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 event needs attention (e.g. a signal handler or
1755 async I/O handler); see Py_AddPendingCall() and
1756 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Eric Snow7bda9de2019-03-08 17:25:54 -07001758 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001759 opcode = _Py_OPCODE(*next_instr);
1760 if (opcode == SETUP_FINALLY ||
1761 opcode == SETUP_WITH ||
1762 opcode == BEFORE_ASYNC_WITH ||
1763 opcode == YIELD_FROM) {
1764 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001765 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001766 - If we're about to enter the 'with:'. It will prevent
1767 emitting a resource warning in the common idiom
1768 'with open(path) as file:'.
1769 - If we're about to enter the 'async with:'.
1770 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001771 *very* useful, but might help in some cases and it's
1772 traditional)
1773 - If we're resuming a chain of nested 'yield from' or
1774 'await' calls, then each frame is parked with YIELD_FROM
1775 as its next opcode. If the user hit control-C we want to
1776 wait until we've reached the innermost frame before
1777 running the signal handler and raising KeyboardInterrupt
1778 (see bpo-30039).
1779 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 goto fast_next_opcode;
1781 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001782
Victor Stinnerda2914d2020-03-20 09:29:08 +01001783 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
1786 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 fast_next_opcode:
1789 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Łukasz Langaa785c872016-09-09 17:37:37 -07001791 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001792 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001795
Victor Stinnerdab84232020-03-17 18:56:44 +01001796 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001797 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001799 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001801 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 err = maybe_call_line_trace(tstate->c_tracefunc,
1804 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001805 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001806 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 /* Reload possibly changed frame fields */
1808 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001809 stack_pointer = f->f_valuestack+f->f_stackdepth;
1810 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001817
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001818 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001819 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001820#ifdef DYNAMIC_EXECUTION_PROFILE
1821#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 dxpairs[lastopcode][opcode]++;
1823 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001824#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001826#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001827
Guido van Rossum96a42c81992-01-12 02:29:51 +00001828#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (lltrace) {
1832 if (HAS_ARG(opcode)) {
1833 printf("%d: %d, %d\n",
1834 f->f_lasti, opcode, oparg);
1835 }
1836 else {
1837 printf("%d: %d\n",
1838 f->f_lasti, opcode);
1839 }
1840 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001841#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001846 It is essential that any operation that fails must goto error
1847 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001848
Benjamin Petersonddd19492018-09-16 22:38:02 -07001849 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001851 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *value = GETLOCAL(oparg);
1855 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001856 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 UNBOUNDLOCAL_ERROR_MSG,
1858 PyTuple_GetItem(co->co_varnames, oparg));
1859 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_INCREF(value);
1862 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 }
1865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(LOAD_CONST): {
1867 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *value = GETITEM(consts, oparg);
1869 Py_INCREF(value);
1870 PUSH(value);
1871 FAST_DISPATCH();
1872 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001873
Benjamin Petersonddd19492018-09-16 22:38:02 -07001874 case TARGET(STORE_FAST): {
1875 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 PyObject *value = POP();
1877 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001880
Benjamin Petersonddd19492018-09-16 22:38:02 -07001881 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 PyObject *value = POP();
1883 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Benjamin Petersonddd19492018-09-16 22:38:02 -07001887 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 PyObject *top = TOP();
1889 PyObject *second = SECOND();
1890 SET_TOP(second);
1891 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Benjamin Petersonddd19492018-09-16 22:38:02 -07001895 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 PyObject *top = TOP();
1897 PyObject *second = SECOND();
1898 PyObject *third = THIRD();
1899 SET_TOP(second);
1900 SET_SECOND(third);
1901 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Benjamin Petersonddd19492018-09-16 22:38:02 -07001905 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001906 PyObject *top = TOP();
1907 PyObject *second = SECOND();
1908 PyObject *third = THIRD();
1909 PyObject *fourth = FOURTH();
1910 SET_TOP(second);
1911 SET_SECOND(third);
1912 SET_THIRD(fourth);
1913 SET_FOURTH(top);
1914 FAST_DISPATCH();
1915 }
1916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 PyObject *top = TOP();
1919 Py_INCREF(top);
1920 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001923
Benjamin Petersonddd19492018-09-16 22:38:02 -07001924 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001927 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001929 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 SET_TOP(top);
1931 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001932 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001934
Benjamin Petersonddd19492018-09-16 22:38:02 -07001935 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 PyObject *value = TOP();
1937 PyObject *res = PyNumber_Positive(value);
1938 Py_DECREF(value);
1939 SET_TOP(res);
1940 if (res == NULL)
1941 goto error;
1942 DISPATCH();
1943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Benjamin Petersonddd19492018-09-16 22:38:02 -07001945 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 PyObject *value = TOP();
1947 PyObject *res = PyNumber_Negative(value);
1948 Py_DECREF(value);
1949 SET_TOP(res);
1950 if (res == NULL)
1951 goto error;
1952 DISPATCH();
1953 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Benjamin Petersonddd19492018-09-16 22:38:02 -07001955 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001956 PyObject *value = TOP();
1957 int err = PyObject_IsTrue(value);
1958 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (err == 0) {
1960 Py_INCREF(Py_True);
1961 SET_TOP(Py_True);
1962 DISPATCH();
1963 }
1964 else if (err > 0) {
1965 Py_INCREF(Py_False);
1966 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 DISPATCH();
1968 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001969 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 goto error;
1971 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Benjamin Petersonddd19492018-09-16 22:38:02 -07001973 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 PyObject *value = TOP();
1975 PyObject *res = PyNumber_Invert(value);
1976 Py_DECREF(value);
1977 SET_TOP(res);
1978 if (res == NULL)
1979 goto error;
1980 DISPATCH();
1981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Benjamin Petersonddd19492018-09-16 22:38:02 -07001983 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 PyObject *exp = POP();
1985 PyObject *base = TOP();
1986 PyObject *res = PyNumber_Power(base, exp, Py_None);
1987 Py_DECREF(base);
1988 Py_DECREF(exp);
1989 SET_TOP(res);
1990 if (res == NULL)
1991 goto error;
1992 DISPATCH();
1993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Benjamin Petersonddd19492018-09-16 22:38:02 -07001995 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 PyObject *right = POP();
1997 PyObject *left = TOP();
1998 PyObject *res = PyNumber_Multiply(left, right);
1999 Py_DECREF(left);
2000 Py_DECREF(right);
2001 SET_TOP(res);
2002 if (res == NULL)
2003 goto error;
2004 DISPATCH();
2005 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002006
Benjamin Petersonddd19492018-09-16 22:38:02 -07002007 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002008 PyObject *right = POP();
2009 PyObject *left = TOP();
2010 PyObject *res = PyNumber_MatrixMultiply(left, right);
2011 Py_DECREF(left);
2012 Py_DECREF(right);
2013 SET_TOP(res);
2014 if (res == NULL)
2015 goto error;
2016 DISPATCH();
2017 }
2018
Benjamin Petersonddd19492018-09-16 22:38:02 -07002019 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 PyObject *divisor = POP();
2021 PyObject *dividend = TOP();
2022 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2023 Py_DECREF(dividend);
2024 Py_DECREF(divisor);
2025 SET_TOP(quotient);
2026 if (quotient == NULL)
2027 goto error;
2028 DISPATCH();
2029 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Benjamin Petersonddd19492018-09-16 22:38:02 -07002031 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 PyObject *divisor = POP();
2033 PyObject *dividend = TOP();
2034 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2035 Py_DECREF(dividend);
2036 Py_DECREF(divisor);
2037 SET_TOP(quotient);
2038 if (quotient == NULL)
2039 goto error;
2040 DISPATCH();
2041 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002042
Benjamin Petersonddd19492018-09-16 22:38:02 -07002043 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 PyObject *divisor = POP();
2045 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002046 PyObject *res;
2047 if (PyUnicode_CheckExact(dividend) && (
2048 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2049 // fast path; string formatting, but not if the RHS is a str subclass
2050 // (see issue28598)
2051 res = PyUnicode_Format(dividend, divisor);
2052 } else {
2053 res = PyNumber_Remainder(dividend, divisor);
2054 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 Py_DECREF(divisor);
2056 Py_DECREF(dividend);
2057 SET_TOP(res);
2058 if (res == NULL)
2059 goto error;
2060 DISPATCH();
2061 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002062
Benjamin Petersonddd19492018-09-16 22:38:02 -07002063 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 PyObject *right = POP();
2065 PyObject *left = TOP();
2066 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002067 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002068 CPython using bytecode, it is simply worthless.
2069 See http://bugs.python.org/issue21955 and
2070 http://bugs.python.org/issue10044 for the discussion. In short,
2071 no patch shown any impact on a realistic benchmark, only a minor
2072 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 if (PyUnicode_CheckExact(left) &&
2074 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002075 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002076 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002077 }
2078 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 sum = PyNumber_Add(left, right);
2080 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 Py_DECREF(right);
2083 SET_TOP(sum);
2084 if (sum == NULL)
2085 goto error;
2086 DISPATCH();
2087 }
2088
Benjamin Petersonddd19492018-09-16 22:38:02 -07002089 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 PyObject *right = POP();
2091 PyObject *left = TOP();
2092 PyObject *diff = PyNumber_Subtract(left, right);
2093 Py_DECREF(right);
2094 Py_DECREF(left);
2095 SET_TOP(diff);
2096 if (diff == NULL)
2097 goto error;
2098 DISPATCH();
2099 }
2100
Benjamin Petersonddd19492018-09-16 22:38:02 -07002101 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 PyObject *sub = POP();
2103 PyObject *container = TOP();
2104 PyObject *res = PyObject_GetItem(container, sub);
2105 Py_DECREF(container);
2106 Py_DECREF(sub);
2107 SET_TOP(res);
2108 if (res == NULL)
2109 goto error;
2110 DISPATCH();
2111 }
2112
Benjamin Petersonddd19492018-09-16 22:38:02 -07002113 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 PyObject *right = POP();
2115 PyObject *left = TOP();
2116 PyObject *res = PyNumber_Lshift(left, right);
2117 Py_DECREF(left);
2118 Py_DECREF(right);
2119 SET_TOP(res);
2120 if (res == NULL)
2121 goto error;
2122 DISPATCH();
2123 }
2124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 PyObject *right = POP();
2127 PyObject *left = TOP();
2128 PyObject *res = PyNumber_Rshift(left, right);
2129 Py_DECREF(left);
2130 Py_DECREF(right);
2131 SET_TOP(res);
2132 if (res == NULL)
2133 goto error;
2134 DISPATCH();
2135 }
2136
Benjamin Petersonddd19492018-09-16 22:38:02 -07002137 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 PyObject *right = POP();
2139 PyObject *left = TOP();
2140 PyObject *res = PyNumber_And(left, right);
2141 Py_DECREF(left);
2142 Py_DECREF(right);
2143 SET_TOP(res);
2144 if (res == NULL)
2145 goto error;
2146 DISPATCH();
2147 }
2148
Benjamin Petersonddd19492018-09-16 22:38:02 -07002149 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *right = POP();
2151 PyObject *left = TOP();
2152 PyObject *res = PyNumber_Xor(left, right);
2153 Py_DECREF(left);
2154 Py_DECREF(right);
2155 SET_TOP(res);
2156 if (res == NULL)
2157 goto error;
2158 DISPATCH();
2159 }
2160
Benjamin Petersonddd19492018-09-16 22:38:02 -07002161 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 PyObject *right = POP();
2163 PyObject *left = TOP();
2164 PyObject *res = PyNumber_Or(left, right);
2165 Py_DECREF(left);
2166 Py_DECREF(right);
2167 SET_TOP(res);
2168 if (res == NULL)
2169 goto error;
2170 DISPATCH();
2171 }
2172
Benjamin Petersonddd19492018-09-16 22:38:02 -07002173 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 PyObject *v = POP();
2175 PyObject *list = PEEK(oparg);
2176 int err;
2177 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 if (err != 0)
2180 goto error;
2181 PREDICT(JUMP_ABSOLUTE);
2182 DISPATCH();
2183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002184
Benjamin Petersonddd19492018-09-16 22:38:02 -07002185 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002187 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 int err;
2189 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 if (err != 0)
2192 goto error;
2193 PREDICT(JUMP_ABSOLUTE);
2194 DISPATCH();
2195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Benjamin Petersonddd19492018-09-16 22:38:02 -07002197 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 PyObject *exp = POP();
2199 PyObject *base = TOP();
2200 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2201 Py_DECREF(base);
2202 Py_DECREF(exp);
2203 SET_TOP(res);
2204 if (res == NULL)
2205 goto error;
2206 DISPATCH();
2207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Petersonddd19492018-09-16 22:38:02 -07002209 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 PyObject *right = POP();
2211 PyObject *left = TOP();
2212 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2213 Py_DECREF(left);
2214 Py_DECREF(right);
2215 SET_TOP(res);
2216 if (res == NULL)
2217 goto error;
2218 DISPATCH();
2219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Benjamin Petersonddd19492018-09-16 22:38:02 -07002221 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002222 PyObject *right = POP();
2223 PyObject *left = TOP();
2224 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2225 Py_DECREF(left);
2226 Py_DECREF(right);
2227 SET_TOP(res);
2228 if (res == NULL)
2229 goto error;
2230 DISPATCH();
2231 }
2232
Benjamin Petersonddd19492018-09-16 22:38:02 -07002233 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 PyObject *divisor = POP();
2235 PyObject *dividend = TOP();
2236 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2237 Py_DECREF(dividend);
2238 Py_DECREF(divisor);
2239 SET_TOP(quotient);
2240 if (quotient == NULL)
2241 goto error;
2242 DISPATCH();
2243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002244
Benjamin Petersonddd19492018-09-16 22:38:02 -07002245 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 PyObject *divisor = POP();
2247 PyObject *dividend = TOP();
2248 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2249 Py_DECREF(dividend);
2250 Py_DECREF(divisor);
2251 SET_TOP(quotient);
2252 if (quotient == NULL)
2253 goto error;
2254 DISPATCH();
2255 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Benjamin Petersonddd19492018-09-16 22:38:02 -07002257 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 PyObject *right = POP();
2259 PyObject *left = TOP();
2260 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2261 Py_DECREF(left);
2262 Py_DECREF(right);
2263 SET_TOP(mod);
2264 if (mod == NULL)
2265 goto error;
2266 DISPATCH();
2267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002268
Benjamin Petersonddd19492018-09-16 22:38:02 -07002269 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 PyObject *right = POP();
2271 PyObject *left = TOP();
2272 PyObject *sum;
2273 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002274 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002275 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002276 }
2277 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 sum = PyNumber_InPlaceAdd(left, right);
2279 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 Py_DECREF(right);
2282 SET_TOP(sum);
2283 if (sum == NULL)
2284 goto error;
2285 DISPATCH();
2286 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002287
Benjamin Petersonddd19492018-09-16 22:38:02 -07002288 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 PyObject *right = POP();
2290 PyObject *left = TOP();
2291 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2292 Py_DECREF(left);
2293 Py_DECREF(right);
2294 SET_TOP(diff);
2295 if (diff == NULL)
2296 goto error;
2297 DISPATCH();
2298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002299
Benjamin Petersonddd19492018-09-16 22:38:02 -07002300 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 PyObject *right = POP();
2302 PyObject *left = TOP();
2303 PyObject *res = PyNumber_InPlaceLshift(left, right);
2304 Py_DECREF(left);
2305 Py_DECREF(right);
2306 SET_TOP(res);
2307 if (res == NULL)
2308 goto error;
2309 DISPATCH();
2310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 PyObject *right = POP();
2314 PyObject *left = TOP();
2315 PyObject *res = PyNumber_InPlaceRshift(left, right);
2316 Py_DECREF(left);
2317 Py_DECREF(right);
2318 SET_TOP(res);
2319 if (res == NULL)
2320 goto error;
2321 DISPATCH();
2322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Benjamin Petersonddd19492018-09-16 22:38:02 -07002324 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 PyObject *right = POP();
2326 PyObject *left = TOP();
2327 PyObject *res = PyNumber_InPlaceAnd(left, right);
2328 Py_DECREF(left);
2329 Py_DECREF(right);
2330 SET_TOP(res);
2331 if (res == NULL)
2332 goto error;
2333 DISPATCH();
2334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Benjamin Petersonddd19492018-09-16 22:38:02 -07002336 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 PyObject *right = POP();
2338 PyObject *left = TOP();
2339 PyObject *res = PyNumber_InPlaceXor(left, right);
2340 Py_DECREF(left);
2341 Py_DECREF(right);
2342 SET_TOP(res);
2343 if (res == NULL)
2344 goto error;
2345 DISPATCH();
2346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002347
Benjamin Petersonddd19492018-09-16 22:38:02 -07002348 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 PyObject *right = POP();
2350 PyObject *left = TOP();
2351 PyObject *res = PyNumber_InPlaceOr(left, right);
2352 Py_DECREF(left);
2353 Py_DECREF(right);
2354 SET_TOP(res);
2355 if (res == NULL)
2356 goto error;
2357 DISPATCH();
2358 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002359
Benjamin Petersonddd19492018-09-16 22:38:02 -07002360 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 PyObject *sub = TOP();
2362 PyObject *container = SECOND();
2363 PyObject *v = THIRD();
2364 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002365 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002366 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 Py_DECREF(container);
2370 Py_DECREF(sub);
2371 if (err != 0)
2372 goto error;
2373 DISPATCH();
2374 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002375
Benjamin Petersonddd19492018-09-16 22:38:02 -07002376 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 PyObject *sub = TOP();
2378 PyObject *container = SECOND();
2379 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002380 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002381 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 err = PyObject_DelItem(container, sub);
2383 Py_DECREF(container);
2384 Py_DECREF(sub);
2385 if (err != 0)
2386 goto error;
2387 DISPATCH();
2388 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002389
Benjamin Petersonddd19492018-09-16 22:38:02 -07002390 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002391 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002393 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002394 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002396 _PyErr_SetString(tstate, PyExc_RuntimeError,
2397 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 Py_DECREF(value);
2399 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002401 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 Py_DECREF(value);
2403 if (res == NULL)
2404 goto error;
2405 Py_DECREF(res);
2406 DISPATCH();
2407 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002408
Benjamin Petersonddd19492018-09-16 22:38:02 -07002409 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 switch (oparg) {
2412 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002414 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002417 /* fall through */
2418 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002419 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002420 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 break;
2423 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002424 _PyErr_SetString(tstate, PyExc_SystemError,
2425 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 break;
2427 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 goto error;
2429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Benjamin Petersonddd19492018-09-16 22:38:02 -07002431 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002433 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002434 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002435 f->f_state = FRAME_RETURNED;
2436 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002437 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002439
Benjamin Petersonddd19492018-09-16 22:38:02 -07002440 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002441 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002442 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002443 PyObject *obj = TOP();
2444 PyTypeObject *type = Py_TYPE(obj);
2445
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002446 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002447 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002448 }
Yury Selivanov75445082015-05-11 22:57:16 -04002449
2450 if (getter != NULL) {
2451 iter = (*getter)(obj);
2452 Py_DECREF(obj);
2453 if (iter == NULL) {
2454 SET_TOP(NULL);
2455 goto error;
2456 }
2457 }
2458 else {
2459 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002460 _PyErr_Format(tstate, PyExc_TypeError,
2461 "'async for' requires an object with "
2462 "__aiter__ method, got %.100s",
2463 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002464 Py_DECREF(obj);
2465 goto error;
2466 }
2467
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002468 if (Py_TYPE(iter)->tp_as_async == NULL ||
2469 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002470
Yury Selivanov398ff912017-03-02 22:20:00 -05002471 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 _PyErr_Format(tstate, PyExc_TypeError,
2473 "'async for' received an object from __aiter__ "
2474 "that does not implement __anext__: %.100s",
2475 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002476 Py_DECREF(iter);
2477 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002478 }
2479
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002480 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002481 DISPATCH();
2482 }
2483
Benjamin Petersonddd19492018-09-16 22:38:02 -07002484 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002485 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002486 PyObject *next_iter = NULL;
2487 PyObject *awaitable = NULL;
2488 PyObject *aiter = TOP();
2489 PyTypeObject *type = Py_TYPE(aiter);
2490
Yury Selivanoveb636452016-09-08 22:01:51 -07002491 if (PyAsyncGen_CheckExact(aiter)) {
2492 awaitable = type->tp_as_async->am_anext(aiter);
2493 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002494 goto error;
2495 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002496 } else {
2497 if (type->tp_as_async != NULL){
2498 getter = type->tp_as_async->am_anext;
2499 }
Yury Selivanov75445082015-05-11 22:57:16 -04002500
Yury Selivanoveb636452016-09-08 22:01:51 -07002501 if (getter != NULL) {
2502 next_iter = (*getter)(aiter);
2503 if (next_iter == NULL) {
2504 goto error;
2505 }
2506 }
2507 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002508 _PyErr_Format(tstate, PyExc_TypeError,
2509 "'async for' requires an iterator with "
2510 "__anext__ method, got %.100s",
2511 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002512 goto error;
2513 }
Yury Selivanov75445082015-05-11 22:57:16 -04002514
Yury Selivanoveb636452016-09-08 22:01:51 -07002515 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2516 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002517 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002518 PyExc_TypeError,
2519 "'async for' received an invalid object "
2520 "from __anext__: %.100s",
2521 Py_TYPE(next_iter)->tp_name);
2522
2523 Py_DECREF(next_iter);
2524 goto error;
2525 } else {
2526 Py_DECREF(next_iter);
2527 }
2528 }
Yury Selivanov75445082015-05-11 22:57:16 -04002529
2530 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002531 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002532 DISPATCH();
2533 }
2534
Benjamin Petersonddd19492018-09-16 22:38:02 -07002535 case TARGET(GET_AWAITABLE): {
2536 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002537 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002538 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002539
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002540 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002541 int opcode_at_minus_3 = 0;
2542 if ((next_instr - first_instr) > 2) {
2543 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2544 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002545 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002546 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002547 _Py_OPCODE(next_instr[-2]));
2548 }
2549
Yury Selivanov75445082015-05-11 22:57:16 -04002550 Py_DECREF(iterable);
2551
Yury Selivanovc724bae2016-03-02 11:30:46 -05002552 if (iter != NULL && PyCoro_CheckExact(iter)) {
2553 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2554 if (yf != NULL) {
2555 /* `iter` is a coroutine object that is being
2556 awaited, `yf` is a pointer to the current awaitable
2557 being awaited on. */
2558 Py_DECREF(yf);
2559 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 _PyErr_SetString(tstate, PyExc_RuntimeError,
2561 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002562 /* The code below jumps to `error` if `iter` is NULL. */
2563 }
2564 }
2565
Yury Selivanov75445082015-05-11 22:57:16 -04002566 SET_TOP(iter); /* Even if it's NULL */
2567
2568 if (iter == NULL) {
2569 goto error;
2570 }
2571
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002572 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002573 DISPATCH();
2574 }
2575
Benjamin Petersonddd19492018-09-16 22:38:02 -07002576 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002578 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002579 PySendResult gen_status;
2580 if (tstate->c_tracefunc == NULL) {
2581 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002582 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002583 _Py_IDENTIFIER(send);
2584 if (v == Py_None && PyIter_Check(receiver)) {
2585 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002586 }
2587 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002588 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002589 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002590 if (retval == NULL) {
2591 if (tstate->c_tracefunc != NULL
2592 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002593 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002594 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2595 gen_status = PYGEN_RETURN;
2596 }
2597 else {
2598 gen_status = PYGEN_ERROR;
2599 }
2600 }
2601 else {
2602 gen_status = PYGEN_NEXT;
2603 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002604 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002606 if (gen_status == PYGEN_ERROR) {
2607 assert (retval == NULL);
2608 goto error;
2609 }
2610 if (gen_status == PYGEN_RETURN) {
2611 assert (retval != NULL);
2612
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002613 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002614 SET_TOP(retval);
2615 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002617 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002618 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002619 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002620 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002621 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002622 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002623 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002624 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002625 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002627
Benjamin Petersonddd19492018-09-16 22:38:02 -07002628 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002630
2631 if (co->co_flags & CO_ASYNC_GENERATOR) {
2632 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2633 Py_DECREF(retval);
2634 if (w == NULL) {
2635 retval = NULL;
2636 goto error;
2637 }
2638 retval = w;
2639 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002640 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002641 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002642 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002643 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002644
Benjamin Petersonddd19492018-09-16 22:38:02 -07002645 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002646 PyObject *type, *value, *traceback;
2647 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 PyTryBlock *b = PyFrame_BlockPop(f);
2649 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002650 _PyErr_SetString(tstate, PyExc_SystemError,
2651 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002654 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2655 STACK_LEVEL() <= (b)->b_level + 4);
2656 exc_info = tstate->exc_info;
2657 type = exc_info->exc_type;
2658 value = exc_info->exc_value;
2659 traceback = exc_info->exc_traceback;
2660 exc_info->exc_type = POP();
2661 exc_info->exc_value = POP();
2662 exc_info->exc_traceback = POP();
2663 Py_XDECREF(type);
2664 Py_XDECREF(value);
2665 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002668
Benjamin Petersonddd19492018-09-16 22:38:02 -07002669 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002670 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002673
Mark Shannonfee55262019-11-21 09:11:43 +00002674 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002675 assert(f->f_iblock > 0);
2676 if (oparg) {
2677 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2678 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002679 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002680 PyObject *val = POP();
2681 PyObject *tb = POP();
2682 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002683 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002684 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002686
Benjamin Petersonddd19492018-09-16 22:38:02 -07002687 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002688 PyObject *exc = POP();
2689 assert(PyExceptionClass_Check(exc));
2690 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2691 PyTryBlock *b = PyFrame_BlockPop(f);
2692 assert(b->b_type == EXCEPT_HANDLER);
2693 Py_DECREF(exc);
2694 UNWIND_EXCEPT_HANDLER(b);
2695 Py_DECREF(POP());
2696 JUMPBY(oparg);
2697 FAST_DISPATCH();
2698 }
2699 else {
2700 PyObject *val = POP();
2701 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002702 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002703 goto exception_unwind;
2704 }
2705 }
2706
Zackery Spytzce6a0702019-08-25 03:44:09 -06002707 case TARGET(LOAD_ASSERTION_ERROR): {
2708 PyObject *value = PyExc_AssertionError;
2709 Py_INCREF(value);
2710 PUSH(value);
2711 FAST_DISPATCH();
2712 }
2713
Benjamin Petersonddd19492018-09-16 22:38:02 -07002714 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002715 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002716
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002718 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002719 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002721 if (!_PyErr_Occurred(tstate)) {
2722 _PyErr_SetString(tstate, PyExc_NameError,
2723 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002724 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002726 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002728 }
2729 else {
2730 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2731 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002732 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2734 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002735 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2736 _PyErr_SetString(tstate, PyExc_NameError,
2737 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002742 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002744
Benjamin Petersonddd19492018-09-16 22:38:02 -07002745 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 PyObject *name = GETITEM(names, oparg);
2747 PyObject *v = POP();
2748 PyObject *ns = f->f_locals;
2749 int err;
2750 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002751 _PyErr_Format(tstate, PyExc_SystemError,
2752 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 if (PyDict_CheckExact(ns))
2757 err = PyDict_SetItem(ns, name, v);
2758 else
2759 err = PyObject_SetItem(ns, name, v);
2760 Py_DECREF(v);
2761 if (err != 0)
2762 goto error;
2763 DISPATCH();
2764 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002765
Benjamin Petersonddd19492018-09-16 22:38:02 -07002766 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 PyObject *name = GETITEM(names, oparg);
2768 PyObject *ns = f->f_locals;
2769 int err;
2770 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002771 _PyErr_Format(tstate, PyExc_SystemError,
2772 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 err = PyObject_DelItem(ns, name);
2776 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002777 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 NAME_ERROR_MSG,
2779 name);
2780 goto error;
2781 }
2782 DISPATCH();
2783 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(UNPACK_SEQUENCE): {
2786 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 PyObject *seq = POP(), *item, **items;
2788 if (PyTuple_CheckExact(seq) &&
2789 PyTuple_GET_SIZE(seq) == oparg) {
2790 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 item = items[oparg];
2793 Py_INCREF(item);
2794 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 } else if (PyList_CheckExact(seq) &&
2797 PyList_GET_SIZE(seq) == oparg) {
2798 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 item = items[oparg];
2801 Py_INCREF(item);
2802 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002804 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002806 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 } else {
2808 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 Py_DECREF(seq);
2810 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002813 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002815
Benjamin Petersonddd19492018-09-16 22:38:02 -07002816 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2818 PyObject *seq = POP();
2819
Victor Stinner438a12d2019-05-24 17:01:38 +02002820 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 stack_pointer + totalargs)) {
2822 stack_pointer += totalargs;
2823 } else {
2824 Py_DECREF(seq);
2825 goto error;
2826 }
2827 Py_DECREF(seq);
2828 DISPATCH();
2829 }
2830
Benjamin Petersonddd19492018-09-16 22:38:02 -07002831 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PyObject *name = GETITEM(names, oparg);
2833 PyObject *owner = TOP();
2834 PyObject *v = SECOND();
2835 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002836 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 Py_DECREF(owner);
2840 if (err != 0)
2841 goto error;
2842 DISPATCH();
2843 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002844
Benjamin Petersonddd19492018-09-16 22:38:02 -07002845 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 PyObject *name = GETITEM(names, oparg);
2847 PyObject *owner = POP();
2848 int err;
2849 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2850 Py_DECREF(owner);
2851 if (err != 0)
2852 goto error;
2853 DISPATCH();
2854 }
2855
Benjamin Petersonddd19492018-09-16 22:38:02 -07002856 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 PyObject *name = GETITEM(names, oparg);
2858 PyObject *v = POP();
2859 int err;
2860 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 if (err != 0)
2863 goto error;
2864 DISPATCH();
2865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Benjamin Petersonddd19492018-09-16 22:38:02 -07002867 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 PyObject *name = GETITEM(names, oparg);
2869 int err;
2870 err = PyDict_DelItem(f->f_globals, name);
2871 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002872 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2873 format_exc_check_arg(tstate, PyExc_NameError,
2874 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002875 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002877 }
2878 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Benjamin Petersonddd19492018-09-16 22:38:02 -07002881 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 PyObject *name = GETITEM(names, oparg);
2883 PyObject *locals = f->f_locals;
2884 PyObject *v;
2885 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002886 _PyErr_Format(tstate, PyExc_SystemError,
2887 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002891 v = PyDict_GetItemWithError(locals, name);
2892 if (v != NULL) {
2893 Py_INCREF(v);
2894 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002895 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002896 goto error;
2897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 }
2899 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002901 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002902 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002903 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002904 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 }
2906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002908 v = PyDict_GetItemWithError(f->f_globals, name);
2909 if (v != NULL) {
2910 Py_INCREF(v);
2911 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002912 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002913 goto error;
2914 }
2915 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002916 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002917 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002919 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002920 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002921 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002923 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002925 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002927 }
2928 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 v = PyObject_GetItem(f->f_builtins, name);
2930 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002931 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002932 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002933 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002935 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002937 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002944
Benjamin Petersonddd19492018-09-16 22:38:02 -07002945 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002946 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002948 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002949 && PyDict_CheckExact(f->f_builtins))
2950 {
Inada Naoki91234a12019-06-03 21:30:58 +09002951 OPCACHE_CHECK();
2952 if (co_opcache != NULL && co_opcache->optimized > 0) {
2953 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2954
2955 if (lg->globals_ver ==
2956 ((PyDictObject *)f->f_globals)->ma_version_tag
2957 && lg->builtins_ver ==
2958 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2959 {
2960 PyObject *ptr = lg->ptr;
2961 OPCACHE_STAT_GLOBAL_HIT();
2962 assert(ptr != NULL);
2963 Py_INCREF(ptr);
2964 PUSH(ptr);
2965 DISPATCH();
2966 }
2967 }
2968
2969 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002971 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 name);
2973 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002974 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002975 /* _PyDict_LoadGlobal() returns NULL without raising
2976 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002977 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002978 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 }
Inada Naoki91234a12019-06-03 21:30:58 +09002982
2983 if (co_opcache != NULL) {
2984 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2985
2986 if (co_opcache->optimized == 0) {
2987 /* Wasn't optimized before. */
2988 OPCACHE_STAT_GLOBAL_OPT();
2989 } else {
2990 OPCACHE_STAT_GLOBAL_MISS();
2991 }
2992
2993 co_opcache->optimized = 1;
2994 lg->globals_ver =
2995 ((PyDictObject *)f->f_globals)->ma_version_tag;
2996 lg->builtins_ver =
2997 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2998 lg->ptr = v; /* borrowed */
2999 }
3000
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003003 else {
3004 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003005
3006 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003007 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 v = PyObject_GetItem(f->f_globals, name);
3009 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003010 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003011 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003012 }
3013 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003014
Victor Stinnerb4efc962015-11-20 09:24:02 +01003015 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 v = PyObject_GetItem(f->f_builtins, name);
3017 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003018 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003019 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003020 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003021 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003024 }
3025 }
3026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003030
Benjamin Petersonddd19492018-09-16 22:38:02 -07003031 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 PyObject *v = GETLOCAL(oparg);
3033 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 SETLOCAL(oparg, NULL);
3035 DISPATCH();
3036 }
3037 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003038 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 UNBOUNDLOCAL_ERROR_MSG,
3040 PyTuple_GetItem(co->co_varnames, oparg)
3041 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 goto error;
3043 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003044
Benjamin Petersonddd19492018-09-16 22:38:02 -07003045 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003047 PyObject *oldobj = PyCell_GET(cell);
3048 if (oldobj != NULL) {
3049 PyCell_SET(cell, NULL);
3050 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003051 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003052 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003053 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 goto error;
3055 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003056
Benjamin Petersonddd19492018-09-16 22:38:02 -07003057 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 PyObject *cell = freevars[oparg];
3059 Py_INCREF(cell);
3060 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003065 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003066 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003067 assert(locals);
3068 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3069 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3070 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3071 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3072 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003073 value = PyDict_GetItemWithError(locals, name);
3074 if (value != NULL) {
3075 Py_INCREF(value);
3076 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003077 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003078 goto error;
3079 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003080 }
3081 else {
3082 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003083 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003084 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003085 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003086 }
3087 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003088 }
3089 }
3090 if (!value) {
3091 PyObject *cell = freevars[oparg];
3092 value = PyCell_GET(cell);
3093 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003094 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003095 goto error;
3096 }
3097 Py_INCREF(value);
3098 }
3099 PUSH(value);
3100 DISPATCH();
3101 }
3102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 PyObject *cell = freevars[oparg];
3105 PyObject *value = PyCell_GET(cell);
3106 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003107 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 Py_INCREF(value);
3111 PUSH(value);
3112 DISPATCH();
3113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003114
Benjamin Petersonddd19492018-09-16 22:38:02 -07003115 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 PyObject *v = POP();
3117 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003118 PyObject *oldobj = PyCell_GET(cell);
3119 PyCell_SET(cell, v);
3120 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 DISPATCH();
3122 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003123
Benjamin Petersonddd19492018-09-16 22:38:02 -07003124 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003125 PyObject *str;
3126 PyObject *empty = PyUnicode_New(0, 0);
3127 if (empty == NULL) {
3128 goto error;
3129 }
3130 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3131 Py_DECREF(empty);
3132 if (str == NULL)
3133 goto error;
3134 while (--oparg >= 0) {
3135 PyObject *item = POP();
3136 Py_DECREF(item);
3137 }
3138 PUSH(str);
3139 DISPATCH();
3140 }
3141
Benjamin Petersonddd19492018-09-16 22:38:02 -07003142 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 PyObject *tup = PyTuple_New(oparg);
3144 if (tup == NULL)
3145 goto error;
3146 while (--oparg >= 0) {
3147 PyObject *item = POP();
3148 PyTuple_SET_ITEM(tup, oparg, item);
3149 }
3150 PUSH(tup);
3151 DISPATCH();
3152 }
3153
Benjamin Petersonddd19492018-09-16 22:38:02 -07003154 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 PyObject *list = PyList_New(oparg);
3156 if (list == NULL)
3157 goto error;
3158 while (--oparg >= 0) {
3159 PyObject *item = POP();
3160 PyList_SET_ITEM(list, oparg, item);
3161 }
3162 PUSH(list);
3163 DISPATCH();
3164 }
3165
Mark Shannon13bc1392020-01-23 09:25:17 +00003166 case TARGET(LIST_TO_TUPLE): {
3167 PyObject *list = POP();
3168 PyObject *tuple = PyList_AsTuple(list);
3169 Py_DECREF(list);
3170 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003171 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003172 }
3173 PUSH(tuple);
3174 DISPATCH();
3175 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003176
Mark Shannon13bc1392020-01-23 09:25:17 +00003177 case TARGET(LIST_EXTEND): {
3178 PyObject *iterable = POP();
3179 PyObject *list = PEEK(oparg);
3180 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3181 if (none_val == NULL) {
3182 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003183 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003184 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003185 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003186 _PyErr_Format(tstate, PyExc_TypeError,
3187 "Value after * must be an iterable, not %.200s",
3188 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003189 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003190 Py_DECREF(iterable);
3191 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003192 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003193 Py_DECREF(none_val);
3194 Py_DECREF(iterable);
3195 DISPATCH();
3196 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003197
Mark Shannon13bc1392020-01-23 09:25:17 +00003198 case TARGET(SET_UPDATE): {
3199 PyObject *iterable = POP();
3200 PyObject *set = PEEK(oparg);
3201 int err = _PySet_Update(set, iterable);
3202 Py_DECREF(iterable);
3203 if (err < 0) {
3204 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003205 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003206 DISPATCH();
3207 }
3208
Benjamin Petersonddd19492018-09-16 22:38:02 -07003209 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 PyObject *set = PySet_New(NULL);
3211 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003212 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 if (set == NULL)
3214 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003215 for (i = oparg; i > 0; i--) {
3216 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 if (err == 0)
3218 err = PySet_Add(set, item);
3219 Py_DECREF(item);
3220 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003221 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 if (err != 0) {
3223 Py_DECREF(set);
3224 goto error;
3225 }
3226 PUSH(set);
3227 DISPATCH();
3228 }
3229
Benjamin Petersonddd19492018-09-16 22:38:02 -07003230 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003231 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3233 if (map == NULL)
3234 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003235 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003236 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003237 PyObject *key = PEEK(2*i);
3238 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003239 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003240 if (err != 0) {
3241 Py_DECREF(map);
3242 goto error;
3243 }
3244 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003245
3246 while (oparg--) {
3247 Py_DECREF(POP());
3248 Py_DECREF(POP());
3249 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 PUSH(map);
3251 DISPATCH();
3252 }
3253
Benjamin Petersonddd19492018-09-16 22:38:02 -07003254 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003255 _Py_IDENTIFIER(__annotations__);
3256 int err;
3257 PyObject *ann_dict;
3258 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003259 _PyErr_Format(tstate, PyExc_SystemError,
3260 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003261 goto error;
3262 }
3263 /* check if __annotations__ in locals()... */
3264 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003265 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003266 &PyId___annotations__);
3267 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003268 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003269 goto error;
3270 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003271 /* ...if not, create a new one */
3272 ann_dict = PyDict_New();
3273 if (ann_dict == NULL) {
3274 goto error;
3275 }
3276 err = _PyDict_SetItemId(f->f_locals,
3277 &PyId___annotations__, ann_dict);
3278 Py_DECREF(ann_dict);
3279 if (err != 0) {
3280 goto error;
3281 }
3282 }
3283 }
3284 else {
3285 /* do the same if locals() is not a dict */
3286 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3287 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003288 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003289 }
3290 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3291 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003292 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003293 goto error;
3294 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003295 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003296 ann_dict = PyDict_New();
3297 if (ann_dict == NULL) {
3298 goto error;
3299 }
3300 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3301 Py_DECREF(ann_dict);
3302 if (err != 0) {
3303 goto error;
3304 }
3305 }
3306 else {
3307 Py_DECREF(ann_dict);
3308 }
3309 }
3310 DISPATCH();
3311 }
3312
Benjamin Petersonddd19492018-09-16 22:38:02 -07003313 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003314 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003315 PyObject *map;
3316 PyObject *keys = TOP();
3317 if (!PyTuple_CheckExact(keys) ||
3318 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003319 _PyErr_SetString(tstate, PyExc_SystemError,
3320 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003321 goto error;
3322 }
3323 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3324 if (map == NULL) {
3325 goto error;
3326 }
3327 for (i = oparg; i > 0; i--) {
3328 int err;
3329 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3330 PyObject *value = PEEK(i + 1);
3331 err = PyDict_SetItem(map, key, value);
3332 if (err != 0) {
3333 Py_DECREF(map);
3334 goto error;
3335 }
3336 }
3337
3338 Py_DECREF(POP());
3339 while (oparg--) {
3340 Py_DECREF(POP());
3341 }
3342 PUSH(map);
3343 DISPATCH();
3344 }
3345
Mark Shannon8a4cd702020-01-27 09:57:45 +00003346 case TARGET(DICT_UPDATE): {
3347 PyObject *update = POP();
3348 PyObject *dict = PEEK(oparg);
3349 if (PyDict_Update(dict, update) < 0) {
3350 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3351 _PyErr_Format(tstate, PyExc_TypeError,
3352 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003353 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003354 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003355 Py_DECREF(update);
3356 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003357 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003358 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003359 DISPATCH();
3360 }
3361
Mark Shannon8a4cd702020-01-27 09:57:45 +00003362 case TARGET(DICT_MERGE): {
3363 PyObject *update = POP();
3364 PyObject *dict = PEEK(oparg);
3365
3366 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3367 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3368 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003369 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003370 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003371 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003372 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003373 DISPATCH();
3374 }
3375
Benjamin Petersonddd19492018-09-16 22:38:02 -07003376 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003377 PyObject *value = TOP();
3378 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 PyObject *map;
3380 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003381 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003382 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003384 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 Py_DECREF(value);
3386 Py_DECREF(key);
3387 if (err != 0)
3388 goto error;
3389 PREDICT(JUMP_ABSOLUTE);
3390 DISPATCH();
3391 }
3392
Benjamin Petersonddd19492018-09-16 22:38:02 -07003393 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 PyObject *name = GETITEM(names, oparg);
3395 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003396
3397 PyTypeObject *type = Py_TYPE(owner);
3398 PyObject *res;
3399 PyObject **dictptr;
3400 PyObject *dict;
3401 _PyOpCodeOpt_LoadAttr *la;
3402
3403 OPCACHE_STAT_ATTR_TOTAL();
3404
3405 OPCACHE_CHECK();
3406 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3407 {
3408 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003409 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003410 la = &co_opcache->u.la;
3411 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3412 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003413 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3414 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3415 // so ~offset is strictly < -1 (assuming 2's complement).
3416 if (la->hint < -1) {
3417 // Even faster path -- slot hint.
3418 Py_ssize_t offset = ~la->hint;
3419 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3420 char *addr = (char *)owner + offset;
3421 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003422 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003423 Py_INCREF(res);
3424 SET_TOP(res);
3425 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003426 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003427 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003428 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3429 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003430 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003431 // Fast path for dict.
3432 assert(type->tp_dict != NULL);
3433 assert(type->tp_dictoffset > 0);
3434
3435 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3436 dict = *dictptr;
3437 if (dict != NULL && PyDict_CheckExact(dict)) {
3438 Py_ssize_t hint = la->hint;
3439 Py_INCREF(dict);
3440 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003441 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003442 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003443 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003444 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003445 if (la->hint == hint && hint >= 0) {
3446 // Our hint has helped -- cache hit.
3447 OPCACHE_STAT_ATTR_HIT();
3448 } else {
3449 // The hint we provided didn't work.
3450 // Maybe next time?
3451 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3452 }
3453
3454 Py_INCREF(res);
3455 SET_TOP(res);
3456 Py_DECREF(owner);
3457 Py_DECREF(dict);
3458 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003459 }
3460 else {
3461 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003462 // This attribute can be missing sometimes;
3463 // we don't want to optimize this lookup.
3464 OPCACHE_DEOPT_LOAD_ATTR();
3465 Py_DECREF(dict);
3466 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003467 }
3468 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003469 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3470 OPCACHE_DEOPT_LOAD_ATTR();
3471 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003472 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003473 }
3474 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003475 // The type of the object has either been updated,
3476 // or is different. Maybe it will stabilize?
3477 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3478 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003479 OPCACHE_STAT_ATTR_MISS();
3480 }
3481
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003482 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003483 type->tp_getattro == PyObject_GenericGetAttr)
3484 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003485 if (type->tp_dict == NULL) {
3486 if (PyType_Ready(type) < 0) {
3487 Py_DECREF(owner);
3488 SET_TOP(NULL);
3489 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003490 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003491 }
3492 PyObject *descr = _PyType_Lookup(type, name);
3493 if (descr != NULL) {
3494 // We found an attribute with a data-like descriptor.
3495 PyTypeObject *dtype = Py_TYPE(descr);
3496 if (dtype == &PyMemberDescr_Type) { // It's a slot
3497 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3498 struct PyMemberDef *dmem = member->d_member;
3499 if (dmem->type == T_OBJECT_EX) {
3500 Py_ssize_t offset = dmem->offset;
3501 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003502
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003503 if (co_opcache->optimized == 0) {
3504 // First time we optimize this opcode.
3505 OPCACHE_STAT_ATTR_OPT();
3506 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3507 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3508 }
3509
3510 la = &co_opcache->u.la;
3511 la->type = type;
3512 la->tp_version_tag = type->tp_version_tag;
3513 la->hint = ~offset;
3514
3515 char *addr = (char *)owner + offset;
3516 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003517 if (res != NULL) {
3518 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003519 Py_DECREF(owner);
3520 SET_TOP(res);
3521
Pablo Galindo109826c2020-10-20 06:22:44 +01003522 DISPATCH();
3523 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003524 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003525 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003526 // Else it's a slot of a different type. We don't handle those.
3527 }
3528 // Else it's some other kind of descriptor that we don't handle.
3529 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003530 }
3531 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003532 // We found an instance with a __dict__.
3533 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3534 dict = *dictptr;
3535
3536 if (dict != NULL && PyDict_CheckExact(dict)) {
3537 Py_INCREF(dict);
3538 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003539 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003540 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3541 if (res != NULL) {
3542 Py_INCREF(res);
3543 Py_DECREF(dict);
3544 Py_DECREF(owner);
3545 SET_TOP(res);
3546
3547 if (co_opcache->optimized == 0) {
3548 // First time we optimize this opcode.
3549 OPCACHE_STAT_ATTR_OPT();
3550 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3551 }
3552
3553 la = &co_opcache->u.la;
3554 la->type = type;
3555 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003556 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003557 la->hint = hint;
3558
3559 DISPATCH();
3560 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003561 else {
3562 _PyErr_Clear(tstate);
3563 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003564 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003565 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003566 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003567 OPCACHE_DEOPT_LOAD_ATTR();
3568 }
3569 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003570 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003571 OPCACHE_DEOPT_LOAD_ATTR();
3572 }
3573 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3574 OPCACHE_DEOPT_LOAD_ATTR();
3575 }
3576 }
3577
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003578 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003579 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 Py_DECREF(owner);
3581 SET_TOP(res);
3582 if (res == NULL)
3583 goto error;
3584 DISPATCH();
3585 }
3586
Benjamin Petersonddd19492018-09-16 22:38:02 -07003587 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003588 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 PyObject *right = POP();
3590 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003591 PyObject *res = PyObject_RichCompare(left, right, oparg);
3592 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 Py_DECREF(left);
3594 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003595 if (res == NULL)
3596 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 PREDICT(POP_JUMP_IF_FALSE);
3598 PREDICT(POP_JUMP_IF_TRUE);
3599 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003600 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003601
Mark Shannon9af0e472020-01-14 10:12:45 +00003602 case TARGET(IS_OP): {
3603 PyObject *right = POP();
3604 PyObject *left = TOP();
3605 int res = (left == right)^oparg;
3606 PyObject *b = res ? Py_True : Py_False;
3607 Py_INCREF(b);
3608 SET_TOP(b);
3609 Py_DECREF(left);
3610 Py_DECREF(right);
3611 PREDICT(POP_JUMP_IF_FALSE);
3612 PREDICT(POP_JUMP_IF_TRUE);
3613 FAST_DISPATCH();
3614 }
3615
3616 case TARGET(CONTAINS_OP): {
3617 PyObject *right = POP();
3618 PyObject *left = POP();
3619 int res = PySequence_Contains(right, left);
3620 Py_DECREF(left);
3621 Py_DECREF(right);
3622 if (res < 0) {
3623 goto error;
3624 }
3625 PyObject *b = (res^oparg) ? Py_True : Py_False;
3626 Py_INCREF(b);
3627 PUSH(b);
3628 PREDICT(POP_JUMP_IF_FALSE);
3629 PREDICT(POP_JUMP_IF_TRUE);
3630 FAST_DISPATCH();
3631 }
3632
3633#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3634 "BaseException is not allowed"
3635
3636 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3637 PyObject *right = POP();
3638 PyObject *left = POP();
3639 if (PyTuple_Check(right)) {
3640 Py_ssize_t i, length;
3641 length = PyTuple_GET_SIZE(right);
3642 for (i = 0; i < length; i++) {
3643 PyObject *exc = PyTuple_GET_ITEM(right, i);
3644 if (!PyExceptionClass_Check(exc)) {
3645 _PyErr_SetString(tstate, PyExc_TypeError,
3646 CANNOT_CATCH_MSG);
3647 Py_DECREF(left);
3648 Py_DECREF(right);
3649 goto error;
3650 }
3651 }
3652 }
3653 else {
3654 if (!PyExceptionClass_Check(right)) {
3655 _PyErr_SetString(tstate, PyExc_TypeError,
3656 CANNOT_CATCH_MSG);
3657 Py_DECREF(left);
3658 Py_DECREF(right);
3659 goto error;
3660 }
3661 }
3662 int res = PyErr_GivenExceptionMatches(left, right);
3663 Py_DECREF(left);
3664 Py_DECREF(right);
3665 if (res > 0) {
3666 /* Exception matches -- Do nothing */;
3667 }
3668 else if (res == 0) {
3669 JUMPTO(oparg);
3670 }
3671 else {
3672 goto error;
3673 }
3674 DISPATCH();
3675 }
3676
Benjamin Petersonddd19492018-09-16 22:38:02 -07003677 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003678 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003679 PyObject *fromlist = POP();
3680 PyObject *level = TOP();
3681 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003682 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003683 Py_DECREF(level);
3684 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685 SET_TOP(res);
3686 if (res == NULL)
3687 goto error;
3688 DISPATCH();
3689 }
3690
Benjamin Petersonddd19492018-09-16 22:38:02 -07003691 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 PyObject *from = POP(), *locals;
3693 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003694 if (PyFrame_FastToLocalsWithError(f) < 0) {
3695 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003696 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003697 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003698
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003699 locals = f->f_locals;
3700 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003701 _PyErr_SetString(tstate, PyExc_SystemError,
3702 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003703 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003704 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003706 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003708 Py_DECREF(from);
3709 if (err != 0)
3710 goto error;
3711 DISPATCH();
3712 }
Guido van Rossum25831651993-05-19 14:50:45 +00003713
Benjamin Petersonddd19492018-09-16 22:38:02 -07003714 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003715 PyObject *name = GETITEM(names, oparg);
3716 PyObject *from = TOP();
3717 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003718 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003719 PUSH(res);
3720 if (res == NULL)
3721 goto error;
3722 DISPATCH();
3723 }
Thomas Wouters52152252000-08-17 22:55:00 +00003724
Benjamin Petersonddd19492018-09-16 22:38:02 -07003725 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 JUMPBY(oparg);
3727 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003728 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003729
Benjamin Petersonddd19492018-09-16 22:38:02 -07003730 case TARGET(POP_JUMP_IF_FALSE): {
3731 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003732 PyObject *cond = POP();
3733 int err;
3734 if (cond == Py_True) {
3735 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 FAST_DISPATCH();
3737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003738 if (cond == Py_False) {
3739 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 JUMPTO(oparg);
3741 FAST_DISPATCH();
3742 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003743 err = PyObject_IsTrue(cond);
3744 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003746 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 else if (err == 0)
3748 JUMPTO(oparg);
3749 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003750 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003752 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003753
Benjamin Petersonddd19492018-09-16 22:38:02 -07003754 case TARGET(POP_JUMP_IF_TRUE): {
3755 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003756 PyObject *cond = POP();
3757 int err;
3758 if (cond == Py_False) {
3759 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 FAST_DISPATCH();
3761 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003762 if (cond == Py_True) {
3763 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 JUMPTO(oparg);
3765 FAST_DISPATCH();
3766 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003767 err = PyObject_IsTrue(cond);
3768 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 JUMPTO(oparg);
3771 }
3772 else if (err == 0)
3773 ;
3774 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003775 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003777 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003778
Benjamin Petersonddd19492018-09-16 22:38:02 -07003779 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 PyObject *cond = TOP();
3781 int err;
3782 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003783 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003784 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 FAST_DISPATCH();
3786 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 JUMPTO(oparg);
3789 FAST_DISPATCH();
3790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003791 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003793 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003794 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 }
3796 else if (err == 0)
3797 JUMPTO(oparg);
3798 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003799 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003802
Benjamin Petersonddd19492018-09-16 22:38:02 -07003803 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003804 PyObject *cond = TOP();
3805 int err;
3806 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003807 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003808 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 FAST_DISPATCH();
3810 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003811 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 JUMPTO(oparg);
3813 FAST_DISPATCH();
3814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003815 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 JUMPTO(oparg);
3818 }
3819 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003820 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003821 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 }
3823 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003824 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003827
Benjamin Petersonddd19492018-09-16 22:38:02 -07003828 case TARGET(JUMP_ABSOLUTE): {
3829 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003831#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 /* Enabling this path speeds-up all while and for-loops by bypassing
3833 the per-loop checks for signals. By default, this should be turned-off
3834 because it prevents detection of a control-break in tight loops like
3835 "while 1: pass". Compile with this option turned-on when you need
3836 the speed-up and do not need break checking inside tight loops (ones
3837 that contain only instructions ending with FAST_DISPATCH).
3838 */
3839 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003840#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003842#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003843 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003844
Brandt Bucher145bf262021-02-26 14:51:55 -08003845 case TARGET(GET_LEN): {
3846 // PUSH(len(TOS))
3847 Py_ssize_t len_i = PyObject_Length(TOP());
3848 if (len_i < 0) {
3849 goto error;
3850 }
3851 PyObject *len_o = PyLong_FromSsize_t(len_i);
3852 if (len_o == NULL) {
3853 goto error;
3854 }
3855 PUSH(len_o);
3856 DISPATCH();
3857 }
3858
3859 case TARGET(MATCH_CLASS): {
3860 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3861 // attributes. On failure, set TOS to False.
3862 PyObject *names = POP();
3863 PyObject *type = TOP();
3864 PyObject *subject = SECOND();
3865 assert(PyTuple_CheckExact(names));
3866 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3867 Py_DECREF(names);
3868 if (attrs) {
3869 // Success!
3870 assert(PyTuple_CheckExact(attrs));
3871 Py_DECREF(subject);
3872 SET_SECOND(attrs);
3873 }
3874 else if (_PyErr_Occurred(tstate)) {
3875 goto error;
3876 }
3877 Py_DECREF(type);
3878 SET_TOP(PyBool_FromLong(!!attrs));
3879 DISPATCH();
3880 }
3881
3882 case TARGET(MATCH_MAPPING): {
3883 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3884 PyObject *subject = TOP();
3885 // Fast path for dicts:
3886 if (PyDict_Check(subject)) {
3887 Py_INCREF(Py_True);
3888 PUSH(Py_True);
3889 DISPATCH();
3890 }
3891 // Lazily import _collections_abc.Mapping, and keep it handy on the
3892 // PyInterpreterState struct (it gets cleaned up at exit):
3893 PyInterpreterState *interp = PyInterpreterState_Get();
3894 if (interp->map_abc == NULL) {
3895 PyObject *abc = PyImport_ImportModule("_collections_abc");
3896 if (abc == NULL) {
3897 goto error;
3898 }
3899 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3900 if (interp->map_abc == NULL) {
3901 goto error;
3902 }
3903 }
3904 int match = PyObject_IsInstance(subject, interp->map_abc);
3905 if (match < 0) {
3906 goto error;
3907 }
3908 PUSH(PyBool_FromLong(match));
3909 DISPATCH();
3910 }
3911
3912 case TARGET(MATCH_SEQUENCE): {
3913 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3914 // and isinstance(TOS, _collections_abc.Sequence))
3915 PyObject *subject = TOP();
3916 // Fast path for lists and tuples:
3917 if (PyType_FastSubclass(Py_TYPE(subject),
3918 Py_TPFLAGS_LIST_SUBCLASS |
3919 Py_TPFLAGS_TUPLE_SUBCLASS))
3920 {
3921 Py_INCREF(Py_True);
3922 PUSH(Py_True);
3923 DISPATCH();
3924 }
3925 // Bail on some possible Sequences that we intentionally exclude:
3926 if (PyType_FastSubclass(Py_TYPE(subject),
3927 Py_TPFLAGS_BYTES_SUBCLASS |
3928 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3929 PyByteArray_Check(subject))
3930 {
3931 Py_INCREF(Py_False);
3932 PUSH(Py_False);
3933 DISPATCH();
3934 }
3935 // Lazily import _collections_abc.Sequence, and keep it handy on the
3936 // PyInterpreterState struct (it gets cleaned up at exit):
3937 PyInterpreterState *interp = PyInterpreterState_Get();
3938 if (interp->seq_abc == NULL) {
3939 PyObject *abc = PyImport_ImportModule("_collections_abc");
3940 if (abc == NULL) {
3941 goto error;
3942 }
3943 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3944 if (interp->seq_abc == NULL) {
3945 goto error;
3946 }
3947 }
3948 int match = PyObject_IsInstance(subject, interp->seq_abc);
3949 if (match < 0) {
3950 goto error;
3951 }
3952 PUSH(PyBool_FromLong(match));
3953 DISPATCH();
3954 }
3955
3956 case TARGET(MATCH_KEYS): {
3957 // On successful match for all keys, PUSH(values) and PUSH(True).
3958 // Otherwise, PUSH(None) and PUSH(False).
3959 PyObject *keys = TOP();
3960 PyObject *subject = SECOND();
3961 PyObject *values_or_none = match_keys(tstate, subject, keys);
3962 if (values_or_none == NULL) {
3963 goto error;
3964 }
3965 PUSH(values_or_none);
3966 if (values_or_none == Py_None) {
3967 Py_INCREF(Py_False);
3968 PUSH(Py_False);
3969 DISPATCH();
3970 }
3971 assert(PyTuple_CheckExact(values_or_none));
3972 Py_INCREF(Py_True);
3973 PUSH(Py_True);
3974 DISPATCH();
3975 }
3976
3977 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3978 // rest = dict(TOS1)
3979 // for key in TOS:
3980 // del rest[key]
3981 // SET_TOP(rest)
3982 PyObject *keys = TOP();
3983 PyObject *subject = SECOND();
3984 PyObject *rest = PyDict_New();
3985 if (rest == NULL || PyDict_Update(rest, subject)) {
3986 Py_XDECREF(rest);
3987 goto error;
3988 }
3989 // This may seem a bit inefficient, but keys is rarely big enough to
3990 // actually impact runtime.
3991 assert(PyTuple_CheckExact(keys));
3992 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3993 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3994 Py_DECREF(rest);
3995 goto error;
3996 }
3997 }
3998 Py_DECREF(keys);
3999 SET_TOP(rest);
4000 DISPATCH();
4001 }
4002
Benjamin Petersonddd19492018-09-16 22:38:02 -07004003 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004005 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004006 PyObject *iter = PyObject_GetIter(iterable);
4007 Py_DECREF(iterable);
4008 SET_TOP(iter);
4009 if (iter == NULL)
4010 goto error;
4011 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004012 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004013 DISPATCH();
4014 }
4015
Benjamin Petersonddd19492018-09-16 22:38:02 -07004016 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004017 /* before: [obj]; after [getiter(obj)] */
4018 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004019 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004020 if (PyCoro_CheckExact(iterable)) {
4021 /* `iterable` is a coroutine */
4022 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4023 /* and it is used in a 'yield from' expression of a
4024 regular generator. */
4025 Py_DECREF(iterable);
4026 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004027 _PyErr_SetString(tstate, PyExc_TypeError,
4028 "cannot 'yield from' a coroutine object "
4029 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004030 goto error;
4031 }
4032 }
4033 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004034 /* `iterable` is not a generator. */
4035 iter = PyObject_GetIter(iterable);
4036 Py_DECREF(iterable);
4037 SET_TOP(iter);
4038 if (iter == NULL)
4039 goto error;
4040 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004041 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004042 DISPATCH();
4043 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004044
Benjamin Petersonddd19492018-09-16 22:38:02 -07004045 case TARGET(FOR_ITER): {
4046 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004048 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004049 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004050 if (next != NULL) {
4051 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 PREDICT(STORE_FAST);
4053 PREDICT(UNPACK_SEQUENCE);
4054 DISPATCH();
4055 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004056 if (_PyErr_Occurred(tstate)) {
4057 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004058 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 }
4060 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00004061 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02004062 }
4063 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
4065 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004066 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004067 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 JUMPBY(oparg);
4069 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004070 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004071
Benjamin Petersonddd19492018-09-16 22:38:02 -07004072 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004073 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 STACK_LEVEL());
4075 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004077
Benjamin Petersonddd19492018-09-16 22:38:02 -07004078 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004079 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004080 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004081 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004082 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004083 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004084 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004085 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004086 }
4087 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4088 if (exit == NULL) {
4089 Py_DECREF(enter);
4090 goto error;
4091 }
Yury Selivanov75445082015-05-11 22:57:16 -04004092 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004093 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004094 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004095 Py_DECREF(enter);
4096 if (res == NULL)
4097 goto error;
4098 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004099 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004100 DISPATCH();
4101 }
4102
Benjamin Petersonddd19492018-09-16 22:38:02 -07004103 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004104 PyObject *res = POP();
4105 /* Setup the finally block before pushing the result
4106 of __aenter__ on the stack. */
4107 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4108 STACK_LEVEL());
4109 PUSH(res);
4110 DISPATCH();
4111 }
4112
Benjamin Petersonddd19492018-09-16 22:38:02 -07004113 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004114 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004115 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004117 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004118 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004119 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004120 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004121 }
4122 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004123 if (exit == NULL) {
4124 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004125 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004126 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004127 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004128 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004129 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004130 Py_DECREF(enter);
4131 if (res == NULL)
4132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 /* Setup the finally block before pushing the result
4134 of __enter__ on the stack. */
4135 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4136 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004137
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004138 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 DISPATCH();
4140 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004141
Mark Shannonfee55262019-11-21 09:11:43 +00004142 case TARGET(WITH_EXCEPT_START): {
4143 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004145 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4146 - SEVENTH: the context.__exit__ bound method
4147 We call SEVENTH(TOP, SECOND, THIRD).
4148 Then we push again the TOP exception and the __exit__
4149 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004152 PyObject *exc, *val, *tb, *res;
4153
Victor Stinner842cfff2016-12-01 14:45:31 +01004154 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004155 val = SECOND();
4156 tb = THIRD();
4157 assert(exc != Py_None);
4158 assert(!PyLong_Check(exc));
4159 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004160 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004161 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004162 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004163 if (res == NULL)
4164 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004165
Yury Selivanov75445082015-05-11 22:57:16 -04004166 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004167 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004169
Benjamin Petersonddd19492018-09-16 22:38:02 -07004170 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004171 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004172 PyObject *name = GETITEM(names, oparg);
4173 PyObject *obj = TOP();
4174 PyObject *meth = NULL;
4175
4176 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4177
Yury Selivanovf2392132016-12-13 19:03:51 -05004178 if (meth == NULL) {
4179 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004180 goto error;
4181 }
4182
4183 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004184 /* We can bypass temporary bound method object.
4185 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004186
INADA Naoki015bce62017-01-16 17:23:30 +09004187 meth | self | arg1 | ... | argN
4188 */
4189 SET_TOP(meth);
4190 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004191 }
4192 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004193 /* meth is not an unbound method (but a regular attr, or
4194 something was returned by a descriptor protocol). Set
4195 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004196 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004197
4198 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004199 */
INADA Naoki015bce62017-01-16 17:23:30 +09004200 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004201 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004202 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004203 }
4204 DISPATCH();
4205 }
4206
Benjamin Petersonddd19492018-09-16 22:38:02 -07004207 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004208 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004209 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004210
4211 sp = stack_pointer;
4212
INADA Naoki015bce62017-01-16 17:23:30 +09004213 meth = PEEK(oparg + 2);
4214 if (meth == NULL) {
4215 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4216 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004217
4218 Stack layout:
4219
INADA Naoki015bce62017-01-16 17:23:30 +09004220 ... | NULL | callable | arg1 | ... | argN
4221 ^- TOP()
4222 ^- (-oparg)
4223 ^- (-oparg-1)
4224 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004225
Ville Skyttä49b27342017-08-03 09:00:59 +03004226 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004227 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004228 */
Mark Shannon86433452021-01-07 16:49:02 +00004229 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004230 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004231 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004232 }
4233 else {
4234 /* This is a method call. Stack layout:
4235
INADA Naoki015bce62017-01-16 17:23:30 +09004236 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004237 ^- TOP()
4238 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004239 ^- (-oparg-1)
4240 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004241
INADA Naoki015bce62017-01-16 17:23:30 +09004242 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004243 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004244 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004245 */
Mark Shannon86433452021-01-07 16:49:02 +00004246 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004247 stack_pointer = sp;
4248 }
4249
4250 PUSH(res);
4251 if (res == NULL)
4252 goto error;
4253 DISPATCH();
4254 }
4255
Benjamin Petersonddd19492018-09-16 22:38:02 -07004256 case TARGET(CALL_FUNCTION): {
4257 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004258 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004260 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004262 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004263 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004264 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004266 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004268
Benjamin Petersonddd19492018-09-16 22:38:02 -07004269 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004270 PyObject **sp, *res, *names;
4271
4272 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004273 assert(PyTuple_Check(names));
4274 assert(PyTuple_GET_SIZE(names) <= oparg);
4275 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004277 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004279 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004280 Py_DECREF(names);
4281
4282 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004283 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004284 }
4285 DISPATCH();
4286 }
4287
Benjamin Petersonddd19492018-09-16 22:38:02 -07004288 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004289 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004290 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004291 if (oparg & 0x01) {
4292 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004293 if (!PyDict_CheckExact(kwargs)) {
4294 PyObject *d = PyDict_New();
4295 if (d == NULL)
4296 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004297 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004298 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004299 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004300 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004301 goto error;
4302 }
4303 Py_DECREF(kwargs);
4304 kwargs = d;
4305 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004306 assert(PyDict_CheckExact(kwargs));
4307 }
4308 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004309 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004310 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004311 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004312 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004313 goto error;
4314 }
4315 Py_SETREF(callargs, PySequence_Tuple(callargs));
4316 if (callargs == NULL) {
4317 goto error;
4318 }
4319 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004320 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004321
Mark Shannon86433452021-01-07 16:49:02 +00004322 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004323 Py_DECREF(func);
4324 Py_DECREF(callargs);
4325 Py_XDECREF(kwargs);
4326
4327 SET_TOP(result);
4328 if (result == NULL) {
4329 goto error;
4330 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004331 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004333
Benjamin Petersonddd19492018-09-16 22:38:02 -07004334 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004335 PyObject *qualname = POP();
4336 PyObject *codeobj = POP();
4337 PyFunctionObject *func = (PyFunctionObject *)
4338 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004339
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004340 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004341 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004342 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004345
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004346 if (oparg & 0x08) {
4347 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004348 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004349 }
4350 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004351 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004352 func->func_annotations = POP();
4353 }
4354 if (oparg & 0x02) {
4355 assert(PyDict_CheckExact(TOP()));
4356 func->func_kwdefaults = POP();
4357 }
4358 if (oparg & 0x01) {
4359 assert(PyTuple_CheckExact(TOP()));
4360 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004362
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004363 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004364 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004366
Benjamin Petersonddd19492018-09-16 22:38:02 -07004367 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004368 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004372 step = NULL;
4373 stop = POP();
4374 start = TOP();
4375 slice = PySlice_New(start, stop, step);
4376 Py_DECREF(start);
4377 Py_DECREF(stop);
4378 Py_XDECREF(step);
4379 SET_TOP(slice);
4380 if (slice == NULL)
4381 goto error;
4382 DISPATCH();
4383 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004384
Benjamin Petersonddd19492018-09-16 22:38:02 -07004385 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004386 /* Handles f-string value formatting. */
4387 PyObject *result;
4388 PyObject *fmt_spec;
4389 PyObject *value;
4390 PyObject *(*conv_fn)(PyObject *);
4391 int which_conversion = oparg & FVC_MASK;
4392 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4393
4394 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004395 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004396
4397 /* See if any conversion is specified. */
4398 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004399 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004400 case FVC_STR: conv_fn = PyObject_Str; break;
4401 case FVC_REPR: conv_fn = PyObject_Repr; break;
4402 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004403 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004404 _PyErr_Format(tstate, PyExc_SystemError,
4405 "unexpected conversion flag %d",
4406 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004407 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004408 }
4409
4410 /* If there's a conversion function, call it and replace
4411 value with that result. Otherwise, just use value,
4412 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004413 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004414 result = conv_fn(value);
4415 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004416 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004417 Py_XDECREF(fmt_spec);
4418 goto error;
4419 }
4420 value = result;
4421 }
4422
4423 /* If value is a unicode object, and there's no fmt_spec,
4424 then we know the result of format(value) is value
4425 itself. In that case, skip calling format(). I plan to
4426 move this optimization in to PyObject_Format()
4427 itself. */
4428 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4429 /* Do nothing, just transfer ownership to result. */
4430 result = value;
4431 } else {
4432 /* Actually call format(). */
4433 result = PyObject_Format(value, fmt_spec);
4434 Py_DECREF(value);
4435 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004436 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004437 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004438 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004439 }
4440
Eric V. Smith135d5f42016-02-05 18:23:08 -05004441 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004442 DISPATCH();
4443 }
4444
Benjamin Petersonddd19492018-09-16 22:38:02 -07004445 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004446 int oldoparg = oparg;
4447 NEXTOPARG();
4448 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004450 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004451
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004452
Antoine Pitrou042b1282010-08-13 21:15:58 +00004453#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004455#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 default:
4457 fprintf(stderr,
4458 "XXX lineno: %d, opcode: %d\n",
4459 PyFrame_GetLineNumber(f),
4460 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004461 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004462 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004465
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004466 /* This should never be reached. Every opcode should end with DISPATCH()
4467 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004468 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004470error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004471 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004472#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 if (!_PyErr_Occurred(tstate)) {
4474 _PyErr_SetString(tstate, PyExc_SystemError,
4475 "error return without exception set");
4476 }
Victor Stinner365b6932013-07-12 00:11:58 +02004477#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004478 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004479#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004480
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004481 /* Log traceback info. */
4482 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004483
Mark Shannoncb9879b2020-07-17 11:44:23 +01004484 if (tstate->c_tracefunc != NULL) {
4485 /* Make sure state is set to FRAME_EXECUTING for tracing */
4486 assert(f->f_state == FRAME_EXECUTING);
4487 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004488 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004489 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004490 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004491exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004492 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004493 /* Unwind stacks if an exception occurred */
4494 while (f->f_iblock > 0) {
4495 /* Pop the current block. */
4496 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 if (b->b_type == EXCEPT_HANDLER) {
4499 UNWIND_EXCEPT_HANDLER(b);
4500 continue;
4501 }
4502 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004503 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 PyObject *exc, *val, *tb;
4505 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004506 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004508 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004509 PUSH(exc_info->exc_traceback);
4510 PUSH(exc_info->exc_value);
4511 if (exc_info->exc_type != NULL) {
4512 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 }
4514 else {
4515 Py_INCREF(Py_None);
4516 PUSH(Py_None);
4517 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004518 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 /* Make the raw exception data
4520 available to the handler,
4521 so a program can emulate the
4522 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004524 if (tb != NULL)
4525 PyException_SetTraceback(val, tb);
4526 else
4527 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004529 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004531 exc_info->exc_value = val;
4532 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 if (tb == NULL)
4534 tb = Py_None;
4535 Py_INCREF(tb);
4536 PUSH(tb);
4537 PUSH(val);
4538 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004540 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004541 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004542 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004543 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004544 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004545 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 }
4547 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004548
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004549 /* End the loop as we still have an error */
4550 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004552
Pablo Galindof00828a2019-05-09 16:52:02 +01004553 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004554 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 /* Pop remaining stack entries. */
4557 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004558 PyObject *o = POP();
4559 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004561 f->f_stackdepth = 0;
4562 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004563exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004565 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004566 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004567 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004568 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 }
4570 }
4571 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004572 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004573 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004574 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 }
4576 }
4577 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004580exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004581 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4582 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004583 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004585
Victor Stinner0b72b232020-03-12 23:18:39 +01004586 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004587}
4588
Benjamin Petersonb204a422011-06-05 22:04:07 -05004589static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004590format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004591 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004592{
4593 int err;
4594 Py_ssize_t len = PyList_GET_SIZE(names);
4595 PyObject *name_str, *comma, *tail, *tmp;
4596
4597 assert(PyList_CheckExact(names));
4598 assert(len >= 1);
4599 /* Deal with the joys of natural language. */
4600 switch (len) {
4601 case 1:
4602 name_str = PyList_GET_ITEM(names, 0);
4603 Py_INCREF(name_str);
4604 break;
4605 case 2:
4606 name_str = PyUnicode_FromFormat("%U and %U",
4607 PyList_GET_ITEM(names, len - 2),
4608 PyList_GET_ITEM(names, len - 1));
4609 break;
4610 default:
4611 tail = PyUnicode_FromFormat(", %U, and %U",
4612 PyList_GET_ITEM(names, len - 2),
4613 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004614 if (tail == NULL)
4615 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004616 /* Chop off the last two objects in the list. This shouldn't actually
4617 fail, but we can't be too careful. */
4618 err = PyList_SetSlice(names, len - 2, len, NULL);
4619 if (err == -1) {
4620 Py_DECREF(tail);
4621 return;
4622 }
4623 /* Stitch everything up into a nice comma-separated list. */
4624 comma = PyUnicode_FromString(", ");
4625 if (comma == NULL) {
4626 Py_DECREF(tail);
4627 return;
4628 }
4629 tmp = PyUnicode_Join(comma, names);
4630 Py_DECREF(comma);
4631 if (tmp == NULL) {
4632 Py_DECREF(tail);
4633 return;
4634 }
4635 name_str = PyUnicode_Concat(tmp, tail);
4636 Py_DECREF(tmp);
4637 Py_DECREF(tail);
4638 break;
4639 }
4640 if (name_str == NULL)
4641 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004642 _PyErr_Format(tstate, PyExc_TypeError,
4643 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004644 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004645 len,
4646 kind,
4647 len == 1 ? "" : "s",
4648 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004649 Py_DECREF(name_str);
4650}
4651
4652static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004653missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4654 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004655 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004656{
Victor Stinner74319ae2016-08-25 00:04:09 +02004657 Py_ssize_t i, j = 0;
4658 Py_ssize_t start, end;
4659 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004660 const char *kind = positional ? "positional" : "keyword-only";
4661 PyObject *missing_names;
4662
4663 /* Compute the names of the arguments that are missing. */
4664 missing_names = PyList_New(missing);
4665 if (missing_names == NULL)
4666 return;
4667 if (positional) {
4668 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004669 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004670 }
4671 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004672 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004673 end = start + co->co_kwonlyargcount;
4674 }
4675 for (i = start; i < end; i++) {
4676 if (GETLOCAL(i) == NULL) {
4677 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4678 PyObject *name = PyObject_Repr(raw);
4679 if (name == NULL) {
4680 Py_DECREF(missing_names);
4681 return;
4682 }
4683 PyList_SET_ITEM(missing_names, j++, name);
4684 }
4685 }
4686 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004687 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004688 Py_DECREF(missing_names);
4689}
4690
4691static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004692too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004693 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004694 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004695{
4696 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004697 Py_ssize_t kwonly_given = 0;
4698 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004699 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004700 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004701
Benjamin Petersone109c702011-06-24 09:37:26 -05004702 assert((co->co_flags & CO_VARARGS) == 0);
4703 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004704 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004705 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004706 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004707 }
4708 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004709 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004710 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004711 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004712 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004713 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004714 }
4715 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004716 plural = (co_argcount != 1);
4717 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004718 }
4719 if (sig == NULL)
4720 return;
4721 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004722 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4723 kwonly_sig = PyUnicode_FromFormat(format,
4724 given != 1 ? "s" : "",
4725 kwonly_given,
4726 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004727 if (kwonly_sig == NULL) {
4728 Py_DECREF(sig);
4729 return;
4730 }
4731 }
4732 else {
4733 /* This will not fail. */
4734 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004735 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004736 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004737 _PyErr_Format(tstate, PyExc_TypeError,
4738 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004739 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004740 sig,
4741 plural ? "s" : "",
4742 given,
4743 kwonly_sig,
4744 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004745 Py_DECREF(sig);
4746 Py_DECREF(kwonly_sig);
4747}
4748
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004749static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004750positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004751 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004752 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004753{
4754 int posonly_conflicts = 0;
4755 PyObject* posonly_names = PyList_New(0);
4756
4757 for(int k=0; k < co->co_posonlyargcount; k++){
4758 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4759
4760 for (int k2=0; k2<kwcount; k2++){
4761 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004762 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004763 if (kwname == posonly_name){
4764 if(PyList_Append(posonly_names, kwname) != 0) {
4765 goto fail;
4766 }
4767 posonly_conflicts++;
4768 continue;
4769 }
4770
4771 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4772
4773 if ( cmp > 0) {
4774 if(PyList_Append(posonly_names, kwname) != 0) {
4775 goto fail;
4776 }
4777 posonly_conflicts++;
4778 } else if (cmp < 0) {
4779 goto fail;
4780 }
4781
4782 }
4783 }
4784 if (posonly_conflicts) {
4785 PyObject* comma = PyUnicode_FromString(", ");
4786 if (comma == NULL) {
4787 goto fail;
4788 }
4789 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4790 Py_DECREF(comma);
4791 if (error_names == NULL) {
4792 goto fail;
4793 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004794 _PyErr_Format(tstate, PyExc_TypeError,
4795 "%U() got some positional-only arguments passed"
4796 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004797 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004798 Py_DECREF(error_names);
4799 goto fail;
4800 }
4801
4802 Py_DECREF(posonly_names);
4803 return 0;
4804
4805fail:
4806 Py_XDECREF(posonly_names);
4807 return 1;
4808
4809}
4810
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004811
Mark Shannon0332e562021-02-01 10:42:03 +00004812PyFrameObject *
4813_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004814 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004815 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004816 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004817{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004818 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004819
Mark Shannond6c33fb2021-01-29 13:24:55 +00004820 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4821 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004822 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004823
Victor Stinnerc7020012016-08-16 23:40:29 +02004824 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004825 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004826 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004828 }
Victor Stinner232dda62020-06-04 15:19:02 +02004829 PyObject **fastlocals = f->f_localsplus;
4830 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004831
Victor Stinnerc7020012016-08-16 23:40:29 +02004832 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004833 PyObject *kwdict;
4834 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004835 if (co->co_flags & CO_VARKEYWORDS) {
4836 kwdict = PyDict_New();
4837 if (kwdict == NULL)
4838 goto fail;
4839 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004840 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004841 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004842 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004843 SETLOCAL(i, kwdict);
4844 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004845 else {
4846 kwdict = NULL;
4847 }
4848
Pablo Galindocd74e662019-06-01 18:08:04 +01004849 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004850 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004851 if (argcount > co->co_argcount) {
4852 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004853 }
4854 else {
4855 n = argcount;
4856 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004857 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004858 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004859 Py_INCREF(x);
4860 SETLOCAL(j, x);
4861 }
4862
Victor Stinnerc7020012016-08-16 23:40:29 +02004863 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004864 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004865 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004866 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004867 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004868 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004869 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004870 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004871
Mark Shannon0332e562021-02-01 10:42:03 +00004872 /* Handle keyword arguments */
4873 if (kwnames != NULL) {
4874 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4875 for (i = 0; i < kwcount; i++) {
4876 PyObject **co_varnames;
4877 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4878 PyObject *value = args[i+argcount];
4879 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004880
Mark Shannon0332e562021-02-01 10:42:03 +00004881 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4882 _PyErr_Format(tstate, PyExc_TypeError,
4883 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004884 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004886 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004887
Mark Shannon0332e562021-02-01 10:42:03 +00004888 /* Speed hack: do raw pointer compares. As names are
4889 normally interned this should almost always hit. */
4890 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4891 for (j = co->co_posonlyargcount; j < total_args; j++) {
4892 PyObject *varname = co_varnames[j];
4893 if (varname == keyword) {
4894 goto kw_found;
4895 }
4896 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004897
Mark Shannon0332e562021-02-01 10:42:03 +00004898 /* Slow fallback, just in case */
4899 for (j = co->co_posonlyargcount; j < total_args; j++) {
4900 PyObject *varname = co_varnames[j];
4901 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4902 if (cmp > 0) {
4903 goto kw_found;
4904 }
4905 else if (cmp < 0) {
4906 goto fail;
4907 }
4908 }
4909
4910 assert(j >= total_args);
4911 if (kwdict == NULL) {
4912
4913 if (co->co_posonlyargcount
4914 && positional_only_passed_as_keyword(tstate, co,
4915 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004916 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004917 {
4918 goto fail;
4919 }
4920
4921 _PyErr_Format(tstate, PyExc_TypeError,
4922 "%U() got an unexpected keyword argument '%S'",
4923 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004924 goto fail;
4925 }
4926
Mark Shannon0332e562021-02-01 10:42:03 +00004927 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4928 goto fail;
4929 }
4930 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004931
Mark Shannon0332e562021-02-01 10:42:03 +00004932 kw_found:
4933 if (GETLOCAL(j) != NULL) {
4934 _PyErr_Format(tstate, PyExc_TypeError,
4935 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004936 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004937 goto fail;
4938 }
4939 Py_INCREF(value);
4940 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004943
4944 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004945 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004946 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4947 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 goto fail;
4949 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004950
4951 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004952 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004953 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004954 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004955 Py_ssize_t missing = 0;
4956 for (i = argcount; i < m; i++) {
4957 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004958 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004959 }
4960 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004961 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004962 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004963 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004964 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004965 }
4966 if (n > m)
4967 i = n - m;
4968 else
4969 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004970 if (defcount) {
4971 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4972 for (; i < defcount; i++) {
4973 if (GETLOCAL(m+i) == NULL) {
4974 PyObject *def = defs[i];
4975 Py_INCREF(def);
4976 SETLOCAL(m+i, def);
4977 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004978 }
4979 }
4980 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004981
4982 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004983 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004984 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004985 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004986 if (GETLOCAL(i) != NULL)
4987 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004988 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004989 if (con->fc_kwdefaults != NULL) {
4990 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004991 if (def) {
4992 Py_INCREF(def);
4993 SETLOCAL(i, def);
4994 continue;
4995 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004996 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004997 goto fail;
4998 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004999 }
Benjamin Petersone109c702011-06-24 09:37:26 -05005000 missing++;
5001 }
5002 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005003 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005004 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005005 goto fail;
5006 }
5007 }
5008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005010 vars into frame. */
5011 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005013 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005014 /* Possibly account for the cell variable being an argument. */
5015 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005016 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005017 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005018 /* Clear the local copy. */
5019 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005020 }
5021 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005022 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005023 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005024 if (c == NULL)
5025 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005026 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005028
5029 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005030 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005031 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005032 Py_INCREF(o);
5033 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005035
Mark Shannon0332e562021-02-01 10:42:03 +00005036 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005037
Thomas Woutersce272b62007-09-19 21:19:28 +00005038fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 /* decref'ing the frame can cause __del__ methods to get invoked,
5041 which can call back into Python. While we're done with the
5042 current Python frame (f), the associated C stack is still in use,
5043 so recursion_depth must be boosted for the duration.
5044 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005045 if (Py_REFCNT(f) > 1) {
5046 Py_DECREF(f);
5047 _PyObject_GC_TRACK(f);
5048 }
5049 else {
5050 ++tstate->recursion_depth;
5051 Py_DECREF(f);
5052 --tstate->recursion_depth;
5053 }
Mark Shannon0332e562021-02-01 10:42:03 +00005054 return NULL;
5055}
5056
5057static PyObject *
5058make_coro(PyFrameConstructor *con, PyFrameObject *f)
5059{
5060 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5061 PyObject *gen;
5062 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5063
5064 /* Don't need to keep the reference to f_back, it will be set
5065 * when the generator is resumed. */
5066 Py_CLEAR(f->f_back);
5067
5068 /* Create a new generator that owns the ready to run frame
5069 * and return that as the value. */
5070 if (is_coro) {
5071 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5072 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5073 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5074 } else {
5075 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5076 }
5077 if (gen == NULL) {
5078 return NULL;
5079 }
5080
5081 _PyObject_GC_TRACK(f);
5082
5083 return gen;
5084}
5085
5086PyObject *
5087_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5088 PyObject *locals,
5089 PyObject* const* args, size_t argcount,
5090 PyObject *kwnames)
5091{
5092 PyFrameObject *f = _PyEval_MakeFrameVector(
5093 tstate, con, locals, args, argcount, kwnames);
5094 if (f == NULL) {
5095 return NULL;
5096 }
5097 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5098 return make_coro(con, f);
5099 }
5100 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5101
5102 /* decref'ing the frame can cause __del__ methods to get invoked,
5103 which can call back into Python. While we're done with the
5104 current Python frame (f), the associated C stack is still in use,
5105 so recursion_depth must be boosted for the duration.
5106 */
5107 if (Py_REFCNT(f) > 1) {
5108 Py_DECREF(f);
5109 _PyObject_GC_TRACK(f);
5110 }
5111 else {
5112 ++tstate->recursion_depth;
5113 Py_DECREF(f);
5114 --tstate->recursion_depth;
5115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005117}
5118
Mark Shannond6c33fb2021-01-29 13:24:55 +00005119/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005120PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005121PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5122 PyObject *const *args, int argcount,
5123 PyObject *const *kws, int kwcount,
5124 PyObject *const *defs, int defcount,
5125 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005126{
Victor Stinner46496f92021-02-20 15:17:18 +01005127 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005128 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005129 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5130 if (defaults == NULL) {
5131 return NULL;
5132 }
Victor Stinner46496f92021-02-20 15:17:18 +01005133 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005134 if (builtins == NULL) {
5135 Py_DECREF(defaults);
5136 return NULL;
5137 }
Dong-hee Na3cf08332021-02-14 15:54:39 +09005138 assert ((((PyCodeObject *)_co)->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
Mark Shannon0332e562021-02-01 10:42:03 +00005139 if (locals == NULL) {
5140 locals = globals;
5141 }
5142 PyObject *kwnames;
5143 PyObject *const *allargs;
5144 PyObject **newargs;
5145 if (kwcount == 0) {
5146 allargs = args;
5147 kwnames = NULL;
5148 }
5149 else {
5150 kwnames = PyTuple_New(kwcount);
5151 if (kwnames == NULL) {
5152 res = NULL;
5153 goto fail;
5154 }
5155 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5156 if (newargs == NULL) {
5157 res = NULL;
5158 Py_DECREF(kwnames);
5159 goto fail;
5160 }
5161 for (int i = 0; i < argcount; i++) {
5162 newargs[i] = args[i];
5163 }
5164 for (int i = 0; i < kwcount; i++) {
5165 Py_INCREF(kws[2*i]);
5166 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5167 newargs[argcount+i] = kws[2*i+1];
5168 }
5169 allargs = newargs;
5170 }
5171 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5172 if (kwargs == NULL) {
5173 res = NULL;
5174 Py_DECREF(kwnames);
5175 goto fail;
5176 }
5177 for (int i = 0; i < kwcount; i++) {
5178 Py_INCREF(kws[2*i]);
5179 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5180 kwargs[i] = kws[2*i+1];
5181 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005182 PyFrameConstructor constr = {
5183 .fc_globals = globals,
5184 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005185 .fc_name = ((PyCodeObject *)_co)->co_name,
5186 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005187 .fc_code = _co,
5188 .fc_defaults = defaults,
5189 .fc_kwdefaults = kwdefs,
5190 .fc_closure = closure
5191 };
Mark Shannon0332e562021-02-01 10:42:03 +00005192 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005193 allargs, argcount,
5194 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005195 if (kwcount) {
5196 Py_DECREF(kwnames);
5197 PyMem_Free(newargs);
5198 }
5199fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005200 Py_DECREF(defaults);
5201 Py_DECREF(builtins);
5202 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005203}
5204
Tim Peters5ca576e2001-06-18 22:08:13 +00005205
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005206static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005207special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005210 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005211 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005212 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 return NULL;
5214 }
5215 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005216}
5217
5218
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005219/* Logic for the raise statement (too complicated for inlining).
5220 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005221static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005222do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005226 if (exc == NULL) {
5227 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005228 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005230 type = exc_info->exc_type;
5231 value = exc_info->exc_value;
5232 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005233 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005234 _PyErr_SetString(tstate, PyExc_RuntimeError,
5235 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005236 return 0;
5237 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 Py_XINCREF(type);
5239 Py_XINCREF(value);
5240 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005241 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005242 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 /* We support the following forms of raise:
5246 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005247 raise <instance>
5248 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 if (PyExceptionClass_Check(exc)) {
5251 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005252 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 if (value == NULL)
5254 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005255 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005256 _PyErr_Format(tstate, PyExc_TypeError,
5257 "calling %R should have returned an instance of "
5258 "BaseException, not %R",
5259 type, Py_TYPE(value));
5260 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 }
5263 else if (PyExceptionInstance_Check(exc)) {
5264 value = exc;
5265 type = PyExceptionInstance_Class(exc);
5266 Py_INCREF(type);
5267 }
5268 else {
5269 /* Not something you can raise. You get an exception
5270 anyway, just not what you specified :-) */
5271 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005272 _PyErr_SetString(tstate, PyExc_TypeError,
5273 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 goto raise_error;
5275 }
Collin Winter828f04a2007-08-31 00:04:24 +00005276
Serhiy Storchakac0191582016-09-27 11:37:10 +03005277 assert(type != NULL);
5278 assert(value != NULL);
5279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 if (cause) {
5281 PyObject *fixed_cause;
5282 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005283 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 if (fixed_cause == NULL)
5285 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005286 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005288 else if (PyExceptionInstance_Check(cause)) {
5289 fixed_cause = cause;
5290 }
5291 else if (cause == Py_None) {
5292 Py_DECREF(cause);
5293 fixed_cause = NULL;
5294 }
5295 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005296 _PyErr_SetString(tstate, PyExc_TypeError,
5297 "exception causes must derive from "
5298 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 goto raise_error;
5300 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005301 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 }
Collin Winter828f04a2007-08-31 00:04:24 +00005303
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005305 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005306 Py_DECREF(value);
5307 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005308 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005309
5310raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 Py_XDECREF(value);
5312 Py_XDECREF(type);
5313 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005314 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005315}
5316
Tim Petersd6d010b2001-06-21 02:49:55 +00005317/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005318 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005319
Guido van Rossum0368b722007-05-11 16:50:42 +00005320 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5321 with a variable target.
5322*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005323
Barry Warsawe42b18f1997-08-25 22:13:04 +00005324static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005325unpack_iterable(PyThreadState *tstate, PyObject *v,
5326 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 int i = 0, j = 0;
5329 Py_ssize_t ll = 0;
5330 PyObject *it; /* iter(v) */
5331 PyObject *w;
5332 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005337 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005338 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005339 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005340 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005341 _PyErr_Format(tstate, PyExc_TypeError,
5342 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005343 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005344 }
5345 return 0;
5346 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005348 for (; i < argcnt; i++) {
5349 w = PyIter_Next(it);
5350 if (w == NULL) {
5351 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005352 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005353 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 _PyErr_Format(tstate, PyExc_ValueError,
5355 "not enough values to unpack "
5356 "(expected %d, got %d)",
5357 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005358 }
5359 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005360 _PyErr_Format(tstate, PyExc_ValueError,
5361 "not enough values to unpack "
5362 "(expected at least %d, got %d)",
5363 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 }
5366 goto Error;
5367 }
5368 *--sp = w;
5369 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005371 if (argcntafter == -1) {
5372 /* We better have exhausted the iterator now. */
5373 w = PyIter_Next(it);
5374 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005375 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 goto Error;
5377 Py_DECREF(it);
5378 return 1;
5379 }
5380 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005381 _PyErr_Format(tstate, PyExc_ValueError,
5382 "too many values to unpack (expected %d)",
5383 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 goto Error;
5385 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 l = PySequence_List(it);
5388 if (l == NULL)
5389 goto Error;
5390 *--sp = l;
5391 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 ll = PyList_GET_SIZE(l);
5394 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005395 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005396 "not enough values to unpack (expected at least %d, got %zd)",
5397 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005398 goto Error;
5399 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 /* Pop the "after-variable" args off the list. */
5402 for (j = argcntafter; j > 0; j--, i++) {
5403 *--sp = PyList_GET_ITEM(l, ll - j);
5404 }
5405 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005406 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 Py_DECREF(it);
5408 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005409
Tim Petersd6d010b2001-06-21 02:49:55 +00005410Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 for (; i > 0; i--, sp++)
5412 Py_DECREF(*sp);
5413 Py_XDECREF(it);
5414 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005415}
5416
5417
Guido van Rossum96a42c81992-01-12 02:29:51 +00005418#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005419static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005420prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005423 if (PyObject_Print(v, stdout, 0) != 0) {
5424 /* Don't know what else to do */
5425 _PyErr_Clear(tstate);
5426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 printf("\n");
5428 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005429}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005430#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005431
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005432static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005433call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005434 PyThreadState *tstate,
5435 PyFrameObject *f,
5436 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005437{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005438 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005440 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 if (value == NULL) {
5442 value = Py_None;
5443 Py_INCREF(value);
5444 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005446 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 arg = PyTuple_Pack(3, type, value, traceback);
5448 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005449 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 return;
5451 }
Mark Shannon86433452021-01-07 16:49:02 +00005452 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005454 if (err == 0) {
5455 _PyErr_Restore(tstate, type, value, orig_traceback);
5456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005457 else {
5458 Py_XDECREF(type);
5459 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005460 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005462}
5463
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005464static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005465call_trace_protected(Py_tracefunc func, PyObject *obj,
5466 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005467 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 PyObject *type, *value, *traceback;
5471 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005472 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00005473 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 if (err == 0)
5475 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005476 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005477 return 0;
5478 }
5479 else {
5480 Py_XDECREF(type);
5481 Py_XDECREF(value);
5482 Py_XDECREF(traceback);
5483 return -1;
5484 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005485}
5486
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005487static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005488call_trace(Py_tracefunc func, PyObject *obj,
5489 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005490 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 int result;
5494 if (tstate->tracing)
5495 return 0;
5496 tstate->tracing++;
5497 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005498 if (frame->f_lasti < 0) {
5499 frame->f_lineno = frame->f_code->co_firstlineno;
5500 }
5501 else {
5502 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5503 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005505 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005506 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5507 || (tstate->c_profilefunc != NULL));
5508 tstate->tracing--;
5509 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005510}
5511
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005512PyObject *
5513_PyEval_CallTracing(PyObject *func, PyObject *args)
5514{
Victor Stinner50b48572018-11-01 01:51:40 +01005515 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 int save_tracing = tstate->tracing;
5517 int save_use_tracing = tstate->use_tracing;
5518 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 tstate->tracing = 0;
5521 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5522 || (tstate->c_profilefunc != NULL));
5523 result = PyObject_Call(func, args, NULL);
5524 tstate->tracing = save_tracing;
5525 tstate->use_tracing = save_use_tracing;
5526 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005527}
5528
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005529/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005530static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005531maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005532 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005533 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005536
Nick Coghlan5a851672017-09-08 10:14:16 +10005537 /* If the last instruction falls at the start of a line or if it
5538 represents a jump backwards, update the frame's line number and
5539 then call the trace function if we're tracing source lines.
5540 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005541 int lastline = bounds->ar_line;
5542 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5543 if (line != -1 && frame->f_trace_lines) {
5544 /* Trace backward edges or first instruction of a new line */
5545 if (frame->f_lasti < *instr_prev ||
5546 (line != lastline && frame->f_lasti == bounds->ar_start))
5547 {
Mark Shannon86433452021-01-07 16:49:02 +00005548 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 }
George King20faa682017-10-18 17:44:22 -07005551 /* Always emit an opcode event if we're tracing all opcodes. */
5552 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005553 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 *instr_prev = frame->f_lasti;
5556 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005557}
5558
Victor Stinner309d7cc2020-03-13 16:39:12 +01005559int
5560_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5561{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005562 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005563 /* The caller must hold the GIL */
5564 assert(PyGILState_Check());
5565
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005566 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005567 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005568 PyThreadState *current_tstate = _PyThreadState_GET();
5569 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005570 return -1;
5571 }
5572
5573 PyObject *profileobj = tstate->c_profileobj;
5574
5575 tstate->c_profilefunc = NULL;
5576 tstate->c_profileobj = NULL;
5577 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5578 tstate->use_tracing = tstate->c_tracefunc != NULL;
5579 Py_XDECREF(profileobj);
5580
5581 Py_XINCREF(arg);
5582 tstate->c_profileobj = arg;
5583 tstate->c_profilefunc = func;
5584
5585 /* Flag that tracing or profiling is turned on */
5586 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5587 return 0;
5588}
5589
Fred Drake5755ce62001-06-27 19:19:46 +00005590void
5591PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005592{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005593 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005594 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005595 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005596 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5597 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005598}
5599
5600int
5601_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5602{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005603 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005604 /* The caller must hold the GIL */
5605 assert(PyGILState_Check());
5606
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005607 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005608 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005609 PyThreadState *current_tstate = _PyThreadState_GET();
5610 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005611 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005612 }
5613
Victor Stinnerda2914d2020-03-20 09:29:08 +01005614 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005615 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005616 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005617
5618 tstate->c_tracefunc = NULL;
5619 tstate->c_traceobj = NULL;
5620 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5621 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5622 Py_XDECREF(traceobj);
5623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005624 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005625 tstate->c_traceobj = arg;
5626 tstate->c_tracefunc = func;
5627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005628 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005629 tstate->use_tracing = ((func != NULL)
5630 || (tstate->c_profilefunc != NULL));
5631
5632 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005633}
5634
5635void
5636PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5637{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005638 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005639 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005640 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005641 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5642 }
Fred Draked0838392001-06-16 21:02:31 +00005643}
5644
Victor Stinner309d7cc2020-03-13 16:39:12 +01005645
Yury Selivanov75445082015-05-11 22:57:16 -04005646void
Victor Stinner838f2642019-06-13 22:41:23 +02005647_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005648{
5649 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005650 tstate->coroutine_origin_tracking_depth = new_depth;
5651}
5652
5653int
5654_PyEval_GetCoroutineOriginTrackingDepth(void)
5655{
Victor Stinner50b48572018-11-01 01:51:40 +01005656 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005657 return tstate->coroutine_origin_tracking_depth;
5658}
5659
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005660int
Yury Selivanoveb636452016-09-08 22:01:51 -07005661_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5662{
Victor Stinner50b48572018-11-01 01:51:40 +01005663 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005664
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005665 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005666 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005667 }
5668
Yury Selivanoveb636452016-09-08 22:01:51 -07005669 Py_XINCREF(firstiter);
5670 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005671 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005672}
5673
5674PyObject *
5675_PyEval_GetAsyncGenFirstiter(void)
5676{
Victor Stinner50b48572018-11-01 01:51:40 +01005677 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005678 return tstate->async_gen_firstiter;
5679}
5680
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005681int
Yury Selivanoveb636452016-09-08 22:01:51 -07005682_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5683{
Victor Stinner50b48572018-11-01 01:51:40 +01005684 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005685
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005686 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005687 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005688 }
5689
Yury Selivanoveb636452016-09-08 22:01:51 -07005690 Py_XINCREF(finalizer);
5691 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005692 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005693}
5694
5695PyObject *
5696_PyEval_GetAsyncGenFinalizer(void)
5697{
Victor Stinner50b48572018-11-01 01:51:40 +01005698 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005699 return tstate->async_gen_finalizer;
5700}
5701
Victor Stinner438a12d2019-05-24 17:01:38 +02005702PyFrameObject *
5703PyEval_GetFrame(void)
5704{
5705 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005706 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005707}
5708
Guido van Rossumb209a111997-04-29 18:18:01 +00005709PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005710_PyEval_GetBuiltins(PyThreadState *tstate)
5711{
5712 PyFrameObject *frame = tstate->frame;
5713 if (frame != NULL) {
5714 return frame->f_builtins;
5715 }
5716 return tstate->interp->builtins;
5717}
5718
5719PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005720PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005721{
Victor Stinner438a12d2019-05-24 17:01:38 +02005722 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005723 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005724}
5725
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005726/* Convenience function to get a builtin from its name */
5727PyObject *
5728_PyEval_GetBuiltinId(_Py_Identifier *name)
5729{
Victor Stinner438a12d2019-05-24 17:01:38 +02005730 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005731 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5732 if (attr) {
5733 Py_INCREF(attr);
5734 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005735 else if (!_PyErr_Occurred(tstate)) {
5736 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005737 }
5738 return attr;
5739}
5740
Guido van Rossumb209a111997-04-29 18:18:01 +00005741PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005742PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005743{
Victor Stinner438a12d2019-05-24 17:01:38 +02005744 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005745 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005746 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005747 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005748 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005749 }
5750
Victor Stinner438a12d2019-05-24 17:01:38 +02005751 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005752 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005753 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005754
5755 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005756 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005757}
5758
Guido van Rossumb209a111997-04-29 18:18:01 +00005759PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005760PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005761{
Victor Stinner438a12d2019-05-24 17:01:38 +02005762 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005763 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005764 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005765 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005766 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005767
5768 assert(current_frame->f_globals != NULL);
5769 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005770}
5771
Guido van Rossum6135a871995-01-09 17:53:26 +00005772int
Tim Peters5ba58662001-07-16 02:29:45 +00005773PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005774{
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005776 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005777 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005779 if (current_frame != NULL) {
5780 const int codeflags = current_frame->f_code->co_flags;
5781 const int compilerflags = codeflags & PyCF_MASK;
5782 if (compilerflags) {
5783 result = 1;
5784 cf->cf_flags |= compilerflags;
5785 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005786#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005787 if (codeflags & CO_GENERATOR_ALLOWED) {
5788 result = 1;
5789 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5790 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005791#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005792 }
5793 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005794}
5795
Guido van Rossum3f5da241990-12-20 15:06:42 +00005796
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005797const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005798PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005800 if (PyMethod_Check(func))
5801 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5802 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005803 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005804 else if (PyCFunction_Check(func))
5805 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5806 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005807 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005808}
5809
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005810const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005811PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005813 if (PyMethod_Check(func))
5814 return "()";
5815 else if (PyFunction_Check(func))
5816 return "()";
5817 else if (PyCFunction_Check(func))
5818 return "()";
5819 else
5820 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005821}
5822
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005823#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005824if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005825 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005826 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005827 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005828 x = NULL; \
5829 } \
5830 else { \
5831 x = call; \
5832 if (tstate->c_profilefunc != NULL) { \
5833 if (x == NULL) { \
5834 call_trace_protected(tstate->c_profilefunc, \
5835 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005836 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005837 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005838 /* XXX should pass (type, value, tb) */ \
5839 } else { \
5840 if (call_trace(tstate->c_profilefunc, \
5841 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005842 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005843 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005844 Py_DECREF(x); \
5845 x = NULL; \
5846 } \
5847 } \
5848 } \
5849 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005850} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005851 x = call; \
5852 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005853
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005854
5855static PyObject *
5856trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005857 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005858 PyObject *func,
5859 PyObject **args, Py_ssize_t nargs,
5860 PyObject *kwnames)
5861{
5862 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005863 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005864 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005865 return x;
5866 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005867 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005868 /* We need to create a temporary bound method as argument
5869 for profiling.
5870
5871 If nargs == 0, then this cannot work because we have no
5872 "self". In any case, the call itself would raise
5873 TypeError (foo needs an argument), so we just skip
5874 profiling. */
5875 PyObject *self = args[0];
5876 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5877 if (func == NULL) {
5878 return NULL;
5879 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005880 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005881 args+1, nargs-1,
5882 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005883 Py_DECREF(func);
5884 return x;
5885 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005886 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005887}
5888
Victor Stinner415c5102017-01-11 00:54:57 +01005889/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5890 to reduce the stack consumption. */
5891Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005892call_function(PyThreadState *tstate,
5893 PyCodeAddressRange *bounds,
5894 PyObject ***pp_stack,
5895 Py_ssize_t oparg,
5896 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005897{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005898 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005899 PyObject *func = *pfunc;
5900 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005901 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5902 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005903 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005904
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005905 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005906 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005907 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005908 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005909 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005910 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005911
Victor Stinner438a12d2019-05-24 17:01:38 +02005912 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005913
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005914 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005915 while ((*pp_stack) > pfunc) {
5916 w = EXT_POP(*pp_stack);
5917 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005918 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005920 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005921}
5922
Jeremy Hylton52820442001-01-03 23:52:36 +00005923static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005924do_call_core(PyThreadState *tstate,
5925 PyCodeAddressRange *bounds,
5926 PyObject *func,
5927 PyObject *callargs,
5928 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005929{
jdemeyere89de732018-09-19 12:06:20 +02005930 PyObject *result;
5931
scoder4c9ea092020-05-12 16:12:41 +02005932 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005933 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005934 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005935 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005936 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005937 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5938 if (nargs > 0 && tstate->use_tracing) {
5939 /* We need to create a temporary bound method as argument
5940 for profiling.
5941
5942 If nargs == 0, then this cannot work because we have no
5943 "self". In any case, the call itself would raise
5944 TypeError (foo needs an argument), so we just skip
5945 profiling. */
5946 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5947 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5948 if (func == NULL) {
5949 return NULL;
5950 }
5951
Victor Stinner4d231bc2019-11-14 13:36:21 +01005952 C_TRACE(result, _PyObject_FastCallDictTstate(
5953 tstate, func,
5954 &_PyTuple_ITEMS(callargs)[1],
5955 nargs - 1,
5956 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005957 Py_DECREF(func);
5958 return result;
5959 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005960 }
jdemeyere89de732018-09-19 12:06:20 +02005961 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005962}
5963
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005964/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005965 nb_index slot defined, and store in *pi.
5966 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005967 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005968 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005969*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005970int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005971_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005972{
Victor Stinner438a12d2019-05-24 17:01:38 +02005973 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005974 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005975 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005976 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005977 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005978 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 return 0;
5980 }
5981 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005982 _PyErr_SetString(tstate, PyExc_TypeError,
5983 "slice indices must be integers or "
5984 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005985 return 0;
5986 }
5987 *pi = x;
5988 }
5989 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005990}
5991
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005992int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005993_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005994{
Victor Stinner438a12d2019-05-24 17:01:38 +02005995 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005996 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005997 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005998 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005999 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006000 return 0;
6001 }
6002 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006003 _PyErr_SetString(tstate, PyExc_TypeError,
6004 "slice indices must be integers or "
6005 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006006 return 0;
6007 }
6008 *pi = x;
6009 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006010}
6011
Thomas Wouters52152252000-08-17 22:55:00 +00006012static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006013import_name(PyThreadState *tstate, PyFrameObject *f,
6014 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006015{
6016 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006017 PyObject *import_func, *res;
6018 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006019
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006020 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006021 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006022 if (!_PyErr_Occurred(tstate)) {
6023 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006024 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006025 return NULL;
6026 }
6027
6028 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006029 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006030 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006031 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006032 return NULL;
6033 }
6034 res = PyImport_ImportModuleLevelObject(
6035 name,
6036 f->f_globals,
6037 f->f_locals == NULL ? Py_None : f->f_locals,
6038 fromlist,
6039 ilevel);
6040 return res;
6041 }
6042
6043 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006044
6045 stack[0] = name;
6046 stack[1] = f->f_globals;
6047 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6048 stack[3] = fromlist;
6049 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006050 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006051 Py_DECREF(import_func);
6052 return res;
6053}
6054
6055static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006056import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006058 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006059 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006060
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006061 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006062 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006063 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006064 /* Issue #17636: in case this failed because of a circular relative
6065 import, try to fallback on reading the module directly from
6066 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006067 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006068 if (pkgname == NULL) {
6069 goto error;
6070 }
Oren Milman6db70332017-09-19 14:23:01 +03006071 if (!PyUnicode_Check(pkgname)) {
6072 Py_CLEAR(pkgname);
6073 goto error;
6074 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006075 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006076 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006077 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006078 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006079 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006080 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006081 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006082 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006083 goto error;
6084 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006085 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006086 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006087 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006088 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006089 if (pkgname == NULL) {
6090 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6091 if (pkgname_or_unknown == NULL) {
6092 Py_XDECREF(pkgpath);
6093 return NULL;
6094 }
6095 } else {
6096 pkgname_or_unknown = pkgname;
6097 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006098
6099 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006100 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006101 errmsg = PyUnicode_FromFormat(
6102 "cannot import name %R from %R (unknown location)",
6103 name, pkgname_or_unknown
6104 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006105 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006106 PyErr_SetImportError(errmsg, pkgname, NULL);
6107 }
6108 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006109 _Py_IDENTIFIER(__spec__);
6110 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006111 const char *fmt =
6112 _PyModuleSpec_IsInitializing(spec) ?
6113 "cannot import name %R from partially initialized module %R "
6114 "(most likely due to a circular import) (%S)" :
6115 "cannot import name %R from %R (%S)";
6116 Py_XDECREF(spec);
6117
6118 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006119 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006120 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006121 }
6122
Xiang Zhang4830f582017-03-21 11:13:42 +08006123 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006124 Py_XDECREF(pkgname_or_unknown);
6125 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006126 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006127}
Guido van Rossumac7be682001-01-17 15:42:30 +00006128
Thomas Wouters52152252000-08-17 22:55:00 +00006129static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006130import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006131{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006132 _Py_IDENTIFIER(__all__);
6133 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006134 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006135 int skip_leading_underscores = 0;
6136 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006137
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006138 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6139 return -1; /* Unexpected error */
6140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006142 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6143 return -1;
6144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006145 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006146 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006147 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006148 return -1;
6149 }
6150 all = PyMapping_Keys(dict);
6151 Py_DECREF(dict);
6152 if (all == NULL)
6153 return -1;
6154 skip_leading_underscores = 1;
6155 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006157 for (pos = 0, err = 0; ; pos++) {
6158 name = PySequence_GetItem(all, pos);
6159 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006160 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006161 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006162 }
6163 else {
6164 _PyErr_Clear(tstate);
6165 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006166 break;
6167 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006168 if (!PyUnicode_Check(name)) {
6169 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6170 if (modname == NULL) {
6171 Py_DECREF(name);
6172 err = -1;
6173 break;
6174 }
6175 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006176 _PyErr_Format(tstate, PyExc_TypeError,
6177 "module __name__ must be a string, not %.100s",
6178 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006179 }
6180 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006181 _PyErr_Format(tstate, PyExc_TypeError,
6182 "%s in %U.%s must be str, not %.100s",
6183 skip_leading_underscores ? "Key" : "Item",
6184 modname,
6185 skip_leading_underscores ? "__dict__" : "__all__",
6186 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006187 }
6188 Py_DECREF(modname);
6189 Py_DECREF(name);
6190 err = -1;
6191 break;
6192 }
6193 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006194 if (PyUnicode_READY(name) == -1) {
6195 Py_DECREF(name);
6196 err = -1;
6197 break;
6198 }
6199 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6200 Py_DECREF(name);
6201 continue;
6202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006203 }
6204 value = PyObject_GetAttr(v, name);
6205 if (value == NULL)
6206 err = -1;
6207 else if (PyDict_CheckExact(locals))
6208 err = PyDict_SetItem(locals, name, value);
6209 else
6210 err = PyObject_SetItem(locals, name, value);
6211 Py_DECREF(name);
6212 Py_XDECREF(value);
6213 if (err != 0)
6214 break;
6215 }
6216 Py_DECREF(all);
6217 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006218}
6219
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006220static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006221check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006222{
Victor Stinnera102ed72020-02-07 02:24:48 +01006223 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006224 /* check_args_iterable() may be called with a live exception:
6225 * clear it to prevent calling _PyObject_FunctionStr() with an
6226 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006227 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006228 PyObject *funcstr = _PyObject_FunctionStr(func);
6229 if (funcstr != NULL) {
6230 _PyErr_Format(tstate, PyExc_TypeError,
6231 "%U argument after * must be an iterable, not %.200s",
6232 funcstr, Py_TYPE(args)->tp_name);
6233 Py_DECREF(funcstr);
6234 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006235 return -1;
6236 }
6237 return 0;
6238}
6239
6240static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006241format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006242{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006243 /* _PyDict_MergeEx raises attribute
6244 * error (percolated from an attempt
6245 * to get 'keys' attribute) instead of
6246 * a type error if its second argument
6247 * is not a mapping.
6248 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006249 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006250 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006251 PyObject *funcstr = _PyObject_FunctionStr(func);
6252 if (funcstr != NULL) {
6253 _PyErr_Format(
6254 tstate, PyExc_TypeError,
6255 "%U argument after ** must be a mapping, not %.200s",
6256 funcstr, Py_TYPE(kwargs)->tp_name);
6257 Py_DECREF(funcstr);
6258 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006259 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006260 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006261 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006262 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006263 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006264 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006265 PyObject *funcstr = _PyObject_FunctionStr(func);
6266 if (funcstr != NULL) {
6267 PyObject *key = PyTuple_GET_ITEM(val, 0);
6268 _PyErr_Format(
6269 tstate, PyExc_TypeError,
6270 "%U got multiple values for keyword argument '%S'",
6271 funcstr, key);
6272 Py_DECREF(funcstr);
6273 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006274 Py_XDECREF(exc);
6275 Py_XDECREF(val);
6276 Py_XDECREF(tb);
6277 }
6278 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006279 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006280 }
6281 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006282}
6283
Guido van Rossumac7be682001-01-17 15:42:30 +00006284static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006285format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6286 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006290 if (!obj)
6291 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006292
Serhiy Storchaka06515832016-11-20 09:13:07 +02006293 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006294 if (!obj_str)
6295 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006296
Victor Stinner438a12d2019-05-24 17:01:38 +02006297 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006298}
Guido van Rossum950361c1997-01-24 13:49:28 +00006299
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006300static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006301format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006302{
6303 PyObject *name;
6304 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006305 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006306 return;
6307 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6308 name = PyTuple_GET_ITEM(co->co_cellvars,
6309 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006310 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006311 PyExc_UnboundLocalError,
6312 UNBOUNDLOCAL_ERROR_MSG,
6313 name);
6314 } else {
6315 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6316 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006317 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006318 UNBOUNDFREE_ERROR_MSG, name);
6319 }
6320}
6321
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006322static void
Mark Shannonfee55262019-11-21 09:11:43 +00006323format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006324{
6325 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6326 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006327 _PyErr_Format(tstate, PyExc_TypeError,
6328 "'async with' received an object from __aenter__ "
6329 "that does not implement __await__: %.100s",
6330 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006331 }
Mark Shannonfee55262019-11-21 09:11:43 +00006332 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006333 _PyErr_Format(tstate, PyExc_TypeError,
6334 "'async with' received an object from __aexit__ "
6335 "that does not implement __await__: %.100s",
6336 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006337 }
6338 }
6339}
6340
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006341static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006342unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006343 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006344{
6345 PyObject *res;
6346 if (Py_REFCNT(v) == 2) {
6347 /* In the common case, there are 2 references to the value
6348 * stored in 'variable' when the += is performed: one on the
6349 * value stack (in 'v') and one still stored in the
6350 * 'variable'. We try to delete the variable now to reduce
6351 * the refcnt to 1.
6352 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006353 int opcode, oparg;
6354 NEXTOPARG();
6355 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006356 case STORE_FAST:
6357 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006358 PyObject **fastlocals = f->f_localsplus;
6359 if (GETLOCAL(oparg) == v)
6360 SETLOCAL(oparg, NULL);
6361 break;
6362 }
6363 case STORE_DEREF:
6364 {
6365 PyObject **freevars = (f->f_localsplus +
6366 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006367 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006368 if (PyCell_GET(c) == v) {
6369 PyCell_SET(c, NULL);
6370 Py_DECREF(v);
6371 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006372 break;
6373 }
6374 case STORE_NAME:
6375 {
6376 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006377 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006378 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006379 if (locals && PyDict_CheckExact(locals)) {
6380 PyObject *w = PyDict_GetItemWithError(locals, name);
6381 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006382 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006383 {
6384 Py_DECREF(v);
6385 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006386 }
6387 }
6388 break;
6389 }
6390 }
6391 }
6392 res = v;
6393 PyUnicode_Append(&res, w);
6394 return res;
6395}
6396
Guido van Rossum950361c1997-01-24 13:49:28 +00006397#ifdef DYNAMIC_EXECUTION_PROFILE
6398
Skip Montanarof118cb12001-10-15 20:51:38 +00006399static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006400getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006402 int i;
6403 PyObject *l = PyList_New(256);
6404 if (l == NULL) return NULL;
6405 for (i = 0; i < 256; i++) {
6406 PyObject *x = PyLong_FromLong(a[i]);
6407 if (x == NULL) {
6408 Py_DECREF(l);
6409 return NULL;
6410 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006411 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006412 }
6413 for (i = 0; i < 256; i++)
6414 a[i] = 0;
6415 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006416}
6417
6418PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006419_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006420{
6421#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006422 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006423#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006424 int i;
6425 PyObject *l = PyList_New(257);
6426 if (l == NULL) return NULL;
6427 for (i = 0; i < 257; i++) {
6428 PyObject *x = getarray(dxpairs[i]);
6429 if (x == NULL) {
6430 Py_DECREF(l);
6431 return NULL;
6432 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006433 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006434 }
6435 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006436#endif
6437}
6438
6439#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006440
6441Py_ssize_t
6442_PyEval_RequestCodeExtraIndex(freefunc free)
6443{
Victor Stinner81a7be32020-04-14 15:14:01 +02006444 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006445 Py_ssize_t new_index;
6446
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006447 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006448 return -1;
6449 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006450 new_index = interp->co_extra_user_count++;
6451 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006452 return new_index;
6453}
Łukasz Langaa785c872016-09-09 17:37:37 -07006454
6455static void
6456dtrace_function_entry(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);
6465 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006466
Andy Lestere6be9b52020-02-11 20:28:35 -06006467 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006468}
6469
6470static void
6471dtrace_function_return(PyFrameObject *f)
6472{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006473 const char *filename;
6474 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006475 int lineno;
6476
Victor Stinner6d86a232020-04-29 00:56:58 +02006477 PyCodeObject *code = f->f_code;
6478 filename = PyUnicode_AsUTF8(code->co_filename);
6479 funcname = PyUnicode_AsUTF8(code->co_name);
6480 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006481
Andy Lestere6be9b52020-02-11 20:28:35 -06006482 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006483}
6484
6485/* DTrace equivalent of maybe_call_line_trace. */
6486static void
6487maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00006488 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006489{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006490 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006491
6492 /* If the last instruction executed isn't in the current
6493 instruction window, reset the window.
6494 */
Mark Shannon877df852020-11-12 09:43:29 +00006495 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006496 /* If the last instruction falls at the start of a line or if
6497 it represents a jump backwards, update the frame's line
6498 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00006499 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
6500 if (line != -1) {
6501 frame->f_lineno = line;
6502 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6503 if (!co_filename)
6504 co_filename = "?";
6505 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6506 if (!co_name)
6507 co_name = "?";
6508 PyDTrace_LINE(co_filename, co_name, line);
6509 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006510 }
6511 *instr_prev = frame->f_lasti;
6512}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006513
6514
6515/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6516 for the limited API. */
6517
6518#undef Py_EnterRecursiveCall
6519
6520int Py_EnterRecursiveCall(const char *where)
6521{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006522 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006523}
6524
6525#undef Py_LeaveRecursiveCall
6526
6527void Py_LeaveRecursiveCall(void)
6528{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006529 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006530}