blob: abdea1de09b43cfdc73f56a1a242be78eebc267b [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 */
db3l131d5512021-03-03 22:09:48 -050010/* affects both release and debug builds - see bpo-43271 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011#define PY_LOCAL_AGGRESSIVE
12
Guido van Rossumb209a111997-04-29 18:18:01 +000013#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020014#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020015#include "pycore_call.h" // _PyObject_FastCallDictTstate()
16#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
17#include "pycore_code.h" // _PyCode_InitOpcache()
18#include "pycore_initconfig.h" // _PyStatus_OK()
19#include "pycore_object.h" // _PyObject_GC_TRACK()
20#include "pycore_pyerrors.h" // _PyErr_Fetch()
21#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020022#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
23#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020024#include "pycore_sysmodule.h" // _PySys_Audit()
25#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040028#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070031#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040032#include "setobject.h"
Guido van Rossum5c5a9382021-01-29 18:02:29 -080033#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034
Guido van Rossumc6004111993-11-05 10:22:19 +000035#include <ctype.h>
36
Guido van Rossum408027e1996-12-30 16:17:54 +000037#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000038/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039#define LLTRACE 1 /* Low-level trace feature */
40#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000041#endif
42
Victor Stinner5c75f372019-04-17 23:02:26 +020043#if !defined(Py_BUILD_CORE)
44# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
45#endif
46
Hai Shi46874c22020-01-30 17:20:25 -060047_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000048
Guido van Rossum374a9221991-04-04 10:40:29 +000049/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020050Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon86433452021-01-07 16:49:02 +000051 PyThreadState *tstate, PyCodeAddressRange *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020052 Py_ssize_t oparg, PyObject *kwnames);
53static PyObject * do_call_core(
Mark Shannon86433452021-01-07 16:49:02 +000054 PyThreadState *tstate, PyCodeAddressRange *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020055 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000056
Guido van Rossum0a066c01992-03-27 17:29:15 +000057#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000058static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020059static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000060#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061static int call_trace(Py_tracefunc, PyObject *,
62 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000063 PyCodeAddressRange *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000065static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010066 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000067 PyCodeAddressRange *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010068 int, PyObject *);
69static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000070 PyThreadState *, PyFrameObject *,
71 PyCodeAddressRange *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000072static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060073 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000074 PyCodeAddressRange *, int *);
75static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070076static void dtrace_function_entry(PyFrameObject *);
77static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000078
Victor Stinner438a12d2019-05-24 17:01:38 +020079static PyObject * import_name(PyThreadState *, PyFrameObject *,
80 PyObject *, PyObject *, PyObject *);
81static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
82static int import_all_from(PyThreadState *, PyObject *, PyObject *);
83static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
84static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
85static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030086 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020087static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
88static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
89static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000090static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000091
Paul Prescode68140d2000-08-30 20:25:01 +000092#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000094#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000096#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 "free variable '%.200s' referenced before assignment" \
98 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000099
Guido van Rossum950361c1997-01-24 13:49:28 +0000100/* Dynamic execution profile */
101#ifdef DYNAMIC_EXECUTION_PROFILE
102#ifdef DXPAIRS
103static long dxpairs[257][256];
104#define dxp dxpairs[256]
105#else
106static long dxp[256];
107#endif
108#endif
109
Inada Naoki91234a12019-06-03 21:30:58 +0900110/* per opcode cache */
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000111static int opcache_min_runs = 1024; /* create opcache when code executed this many times */
Pablo Galindo109826c2020-10-20 06:22:44 +0100112#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900113#define OPCACHE_STATS 0 /* Enable stats */
114
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000115// This function allows to deactivate the opcode cache. As different cache mechanisms may hold
116// references, this can mess with the reference leak detector functionality so the cache needs
117// to be deactivated in such scenarios to avoid false positives. See bpo-3714 for more information.
118void
119_PyEval_DeactivateOpCache(void)
120{
121 opcache_min_runs = 0;
122}
123
Inada Naoki91234a12019-06-03 21:30:58 +0900124#if OPCACHE_STATS
125static size_t opcache_code_objects = 0;
126static size_t opcache_code_objects_extra_mem = 0;
127
128static size_t opcache_global_opts = 0;
129static size_t opcache_global_hits = 0;
130static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100131
132static size_t opcache_attr_opts = 0;
133static size_t opcache_attr_hits = 0;
134static size_t opcache_attr_misses = 0;
135static size_t opcache_attr_deopts = 0;
136static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900137#endif
138
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100139
Victor Stinnerda2914d2020-03-20 09:29:08 +0100140#ifndef NDEBUG
141/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
142 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
143 when a thread continues to run after Python finalization, especially
144 daemon threads. */
145static int
146is_tstate_valid(PyThreadState *tstate)
147{
148 assert(!_PyMem_IsPtrFreed(tstate));
149 assert(!_PyMem_IsPtrFreed(tstate->interp));
150 return 1;
151}
152#endif
153
154
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000155/* This can set eval_breaker to 0 even though gil_drop_request became
156 1. We believe this is all right because the eval loop will release
157 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100158static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200159COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 struct _ceval_runtime_state *ceval,
161 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100162{
Victor Stinner299b8c62020-05-05 17:40:18 +0200163 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
164 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200165 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200166 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200167 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100168 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200169 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000171
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000172
Victor Stinnerda2914d2020-03-20 09:29:08 +0100173static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200174SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175{
Victor Stinner299b8c62020-05-05 17:40:18 +0200176 struct _ceval_state *ceval2 = &interp->ceval;
177 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
178 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100179}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000180
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000181
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200183RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100184{
Victor Stinner299b8c62020-05-05 17:40:18 +0200185 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
186 struct _ceval_state *ceval2 = &interp->ceval;
187 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
188 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000190
Eric Snowfdf282d2019-01-11 14:26:55 -0700191
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200193SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100194{
Victor Stinner299b8c62020-05-05 17:40:18 +0200195 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
196 struct _ceval_state *ceval2 = &interp->ceval;
197 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
198 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100199}
Eric Snowfdf282d2019-01-11 14:26:55 -0700200
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000201
Victor Stinnerda2914d2020-03-20 09:29:08 +0100202static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200203UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100204{
Victor Stinner299b8c62020-05-05 17:40:18 +0200205 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
206 struct _ceval_state *ceval2 = &interp->ceval;
207 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
208 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100209}
210
211
212static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100213SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100214{
Victor Stinner299b8c62020-05-05 17:40:18 +0200215 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
216 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200217 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100218 if (force) {
219 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
220 }
221 else {
222 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
223 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
224 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100225}
226
227
228static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200229UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100230{
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
232 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200233 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200234 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235}
236
237
238static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200239SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100240{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200241 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100242 ceval2->pending.async_exc = 1;
243 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
244}
245
246
247static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200248UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100249{
Victor Stinner299b8c62020-05-05 17:40:18 +0200250 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
251 struct _ceval_state *ceval2 = &interp->ceval;
252 ceval2->pending.async_exc = 0;
253 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100254}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
256
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000258#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000259#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261
Victor Stinner3026cad2020-06-01 16:02:40 +0200262void _Py_NO_RETURN
263_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100264{
Victor Stinner3026cad2020-06-01 16:02:40 +0200265 _Py_FatalErrorFunc(func,
266 "the function must be called with the GIL held, "
267 "but the GIL is released "
268 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100269}
270
Victor Stinner7be4e352020-05-05 20:27:47 +0200271#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
272int
273_PyEval_ThreadsInitialized(PyInterpreterState *interp)
274{
275 return gil_created(&interp->ceval.gil);
276}
277
278int
279PyEval_ThreadsInitialized(void)
280{
281 // Fatal error if there is no current interpreter
282 PyInterpreterState *interp = PyInterpreterState_Get();
283 return _PyEval_ThreadsInitialized(interp);
284}
285#else
Tim Peters7f468f22004-10-11 02:40:51 +0000286int
Victor Stinner175a7042020-03-10 00:37:48 +0100287_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
288{
289 return gil_created(&runtime->ceval.gil);
290}
291
292int
Tim Peters7f468f22004-10-11 02:40:51 +0000293PyEval_ThreadsInitialized(void)
294{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100295 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100296 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000297}
Victor Stinner7be4e352020-05-05 20:27:47 +0200298#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000299
Victor Stinner111e4ee2020-03-09 21:24:14 +0100300PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200301_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302{
Victor Stinner7be4e352020-05-05 20:27:47 +0200303#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100304 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200305 /* Currently, the GIL is shared by all interpreters,
306 and only the main interpreter is responsible to create
307 and destroy it. */
308 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100309 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200310#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100311
Victor Stinner7be4e352020-05-05 20:27:47 +0200312#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
313 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
314#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200315 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200316#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200317 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100318
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200319 PyThread_init_thread();
320 create_gil(gil);
321
322 take_gil(tstate);
323
324 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100325 return _PyStatus_OK();
326}
327
328void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100329_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200330{
Victor Stinner7be4e352020-05-05 20:27:47 +0200331#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100332 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200333 /* Currently, the GIL is shared by all interpreters,
334 and only the main interpreter is responsible to create
335 and destroy it. */
336 return;
337 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200338#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200339
Victor Stinner7be4e352020-05-05 20:27:47 +0200340#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100341 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200342#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100343 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200344#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200345 if (!gil_created(gil)) {
346 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
347 yet: do nothing. */
348 return;
349 }
350
351 destroy_gil(gil);
352 assert(!gil_created(gil));
353}
354
355void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100356PyEval_InitThreads(void)
357{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100358 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000359}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000360
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000361void
Inada Naoki91234a12019-06-03 21:30:58 +0900362_PyEval_Fini(void)
363{
364#if OPCACHE_STATS
365 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
366 opcache_code_objects);
367
368 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
369 opcache_code_objects_extra_mem);
370
371 fprintf(stderr, "\n");
372
373 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
374 opcache_global_hits,
375 (int) (100.0 * opcache_global_hits /
376 (opcache_global_hits + opcache_global_misses)));
377
378 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
379 opcache_global_misses,
380 (int) (100.0 * opcache_global_misses /
381 (opcache_global_hits + opcache_global_misses)));
382
383 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
384 opcache_global_opts);
385
386 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100387
388 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
389 opcache_attr_hits,
390 (int) (100.0 * opcache_attr_hits /
391 opcache_attr_total));
392
393 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
394 opcache_attr_misses,
395 (int) (100.0 * opcache_attr_misses /
396 opcache_attr_total));
397
398 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
399 opcache_attr_opts);
400
401 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
402 opcache_attr_deopts);
403
404 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
405 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900406#endif
407}
408
409void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000410PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000411{
Victor Stinner09532fe2019-05-10 23:39:09 +0200412 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200413 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200414 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100415
Victor Stinner85f5a692020-03-09 22:12:04 +0100416 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000417}
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000421{
Victor Stinner09532fe2019-05-10 23:39:09 +0200422 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200423 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100425 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100426 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200427 struct _ceval_runtime_state *ceval = &runtime->ceval;
428 struct _ceval_state *ceval2 = &tstate->interp->ceval;
429 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430}
431
432void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100433_PyEval_ReleaseLock(PyThreadState *tstate)
434{
435 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200436 struct _ceval_state *ceval2 = &tstate->interp->ceval;
437 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100438}
439
440void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000442{
Victor Stinner3026cad2020-06-01 16:02:40 +0200443 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100444
Victor Stinner85f5a692020-03-09 22:12:04 +0100445 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200446
Victor Stinner85f5a692020-03-09 22:12:04 +0100447 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200448#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
449 (void)_PyThreadState_Swap(gilstate, tstate);
450#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100451 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100452 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200453 }
Victor Stinnere838a932020-05-05 19:56:48 +0200454#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000455}
456
457void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000458PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000459{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100460 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200461
Victor Stinner01b1cc12019-11-20 02:27:56 +0100462 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200463 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
464 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100465 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200466 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200467 struct _ceval_runtime_state *ceval = &runtime->ceval;
468 struct _ceval_state *ceval2 = &tstate->interp->ceval;
469 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000470}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000471
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900472#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200473/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200474 which are not running in the child process, and clear internal locks
475 which might be held by those threads. */
476PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200477_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000478{
Victor Stinner317bab02020-06-02 18:44:54 +0200479 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200480
481#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
482 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
483#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100484 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200485#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100486 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200487 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200488 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100489 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100490
491 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700492
Victor Stinner50e6e992020-03-19 02:41:21 +0100493 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900494 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200495 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700496 }
Jesse Nollera8513972008-07-17 16:49:17 +0000497
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200498 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100499 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200500 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000501}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900502#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000504/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600505 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000506
507void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100508_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000509{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100510 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000511}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000513PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000514PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000515{
Victor Stinner09532fe2019-05-10 23:39:09 +0200516 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200517#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
518 PyThreadState *old_tstate = _PyThreadState_GET();
519 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
520#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200521 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200522#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200523 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100524
Victor Stinner0b1e3302020-05-05 16:14:31 +0200525 struct _ceval_runtime_state *ceval = &runtime->ceval;
526 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200527#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
528 assert(gil_created(&ceval2->gil));
529#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200530 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200531#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200532 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000534}
535
536void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000537PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000538{
Victor Stinner3026cad2020-06-01 16:02:40 +0200539 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100540
Victor Stinner85f5a692020-03-09 22:12:04 +0100541 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100542
Victor Stinner85f5a692020-03-09 22:12:04 +0100543 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
544 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000545}
546
547
Guido van Rossuma9672091994-09-14 13:31:22 +0000548/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
549 signal handlers or Mac I/O completion routines) can schedule calls
550 to a function to be called synchronously.
551 The synchronous function is called with one void* argument.
552 It should return 0 for success or -1 for failure -- failure should
553 be accompanied by an exception.
554
555 If registry succeeds, the registry function returns 0; if it fails
556 (e.g. due to too many pending calls) it returns -1 (without setting
557 an exception condition).
558
559 Note that because registry may occur from within signal handlers,
560 or other asynchronous events, calling malloc() is unsafe!
561
Guido van Rossuma9672091994-09-14 13:31:22 +0000562 Any thread can schedule pending calls, but only the main thread
563 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000564 There is no facility to schedule calls to a particular thread, but
565 that should be easy to change, should that ever be required. In
566 that case, the static variables here should go into the python
567 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000568*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000569
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200570void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200571_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200572{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100573#ifdef MS_WINDOWS
574 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
575 // handler which can run in a thread different than the Python thread, in
576 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
577 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
578 //
579 // The next eval_frame_handle_pending() call will call
580 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
581 int force = 1;
582#else
583 int force = 0;
584#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200585 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100586 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200587 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100588 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200589}
590
Eric Snow5be45a62019-03-08 22:47:07 -0700591/* Push one item onto the queue while holding the lock. */
592static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200593_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600594 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700595{
Eric Snow842a2f02019-03-15 15:47:51 -0600596 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700597 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600598 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700599 return -1; /* Queue full */
600 }
Eric Snow842a2f02019-03-15 15:47:51 -0600601 pending->calls[i].func = func;
602 pending->calls[i].arg = arg;
603 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700604 return 0;
605}
606
607/* Pop one item off the queue while holding the lock. */
608static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200609_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600610 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700611{
Eric Snow842a2f02019-03-15 15:47:51 -0600612 int i = pending->first;
613 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700614 return; /* Queue empty */
615 }
616
Eric Snow842a2f02019-03-15 15:47:51 -0600617 *func = pending->calls[i].func;
618 *arg = pending->calls[i].arg;
619 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700620}
621
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200622/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000623 scheduling to be made from any thread, and even from an executing
624 callback.
625 */
626
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000627int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200628_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200629 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000630{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200631 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600632
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200633 /* Ensure that _PyEval_InitPendingCalls() was called
634 and that _PyEval_FiniPendingCalls() is not called yet. */
635 assert(pending->lock != NULL);
636
Eric Snow842a2f02019-03-15 15:47:51 -0600637 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200638 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600639 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700640
Victor Stinnere225beb2019-06-03 18:14:24 +0200641 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200642 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000644}
645
Victor Stinner09532fe2019-05-10 23:39:09 +0200646int
647Py_AddPendingCall(int (*func)(void *), void *arg)
648{
Victor Stinner50e6e992020-03-19 02:41:21 +0100649 /* Best-effort to support subinterpreters and calls with the GIL released.
650
651 First attempt _PyThreadState_GET() since it supports subinterpreters.
652
653 If the GIL is released, _PyThreadState_GET() returns NULL . In this
654 case, use PyGILState_GetThisThreadState() which works even if the GIL
655 is released.
656
657 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
658 see bpo-10915 and bpo-15751.
659
Victor Stinner8849e592020-03-18 19:28:53 +0100660 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100661 PyThreadState *tstate = _PyThreadState_GET();
662 if (tstate == NULL) {
663 tstate = PyGILState_GetThisThreadState();
664 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200665
666 PyInterpreterState *interp;
667 if (tstate != NULL) {
668 interp = tstate->interp;
669 }
670 else {
671 /* Last resort: use the main interpreter */
672 interp = _PyRuntime.interpreters.main;
673 }
674 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200675}
676
Eric Snowfdf282d2019-01-11 14:26:55 -0700677static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100678handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700679{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200680 assert(is_tstate_valid(tstate));
681 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700682 return 0;
683 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700684
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200685 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100686 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
687 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100688 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700689 return -1;
690 }
691 return 0;
692}
693
694static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100695make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000696{
Victor Stinnerd8316882020-03-20 14:50:35 +0100697 /* only execute pending calls on main thread */
698 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200699 return 0;
700 }
701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100703 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700704 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700706 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200707 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100708
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200709 /* unsignal before starting to call callbacks, so that any callback
710 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100711 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700712 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100715 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700716 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700717 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 void *arg = NULL;
719
720 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600721 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200722 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600723 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700724
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100725 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700726 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100727 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700728 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700729 res = func(arg);
730 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200731 goto error;
732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200734
Charles-François Natalif23339a2011-07-23 18:15:43 +0200735 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700736 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200737
738error:
739 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100740 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700741 return res;
742}
743
Eric Snow842a2f02019-03-15 15:47:51 -0600744void
Victor Stinner2b1df452020-01-13 18:46:59 +0100745_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600746{
Eric Snow842a2f02019-03-15 15:47:51 -0600747 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100748 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600749
Victor Stinner50e6e992020-03-19 02:41:21 +0100750 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200751
Eric Snow842a2f02019-03-15 15:47:51 -0600752 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
753 return;
754 }
755
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100756 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200757 PyObject *exc, *val, *tb;
758 _PyErr_Fetch(tstate, &exc, &val, &tb);
759 PyErr_BadInternalCall();
760 _PyErr_ChainExceptions(exc, val, tb);
761 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600762 }
763}
764
Eric Snowfdf282d2019-01-11 14:26:55 -0700765/* Py_MakePendingCalls() is a simple wrapper for the sake
766 of backward-compatibility. */
767int
768Py_MakePendingCalls(void)
769{
770 assert(PyGILState_Check());
771
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100772 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100773 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100774
Eric Snowfdf282d2019-01-11 14:26:55 -0700775 /* Python signal handler doesn't really queue a callback: it only signals
776 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100777 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700778 if (res != 0) {
779 return res;
780 }
781
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100782 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600783 if (res != 0) {
784 return res;
785 }
786
787 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000788}
789
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000790/* The interpreter's recursion limit */
791
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000792#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200793# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000794#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600795
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600796void
Victor Stinnerdab84232020-03-17 18:56:44 +0100797_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600798{
Victor Stinner7be4e352020-05-05 20:27:47 +0200799#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100800 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200801#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100802}
803
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200804int
Victor Stinnerdab84232020-03-17 18:56:44 +0100805_PyEval_InitState(struct _ceval_state *ceval)
806{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200807 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
808
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200809 struct _pending_calls *pending = &ceval->pending;
810 assert(pending->lock == NULL);
811
812 pending->lock = PyThread_allocate_lock();
813 if (pending->lock == NULL) {
814 return -1;
815 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200816
817#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
818 _gil_initialize(&ceval->gil);
819#endif
820
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200821 return 0;
822}
823
824void
825_PyEval_FiniState(struct _ceval_state *ceval)
826{
827 struct _pending_calls *pending = &ceval->pending;
828 if (pending->lock != NULL) {
829 PyThread_free_lock(pending->lock);
830 pending->lock = NULL;
831 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600832}
833
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000834int
835Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000836{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200837 PyInterpreterState *interp = _PyInterpreterState_GET();
838 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000839}
840
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000841void
842Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000843{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200844 PyThreadState *tstate = _PyThreadState_GET();
845 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000846}
847
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100848/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200849 if the recursion_depth reaches recursion_limit.
850 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000851 to guarantee that _Py_CheckRecursiveCall() is regularly called.
852 Without USE_STACKCHECK, there is no need for this. */
853int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100854_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000855{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200856 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000857
858#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700859 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (PyOS_CheckStack()) {
861 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200862 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 return -1;
864 }
pdox18967932017-10-25 23:03:01 -0700865#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000866 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (tstate->recursion_depth > recursion_limit + 50) {
868 /* Overflowing while handling an overflow. Give up. */
869 Py_FatalError("Cannot recover from stack overflow.");
870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000872 else {
873 if (tstate->recursion_depth > recursion_limit) {
874 tstate->recursion_headroom++;
875 _PyErr_Format(tstate, PyExc_RecursionError,
876 "maximum recursion depth exceeded%s",
877 where);
878 tstate->recursion_headroom--;
879 --tstate->recursion_depth;
880 return -1;
881 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 }
883 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000884}
885
Brandt Bucher145bf262021-02-26 14:51:55 -0800886
887// PEP 634: Structural Pattern Matching
888
889
890// Return a tuple of values corresponding to keys, with error checks for
891// duplicate/missing keys.
892static PyObject*
893match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
894{
895 assert(PyTuple_CheckExact(keys));
896 Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
897 if (!nkeys) {
898 // No keys means no items.
899 return PyTuple_New(0);
900 }
901 PyObject *seen = NULL;
902 PyObject *dummy = NULL;
903 PyObject *values = NULL;
904 // We use the two argument form of map.get(key, default) for two reasons:
905 // - Atomically check for a key and get its value without error handling.
906 // - Don't cause key creation or resizing in dict subclasses like
907 // collections.defaultdict that define __missing__ (or similar).
908 _Py_IDENTIFIER(get);
909 PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
910 if (get == NULL) {
911 goto fail;
912 }
913 seen = PySet_New(NULL);
914 if (seen == NULL) {
915 goto fail;
916 }
917 // dummy = object()
918 dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
919 if (dummy == NULL) {
920 goto fail;
921 }
922 values = PyList_New(0);
923 if (values == NULL) {
924 goto fail;
925 }
926 for (Py_ssize_t i = 0; i < nkeys; i++) {
927 PyObject *key = PyTuple_GET_ITEM(keys, i);
928 if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
929 if (!_PyErr_Occurred(tstate)) {
930 // Seen it before!
931 _PyErr_Format(tstate, PyExc_ValueError,
932 "mapping pattern checks duplicate key (%R)", key);
933 }
934 goto fail;
935 }
936 PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
937 if (value == NULL) {
938 goto fail;
939 }
940 if (value == dummy) {
941 // key not in map!
942 Py_DECREF(value);
943 Py_DECREF(values);
944 // Return None:
945 Py_INCREF(Py_None);
946 values = Py_None;
947 goto done;
948 }
949 PyList_Append(values, value);
950 Py_DECREF(value);
951 }
952 Py_SETREF(values, PyList_AsTuple(values));
953 // Success:
954done:
955 Py_DECREF(get);
956 Py_DECREF(seen);
957 Py_DECREF(dummy);
958 return values;
959fail:
960 Py_XDECREF(get);
961 Py_XDECREF(seen);
962 Py_XDECREF(dummy);
963 Py_XDECREF(values);
964 return NULL;
965}
966
967// Extract a named attribute from the subject, with additional bookkeeping to
968// raise TypeErrors for repeated lookups. On failure, return NULL (with no
969// error set). Use _PyErr_Occurred(tstate) to disambiguate.
970static PyObject*
971match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
972 PyObject *name, PyObject *seen)
973{
974 assert(PyUnicode_CheckExact(name));
975 assert(PySet_CheckExact(seen));
976 if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
977 if (!_PyErr_Occurred(tstate)) {
978 // Seen it before!
979 _PyErr_Format(tstate, PyExc_TypeError,
980 "%s() got multiple sub-patterns for attribute %R",
981 ((PyTypeObject*)type)->tp_name, name);
982 }
983 return NULL;
984 }
985 PyObject *attr = PyObject_GetAttr(subject, name);
986 if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
987 _PyErr_Clear(tstate);
988 }
989 return attr;
990}
991
992// On success (match), return a tuple of extracted attributes. On failure (no
993// match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
994static PyObject*
995match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
996 Py_ssize_t nargs, PyObject *kwargs)
997{
998 if (!PyType_Check(type)) {
999 const char *e = "called match pattern must be a type";
1000 _PyErr_Format(tstate, PyExc_TypeError, e);
1001 return NULL;
1002 }
1003 assert(PyTuple_CheckExact(kwargs));
1004 // First, an isinstance check:
1005 if (PyObject_IsInstance(subject, type) <= 0) {
1006 return NULL;
1007 }
1008 // So far so good:
1009 PyObject *seen = PySet_New(NULL);
1010 if (seen == NULL) {
1011 return NULL;
1012 }
1013 PyObject *attrs = PyList_New(0);
1014 if (attrs == NULL) {
1015 Py_DECREF(seen);
1016 return NULL;
1017 }
1018 // NOTE: From this point on, goto fail on failure:
1019 PyObject *match_args = NULL;
1020 // First, the positional subpatterns:
1021 if (nargs) {
1022 int match_self = 0;
1023 match_args = PyObject_GetAttrString(type, "__match_args__");
1024 if (match_args) {
1025 if (PyList_CheckExact(match_args)) {
1026 Py_SETREF(match_args, PyList_AsTuple(match_args));
1027 }
1028 if (match_args == NULL) {
1029 goto fail;
1030 }
1031 if (!PyTuple_CheckExact(match_args)) {
1032 const char *e = "%s.__match_args__ must be a list or tuple "
1033 "(got %s)";
1034 _PyErr_Format(tstate, PyExc_TypeError, e,
1035 ((PyTypeObject *)type)->tp_name,
1036 Py_TYPE(match_args)->tp_name);
1037 goto fail;
1038 }
1039 }
1040 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1041 _PyErr_Clear(tstate);
1042 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1043 // define __match_args__. This is natural behavior for subclasses:
1044 // it's as if __match_args__ is some "magic" value that is lost as
1045 // soon as they redefine it.
1046 match_args = PyTuple_New(0);
1047 match_self = PyType_HasFeature((PyTypeObject*)type,
1048 _Py_TPFLAGS_MATCH_SELF);
1049 }
1050 else {
1051 goto fail;
1052 }
1053 assert(PyTuple_CheckExact(match_args));
1054 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1055 if (allowed < nargs) {
1056 const char *plural = (allowed == 1) ? "" : "s";
1057 _PyErr_Format(tstate, PyExc_TypeError,
1058 "%s() accepts %d positional sub-pattern%s (%d given)",
1059 ((PyTypeObject*)type)->tp_name,
1060 allowed, plural, nargs);
1061 goto fail;
1062 }
1063 if (match_self) {
1064 // Easy. Copy the subject itself, and move on to kwargs.
1065 PyList_Append(attrs, subject);
1066 }
1067 else {
1068 for (Py_ssize_t i = 0; i < nargs; i++) {
1069 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1070 if (!PyUnicode_CheckExact(name)) {
1071 _PyErr_Format(tstate, PyExc_TypeError,
1072 "__match_args__ elements must be strings "
1073 "(got %s)", Py_TYPE(name)->tp_name);
1074 goto fail;
1075 }
1076 PyObject *attr = match_class_attr(tstate, subject, type, name,
1077 seen);
1078 if (attr == NULL) {
1079 goto fail;
1080 }
1081 PyList_Append(attrs, attr);
1082 Py_DECREF(attr);
1083 }
1084 }
1085 Py_CLEAR(match_args);
1086 }
1087 // Finally, the keyword subpatterns:
1088 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1089 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1090 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1091 if (attr == NULL) {
1092 goto fail;
1093 }
1094 PyList_Append(attrs, attr);
1095 Py_DECREF(attr);
1096 }
1097 Py_SETREF(attrs, PyList_AsTuple(attrs));
1098 Py_DECREF(seen);
1099 return attrs;
1100fail:
1101 // We really don't care whether an error was raised or not... that's our
1102 // caller's problem. All we know is that the match failed.
1103 Py_XDECREF(match_args);
1104 Py_DECREF(seen);
1105 Py_DECREF(attrs);
1106 return NULL;
1107}
1108
1109
Victor Stinner09532fe2019-05-10 23:39:09 +02001110static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001111static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001112
Victor Stinnere225beb2019-06-03 18:14:24 +02001113#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001114
Guido van Rossum374a9221991-04-04 10:40:29 +00001115
Guido van Rossumb209a111997-04-29 18:18:01 +00001116PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001117PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001118{
Victor Stinner46496f92021-02-20 15:17:18 +01001119 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001120 if (locals == NULL) {
1121 locals = globals;
1122 }
Victor Stinner46496f92021-02-20 15:17:18 +01001123 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
Mark Shannon0332e562021-02-01 10:42:03 +00001124 if (builtins == NULL) {
1125 return NULL;
1126 }
1127 PyFrameConstructor desc = {
1128 .fc_globals = globals,
1129 .fc_builtins = builtins,
1130 .fc_name = ((PyCodeObject *)co)->co_name,
1131 .fc_qualname = ((PyCodeObject *)co)->co_name,
1132 .fc_code = co,
1133 .fc_defaults = NULL,
1134 .fc_kwdefaults = NULL,
1135 .fc_closure = NULL
1136 };
Victor Stinner46496f92021-02-20 15:17:18 +01001137 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001138}
1139
1140
1141/* Interpreter main loop */
1142
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001143PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001144PyEval_EvalFrame(PyFrameObject *f)
1145{
Victor Stinner0b72b232020-03-12 23:18:39 +01001146 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001147 PyThreadState *tstate = _PyThreadState_GET();
1148 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001149}
1150
1151PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001153{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001154 PyThreadState *tstate = _PyThreadState_GET();
1155 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001156}
1157
Victor Stinnerda2914d2020-03-20 09:29:08 +01001158
1159/* Handle signals, pending calls, GIL drop request
1160 and asynchronous exception */
1161static int
1162eval_frame_handle_pending(PyThreadState *tstate)
1163{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001164 _PyRuntimeState * const runtime = &_PyRuntime;
1165 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001166
1167 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001168 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001169 if (handle_signals(tstate) != 0) {
1170 return -1;
1171 }
1172 }
1173
1174 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001175 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001176 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001177 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001178 return -1;
1179 }
1180 }
1181
1182 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001183 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001184 /* Give another thread a chance */
1185 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1186 Py_FatalError("tstate mix-up");
1187 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001188 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001189
1190 /* Other threads may run now */
1191
1192 take_gil(tstate);
1193
Victor Stinnere838a932020-05-05 19:56:48 +02001194#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1195 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1196#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001197 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1198 Py_FatalError("orphan tstate");
1199 }
Victor Stinnere838a932020-05-05 19:56:48 +02001200#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001201 }
1202
1203 /* Check for asynchronous exception. */
1204 if (tstate->async_exc != NULL) {
1205 PyObject *exc = tstate->async_exc;
1206 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001207 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001208 _PyErr_SetNone(tstate, exc);
1209 Py_DECREF(exc);
1210 return -1;
1211 }
1212
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001213#ifdef MS_WINDOWS
1214 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1215 // different thread than the Python thread, in which case
1216 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1217 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1218 // value. It prevents to interrupt the eval loop at every instruction if
1219 // the current Python thread cannot handle signals (if
1220 // _Py_ThreadCanHandleSignals() is false).
1221 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1222#endif
1223
Victor Stinnerda2914d2020-03-20 09:29:08 +01001224 return 0;
1225}
1226
Victor Stinnerc6944e72016-11-11 02:13:35 +01001227PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001228_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001229{
Victor Stinner3026cad2020-06-01 16:02:40 +02001230 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001231
Guido van Rossum950361c1997-01-24 13:49:28 +00001232#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001234#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001235 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001236 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001237 int opcode; /* Current opcode */
1238 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001239 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001241 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001242 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 is true when the line being executed has changed. The
1250 initial values are such as to make this false the first
1251 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001252
Serhiy Storchakaab874002016-09-11 13:48:15 +03001253 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 PyObject *names;
1255 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001256 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001257
Brett Cannon368b4b72012-04-02 12:17:59 -04001258#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001259 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001260#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001261
Antoine Pitroub52ec782009-01-25 16:34:23 +00001262/* Computed GOTOs, or
1263 the-optimization-commonly-but-improperly-known-as-"threaded code"
1264 using gcc's labels-as-values extension
1265 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1266
1267 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001269 combined with a lookup table of jump addresses. However, since the
1270 indirect jump instruction is shared by all opcodes, the CPU will have a
1271 hard time making the right prediction for where to jump next (actually,
1272 it will be always wrong except in the uncommon case of a sequence of
1273 several identical opcodes).
1274
1275 "Threaded code" in contrast, uses an explicit jump table and an explicit
1276 indirect jump instruction at the end of each opcode. Since the jump
1277 instruction is at a different address for each opcode, the CPU will make a
1278 separate prediction for each of these instructions, which is equivalent to
1279 predicting the second opcode of each opcode pair. These predictions have
1280 a much better chance to turn out valid, especially in small bytecode loops.
1281
1282 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001284 and potentially many more instructions (depending on the pipeline width).
1285 A correctly predicted branch, however, is nearly free.
1286
1287 At the time of this writing, the "threaded code" version is up to 15-20%
1288 faster than the normal "switch" version, depending on the compiler and the
1289 CPU architecture.
1290
1291 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1292 because it would render the measurements invalid.
1293
1294
1295 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1296 indirect jumps by sharing them between all opcodes. Such optimizations
1297 can be disabled on gcc by using the -fno-gcse flag (or possibly
1298 -fno-crossjumping).
1299*/
1300
Antoine Pitrou042b1282010-08-13 21:15:58 +00001301#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001302#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001303#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304#endif
1305
Antoine Pitrou042b1282010-08-13 21:15:58 +00001306#ifdef HAVE_COMPUTED_GOTOS
1307 #ifndef USE_COMPUTED_GOTOS
1308 #define USE_COMPUTED_GOTOS 1
1309 #endif
1310#else
1311 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1312 #error "Computed gotos are not supported on this compiler."
1313 #endif
1314 #undef USE_COMPUTED_GOTOS
1315 #define USE_COMPUTED_GOTOS 0
1316#endif
1317
1318#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319/* Import the static jump table */
1320#include "opcode_targets.h"
1321
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001323 op: \
1324 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001325
Antoine Pitroub52ec782009-01-25 16:34:23 +00001326#ifdef LLTRACE
1327#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001329 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001331 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001332 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 } \
1334 goto fast_next_opcode; \
1335 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001336#else
1337#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001339 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001341 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001342 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 } \
1344 goto fast_next_opcode; \
1345 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001346#endif
1347
Victor Stinner09532fe2019-05-10 23:39:09 +02001348#define DISPATCH() \
1349 { \
1350 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1351 FAST_DISPATCH(); \
1352 } \
1353 continue; \
1354 }
1355
Antoine Pitroub52ec782009-01-25 16:34:23 +00001356#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001357#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001358#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001359#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001360#endif
1361
1362
Neal Norwitza81d2202002-07-14 00:27:26 +00001363/* Tuple access macros */
1364
1365#ifndef Py_DEBUG
1366#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1367#else
1368#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1369#endif
1370
Guido van Rossum374a9221991-04-04 10:40:29 +00001371/* Code access macros */
1372
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001373/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001374#define INSTR_OFFSET() \
1375 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001376#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001377 _Py_CODEUNIT word = *next_instr; \
1378 opcode = _Py_OPCODE(word); \
1379 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001380 next_instr++; \
1381 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001382#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1383#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001384
Raymond Hettingerf606f872003-03-16 03:11:04 +00001385/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 Some opcodes tend to come in pairs thus making it possible to
1387 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001388 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Verifying the prediction costs a single high-speed test of a register
1391 variable against a constant. If the pairing was good, then the
1392 processor's own internal branch predication has a high likelihood of
1393 success, resulting in a nearly zero-overhead transition to the
1394 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001395 including its unpredictable switch-case branch. Combined with the
1396 processor's internal branch prediction, a successful PREDICT has the
1397 effect of making the two opcodes run as if they were a single new opcode
1398 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001399
Georg Brandl86b2fb92008-07-16 03:43:04 +00001400 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 predictions turned-on and interpret the results as if some opcodes
1402 had been combined or turn-off predictions so that the opcode frequency
1403 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001404
1405 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 the CPU to record separate branch prediction information for each
1407 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001408
Raymond Hettingerf606f872003-03-16 03:11:04 +00001409*/
1410
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001411#define PREDICT_ID(op) PRED_##op
1412
Antoine Pitrou042b1282010-08-13 21:15:58 +00001413#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001414#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001415#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001416#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001417 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001418 _Py_CODEUNIT word = *next_instr; \
1419 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001420 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001421 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001422 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001423 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001424 } \
1425 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001426#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001427#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001428
Raymond Hettingerf606f872003-03-16 03:11:04 +00001429
Guido van Rossum374a9221991-04-04 10:40:29 +00001430/* Stack manipulation macros */
1431
Martin v. Löwis18e16552006-02-15 17:27:45 +00001432/* The stack can grow at most MAXINT deep, as co_nlocals and
1433 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001434#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1435#define EMPTY() (STACK_LEVEL() == 0)
1436#define TOP() (stack_pointer[-1])
1437#define SECOND() (stack_pointer[-2])
1438#define THIRD() (stack_pointer[-3])
1439#define FOURTH() (stack_pointer[-4])
1440#define PEEK(n) (stack_pointer[-(n)])
1441#define SET_TOP(v) (stack_pointer[-1] = (v))
1442#define SET_SECOND(v) (stack_pointer[-2] = (v))
1443#define SET_THIRD(v) (stack_pointer[-3] = (v))
1444#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001445#define BASIC_STACKADJ(n) (stack_pointer += n)
1446#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1447#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001448
Guido van Rossum96a42c81992-01-12 02:29:51 +00001449#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001451 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001452 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001453#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001454 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001455#define STACK_GROW(n) do { \
1456 assert(n >= 0); \
1457 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001458 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001459 assert(STACK_LEVEL() <= co->co_stacksize); \
1460 } while (0)
1461#define STACK_SHRINK(n) do { \
1462 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001463 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001464 (void)(BASIC_STACKADJ(-n)); \
1465 assert(STACK_LEVEL() <= co->co_stacksize); \
1466 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001467#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001468 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001469 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001470#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001471#define PUSH(v) BASIC_PUSH(v)
1472#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001473#define STACK_GROW(n) BASIC_STACKADJ(n)
1474#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001475#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001476#endif
1477
Guido van Rossum681d79a1995-07-18 14:51:37 +00001478/* Local variable macros */
1479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001481
1482/* The SETLOCAL() macro must not DECREF the local variable in-place and
1483 then store the new value; it must copy the old value to a temporary
1484 value, then store the new value, and then DECREF the temporary value.
1485 This is because it is possible that during the DECREF the frame is
1486 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1487 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001489 GETLOCAL(i) = value; \
1490 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001491
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001492
1493#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 while (STACK_LEVEL() > (b)->b_level) { \
1495 PyObject *v = POP(); \
1496 Py_XDECREF(v); \
1497 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001498
1499#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001500 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001502 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1504 while (STACK_LEVEL() > (b)->b_level + 3) { \
1505 value = POP(); \
1506 Py_XDECREF(value); \
1507 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001508 exc_info = tstate->exc_info; \
1509 type = exc_info->exc_type; \
1510 value = exc_info->exc_value; \
1511 traceback = exc_info->exc_traceback; \
1512 exc_info->exc_type = POP(); \
1513 exc_info->exc_value = POP(); \
1514 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 Py_XDECREF(type); \
1516 Py_XDECREF(value); \
1517 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001518 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001519
Inada Naoki91234a12019-06-03 21:30:58 +09001520 /* macros for opcode cache */
1521#define OPCACHE_CHECK() \
1522 do { \
1523 co_opcache = NULL; \
1524 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001525 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001526 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001527 if (co_opcache_offset > 0) { \
1528 assert(co_opcache_offset <= co->co_opcache_size); \
1529 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001530 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001531 } \
1532 } \
1533 } while (0)
1534
Pablo Galindo109826c2020-10-20 06:22:44 +01001535#define OPCACHE_DEOPT() \
1536 do { \
1537 if (co_opcache != NULL) { \
1538 co_opcache->optimized = -1; \
1539 unsigned char co_opcache_offset = \
1540 co->co_opcache_map[next_instr - first_instr]; \
1541 assert(co_opcache_offset <= co->co_opcache_size); \
1542 co->co_opcache_map[co_opcache_offset] = 0; \
1543 co_opcache = NULL; \
1544 } \
1545 } while (0)
1546
1547#define OPCACHE_DEOPT_LOAD_ATTR() \
1548 do { \
1549 if (co_opcache != NULL) { \
1550 OPCACHE_STAT_ATTR_DEOPT(); \
1551 OPCACHE_DEOPT(); \
1552 } \
1553 } while (0)
1554
1555#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1556 do { \
1557 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1558 OPCACHE_DEOPT_LOAD_ATTR(); \
1559 } \
1560 } while (0)
1561
Inada Naoki91234a12019-06-03 21:30:58 +09001562#if OPCACHE_STATS
1563
1564#define OPCACHE_STAT_GLOBAL_HIT() \
1565 do { \
1566 if (co->co_opcache != NULL) opcache_global_hits++; \
1567 } while (0)
1568
1569#define OPCACHE_STAT_GLOBAL_MISS() \
1570 do { \
1571 if (co->co_opcache != NULL) opcache_global_misses++; \
1572 } while (0)
1573
1574#define OPCACHE_STAT_GLOBAL_OPT() \
1575 do { \
1576 if (co->co_opcache != NULL) opcache_global_opts++; \
1577 } while (0)
1578
Pablo Galindo109826c2020-10-20 06:22:44 +01001579#define OPCACHE_STAT_ATTR_HIT() \
1580 do { \
1581 if (co->co_opcache != NULL) opcache_attr_hits++; \
1582 } while (0)
1583
1584#define OPCACHE_STAT_ATTR_MISS() \
1585 do { \
1586 if (co->co_opcache != NULL) opcache_attr_misses++; \
1587 } while (0)
1588
1589#define OPCACHE_STAT_ATTR_OPT() \
1590 do { \
1591 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1592 } while (0)
1593
1594#define OPCACHE_STAT_ATTR_DEOPT() \
1595 do { \
1596 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1597 } while (0)
1598
1599#define OPCACHE_STAT_ATTR_TOTAL() \
1600 do { \
1601 if (co->co_opcache != NULL) opcache_attr_total++; \
1602 } while (0)
1603
Inada Naoki91234a12019-06-03 21:30:58 +09001604#else /* OPCACHE_STATS */
1605
1606#define OPCACHE_STAT_GLOBAL_HIT()
1607#define OPCACHE_STAT_GLOBAL_MISS()
1608#define OPCACHE_STAT_GLOBAL_OPT()
1609
Pablo Galindo109826c2020-10-20 06:22:44 +01001610#define OPCACHE_STAT_ATTR_HIT()
1611#define OPCACHE_STAT_ATTR_MISS()
1612#define OPCACHE_STAT_ATTR_OPT()
1613#define OPCACHE_STAT_ATTR_DEOPT()
1614#define OPCACHE_STAT_ATTR_TOTAL()
1615
Inada Naoki91234a12019-06-03 21:30:58 +09001616#endif
1617
Guido van Rossuma027efa1997-05-05 20:56:21 +00001618/* Start of code */
1619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001621 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001623 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001626 co = f->f_code;
1627 PyCodeAddressRange bounds;
1628 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (tstate->use_tracing) {
1631 if (tstate->c_tracefunc != NULL) {
1632 /* tstate->c_tracefunc, if defined, is a
1633 function that will be called on *every* entry
1634 to a code block. Its return value, if not
1635 None, is a function that will be called at
1636 the start of each executed line of code.
1637 (Actually, the function must return itself
1638 in order to continue tracing.) The trace
1639 functions are called with three arguments:
1640 a pointer to the current frame, a string
1641 indicating why the function is called, and
1642 an argument which depends on the situation.
1643 The global trace function is also called
1644 whenever an exception is detected. */
1645 if (call_trace_protected(tstate->c_tracefunc,
1646 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001647 tstate, f, &bounds,
1648 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 /* Trace function raised an error */
1650 goto exit_eval_frame;
1651 }
1652 }
1653 if (tstate->c_profilefunc != NULL) {
1654 /* Similar for c_profilefunc, except it needn't
1655 return itself and isn't called for "line" events */
1656 if (call_trace_protected(tstate->c_profilefunc,
1657 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001658 tstate, f, &bounds,
1659 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 /* Profile function raised an error */
1661 goto exit_eval_frame;
1662 }
1663 }
1664 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001665
Łukasz Langaa785c872016-09-09 17:37:37 -07001666 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1667 dtrace_function_entry(f);
1668
Mark Shannon877df852020-11-12 09:43:29 +00001669 int instr_prev = -1;
1670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 names = co->co_names;
1672 consts = co->co_consts;
1673 fastlocals = f->f_localsplus;
1674 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001675 assert(PyBytes_Check(co->co_code));
1676 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001677 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1678 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1679 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001680 /*
1681 f->f_lasti refers to the index of the last instruction,
1682 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001683
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001684 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001685 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 When the PREDICT() macros are enabled, some opcode pairs follow in
1688 direct succession without updating f->f_lasti. A successful
1689 prediction effectively links the two codes together as if they
1690 were a single new opcode; accordingly,f->f_lasti will point to
1691 the first code in the pair (for instance, GET_ITER followed by
1692 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001693 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001695 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001696 next_instr = first_instr;
1697 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001698 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1699 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001700 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001701 stack_pointer = f->f_valuestack + f->f_stackdepth;
1702 /* Set f->f_stackdepth to -1.
1703 * Update when returning or calling trace function.
1704 Having f_stackdepth <= 0 ensures that invalid
1705 values are not visible to the cycle GC.
1706 We choose -1 rather than 0 to assist debugging.
1707 */
1708 f->f_stackdepth = -1;
1709 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001710
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001711 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001712 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001713 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001714 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001715 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001716 }
1717#if OPCACHE_STATS
1718 opcache_code_objects_extra_mem +=
1719 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1720 sizeof(_PyOpcache) * co->co_opcache_size;
1721 opcache_code_objects++;
1722#endif
1723 }
1724 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001725
Tim Peters5ca576e2001-06-18 22:08:13 +00001726#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001727 {
1728 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1729 if (r < 0) {
1730 goto exit_eval_frame;
1731 }
1732 lltrace = r;
1733 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001734#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001736 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001738 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001739
Victor Stinnerace47d72013-07-18 01:41:08 +02001740#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001741 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001742 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001743 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001744 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001745#endif
1746
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001747main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1750 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001751 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 /* Do periodic things. Doing this every time through
1754 the loop would add too much overhead, so we do it
1755 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001756 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 event needs attention (e.g. a signal handler or
1758 async I/O handler); see Py_AddPendingCall() and
1759 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Eric Snow7bda9de2019-03-08 17:25:54 -07001761 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001762 opcode = _Py_OPCODE(*next_instr);
1763 if (opcode == SETUP_FINALLY ||
1764 opcode == SETUP_WITH ||
1765 opcode == BEFORE_ASYNC_WITH ||
1766 opcode == YIELD_FROM) {
1767 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001768 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001769 - If we're about to enter the 'with:'. It will prevent
1770 emitting a resource warning in the common idiom
1771 'with open(path) as file:'.
1772 - If we're about to enter the 'async with:'.
1773 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001774 *very* useful, but might help in some cases and it's
1775 traditional)
1776 - If we're resuming a chain of nested 'yield from' or
1777 'await' calls, then each frame is parked with YIELD_FROM
1778 as its next opcode. If the user hit control-C we want to
1779 wait until we've reached the innermost frame before
1780 running the signal handler and raising KeyboardInterrupt
1781 (see bpo-30039).
1782 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 goto fast_next_opcode;
1784 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001785
Victor Stinnerda2914d2020-03-20 09:29:08 +01001786 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 }
1789 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 fast_next_opcode:
1792 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Łukasz Langaa785c872016-09-09 17:37:37 -07001794 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001795 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001798
Victor Stinnerdab84232020-03-17 18:56:44 +01001799 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001800 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001802 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001804 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 err = maybe_call_line_trace(tstate->c_tracefunc,
1807 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001808 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001809 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Reload possibly changed frame fields */
1811 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001812 stack_pointer = f->f_valuestack+f->f_stackdepth;
1813 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001820
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001821 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001822 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001823#ifdef DYNAMIC_EXECUTION_PROFILE
1824#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 dxpairs[lastopcode][opcode]++;
1826 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001827#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001829#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001830
Guido van Rossum96a42c81992-01-12 02:29:51 +00001831#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 if (lltrace) {
1835 if (HAS_ARG(opcode)) {
1836 printf("%d: %d, %d\n",
1837 f->f_lasti, opcode, oparg);
1838 }
1839 else {
1840 printf("%d: %d\n",
1841 f->f_lasti, opcode);
1842 }
1843 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001844#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001849 It is essential that any operation that fails must goto error
1850 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001854 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 PyObject *value = GETLOCAL(oparg);
1858 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001859 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 UNBOUNDLOCAL_ERROR_MSG,
1861 PyTuple_GetItem(co->co_varnames, oparg));
1862 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 Py_INCREF(value);
1865 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 }
1868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(LOAD_CONST): {
1870 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 PyObject *value = GETITEM(consts, oparg);
1872 Py_INCREF(value);
1873 PUSH(value);
1874 FAST_DISPATCH();
1875 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001876
Benjamin Petersonddd19492018-09-16 22:38:02 -07001877 case TARGET(STORE_FAST): {
1878 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *value = POP();
1880 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *value = POP();
1886 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *top = TOP();
1892 PyObject *second = SECOND();
1893 SET_TOP(second);
1894 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001897
Benjamin Petersonddd19492018-09-16 22:38:02 -07001898 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 PyObject *top = TOP();
1900 PyObject *second = SECOND();
1901 PyObject *third = THIRD();
1902 SET_TOP(second);
1903 SET_SECOND(third);
1904 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Benjamin Petersonddd19492018-09-16 22:38:02 -07001908 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001909 PyObject *top = TOP();
1910 PyObject *second = SECOND();
1911 PyObject *third = THIRD();
1912 PyObject *fourth = FOURTH();
1913 SET_TOP(second);
1914 SET_SECOND(third);
1915 SET_THIRD(fourth);
1916 SET_FOURTH(top);
1917 FAST_DISPATCH();
1918 }
1919
Benjamin Petersonddd19492018-09-16 22:38:02 -07001920 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 PyObject *top = TOP();
1922 Py_INCREF(top);
1923 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001930 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001932 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 SET_TOP(top);
1934 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001935 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001937
Benjamin Petersonddd19492018-09-16 22:38:02 -07001938 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 PyObject *value = TOP();
1940 PyObject *res = PyNumber_Positive(value);
1941 Py_DECREF(value);
1942 SET_TOP(res);
1943 if (res == NULL)
1944 goto error;
1945 DISPATCH();
1946 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Benjamin Petersonddd19492018-09-16 22:38:02 -07001948 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001949 PyObject *value = TOP();
1950 PyObject *res = PyNumber_Negative(value);
1951 Py_DECREF(value);
1952 SET_TOP(res);
1953 if (res == NULL)
1954 goto error;
1955 DISPATCH();
1956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001957
Benjamin Petersonddd19492018-09-16 22:38:02 -07001958 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 PyObject *value = TOP();
1960 int err = PyObject_IsTrue(value);
1961 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (err == 0) {
1963 Py_INCREF(Py_True);
1964 SET_TOP(Py_True);
1965 DISPATCH();
1966 }
1967 else if (err > 0) {
1968 Py_INCREF(Py_False);
1969 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 DISPATCH();
1971 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001972 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 goto error;
1974 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001975
Benjamin Petersonddd19492018-09-16 22:38:02 -07001976 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 PyObject *value = TOP();
1978 PyObject *res = PyNumber_Invert(value);
1979 Py_DECREF(value);
1980 SET_TOP(res);
1981 if (res == NULL)
1982 goto error;
1983 DISPATCH();
1984 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001985
Benjamin Petersonddd19492018-09-16 22:38:02 -07001986 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 PyObject *exp = POP();
1988 PyObject *base = TOP();
1989 PyObject *res = PyNumber_Power(base, exp, Py_None);
1990 Py_DECREF(base);
1991 Py_DECREF(exp);
1992 SET_TOP(res);
1993 if (res == NULL)
1994 goto error;
1995 DISPATCH();
1996 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001997
Benjamin Petersonddd19492018-09-16 22:38:02 -07001998 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 PyObject *right = POP();
2000 PyObject *left = TOP();
2001 PyObject *res = PyNumber_Multiply(left, right);
2002 Py_DECREF(left);
2003 Py_DECREF(right);
2004 SET_TOP(res);
2005 if (res == NULL)
2006 goto error;
2007 DISPATCH();
2008 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002009
Benjamin Petersonddd19492018-09-16 22:38:02 -07002010 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002011 PyObject *right = POP();
2012 PyObject *left = TOP();
2013 PyObject *res = PyNumber_MatrixMultiply(left, right);
2014 Py_DECREF(left);
2015 Py_DECREF(right);
2016 SET_TOP(res);
2017 if (res == NULL)
2018 goto error;
2019 DISPATCH();
2020 }
2021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 PyObject *divisor = POP();
2024 PyObject *dividend = TOP();
2025 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2026 Py_DECREF(dividend);
2027 Py_DECREF(divisor);
2028 SET_TOP(quotient);
2029 if (quotient == NULL)
2030 goto error;
2031 DISPATCH();
2032 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002033
Benjamin Petersonddd19492018-09-16 22:38:02 -07002034 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 PyObject *divisor = POP();
2036 PyObject *dividend = TOP();
2037 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2038 Py_DECREF(dividend);
2039 Py_DECREF(divisor);
2040 SET_TOP(quotient);
2041 if (quotient == NULL)
2042 goto error;
2043 DISPATCH();
2044 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 PyObject *divisor = POP();
2048 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002049 PyObject *res;
2050 if (PyUnicode_CheckExact(dividend) && (
2051 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2052 // fast path; string formatting, but not if the RHS is a str subclass
2053 // (see issue28598)
2054 res = PyUnicode_Format(dividend, divisor);
2055 } else {
2056 res = PyNumber_Remainder(dividend, divisor);
2057 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 Py_DECREF(divisor);
2059 Py_DECREF(dividend);
2060 SET_TOP(res);
2061 if (res == NULL)
2062 goto error;
2063 DISPATCH();
2064 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002065
Benjamin Petersonddd19492018-09-16 22:38:02 -07002066 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 PyObject *right = POP();
2068 PyObject *left = TOP();
2069 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002070 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002071 CPython using bytecode, it is simply worthless.
2072 See http://bugs.python.org/issue21955 and
2073 http://bugs.python.org/issue10044 for the discussion. In short,
2074 no patch shown any impact on a realistic benchmark, only a minor
2075 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 if (PyUnicode_CheckExact(left) &&
2077 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002078 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002079 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002080 }
2081 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 sum = PyNumber_Add(left, right);
2083 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 Py_DECREF(right);
2086 SET_TOP(sum);
2087 if (sum == NULL)
2088 goto error;
2089 DISPATCH();
2090 }
2091
Benjamin Petersonddd19492018-09-16 22:38:02 -07002092 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 PyObject *right = POP();
2094 PyObject *left = TOP();
2095 PyObject *diff = PyNumber_Subtract(left, right);
2096 Py_DECREF(right);
2097 Py_DECREF(left);
2098 SET_TOP(diff);
2099 if (diff == NULL)
2100 goto error;
2101 DISPATCH();
2102 }
2103
Benjamin Petersonddd19492018-09-16 22:38:02 -07002104 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PyObject *sub = POP();
2106 PyObject *container = TOP();
2107 PyObject *res = PyObject_GetItem(container, sub);
2108 Py_DECREF(container);
2109 Py_DECREF(sub);
2110 SET_TOP(res);
2111 if (res == NULL)
2112 goto error;
2113 DISPATCH();
2114 }
2115
Benjamin Petersonddd19492018-09-16 22:38:02 -07002116 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PyObject *right = POP();
2118 PyObject *left = TOP();
2119 PyObject *res = PyNumber_Lshift(left, right);
2120 Py_DECREF(left);
2121 Py_DECREF(right);
2122 SET_TOP(res);
2123 if (res == NULL)
2124 goto error;
2125 DISPATCH();
2126 }
2127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 PyObject *right = POP();
2130 PyObject *left = TOP();
2131 PyObject *res = PyNumber_Rshift(left, right);
2132 Py_DECREF(left);
2133 Py_DECREF(right);
2134 SET_TOP(res);
2135 if (res == NULL)
2136 goto error;
2137 DISPATCH();
2138 }
2139
Benjamin Petersonddd19492018-09-16 22:38:02 -07002140 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 PyObject *right = POP();
2142 PyObject *left = TOP();
2143 PyObject *res = PyNumber_And(left, right);
2144 Py_DECREF(left);
2145 Py_DECREF(right);
2146 SET_TOP(res);
2147 if (res == NULL)
2148 goto error;
2149 DISPATCH();
2150 }
2151
Benjamin Petersonddd19492018-09-16 22:38:02 -07002152 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 PyObject *right = POP();
2154 PyObject *left = TOP();
2155 PyObject *res = PyNumber_Xor(left, right);
2156 Py_DECREF(left);
2157 Py_DECREF(right);
2158 SET_TOP(res);
2159 if (res == NULL)
2160 goto error;
2161 DISPATCH();
2162 }
2163
Benjamin Petersonddd19492018-09-16 22:38:02 -07002164 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 PyObject *right = POP();
2166 PyObject *left = TOP();
2167 PyObject *res = PyNumber_Or(left, right);
2168 Py_DECREF(left);
2169 Py_DECREF(right);
2170 SET_TOP(res);
2171 if (res == NULL)
2172 goto error;
2173 DISPATCH();
2174 }
2175
Benjamin Petersonddd19492018-09-16 22:38:02 -07002176 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 PyObject *v = POP();
2178 PyObject *list = PEEK(oparg);
2179 int err;
2180 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 if (err != 0)
2183 goto error;
2184 PREDICT(JUMP_ABSOLUTE);
2185 DISPATCH();
2186 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002187
Benjamin Petersonddd19492018-09-16 22:38:02 -07002188 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002190 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 int err;
2192 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 if (err != 0)
2195 goto error;
2196 PREDICT(JUMP_ABSOLUTE);
2197 DISPATCH();
2198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 PyObject *exp = POP();
2202 PyObject *base = TOP();
2203 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2204 Py_DECREF(base);
2205 Py_DECREF(exp);
2206 SET_TOP(res);
2207 if (res == NULL)
2208 goto error;
2209 DISPATCH();
2210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Benjamin Petersonddd19492018-09-16 22:38:02 -07002212 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 PyObject *right = POP();
2214 PyObject *left = TOP();
2215 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2216 Py_DECREF(left);
2217 Py_DECREF(right);
2218 SET_TOP(res);
2219 if (res == NULL)
2220 goto error;
2221 DISPATCH();
2222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Benjamin Petersonddd19492018-09-16 22:38:02 -07002224 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002225 PyObject *right = POP();
2226 PyObject *left = TOP();
2227 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2228 Py_DECREF(left);
2229 Py_DECREF(right);
2230 SET_TOP(res);
2231 if (res == NULL)
2232 goto error;
2233 DISPATCH();
2234 }
2235
Benjamin Petersonddd19492018-09-16 22:38:02 -07002236 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 PyObject *divisor = POP();
2238 PyObject *dividend = TOP();
2239 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2240 Py_DECREF(dividend);
2241 Py_DECREF(divisor);
2242 SET_TOP(quotient);
2243 if (quotient == NULL)
2244 goto error;
2245 DISPATCH();
2246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Benjamin Petersonddd19492018-09-16 22:38:02 -07002248 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 PyObject *divisor = POP();
2250 PyObject *dividend = TOP();
2251 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2252 Py_DECREF(dividend);
2253 Py_DECREF(divisor);
2254 SET_TOP(quotient);
2255 if (quotient == NULL)
2256 goto error;
2257 DISPATCH();
2258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Benjamin Petersonddd19492018-09-16 22:38:02 -07002260 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 PyObject *right = POP();
2262 PyObject *left = TOP();
2263 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2264 Py_DECREF(left);
2265 Py_DECREF(right);
2266 SET_TOP(mod);
2267 if (mod == NULL)
2268 goto error;
2269 DISPATCH();
2270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Benjamin Petersonddd19492018-09-16 22:38:02 -07002272 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 PyObject *right = POP();
2274 PyObject *left = TOP();
2275 PyObject *sum;
2276 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002277 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002278 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002279 }
2280 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 sum = PyNumber_InPlaceAdd(left, right);
2282 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 Py_DECREF(right);
2285 SET_TOP(sum);
2286 if (sum == NULL)
2287 goto error;
2288 DISPATCH();
2289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002290
Benjamin Petersonddd19492018-09-16 22:38:02 -07002291 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 PyObject *right = POP();
2293 PyObject *left = TOP();
2294 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2295 Py_DECREF(left);
2296 Py_DECREF(right);
2297 SET_TOP(diff);
2298 if (diff == NULL)
2299 goto error;
2300 DISPATCH();
2301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002302
Benjamin Petersonddd19492018-09-16 22:38:02 -07002303 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 PyObject *right = POP();
2305 PyObject *left = TOP();
2306 PyObject *res = PyNumber_InPlaceLshift(left, right);
2307 Py_DECREF(left);
2308 Py_DECREF(right);
2309 SET_TOP(res);
2310 if (res == NULL)
2311 goto error;
2312 DISPATCH();
2313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Benjamin Petersonddd19492018-09-16 22:38:02 -07002315 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 PyObject *right = POP();
2317 PyObject *left = TOP();
2318 PyObject *res = PyNumber_InPlaceRshift(left, right);
2319 Py_DECREF(left);
2320 Py_DECREF(right);
2321 SET_TOP(res);
2322 if (res == NULL)
2323 goto error;
2324 DISPATCH();
2325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *right = POP();
2329 PyObject *left = TOP();
2330 PyObject *res = PyNumber_InPlaceAnd(left, right);
2331 Py_DECREF(left);
2332 Py_DECREF(right);
2333 SET_TOP(res);
2334 if (res == NULL)
2335 goto error;
2336 DISPATCH();
2337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *right = POP();
2341 PyObject *left = TOP();
2342 PyObject *res = PyNumber_InPlaceXor(left, right);
2343 Py_DECREF(left);
2344 Py_DECREF(right);
2345 SET_TOP(res);
2346 if (res == NULL)
2347 goto error;
2348 DISPATCH();
2349 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002350
Benjamin Petersonddd19492018-09-16 22:38:02 -07002351 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 PyObject *right = POP();
2353 PyObject *left = TOP();
2354 PyObject *res = PyNumber_InPlaceOr(left, right);
2355 Py_DECREF(left);
2356 Py_DECREF(right);
2357 SET_TOP(res);
2358 if (res == NULL)
2359 goto error;
2360 DISPATCH();
2361 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *sub = TOP();
2365 PyObject *container = SECOND();
2366 PyObject *v = THIRD();
2367 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002368 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002369 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 Py_DECREF(container);
2373 Py_DECREF(sub);
2374 if (err != 0)
2375 goto error;
2376 DISPATCH();
2377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Benjamin Petersonddd19492018-09-16 22:38:02 -07002379 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 PyObject *sub = TOP();
2381 PyObject *container = SECOND();
2382 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002383 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002384 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 err = PyObject_DelItem(container, sub);
2386 Py_DECREF(container);
2387 Py_DECREF(sub);
2388 if (err != 0)
2389 goto error;
2390 DISPATCH();
2391 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002392
Benjamin Petersonddd19492018-09-16 22:38:02 -07002393 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002394 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002396 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002397 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002399 _PyErr_SetString(tstate, PyExc_RuntimeError,
2400 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 Py_DECREF(value);
2402 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002404 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 Py_DECREF(value);
2406 if (res == NULL)
2407 goto error;
2408 Py_DECREF(res);
2409 DISPATCH();
2410 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002411
Benjamin Petersonddd19492018-09-16 22:38:02 -07002412 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 switch (oparg) {
2415 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002417 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002420 /* fall through */
2421 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002422 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002423 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 break;
2426 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 _PyErr_SetString(tstate, PyExc_SystemError,
2428 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 break;
2430 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
2432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002433
Benjamin Petersonddd19492018-09-16 22:38:02 -07002434 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002436 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002437 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002438 f->f_state = FRAME_RETURNED;
2439 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002440 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002444 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002445 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002446 PyObject *obj = TOP();
2447 PyTypeObject *type = Py_TYPE(obj);
2448
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002449 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002450 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002451 }
Yury Selivanov75445082015-05-11 22:57:16 -04002452
2453 if (getter != NULL) {
2454 iter = (*getter)(obj);
2455 Py_DECREF(obj);
2456 if (iter == NULL) {
2457 SET_TOP(NULL);
2458 goto error;
2459 }
2460 }
2461 else {
2462 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 _PyErr_Format(tstate, PyExc_TypeError,
2464 "'async for' requires an object with "
2465 "__aiter__ method, got %.100s",
2466 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002467 Py_DECREF(obj);
2468 goto error;
2469 }
2470
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002471 if (Py_TYPE(iter)->tp_as_async == NULL ||
2472 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002473
Yury Selivanov398ff912017-03-02 22:20:00 -05002474 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 _PyErr_Format(tstate, PyExc_TypeError,
2476 "'async for' received an object from __aiter__ "
2477 "that does not implement __anext__: %.100s",
2478 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002479 Py_DECREF(iter);
2480 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002481 }
2482
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002483 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002484 DISPATCH();
2485 }
2486
Benjamin Petersonddd19492018-09-16 22:38:02 -07002487 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002488 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002489 PyObject *next_iter = NULL;
2490 PyObject *awaitable = NULL;
2491 PyObject *aiter = TOP();
2492 PyTypeObject *type = Py_TYPE(aiter);
2493
Yury Selivanoveb636452016-09-08 22:01:51 -07002494 if (PyAsyncGen_CheckExact(aiter)) {
2495 awaitable = type->tp_as_async->am_anext(aiter);
2496 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002497 goto error;
2498 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002499 } else {
2500 if (type->tp_as_async != NULL){
2501 getter = type->tp_as_async->am_anext;
2502 }
Yury Selivanov75445082015-05-11 22:57:16 -04002503
Yury Selivanoveb636452016-09-08 22:01:51 -07002504 if (getter != NULL) {
2505 next_iter = (*getter)(aiter);
2506 if (next_iter == NULL) {
2507 goto error;
2508 }
2509 }
2510 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002511 _PyErr_Format(tstate, PyExc_TypeError,
2512 "'async for' requires an iterator with "
2513 "__anext__ method, got %.100s",
2514 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002515 goto error;
2516 }
Yury Selivanov75445082015-05-11 22:57:16 -04002517
Yury Selivanoveb636452016-09-08 22:01:51 -07002518 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2519 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002520 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002521 PyExc_TypeError,
2522 "'async for' received an invalid object "
2523 "from __anext__: %.100s",
2524 Py_TYPE(next_iter)->tp_name);
2525
2526 Py_DECREF(next_iter);
2527 goto error;
2528 } else {
2529 Py_DECREF(next_iter);
2530 }
2531 }
Yury Selivanov75445082015-05-11 22:57:16 -04002532
2533 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002534 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002535 DISPATCH();
2536 }
2537
Benjamin Petersonddd19492018-09-16 22:38:02 -07002538 case TARGET(GET_AWAITABLE): {
2539 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002540 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002541 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002542
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002543 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002544 int opcode_at_minus_3 = 0;
2545 if ((next_instr - first_instr) > 2) {
2546 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2547 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002548 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002549 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002550 _Py_OPCODE(next_instr[-2]));
2551 }
2552
Yury Selivanov75445082015-05-11 22:57:16 -04002553 Py_DECREF(iterable);
2554
Yury Selivanovc724bae2016-03-02 11:30:46 -05002555 if (iter != NULL && PyCoro_CheckExact(iter)) {
2556 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2557 if (yf != NULL) {
2558 /* `iter` is a coroutine object that is being
2559 awaited, `yf` is a pointer to the current awaitable
2560 being awaited on. */
2561 Py_DECREF(yf);
2562 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002563 _PyErr_SetString(tstate, PyExc_RuntimeError,
2564 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002565 /* The code below jumps to `error` if `iter` is NULL. */
2566 }
2567 }
2568
Yury Selivanov75445082015-05-11 22:57:16 -04002569 SET_TOP(iter); /* Even if it's NULL */
2570
2571 if (iter == NULL) {
2572 goto error;
2573 }
2574
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002575 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002576 DISPATCH();
2577 }
2578
Benjamin Petersonddd19492018-09-16 22:38:02 -07002579 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002580 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002581 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002582 PySendResult gen_status;
2583 if (tstate->c_tracefunc == NULL) {
2584 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002585 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002586 _Py_IDENTIFIER(send);
2587 if (v == Py_None && PyIter_Check(receiver)) {
2588 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002589 }
2590 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002591 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002592 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002593 if (retval == NULL) {
2594 if (tstate->c_tracefunc != NULL
2595 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002596 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002597 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2598 gen_status = PYGEN_RETURN;
2599 }
2600 else {
2601 gen_status = PYGEN_ERROR;
2602 }
2603 }
2604 else {
2605 gen_status = PYGEN_NEXT;
2606 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002607 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002609 if (gen_status == PYGEN_ERROR) {
2610 assert (retval == NULL);
2611 goto error;
2612 }
2613 if (gen_status == PYGEN_RETURN) {
2614 assert (retval != NULL);
2615
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002616 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002617 SET_TOP(retval);
2618 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002620 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002621 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002622 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002623 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002624 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002625 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002626 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002627 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002628 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002629 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002630
Benjamin Petersonddd19492018-09-16 22:38:02 -07002631 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002633
2634 if (co->co_flags & CO_ASYNC_GENERATOR) {
2635 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2636 Py_DECREF(retval);
2637 if (w == NULL) {
2638 retval = NULL;
2639 goto error;
2640 }
2641 retval = w;
2642 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002643 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002644 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002645 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002647
Benjamin Petersonddd19492018-09-16 22:38:02 -07002648 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002649 PyObject *type, *value, *traceback;
2650 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 PyTryBlock *b = PyFrame_BlockPop(f);
2652 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002653 _PyErr_SetString(tstate, PyExc_SystemError,
2654 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002657 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2658 STACK_LEVEL() <= (b)->b_level + 4);
2659 exc_info = tstate->exc_info;
2660 type = exc_info->exc_type;
2661 value = exc_info->exc_value;
2662 traceback = exc_info->exc_traceback;
2663 exc_info->exc_type = POP();
2664 exc_info->exc_value = POP();
2665 exc_info->exc_traceback = POP();
2666 Py_XDECREF(type);
2667 Py_XDECREF(value);
2668 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002673 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002676
Mark Shannonfee55262019-11-21 09:11:43 +00002677 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002678 assert(f->f_iblock > 0);
2679 if (oparg) {
2680 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2681 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002682 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002683 PyObject *val = POP();
2684 PyObject *tb = POP();
2685 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002686 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002687 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002689
Benjamin Petersonddd19492018-09-16 22:38:02 -07002690 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002691 PyObject *exc = POP();
2692 assert(PyExceptionClass_Check(exc));
2693 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2694 PyTryBlock *b = PyFrame_BlockPop(f);
2695 assert(b->b_type == EXCEPT_HANDLER);
2696 Py_DECREF(exc);
2697 UNWIND_EXCEPT_HANDLER(b);
2698 Py_DECREF(POP());
2699 JUMPBY(oparg);
2700 FAST_DISPATCH();
2701 }
2702 else {
2703 PyObject *val = POP();
2704 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002705 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002706 goto exception_unwind;
2707 }
2708 }
2709
Zackery Spytzce6a0702019-08-25 03:44:09 -06002710 case TARGET(LOAD_ASSERTION_ERROR): {
2711 PyObject *value = PyExc_AssertionError;
2712 Py_INCREF(value);
2713 PUSH(value);
2714 FAST_DISPATCH();
2715 }
2716
Benjamin Petersonddd19492018-09-16 22:38:02 -07002717 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002718 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002719
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002721 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002722 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002724 if (!_PyErr_Occurred(tstate)) {
2725 _PyErr_SetString(tstate, PyExc_NameError,
2726 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002727 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002729 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002731 }
2732 else {
2733 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2734 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002735 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2737 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002738 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2739 _PyErr_SetString(tstate, PyExc_NameError,
2740 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002742 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002745 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002747
Benjamin Petersonddd19492018-09-16 22:38:02 -07002748 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 PyObject *name = GETITEM(names, oparg);
2750 PyObject *v = POP();
2751 PyObject *ns = f->f_locals;
2752 int err;
2753 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002754 _PyErr_Format(tstate, PyExc_SystemError,
2755 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 if (PyDict_CheckExact(ns))
2760 err = PyDict_SetItem(ns, name, v);
2761 else
2762 err = PyObject_SetItem(ns, name, v);
2763 Py_DECREF(v);
2764 if (err != 0)
2765 goto error;
2766 DISPATCH();
2767 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 PyObject *name = GETITEM(names, oparg);
2771 PyObject *ns = f->f_locals;
2772 int err;
2773 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002774 _PyErr_Format(tstate, PyExc_SystemError,
2775 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 err = PyObject_DelItem(ns, name);
2779 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002780 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 NAME_ERROR_MSG,
2782 name);
2783 goto error;
2784 }
2785 DISPATCH();
2786 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002787
Benjamin Petersonddd19492018-09-16 22:38:02 -07002788 case TARGET(UNPACK_SEQUENCE): {
2789 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 PyObject *seq = POP(), *item, **items;
2791 if (PyTuple_CheckExact(seq) &&
2792 PyTuple_GET_SIZE(seq) == oparg) {
2793 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 item = items[oparg];
2796 Py_INCREF(item);
2797 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 } else if (PyList_CheckExact(seq) &&
2800 PyList_GET_SIZE(seq) == oparg) {
2801 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 item = items[oparg];
2804 Py_INCREF(item);
2805 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002807 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002809 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 } else {
2811 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 Py_DECREF(seq);
2813 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002816 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002818
Benjamin Petersonddd19492018-09-16 22:38:02 -07002819 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2821 PyObject *seq = POP();
2822
Victor Stinner438a12d2019-05-24 17:01:38 +02002823 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 stack_pointer + totalargs)) {
2825 stack_pointer += totalargs;
2826 } else {
2827 Py_DECREF(seq);
2828 goto error;
2829 }
2830 Py_DECREF(seq);
2831 DISPATCH();
2832 }
2833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PyObject *name = GETITEM(names, oparg);
2836 PyObject *owner = TOP();
2837 PyObject *v = SECOND();
2838 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002839 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 Py_DECREF(owner);
2843 if (err != 0)
2844 goto error;
2845 DISPATCH();
2846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002847
Benjamin Petersonddd19492018-09-16 22:38:02 -07002848 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PyObject *name = GETITEM(names, oparg);
2850 PyObject *owner = POP();
2851 int err;
2852 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2853 Py_DECREF(owner);
2854 if (err != 0)
2855 goto error;
2856 DISPATCH();
2857 }
2858
Benjamin Petersonddd19492018-09-16 22:38:02 -07002859 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 PyObject *name = GETITEM(names, oparg);
2861 PyObject *v = POP();
2862 int err;
2863 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 if (err != 0)
2866 goto error;
2867 DISPATCH();
2868 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 PyObject *name = GETITEM(names, oparg);
2872 int err;
2873 err = PyDict_DelItem(f->f_globals, name);
2874 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002875 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2876 format_exc_check_arg(tstate, PyExc_NameError,
2877 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002878 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002880 }
2881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Benjamin Petersonddd19492018-09-16 22:38:02 -07002884 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 PyObject *name = GETITEM(names, oparg);
2886 PyObject *locals = f->f_locals;
2887 PyObject *v;
2888 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002889 _PyErr_Format(tstate, PyExc_SystemError,
2890 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002894 v = PyDict_GetItemWithError(locals, name);
2895 if (v != NULL) {
2896 Py_INCREF(v);
2897 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002898 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002899 goto error;
2900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 }
2902 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002904 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002905 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002906 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002907 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 }
2909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002911 v = PyDict_GetItemWithError(f->f_globals, name);
2912 if (v != NULL) {
2913 Py_INCREF(v);
2914 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002915 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002916 goto error;
2917 }
2918 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002919 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002920 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002922 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002923 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002924 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002930 }
2931 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 v = PyObject_GetItem(f->f_builtins, name);
2933 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002934 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002935 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002936 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002940 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002947
Benjamin Petersonddd19492018-09-16 22:38:02 -07002948 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002949 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002951 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002952 && PyDict_CheckExact(f->f_builtins))
2953 {
Inada Naoki91234a12019-06-03 21:30:58 +09002954 OPCACHE_CHECK();
2955 if (co_opcache != NULL && co_opcache->optimized > 0) {
2956 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2957
2958 if (lg->globals_ver ==
2959 ((PyDictObject *)f->f_globals)->ma_version_tag
2960 && lg->builtins_ver ==
2961 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2962 {
2963 PyObject *ptr = lg->ptr;
2964 OPCACHE_STAT_GLOBAL_HIT();
2965 assert(ptr != NULL);
2966 Py_INCREF(ptr);
2967 PUSH(ptr);
2968 DISPATCH();
2969 }
2970 }
2971
2972 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002974 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 name);
2976 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002977 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002978 /* _PyDict_LoadGlobal() returns NULL without raising
2979 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002980 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002981 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002982 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 }
Inada Naoki91234a12019-06-03 21:30:58 +09002985
2986 if (co_opcache != NULL) {
2987 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2988
2989 if (co_opcache->optimized == 0) {
2990 /* Wasn't optimized before. */
2991 OPCACHE_STAT_GLOBAL_OPT();
2992 } else {
2993 OPCACHE_STAT_GLOBAL_MISS();
2994 }
2995
2996 co_opcache->optimized = 1;
2997 lg->globals_ver =
2998 ((PyDictObject *)f->f_globals)->ma_version_tag;
2999 lg->builtins_ver =
3000 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3001 lg->ptr = v; /* borrowed */
3002 }
3003
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003006 else {
3007 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003008
3009 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003010 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 v = PyObject_GetItem(f->f_globals, name);
3012 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003013 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003014 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003015 }
3016 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003017
Victor Stinnerb4efc962015-11-20 09:24:02 +01003018 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 v = PyObject_GetItem(f->f_builtins, name);
3020 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003021 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003022 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003023 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003024 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003025 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003027 }
3028 }
3029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003033
Benjamin Petersonddd19492018-09-16 22:38:02 -07003034 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 PyObject *v = GETLOCAL(oparg);
3036 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 SETLOCAL(oparg, NULL);
3038 DISPATCH();
3039 }
3040 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003041 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 UNBOUNDLOCAL_ERROR_MSG,
3043 PyTuple_GetItem(co->co_varnames, oparg)
3044 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 goto error;
3046 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003050 PyObject *oldobj = PyCell_GET(cell);
3051 if (oldobj != NULL) {
3052 PyCell_SET(cell, NULL);
3053 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003054 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003055 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003056 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 goto error;
3058 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003059
Benjamin Petersonddd19492018-09-16 22:38:02 -07003060 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 PyObject *cell = freevars[oparg];
3062 Py_INCREF(cell);
3063 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003066
Benjamin Petersonddd19492018-09-16 22:38:02 -07003067 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003068 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003069 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003070 assert(locals);
3071 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3072 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3073 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3074 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3075 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003076 value = PyDict_GetItemWithError(locals, name);
3077 if (value != NULL) {
3078 Py_INCREF(value);
3079 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003080 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003081 goto error;
3082 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003083 }
3084 else {
3085 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003086 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003087 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003088 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003089 }
3090 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003091 }
3092 }
3093 if (!value) {
3094 PyObject *cell = freevars[oparg];
3095 value = PyCell_GET(cell);
3096 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003097 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003098 goto error;
3099 }
3100 Py_INCREF(value);
3101 }
3102 PUSH(value);
3103 DISPATCH();
3104 }
3105
Benjamin Petersonddd19492018-09-16 22:38:02 -07003106 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 PyObject *cell = freevars[oparg];
3108 PyObject *value = PyCell_GET(cell);
3109 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003110 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 Py_INCREF(value);
3114 PUSH(value);
3115 DISPATCH();
3116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003117
Benjamin Petersonddd19492018-09-16 22:38:02 -07003118 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PyObject *v = POP();
3120 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003121 PyObject *oldobj = PyCell_GET(cell);
3122 PyCell_SET(cell, v);
3123 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 DISPATCH();
3125 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003126
Benjamin Petersonddd19492018-09-16 22:38:02 -07003127 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003128 PyObject *str;
3129 PyObject *empty = PyUnicode_New(0, 0);
3130 if (empty == NULL) {
3131 goto error;
3132 }
3133 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3134 Py_DECREF(empty);
3135 if (str == NULL)
3136 goto error;
3137 while (--oparg >= 0) {
3138 PyObject *item = POP();
3139 Py_DECREF(item);
3140 }
3141 PUSH(str);
3142 DISPATCH();
3143 }
3144
Benjamin Petersonddd19492018-09-16 22:38:02 -07003145 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 PyObject *tup = PyTuple_New(oparg);
3147 if (tup == NULL)
3148 goto error;
3149 while (--oparg >= 0) {
3150 PyObject *item = POP();
3151 PyTuple_SET_ITEM(tup, oparg, item);
3152 }
3153 PUSH(tup);
3154 DISPATCH();
3155 }
3156
Benjamin Petersonddd19492018-09-16 22:38:02 -07003157 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 PyObject *list = PyList_New(oparg);
3159 if (list == NULL)
3160 goto error;
3161 while (--oparg >= 0) {
3162 PyObject *item = POP();
3163 PyList_SET_ITEM(list, oparg, item);
3164 }
3165 PUSH(list);
3166 DISPATCH();
3167 }
3168
Mark Shannon13bc1392020-01-23 09:25:17 +00003169 case TARGET(LIST_TO_TUPLE): {
3170 PyObject *list = POP();
3171 PyObject *tuple = PyList_AsTuple(list);
3172 Py_DECREF(list);
3173 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003174 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003175 }
3176 PUSH(tuple);
3177 DISPATCH();
3178 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003179
Mark Shannon13bc1392020-01-23 09:25:17 +00003180 case TARGET(LIST_EXTEND): {
3181 PyObject *iterable = POP();
3182 PyObject *list = PEEK(oparg);
3183 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3184 if (none_val == NULL) {
3185 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003186 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003187 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003188 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003189 _PyErr_Format(tstate, PyExc_TypeError,
3190 "Value after * must be an iterable, not %.200s",
3191 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003192 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003193 Py_DECREF(iterable);
3194 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003195 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003196 Py_DECREF(none_val);
3197 Py_DECREF(iterable);
3198 DISPATCH();
3199 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003200
Mark Shannon13bc1392020-01-23 09:25:17 +00003201 case TARGET(SET_UPDATE): {
3202 PyObject *iterable = POP();
3203 PyObject *set = PEEK(oparg);
3204 int err = _PySet_Update(set, iterable);
3205 Py_DECREF(iterable);
3206 if (err < 0) {
3207 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003208 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003209 DISPATCH();
3210 }
3211
Benjamin Petersonddd19492018-09-16 22:38:02 -07003212 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 PyObject *set = PySet_New(NULL);
3214 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003215 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 if (set == NULL)
3217 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003218 for (i = oparg; i > 0; i--) {
3219 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 if (err == 0)
3221 err = PySet_Add(set, item);
3222 Py_DECREF(item);
3223 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003224 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 if (err != 0) {
3226 Py_DECREF(set);
3227 goto error;
3228 }
3229 PUSH(set);
3230 DISPATCH();
3231 }
3232
Benjamin Petersonddd19492018-09-16 22:38:02 -07003233 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003234 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3236 if (map == NULL)
3237 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003238 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003239 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003240 PyObject *key = PEEK(2*i);
3241 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003242 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003243 if (err != 0) {
3244 Py_DECREF(map);
3245 goto error;
3246 }
3247 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003248
3249 while (oparg--) {
3250 Py_DECREF(POP());
3251 Py_DECREF(POP());
3252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 PUSH(map);
3254 DISPATCH();
3255 }
3256
Benjamin Petersonddd19492018-09-16 22:38:02 -07003257 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003258 _Py_IDENTIFIER(__annotations__);
3259 int err;
3260 PyObject *ann_dict;
3261 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003262 _PyErr_Format(tstate, PyExc_SystemError,
3263 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003264 goto error;
3265 }
3266 /* check if __annotations__ in locals()... */
3267 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003268 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003269 &PyId___annotations__);
3270 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003271 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003272 goto error;
3273 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003274 /* ...if not, create a new one */
3275 ann_dict = PyDict_New();
3276 if (ann_dict == NULL) {
3277 goto error;
3278 }
3279 err = _PyDict_SetItemId(f->f_locals,
3280 &PyId___annotations__, ann_dict);
3281 Py_DECREF(ann_dict);
3282 if (err != 0) {
3283 goto error;
3284 }
3285 }
3286 }
3287 else {
3288 /* do the same if locals() is not a dict */
3289 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3290 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003291 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003292 }
3293 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3294 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003295 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003296 goto error;
3297 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003298 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003299 ann_dict = PyDict_New();
3300 if (ann_dict == NULL) {
3301 goto error;
3302 }
3303 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3304 Py_DECREF(ann_dict);
3305 if (err != 0) {
3306 goto error;
3307 }
3308 }
3309 else {
3310 Py_DECREF(ann_dict);
3311 }
3312 }
3313 DISPATCH();
3314 }
3315
Benjamin Petersonddd19492018-09-16 22:38:02 -07003316 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003317 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003318 PyObject *map;
3319 PyObject *keys = TOP();
3320 if (!PyTuple_CheckExact(keys) ||
3321 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003322 _PyErr_SetString(tstate, PyExc_SystemError,
3323 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003324 goto error;
3325 }
3326 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3327 if (map == NULL) {
3328 goto error;
3329 }
3330 for (i = oparg; i > 0; i--) {
3331 int err;
3332 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3333 PyObject *value = PEEK(i + 1);
3334 err = PyDict_SetItem(map, key, value);
3335 if (err != 0) {
3336 Py_DECREF(map);
3337 goto error;
3338 }
3339 }
3340
3341 Py_DECREF(POP());
3342 while (oparg--) {
3343 Py_DECREF(POP());
3344 }
3345 PUSH(map);
3346 DISPATCH();
3347 }
3348
Mark Shannon8a4cd702020-01-27 09:57:45 +00003349 case TARGET(DICT_UPDATE): {
3350 PyObject *update = POP();
3351 PyObject *dict = PEEK(oparg);
3352 if (PyDict_Update(dict, update) < 0) {
3353 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3354 _PyErr_Format(tstate, PyExc_TypeError,
3355 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003356 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003357 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003358 Py_DECREF(update);
3359 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003360 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003361 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003362 DISPATCH();
3363 }
3364
Mark Shannon8a4cd702020-01-27 09:57:45 +00003365 case TARGET(DICT_MERGE): {
3366 PyObject *update = POP();
3367 PyObject *dict = PEEK(oparg);
3368
3369 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3370 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3371 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003372 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003373 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003374 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003375 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003376 DISPATCH();
3377 }
3378
Benjamin Petersonddd19492018-09-16 22:38:02 -07003379 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003380 PyObject *value = TOP();
3381 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 PyObject *map;
3383 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003384 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003385 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003387 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 Py_DECREF(value);
3389 Py_DECREF(key);
3390 if (err != 0)
3391 goto error;
3392 PREDICT(JUMP_ABSOLUTE);
3393 DISPATCH();
3394 }
3395
Benjamin Petersonddd19492018-09-16 22:38:02 -07003396 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 PyObject *name = GETITEM(names, oparg);
3398 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003399
3400 PyTypeObject *type = Py_TYPE(owner);
3401 PyObject *res;
3402 PyObject **dictptr;
3403 PyObject *dict;
3404 _PyOpCodeOpt_LoadAttr *la;
3405
3406 OPCACHE_STAT_ATTR_TOTAL();
3407
3408 OPCACHE_CHECK();
3409 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3410 {
3411 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003412 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003413 la = &co_opcache->u.la;
3414 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3415 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003416 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3417 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3418 // so ~offset is strictly < -1 (assuming 2's complement).
3419 if (la->hint < -1) {
3420 // Even faster path -- slot hint.
3421 Py_ssize_t offset = ~la->hint;
3422 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3423 char *addr = (char *)owner + offset;
3424 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003425 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003426 Py_INCREF(res);
3427 SET_TOP(res);
3428 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003429 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003430 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003431 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3432 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003433 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003434 // Fast path for dict.
3435 assert(type->tp_dict != NULL);
3436 assert(type->tp_dictoffset > 0);
3437
3438 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3439 dict = *dictptr;
3440 if (dict != NULL && PyDict_CheckExact(dict)) {
3441 Py_ssize_t hint = la->hint;
3442 Py_INCREF(dict);
3443 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003444 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003445 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003446 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003447 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003448 if (la->hint == hint && hint >= 0) {
3449 // Our hint has helped -- cache hit.
3450 OPCACHE_STAT_ATTR_HIT();
3451 } else {
3452 // The hint we provided didn't work.
3453 // Maybe next time?
3454 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3455 }
3456
3457 Py_INCREF(res);
3458 SET_TOP(res);
3459 Py_DECREF(owner);
3460 Py_DECREF(dict);
3461 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003462 }
3463 else {
3464 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003465 // This attribute can be missing sometimes;
3466 // we don't want to optimize this lookup.
3467 OPCACHE_DEOPT_LOAD_ATTR();
3468 Py_DECREF(dict);
3469 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003470 }
3471 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003472 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3473 OPCACHE_DEOPT_LOAD_ATTR();
3474 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003475 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003476 }
3477 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003478 // The type of the object has either been updated,
3479 // or is different. Maybe it will stabilize?
3480 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3481 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003482 OPCACHE_STAT_ATTR_MISS();
3483 }
3484
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003485 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003486 type->tp_getattro == PyObject_GenericGetAttr)
3487 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003488 if (type->tp_dict == NULL) {
3489 if (PyType_Ready(type) < 0) {
3490 Py_DECREF(owner);
3491 SET_TOP(NULL);
3492 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003493 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003494 }
3495 PyObject *descr = _PyType_Lookup(type, name);
3496 if (descr != NULL) {
3497 // We found an attribute with a data-like descriptor.
3498 PyTypeObject *dtype = Py_TYPE(descr);
3499 if (dtype == &PyMemberDescr_Type) { // It's a slot
3500 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3501 struct PyMemberDef *dmem = member->d_member;
3502 if (dmem->type == T_OBJECT_EX) {
3503 Py_ssize_t offset = dmem->offset;
3504 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003505
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003506 if (co_opcache->optimized == 0) {
3507 // First time we optimize this opcode.
3508 OPCACHE_STAT_ATTR_OPT();
3509 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3510 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3511 }
3512
3513 la = &co_opcache->u.la;
3514 la->type = type;
3515 la->tp_version_tag = type->tp_version_tag;
3516 la->hint = ~offset;
3517
3518 char *addr = (char *)owner + offset;
3519 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003520 if (res != NULL) {
3521 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003522 Py_DECREF(owner);
3523 SET_TOP(res);
3524
Pablo Galindo109826c2020-10-20 06:22:44 +01003525 DISPATCH();
3526 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003527 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003528 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003529 // Else it's a slot of a different type. We don't handle those.
3530 }
3531 // Else it's some other kind of descriptor that we don't handle.
3532 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003533 }
3534 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003535 // We found an instance with a __dict__.
3536 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3537 dict = *dictptr;
3538
3539 if (dict != NULL && PyDict_CheckExact(dict)) {
3540 Py_INCREF(dict);
3541 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003542 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003543 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3544 if (res != NULL) {
3545 Py_INCREF(res);
3546 Py_DECREF(dict);
3547 Py_DECREF(owner);
3548 SET_TOP(res);
3549
3550 if (co_opcache->optimized == 0) {
3551 // First time we optimize this opcode.
3552 OPCACHE_STAT_ATTR_OPT();
3553 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3554 }
3555
3556 la = &co_opcache->u.la;
3557 la->type = type;
3558 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003559 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003560 la->hint = hint;
3561
3562 DISPATCH();
3563 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003564 else {
3565 _PyErr_Clear(tstate);
3566 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003567 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003568 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003569 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003570 OPCACHE_DEOPT_LOAD_ATTR();
3571 }
3572 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003573 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003574 OPCACHE_DEOPT_LOAD_ATTR();
3575 }
3576 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3577 OPCACHE_DEOPT_LOAD_ATTR();
3578 }
3579 }
3580
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003581 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003582 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 Py_DECREF(owner);
3584 SET_TOP(res);
3585 if (res == NULL)
3586 goto error;
3587 DISPATCH();
3588 }
3589
Benjamin Petersonddd19492018-09-16 22:38:02 -07003590 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003591 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 PyObject *right = POP();
3593 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003594 PyObject *res = PyObject_RichCompare(left, right, oparg);
3595 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003596 Py_DECREF(left);
3597 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003598 if (res == NULL)
3599 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 PREDICT(POP_JUMP_IF_FALSE);
3601 PREDICT(POP_JUMP_IF_TRUE);
3602 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003604
Mark Shannon9af0e472020-01-14 10:12:45 +00003605 case TARGET(IS_OP): {
3606 PyObject *right = POP();
3607 PyObject *left = TOP();
3608 int res = (left == right)^oparg;
3609 PyObject *b = res ? Py_True : Py_False;
3610 Py_INCREF(b);
3611 SET_TOP(b);
3612 Py_DECREF(left);
3613 Py_DECREF(right);
3614 PREDICT(POP_JUMP_IF_FALSE);
3615 PREDICT(POP_JUMP_IF_TRUE);
3616 FAST_DISPATCH();
3617 }
3618
3619 case TARGET(CONTAINS_OP): {
3620 PyObject *right = POP();
3621 PyObject *left = POP();
3622 int res = PySequence_Contains(right, left);
3623 Py_DECREF(left);
3624 Py_DECREF(right);
3625 if (res < 0) {
3626 goto error;
3627 }
3628 PyObject *b = (res^oparg) ? Py_True : Py_False;
3629 Py_INCREF(b);
3630 PUSH(b);
3631 PREDICT(POP_JUMP_IF_FALSE);
3632 PREDICT(POP_JUMP_IF_TRUE);
3633 FAST_DISPATCH();
3634 }
3635
3636#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3637 "BaseException is not allowed"
3638
3639 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3640 PyObject *right = POP();
3641 PyObject *left = POP();
3642 if (PyTuple_Check(right)) {
3643 Py_ssize_t i, length;
3644 length = PyTuple_GET_SIZE(right);
3645 for (i = 0; i < length; i++) {
3646 PyObject *exc = PyTuple_GET_ITEM(right, i);
3647 if (!PyExceptionClass_Check(exc)) {
3648 _PyErr_SetString(tstate, PyExc_TypeError,
3649 CANNOT_CATCH_MSG);
3650 Py_DECREF(left);
3651 Py_DECREF(right);
3652 goto error;
3653 }
3654 }
3655 }
3656 else {
3657 if (!PyExceptionClass_Check(right)) {
3658 _PyErr_SetString(tstate, PyExc_TypeError,
3659 CANNOT_CATCH_MSG);
3660 Py_DECREF(left);
3661 Py_DECREF(right);
3662 goto error;
3663 }
3664 }
3665 int res = PyErr_GivenExceptionMatches(left, right);
3666 Py_DECREF(left);
3667 Py_DECREF(right);
3668 if (res > 0) {
3669 /* Exception matches -- Do nothing */;
3670 }
3671 else if (res == 0) {
3672 JUMPTO(oparg);
3673 }
3674 else {
3675 goto error;
3676 }
3677 DISPATCH();
3678 }
3679
Benjamin Petersonddd19492018-09-16 22:38:02 -07003680 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003681 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003682 PyObject *fromlist = POP();
3683 PyObject *level = TOP();
3684 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003685 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003686 Py_DECREF(level);
3687 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003688 SET_TOP(res);
3689 if (res == NULL)
3690 goto error;
3691 DISPATCH();
3692 }
3693
Benjamin Petersonddd19492018-09-16 22:38:02 -07003694 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003695 PyObject *from = POP(), *locals;
3696 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003697 if (PyFrame_FastToLocalsWithError(f) < 0) {
3698 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003699 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003700 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003701
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003702 locals = f->f_locals;
3703 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003704 _PyErr_SetString(tstate, PyExc_SystemError,
3705 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003706 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003707 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003709 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711 Py_DECREF(from);
3712 if (err != 0)
3713 goto error;
3714 DISPATCH();
3715 }
Guido van Rossum25831651993-05-19 14:50:45 +00003716
Benjamin Petersonddd19492018-09-16 22:38:02 -07003717 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003718 PyObject *name = GETITEM(names, oparg);
3719 PyObject *from = TOP();
3720 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003721 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003722 PUSH(res);
3723 if (res == NULL)
3724 goto error;
3725 DISPATCH();
3726 }
Thomas Wouters52152252000-08-17 22:55:00 +00003727
Benjamin Petersonddd19492018-09-16 22:38:02 -07003728 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 JUMPBY(oparg);
3730 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003732
Benjamin Petersonddd19492018-09-16 22:38:02 -07003733 case TARGET(POP_JUMP_IF_FALSE): {
3734 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003735 PyObject *cond = POP();
3736 int err;
3737 if (cond == Py_True) {
3738 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 FAST_DISPATCH();
3740 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003741 if (cond == Py_False) {
3742 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 JUMPTO(oparg);
3744 FAST_DISPATCH();
3745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003746 err = PyObject_IsTrue(cond);
3747 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003749 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 else if (err == 0)
3751 JUMPTO(oparg);
3752 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003753 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003755 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003756
Benjamin Petersonddd19492018-09-16 22:38:02 -07003757 case TARGET(POP_JUMP_IF_TRUE): {
3758 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003759 PyObject *cond = POP();
3760 int err;
3761 if (cond == Py_False) {
3762 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 FAST_DISPATCH();
3764 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003765 if (cond == Py_True) {
3766 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 JUMPTO(oparg);
3768 FAST_DISPATCH();
3769 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003770 err = PyObject_IsTrue(cond);
3771 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 JUMPTO(oparg);
3774 }
3775 else if (err == 0)
3776 ;
3777 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003778 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003781
Benjamin Petersonddd19492018-09-16 22:38:02 -07003782 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003783 PyObject *cond = TOP();
3784 int err;
3785 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003786 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 FAST_DISPATCH();
3789 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003790 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 JUMPTO(oparg);
3792 FAST_DISPATCH();
3793 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003794 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003796 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003797 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 }
3799 else if (err == 0)
3800 JUMPTO(oparg);
3801 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003802 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003804 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003805
Benjamin Petersonddd19492018-09-16 22:38:02 -07003806 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003807 PyObject *cond = TOP();
3808 int err;
3809 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003810 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003811 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 FAST_DISPATCH();
3813 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003814 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 JUMPTO(oparg);
3816 FAST_DISPATCH();
3817 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003818 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 JUMPTO(oparg);
3821 }
3822 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003823 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003824 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 }
3826 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003827 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003830
Benjamin Petersonddd19492018-09-16 22:38:02 -07003831 case TARGET(JUMP_ABSOLUTE): {
3832 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003834#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 /* Enabling this path speeds-up all while and for-loops by bypassing
3836 the per-loop checks for signals. By default, this should be turned-off
3837 because it prevents detection of a control-break in tight loops like
3838 "while 1: pass". Compile with this option turned-on when you need
3839 the speed-up and do not need break checking inside tight loops (ones
3840 that contain only instructions ending with FAST_DISPATCH).
3841 */
3842 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003843#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003845#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003847
Brandt Bucher145bf262021-02-26 14:51:55 -08003848 case TARGET(GET_LEN): {
3849 // PUSH(len(TOS))
3850 Py_ssize_t len_i = PyObject_Length(TOP());
3851 if (len_i < 0) {
3852 goto error;
3853 }
3854 PyObject *len_o = PyLong_FromSsize_t(len_i);
3855 if (len_o == NULL) {
3856 goto error;
3857 }
3858 PUSH(len_o);
3859 DISPATCH();
3860 }
3861
3862 case TARGET(MATCH_CLASS): {
3863 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3864 // attributes. On failure, set TOS to False.
3865 PyObject *names = POP();
3866 PyObject *type = TOP();
3867 PyObject *subject = SECOND();
3868 assert(PyTuple_CheckExact(names));
3869 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3870 Py_DECREF(names);
3871 if (attrs) {
3872 // Success!
3873 assert(PyTuple_CheckExact(attrs));
3874 Py_DECREF(subject);
3875 SET_SECOND(attrs);
3876 }
3877 else if (_PyErr_Occurred(tstate)) {
3878 goto error;
3879 }
3880 Py_DECREF(type);
3881 SET_TOP(PyBool_FromLong(!!attrs));
3882 DISPATCH();
3883 }
3884
3885 case TARGET(MATCH_MAPPING): {
3886 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3887 PyObject *subject = TOP();
3888 // Fast path for dicts:
3889 if (PyDict_Check(subject)) {
3890 Py_INCREF(Py_True);
3891 PUSH(Py_True);
3892 DISPATCH();
3893 }
3894 // Lazily import _collections_abc.Mapping, and keep it handy on the
3895 // PyInterpreterState struct (it gets cleaned up at exit):
3896 PyInterpreterState *interp = PyInterpreterState_Get();
3897 if (interp->map_abc == NULL) {
3898 PyObject *abc = PyImport_ImportModule("_collections_abc");
3899 if (abc == NULL) {
3900 goto error;
3901 }
3902 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3903 if (interp->map_abc == NULL) {
3904 goto error;
3905 }
3906 }
3907 int match = PyObject_IsInstance(subject, interp->map_abc);
3908 if (match < 0) {
3909 goto error;
3910 }
3911 PUSH(PyBool_FromLong(match));
3912 DISPATCH();
3913 }
3914
3915 case TARGET(MATCH_SEQUENCE): {
3916 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3917 // and isinstance(TOS, _collections_abc.Sequence))
3918 PyObject *subject = TOP();
3919 // Fast path for lists and tuples:
3920 if (PyType_FastSubclass(Py_TYPE(subject),
3921 Py_TPFLAGS_LIST_SUBCLASS |
3922 Py_TPFLAGS_TUPLE_SUBCLASS))
3923 {
3924 Py_INCREF(Py_True);
3925 PUSH(Py_True);
3926 DISPATCH();
3927 }
3928 // Bail on some possible Sequences that we intentionally exclude:
3929 if (PyType_FastSubclass(Py_TYPE(subject),
3930 Py_TPFLAGS_BYTES_SUBCLASS |
3931 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3932 PyByteArray_Check(subject))
3933 {
3934 Py_INCREF(Py_False);
3935 PUSH(Py_False);
3936 DISPATCH();
3937 }
3938 // Lazily import _collections_abc.Sequence, and keep it handy on the
3939 // PyInterpreterState struct (it gets cleaned up at exit):
3940 PyInterpreterState *interp = PyInterpreterState_Get();
3941 if (interp->seq_abc == NULL) {
3942 PyObject *abc = PyImport_ImportModule("_collections_abc");
3943 if (abc == NULL) {
3944 goto error;
3945 }
3946 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3947 if (interp->seq_abc == NULL) {
3948 goto error;
3949 }
3950 }
3951 int match = PyObject_IsInstance(subject, interp->seq_abc);
3952 if (match < 0) {
3953 goto error;
3954 }
3955 PUSH(PyBool_FromLong(match));
3956 DISPATCH();
3957 }
3958
3959 case TARGET(MATCH_KEYS): {
3960 // On successful match for all keys, PUSH(values) and PUSH(True).
3961 // Otherwise, PUSH(None) and PUSH(False).
3962 PyObject *keys = TOP();
3963 PyObject *subject = SECOND();
3964 PyObject *values_or_none = match_keys(tstate, subject, keys);
3965 if (values_or_none == NULL) {
3966 goto error;
3967 }
3968 PUSH(values_or_none);
3969 if (values_or_none == Py_None) {
3970 Py_INCREF(Py_False);
3971 PUSH(Py_False);
3972 DISPATCH();
3973 }
3974 assert(PyTuple_CheckExact(values_or_none));
3975 Py_INCREF(Py_True);
3976 PUSH(Py_True);
3977 DISPATCH();
3978 }
3979
3980 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3981 // rest = dict(TOS1)
3982 // for key in TOS:
3983 // del rest[key]
3984 // SET_TOP(rest)
3985 PyObject *keys = TOP();
3986 PyObject *subject = SECOND();
3987 PyObject *rest = PyDict_New();
3988 if (rest == NULL || PyDict_Update(rest, subject)) {
3989 Py_XDECREF(rest);
3990 goto error;
3991 }
3992 // This may seem a bit inefficient, but keys is rarely big enough to
3993 // actually impact runtime.
3994 assert(PyTuple_CheckExact(keys));
3995 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3996 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3997 Py_DECREF(rest);
3998 goto error;
3999 }
4000 }
4001 Py_DECREF(keys);
4002 SET_TOP(rest);
4003 DISPATCH();
4004 }
4005
Benjamin Petersonddd19492018-09-16 22:38:02 -07004006 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004008 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004009 PyObject *iter = PyObject_GetIter(iterable);
4010 Py_DECREF(iterable);
4011 SET_TOP(iter);
4012 if (iter == NULL)
4013 goto error;
4014 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004015 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004016 DISPATCH();
4017 }
4018
Benjamin Petersonddd19492018-09-16 22:38:02 -07004019 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004020 /* before: [obj]; after [getiter(obj)] */
4021 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004022 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004023 if (PyCoro_CheckExact(iterable)) {
4024 /* `iterable` is a coroutine */
4025 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4026 /* and it is used in a 'yield from' expression of a
4027 regular generator. */
4028 Py_DECREF(iterable);
4029 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004030 _PyErr_SetString(tstate, PyExc_TypeError,
4031 "cannot 'yield from' a coroutine object "
4032 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004033 goto error;
4034 }
4035 }
4036 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004037 /* `iterable` is not a generator. */
4038 iter = PyObject_GetIter(iterable);
4039 Py_DECREF(iterable);
4040 SET_TOP(iter);
4041 if (iter == NULL)
4042 goto error;
4043 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004044 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004045 DISPATCH();
4046 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004047
Benjamin Petersonddd19492018-09-16 22:38:02 -07004048 case TARGET(FOR_ITER): {
4049 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004051 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004052 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004053 if (next != NULL) {
4054 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 PREDICT(STORE_FAST);
4056 PREDICT(UNPACK_SEQUENCE);
4057 DISPATCH();
4058 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 if (_PyErr_Occurred(tstate)) {
4060 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004061 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004062 }
4063 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00004064 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02004065 }
4066 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
4068 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004069 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004070 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 JUMPBY(oparg);
4072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004073 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004074
Benjamin Petersonddd19492018-09-16 22:38:02 -07004075 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004076 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 STACK_LEVEL());
4078 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004079 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004080
Benjamin Petersonddd19492018-09-16 22:38:02 -07004081 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004082 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004083 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004084 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004085 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004086 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004087 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004088 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004089 }
4090 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4091 if (exit == NULL) {
4092 Py_DECREF(enter);
4093 goto error;
4094 }
Yury Selivanov75445082015-05-11 22:57:16 -04004095 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004096 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004097 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004098 Py_DECREF(enter);
4099 if (res == NULL)
4100 goto error;
4101 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004102 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004103 DISPATCH();
4104 }
4105
Benjamin Petersonddd19492018-09-16 22:38:02 -07004106 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004107 PyObject *res = POP();
4108 /* Setup the finally block before pushing the result
4109 of __aenter__ on the stack. */
4110 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4111 STACK_LEVEL());
4112 PUSH(res);
4113 DISPATCH();
4114 }
4115
Benjamin Petersonddd19492018-09-16 22:38:02 -07004116 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004117 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004118 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004119 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004120 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004121 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004122 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004123 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004124 }
4125 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004126 if (exit == NULL) {
4127 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004128 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004130 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004131 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004132 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004133 Py_DECREF(enter);
4134 if (res == NULL)
4135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 /* Setup the finally block before pushing the result
4137 of __enter__ on the stack. */
4138 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4139 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004141 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 DISPATCH();
4143 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004144
Mark Shannonfee55262019-11-21 09:11:43 +00004145 case TARGET(WITH_EXCEPT_START): {
4146 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004148 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4149 - SEVENTH: the context.__exit__ bound method
4150 We call SEVENTH(TOP, SECOND, THIRD).
4151 Then we push again the TOP exception and the __exit__
4152 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004155 PyObject *exc, *val, *tb, *res;
4156
Victor Stinner842cfff2016-12-01 14:45:31 +01004157 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004158 val = SECOND();
4159 tb = THIRD();
4160 assert(exc != Py_None);
4161 assert(!PyLong_Check(exc));
4162 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004163 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004164 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004165 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004166 if (res == NULL)
4167 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004168
Yury Selivanov75445082015-05-11 22:57:16 -04004169 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004170 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004172
Benjamin Petersonddd19492018-09-16 22:38:02 -07004173 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004174 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004175 PyObject *name = GETITEM(names, oparg);
4176 PyObject *obj = TOP();
4177 PyObject *meth = NULL;
4178
4179 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4180
Yury Selivanovf2392132016-12-13 19:03:51 -05004181 if (meth == NULL) {
4182 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004183 goto error;
4184 }
4185
4186 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004187 /* We can bypass temporary bound method object.
4188 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004189
INADA Naoki015bce62017-01-16 17:23:30 +09004190 meth | self | arg1 | ... | argN
4191 */
4192 SET_TOP(meth);
4193 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004194 }
4195 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004196 /* meth is not an unbound method (but a regular attr, or
4197 something was returned by a descriptor protocol). Set
4198 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004199 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004200
4201 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004202 */
INADA Naoki015bce62017-01-16 17:23:30 +09004203 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004204 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004205 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004206 }
4207 DISPATCH();
4208 }
4209
Benjamin Petersonddd19492018-09-16 22:38:02 -07004210 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004211 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004212 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004213
4214 sp = stack_pointer;
4215
INADA Naoki015bce62017-01-16 17:23:30 +09004216 meth = PEEK(oparg + 2);
4217 if (meth == NULL) {
4218 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4219 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004220
4221 Stack layout:
4222
INADA Naoki015bce62017-01-16 17:23:30 +09004223 ... | NULL | callable | arg1 | ... | argN
4224 ^- TOP()
4225 ^- (-oparg)
4226 ^- (-oparg-1)
4227 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004228
Ville Skyttä49b27342017-08-03 09:00:59 +03004229 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004230 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004231 */
Mark Shannon86433452021-01-07 16:49:02 +00004232 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004233 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004234 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004235 }
4236 else {
4237 /* This is a method call. Stack layout:
4238
INADA Naoki015bce62017-01-16 17:23:30 +09004239 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004240 ^- TOP()
4241 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004242 ^- (-oparg-1)
4243 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004244
INADA Naoki015bce62017-01-16 17:23:30 +09004245 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004246 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004247 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004248 */
Mark Shannon86433452021-01-07 16:49:02 +00004249 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004250 stack_pointer = sp;
4251 }
4252
4253 PUSH(res);
4254 if (res == NULL)
4255 goto error;
4256 DISPATCH();
4257 }
4258
Benjamin Petersonddd19492018-09-16 22:38:02 -07004259 case TARGET(CALL_FUNCTION): {
4260 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004261 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004263 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004265 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004266 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004267 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004269 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004271
Benjamin Petersonddd19492018-09-16 22:38:02 -07004272 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004273 PyObject **sp, *res, *names;
4274
4275 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004276 assert(PyTuple_Check(names));
4277 assert(PyTuple_GET_SIZE(names) <= oparg);
4278 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004280 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004282 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004283 Py_DECREF(names);
4284
4285 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004286 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004287 }
4288 DISPATCH();
4289 }
4290
Benjamin Petersonddd19492018-09-16 22:38:02 -07004291 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004292 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004293 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004294 if (oparg & 0x01) {
4295 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004296 if (!PyDict_CheckExact(kwargs)) {
4297 PyObject *d = PyDict_New();
4298 if (d == NULL)
4299 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004300 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004301 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004302 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004303 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004304 goto error;
4305 }
4306 Py_DECREF(kwargs);
4307 kwargs = d;
4308 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004309 assert(PyDict_CheckExact(kwargs));
4310 }
4311 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004312 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004313 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004314 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004315 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004316 goto error;
4317 }
4318 Py_SETREF(callargs, PySequence_Tuple(callargs));
4319 if (callargs == NULL) {
4320 goto error;
4321 }
4322 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004323 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004324
Mark Shannon86433452021-01-07 16:49:02 +00004325 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004326 Py_DECREF(func);
4327 Py_DECREF(callargs);
4328 Py_XDECREF(kwargs);
4329
4330 SET_TOP(result);
4331 if (result == NULL) {
4332 goto error;
4333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004334 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004336
Benjamin Petersonddd19492018-09-16 22:38:02 -07004337 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004338 PyObject *qualname = POP();
4339 PyObject *codeobj = POP();
4340 PyFunctionObject *func = (PyFunctionObject *)
4341 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004342
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004343 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004344 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004345 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004346 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004348
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004349 if (oparg & 0x08) {
4350 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004351 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004352 }
4353 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004354 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004355 func->func_annotations = POP();
4356 }
4357 if (oparg & 0x02) {
4358 assert(PyDict_CheckExact(TOP()));
4359 func->func_kwdefaults = POP();
4360 }
4361 if (oparg & 0x01) {
4362 assert(PyTuple_CheckExact(TOP()));
4363 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004365
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004366 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004367 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004369
Benjamin Petersonddd19492018-09-16 22:38:02 -07004370 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004371 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004373 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004375 step = NULL;
4376 stop = POP();
4377 start = TOP();
4378 slice = PySlice_New(start, stop, step);
4379 Py_DECREF(start);
4380 Py_DECREF(stop);
4381 Py_XDECREF(step);
4382 SET_TOP(slice);
4383 if (slice == NULL)
4384 goto error;
4385 DISPATCH();
4386 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004387
Benjamin Petersonddd19492018-09-16 22:38:02 -07004388 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004389 /* Handles f-string value formatting. */
4390 PyObject *result;
4391 PyObject *fmt_spec;
4392 PyObject *value;
4393 PyObject *(*conv_fn)(PyObject *);
4394 int which_conversion = oparg & FVC_MASK;
4395 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4396
4397 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004398 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004399
4400 /* See if any conversion is specified. */
4401 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004402 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004403 case FVC_STR: conv_fn = PyObject_Str; break;
4404 case FVC_REPR: conv_fn = PyObject_Repr; break;
4405 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004406 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004407 _PyErr_Format(tstate, PyExc_SystemError,
4408 "unexpected conversion flag %d",
4409 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004410 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004411 }
4412
4413 /* If there's a conversion function, call it and replace
4414 value with that result. Otherwise, just use value,
4415 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004416 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004417 result = conv_fn(value);
4418 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004419 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004420 Py_XDECREF(fmt_spec);
4421 goto error;
4422 }
4423 value = result;
4424 }
4425
4426 /* If value is a unicode object, and there's no fmt_spec,
4427 then we know the result of format(value) is value
4428 itself. In that case, skip calling format(). I plan to
4429 move this optimization in to PyObject_Format()
4430 itself. */
4431 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4432 /* Do nothing, just transfer ownership to result. */
4433 result = value;
4434 } else {
4435 /* Actually call format(). */
4436 result = PyObject_Format(value, fmt_spec);
4437 Py_DECREF(value);
4438 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004439 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004440 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004441 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004442 }
4443
Eric V. Smith135d5f42016-02-05 18:23:08 -05004444 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004445 DISPATCH();
4446 }
4447
Benjamin Petersonddd19492018-09-16 22:38:02 -07004448 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004449 int oldoparg = oparg;
4450 NEXTOPARG();
4451 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004453 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004454
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004455
Antoine Pitrou042b1282010-08-13 21:15:58 +00004456#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 default:
4460 fprintf(stderr,
4461 "XXX lineno: %d, opcode: %d\n",
4462 PyFrame_GetLineNumber(f),
4463 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004464 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004465 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004469 /* This should never be reached. Every opcode should end with DISPATCH()
4470 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004471 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004473error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004474 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004475#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 if (!_PyErr_Occurred(tstate)) {
4477 _PyErr_SetString(tstate, PyExc_SystemError,
4478 "error return without exception set");
4479 }
Victor Stinner365b6932013-07-12 00:11:58 +02004480#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004481 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004482#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004483
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004484 /* Log traceback info. */
4485 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004486
Mark Shannoncb9879b2020-07-17 11:44:23 +01004487 if (tstate->c_tracefunc != NULL) {
4488 /* Make sure state is set to FRAME_EXECUTING for tracing */
4489 assert(f->f_state == FRAME_EXECUTING);
4490 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004491 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004492 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004493 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004494exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004495 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004496 /* Unwind stacks if an exception occurred */
4497 while (f->f_iblock > 0) {
4498 /* Pop the current block. */
4499 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 if (b->b_type == EXCEPT_HANDLER) {
4502 UNWIND_EXCEPT_HANDLER(b);
4503 continue;
4504 }
4505 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004506 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 PyObject *exc, *val, *tb;
4508 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004509 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004511 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004512 PUSH(exc_info->exc_traceback);
4513 PUSH(exc_info->exc_value);
4514 if (exc_info->exc_type != NULL) {
4515 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 }
4517 else {
4518 Py_INCREF(Py_None);
4519 PUSH(Py_None);
4520 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004521 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 /* Make the raw exception data
4523 available to the handler,
4524 so a program can emulate the
4525 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004526 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004527 if (tb != NULL)
4528 PyException_SetTraceback(val, tb);
4529 else
4530 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004532 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004534 exc_info->exc_value = val;
4535 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 if (tb == NULL)
4537 tb = Py_None;
4538 Py_INCREF(tb);
4539 PUSH(tb);
4540 PUSH(val);
4541 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004543 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004544 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004545 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004546 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004547 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004548 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 }
4550 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004551
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004552 /* End the loop as we still have an error */
4553 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004555
Pablo Galindof00828a2019-05-09 16:52:02 +01004556 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004557 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 /* Pop remaining stack entries. */
4560 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004561 PyObject *o = POP();
4562 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004564 f->f_stackdepth = 0;
4565 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004566exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004568 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004569 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004570 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004571 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 }
4573 }
4574 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004575 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004576 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004577 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 }
4579 }
4580 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004583exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004584 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4585 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004586 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004588
Victor Stinner0b72b232020-03-12 23:18:39 +01004589 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004590}
4591
Benjamin Petersonb204a422011-06-05 22:04:07 -05004592static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004593format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004594 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004595{
4596 int err;
4597 Py_ssize_t len = PyList_GET_SIZE(names);
4598 PyObject *name_str, *comma, *tail, *tmp;
4599
4600 assert(PyList_CheckExact(names));
4601 assert(len >= 1);
4602 /* Deal with the joys of natural language. */
4603 switch (len) {
4604 case 1:
4605 name_str = PyList_GET_ITEM(names, 0);
4606 Py_INCREF(name_str);
4607 break;
4608 case 2:
4609 name_str = PyUnicode_FromFormat("%U and %U",
4610 PyList_GET_ITEM(names, len - 2),
4611 PyList_GET_ITEM(names, len - 1));
4612 break;
4613 default:
4614 tail = PyUnicode_FromFormat(", %U, and %U",
4615 PyList_GET_ITEM(names, len - 2),
4616 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004617 if (tail == NULL)
4618 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004619 /* Chop off the last two objects in the list. This shouldn't actually
4620 fail, but we can't be too careful. */
4621 err = PyList_SetSlice(names, len - 2, len, NULL);
4622 if (err == -1) {
4623 Py_DECREF(tail);
4624 return;
4625 }
4626 /* Stitch everything up into a nice comma-separated list. */
4627 comma = PyUnicode_FromString(", ");
4628 if (comma == NULL) {
4629 Py_DECREF(tail);
4630 return;
4631 }
4632 tmp = PyUnicode_Join(comma, names);
4633 Py_DECREF(comma);
4634 if (tmp == NULL) {
4635 Py_DECREF(tail);
4636 return;
4637 }
4638 name_str = PyUnicode_Concat(tmp, tail);
4639 Py_DECREF(tmp);
4640 Py_DECREF(tail);
4641 break;
4642 }
4643 if (name_str == NULL)
4644 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004645 _PyErr_Format(tstate, PyExc_TypeError,
4646 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004647 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004648 len,
4649 kind,
4650 len == 1 ? "" : "s",
4651 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004652 Py_DECREF(name_str);
4653}
4654
4655static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004656missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4657 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004658 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004659{
Victor Stinner74319ae2016-08-25 00:04:09 +02004660 Py_ssize_t i, j = 0;
4661 Py_ssize_t start, end;
4662 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004663 const char *kind = positional ? "positional" : "keyword-only";
4664 PyObject *missing_names;
4665
4666 /* Compute the names of the arguments that are missing. */
4667 missing_names = PyList_New(missing);
4668 if (missing_names == NULL)
4669 return;
4670 if (positional) {
4671 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004672 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004673 }
4674 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004675 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004676 end = start + co->co_kwonlyargcount;
4677 }
4678 for (i = start; i < end; i++) {
4679 if (GETLOCAL(i) == NULL) {
4680 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4681 PyObject *name = PyObject_Repr(raw);
4682 if (name == NULL) {
4683 Py_DECREF(missing_names);
4684 return;
4685 }
4686 PyList_SET_ITEM(missing_names, j++, name);
4687 }
4688 }
4689 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004690 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004691 Py_DECREF(missing_names);
4692}
4693
4694static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004695too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004696 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004697 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004698{
4699 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004700 Py_ssize_t kwonly_given = 0;
4701 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004702 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004703 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004704
Benjamin Petersone109c702011-06-24 09:37:26 -05004705 assert((co->co_flags & CO_VARARGS) == 0);
4706 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004707 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004708 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004709 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004710 }
4711 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004712 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004713 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004714 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004715 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004716 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004717 }
4718 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004719 plural = (co_argcount != 1);
4720 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004721 }
4722 if (sig == NULL)
4723 return;
4724 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004725 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4726 kwonly_sig = PyUnicode_FromFormat(format,
4727 given != 1 ? "s" : "",
4728 kwonly_given,
4729 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004730 if (kwonly_sig == NULL) {
4731 Py_DECREF(sig);
4732 return;
4733 }
4734 }
4735 else {
4736 /* This will not fail. */
4737 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004738 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004739 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004740 _PyErr_Format(tstate, PyExc_TypeError,
4741 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004742 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004743 sig,
4744 plural ? "s" : "",
4745 given,
4746 kwonly_sig,
4747 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004748 Py_DECREF(sig);
4749 Py_DECREF(kwonly_sig);
4750}
4751
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004752static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004753positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004754 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004755 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004756{
4757 int posonly_conflicts = 0;
4758 PyObject* posonly_names = PyList_New(0);
4759
4760 for(int k=0; k < co->co_posonlyargcount; k++){
4761 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4762
4763 for (int k2=0; k2<kwcount; k2++){
4764 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004765 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004766 if (kwname == posonly_name){
4767 if(PyList_Append(posonly_names, kwname) != 0) {
4768 goto fail;
4769 }
4770 posonly_conflicts++;
4771 continue;
4772 }
4773
4774 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4775
4776 if ( cmp > 0) {
4777 if(PyList_Append(posonly_names, kwname) != 0) {
4778 goto fail;
4779 }
4780 posonly_conflicts++;
4781 } else if (cmp < 0) {
4782 goto fail;
4783 }
4784
4785 }
4786 }
4787 if (posonly_conflicts) {
4788 PyObject* comma = PyUnicode_FromString(", ");
4789 if (comma == NULL) {
4790 goto fail;
4791 }
4792 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4793 Py_DECREF(comma);
4794 if (error_names == NULL) {
4795 goto fail;
4796 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004797 _PyErr_Format(tstate, PyExc_TypeError,
4798 "%U() got some positional-only arguments passed"
4799 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004800 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004801 Py_DECREF(error_names);
4802 goto fail;
4803 }
4804
4805 Py_DECREF(posonly_names);
4806 return 0;
4807
4808fail:
4809 Py_XDECREF(posonly_names);
4810 return 1;
4811
4812}
4813
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004814
Mark Shannon0332e562021-02-01 10:42:03 +00004815PyFrameObject *
4816_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004817 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004818 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004819 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004820{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004821 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004822
Mark Shannond6c33fb2021-01-29 13:24:55 +00004823 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4824 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004825 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004826
Victor Stinnerc7020012016-08-16 23:40:29 +02004827 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004828 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004829 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004831 }
Victor Stinner232dda62020-06-04 15:19:02 +02004832 PyObject **fastlocals = f->f_localsplus;
4833 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004834
Victor Stinnerc7020012016-08-16 23:40:29 +02004835 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004836 PyObject *kwdict;
4837 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004838 if (co->co_flags & CO_VARKEYWORDS) {
4839 kwdict = PyDict_New();
4840 if (kwdict == NULL)
4841 goto fail;
4842 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004843 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004844 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004845 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004846 SETLOCAL(i, kwdict);
4847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004848 else {
4849 kwdict = NULL;
4850 }
4851
Pablo Galindocd74e662019-06-01 18:08:04 +01004852 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004853 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004854 if (argcount > co->co_argcount) {
4855 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004856 }
4857 else {
4858 n = argcount;
4859 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004860 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004861 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004862 Py_INCREF(x);
4863 SETLOCAL(j, x);
4864 }
4865
Victor Stinnerc7020012016-08-16 23:40:29 +02004866 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004867 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004868 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004869 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004870 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004871 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004872 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004873 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004874
Mark Shannon0332e562021-02-01 10:42:03 +00004875 /* Handle keyword arguments */
4876 if (kwnames != NULL) {
4877 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4878 for (i = 0; i < kwcount; i++) {
4879 PyObject **co_varnames;
4880 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4881 PyObject *value = args[i+argcount];
4882 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004883
Mark Shannon0332e562021-02-01 10:42:03 +00004884 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4885 _PyErr_Format(tstate, PyExc_TypeError,
4886 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004887 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004889 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004890
Mark Shannon0332e562021-02-01 10:42:03 +00004891 /* Speed hack: do raw pointer compares. As names are
4892 normally interned this should almost always hit. */
4893 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4894 for (j = co->co_posonlyargcount; j < total_args; j++) {
4895 PyObject *varname = co_varnames[j];
4896 if (varname == keyword) {
4897 goto kw_found;
4898 }
4899 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004900
Mark Shannon0332e562021-02-01 10:42:03 +00004901 /* Slow fallback, just in case */
4902 for (j = co->co_posonlyargcount; j < total_args; j++) {
4903 PyObject *varname = co_varnames[j];
4904 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4905 if (cmp > 0) {
4906 goto kw_found;
4907 }
4908 else if (cmp < 0) {
4909 goto fail;
4910 }
4911 }
4912
4913 assert(j >= total_args);
4914 if (kwdict == NULL) {
4915
4916 if (co->co_posonlyargcount
4917 && positional_only_passed_as_keyword(tstate, co,
4918 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004919 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004920 {
4921 goto fail;
4922 }
4923
4924 _PyErr_Format(tstate, PyExc_TypeError,
4925 "%U() got an unexpected keyword argument '%S'",
4926 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004927 goto fail;
4928 }
4929
Mark Shannon0332e562021-02-01 10:42:03 +00004930 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4931 goto fail;
4932 }
4933 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004934
Mark Shannon0332e562021-02-01 10:42:03 +00004935 kw_found:
4936 if (GETLOCAL(j) != NULL) {
4937 _PyErr_Format(tstate, PyExc_TypeError,
4938 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004939 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004940 goto fail;
4941 }
4942 Py_INCREF(value);
4943 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004946
4947 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004948 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004949 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4950 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 goto fail;
4952 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004953
4954 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004955 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004956 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004957 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004958 Py_ssize_t missing = 0;
4959 for (i = argcount; i < m; i++) {
4960 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004961 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004962 }
4963 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004964 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004965 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004966 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004967 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004968 }
4969 if (n > m)
4970 i = n - m;
4971 else
4972 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004973 if (defcount) {
4974 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4975 for (; i < defcount; i++) {
4976 if (GETLOCAL(m+i) == NULL) {
4977 PyObject *def = defs[i];
4978 Py_INCREF(def);
4979 SETLOCAL(m+i, def);
4980 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004981 }
4982 }
4983 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004984
4985 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004986 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004987 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004988 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004989 if (GETLOCAL(i) != NULL)
4990 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004991 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004992 if (con->fc_kwdefaults != NULL) {
4993 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004994 if (def) {
4995 Py_INCREF(def);
4996 SETLOCAL(i, def);
4997 continue;
4998 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004999 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005000 goto fail;
5001 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05005002 }
Benjamin Petersone109c702011-06-24 09:37:26 -05005003 missing++;
5004 }
5005 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005006 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005007 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005008 goto fail;
5009 }
5010 }
5011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005013 vars into frame. */
5014 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005015 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005016 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005017 /* Possibly account for the cell variable being an argument. */
5018 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005019 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005020 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005021 /* Clear the local copy. */
5022 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005023 }
5024 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005025 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005026 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005027 if (c == NULL)
5028 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005029 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005031
5032 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005033 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005034 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005035 Py_INCREF(o);
5036 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005038
Mark Shannon0332e562021-02-01 10:42:03 +00005039 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005040
Thomas Woutersce272b62007-09-19 21:19:28 +00005041fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 /* decref'ing the frame can cause __del__ methods to get invoked,
5044 which can call back into Python. While we're done with the
5045 current Python frame (f), the associated C stack is still in use,
5046 so recursion_depth must be boosted for the duration.
5047 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005048 if (Py_REFCNT(f) > 1) {
5049 Py_DECREF(f);
5050 _PyObject_GC_TRACK(f);
5051 }
5052 else {
5053 ++tstate->recursion_depth;
5054 Py_DECREF(f);
5055 --tstate->recursion_depth;
5056 }
Mark Shannon0332e562021-02-01 10:42:03 +00005057 return NULL;
5058}
5059
5060static PyObject *
5061make_coro(PyFrameConstructor *con, PyFrameObject *f)
5062{
5063 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5064 PyObject *gen;
5065 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5066
5067 /* Don't need to keep the reference to f_back, it will be set
5068 * when the generator is resumed. */
5069 Py_CLEAR(f->f_back);
5070
5071 /* Create a new generator that owns the ready to run frame
5072 * and return that as the value. */
5073 if (is_coro) {
5074 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5075 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5076 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5077 } else {
5078 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5079 }
5080 if (gen == NULL) {
5081 return NULL;
5082 }
5083
5084 _PyObject_GC_TRACK(f);
5085
5086 return gen;
5087}
5088
5089PyObject *
5090_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5091 PyObject *locals,
5092 PyObject* const* args, size_t argcount,
5093 PyObject *kwnames)
5094{
5095 PyFrameObject *f = _PyEval_MakeFrameVector(
5096 tstate, con, locals, args, argcount, kwnames);
5097 if (f == NULL) {
5098 return NULL;
5099 }
5100 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5101 return make_coro(con, f);
5102 }
5103 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5104
5105 /* decref'ing the frame can cause __del__ methods to get invoked,
5106 which can call back into Python. While we're done with the
5107 current Python frame (f), the associated C stack is still in use,
5108 so recursion_depth must be boosted for the duration.
5109 */
5110 if (Py_REFCNT(f) > 1) {
5111 Py_DECREF(f);
5112 _PyObject_GC_TRACK(f);
5113 }
5114 else {
5115 ++tstate->recursion_depth;
5116 Py_DECREF(f);
5117 --tstate->recursion_depth;
5118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005120}
5121
Mark Shannond6c33fb2021-01-29 13:24:55 +00005122/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005123PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005124PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5125 PyObject *const *args, int argcount,
5126 PyObject *const *kws, int kwcount,
5127 PyObject *const *defs, int defcount,
5128 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005129{
Victor Stinner46496f92021-02-20 15:17:18 +01005130 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005131 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005132 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5133 if (defaults == NULL) {
5134 return NULL;
5135 }
Victor Stinner46496f92021-02-20 15:17:18 +01005136 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005137 if (builtins == NULL) {
5138 Py_DECREF(defaults);
5139 return NULL;
5140 }
Dong-hee Na3cf08332021-02-14 15:54:39 +09005141 assert ((((PyCodeObject *)_co)->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
Mark Shannon0332e562021-02-01 10:42:03 +00005142 if (locals == NULL) {
5143 locals = globals;
5144 }
5145 PyObject *kwnames;
5146 PyObject *const *allargs;
5147 PyObject **newargs;
5148 if (kwcount == 0) {
5149 allargs = args;
5150 kwnames = NULL;
5151 }
5152 else {
5153 kwnames = PyTuple_New(kwcount);
5154 if (kwnames == NULL) {
5155 res = NULL;
5156 goto fail;
5157 }
5158 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5159 if (newargs == NULL) {
5160 res = NULL;
5161 Py_DECREF(kwnames);
5162 goto fail;
5163 }
5164 for (int i = 0; i < argcount; i++) {
5165 newargs[i] = args[i];
5166 }
5167 for (int i = 0; i < kwcount; i++) {
5168 Py_INCREF(kws[2*i]);
5169 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5170 newargs[argcount+i] = kws[2*i+1];
5171 }
5172 allargs = newargs;
5173 }
5174 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5175 if (kwargs == NULL) {
5176 res = NULL;
5177 Py_DECREF(kwnames);
5178 goto fail;
5179 }
5180 for (int i = 0; i < kwcount; i++) {
5181 Py_INCREF(kws[2*i]);
5182 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5183 kwargs[i] = kws[2*i+1];
5184 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005185 PyFrameConstructor constr = {
5186 .fc_globals = globals,
5187 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005188 .fc_name = ((PyCodeObject *)_co)->co_name,
5189 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005190 .fc_code = _co,
5191 .fc_defaults = defaults,
5192 .fc_kwdefaults = kwdefs,
5193 .fc_closure = closure
5194 };
Mark Shannon0332e562021-02-01 10:42:03 +00005195 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005196 allargs, argcount,
5197 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005198 if (kwcount) {
5199 Py_DECREF(kwnames);
5200 PyMem_Free(newargs);
5201 }
5202fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005203 Py_DECREF(defaults);
5204 Py_DECREF(builtins);
5205 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005206}
5207
Tim Peters5ca576e2001-06-18 22:08:13 +00005208
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005209static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005210special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005213 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005214 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005215 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 return NULL;
5217 }
5218 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005219}
5220
5221
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005222/* Logic for the raise statement (too complicated for inlining).
5223 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005224static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005225do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 if (exc == NULL) {
5230 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005231 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005233 type = exc_info->exc_type;
5234 value = exc_info->exc_value;
5235 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005236 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005237 _PyErr_SetString(tstate, PyExc_RuntimeError,
5238 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005239 return 0;
5240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 Py_XINCREF(type);
5242 Py_XINCREF(value);
5243 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005244 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005245 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005248 /* We support the following forms of raise:
5249 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005250 raise <instance>
5251 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 if (PyExceptionClass_Check(exc)) {
5254 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005255 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 if (value == NULL)
5257 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005258 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005259 _PyErr_Format(tstate, PyExc_TypeError,
5260 "calling %R should have returned an instance of "
5261 "BaseException, not %R",
5262 type, Py_TYPE(value));
5263 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 }
5266 else if (PyExceptionInstance_Check(exc)) {
5267 value = exc;
5268 type = PyExceptionInstance_Class(exc);
5269 Py_INCREF(type);
5270 }
5271 else {
5272 /* Not something you can raise. You get an exception
5273 anyway, just not what you specified :-) */
5274 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005275 _PyErr_SetString(tstate, PyExc_TypeError,
5276 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 goto raise_error;
5278 }
Collin Winter828f04a2007-08-31 00:04:24 +00005279
Serhiy Storchakac0191582016-09-27 11:37:10 +03005280 assert(type != NULL);
5281 assert(value != NULL);
5282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 if (cause) {
5284 PyObject *fixed_cause;
5285 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005286 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 if (fixed_cause == NULL)
5288 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005289 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005291 else if (PyExceptionInstance_Check(cause)) {
5292 fixed_cause = cause;
5293 }
5294 else if (cause == Py_None) {
5295 Py_DECREF(cause);
5296 fixed_cause = NULL;
5297 }
5298 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005299 _PyErr_SetString(tstate, PyExc_TypeError,
5300 "exception causes must derive from "
5301 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 goto raise_error;
5303 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005304 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005305 }
Collin Winter828f04a2007-08-31 00:04:24 +00005306
Victor Stinner438a12d2019-05-24 17:01:38 +02005307 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005308 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005309 Py_DECREF(value);
5310 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005311 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005312
5313raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 Py_XDECREF(value);
5315 Py_XDECREF(type);
5316 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005317 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005318}
5319
Tim Petersd6d010b2001-06-21 02:49:55 +00005320/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005321 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005322
Guido van Rossum0368b722007-05-11 16:50:42 +00005323 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5324 with a variable target.
5325*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005326
Barry Warsawe42b18f1997-08-25 22:13:04 +00005327static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005328unpack_iterable(PyThreadState *tstate, PyObject *v,
5329 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 int i = 0, j = 0;
5332 Py_ssize_t ll = 0;
5333 PyObject *it; /* iter(v) */
5334 PyObject *w;
5335 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005337 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005340 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005341 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005342 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005343 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005344 _PyErr_Format(tstate, PyExc_TypeError,
5345 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005346 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005347 }
5348 return 0;
5349 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 for (; i < argcnt; i++) {
5352 w = PyIter_Next(it);
5353 if (w == NULL) {
5354 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005355 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005356 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005357 _PyErr_Format(tstate, PyExc_ValueError,
5358 "not enough values to unpack "
5359 "(expected %d, got %d)",
5360 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005361 }
5362 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005363 _PyErr_Format(tstate, PyExc_ValueError,
5364 "not enough values to unpack "
5365 "(expected at least %d, got %d)",
5366 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 }
5369 goto Error;
5370 }
5371 *--sp = w;
5372 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 if (argcntafter == -1) {
5375 /* We better have exhausted the iterator now. */
5376 w = PyIter_Next(it);
5377 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005379 goto Error;
5380 Py_DECREF(it);
5381 return 1;
5382 }
5383 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005384 _PyErr_Format(tstate, PyExc_ValueError,
5385 "too many values to unpack (expected %d)",
5386 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 goto Error;
5388 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 l = PySequence_List(it);
5391 if (l == NULL)
5392 goto Error;
5393 *--sp = l;
5394 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 ll = PyList_GET_SIZE(l);
5397 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005398 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005399 "not enough values to unpack (expected at least %d, got %zd)",
5400 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 goto Error;
5402 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 /* Pop the "after-variable" args off the list. */
5405 for (j = argcntafter; j > 0; j--, i++) {
5406 *--sp = PyList_GET_ITEM(l, ll - j);
5407 }
5408 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005409 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 Py_DECREF(it);
5411 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005412
Tim Petersd6d010b2001-06-21 02:49:55 +00005413Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 for (; i > 0; i--, sp++)
5415 Py_DECREF(*sp);
5416 Py_XDECREF(it);
5417 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005418}
5419
5420
Guido van Rossum96a42c81992-01-12 02:29:51 +00005421#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005422static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005423prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005425 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 if (PyObject_Print(v, stdout, 0) != 0) {
5427 /* Don't know what else to do */
5428 _PyErr_Clear(tstate);
5429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 printf("\n");
5431 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005432}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005433#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005434
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005435static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005436call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005437 PyThreadState *tstate,
5438 PyFrameObject *f,
5439 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005440{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005441 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 if (value == NULL) {
5445 value = Py_None;
5446 Py_INCREF(value);
5447 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005449 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 arg = PyTuple_Pack(3, type, value, traceback);
5451 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005452 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 return;
5454 }
Mark Shannon86433452021-01-07 16:49:02 +00005455 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005457 if (err == 0) {
5458 _PyErr_Restore(tstate, type, value, orig_traceback);
5459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005460 else {
5461 Py_XDECREF(type);
5462 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005463 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005465}
5466
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005467static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005468call_trace_protected(Py_tracefunc func, PyObject *obj,
5469 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005470 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 PyObject *type, *value, *traceback;
5474 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005475 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00005476 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005477 if (err == 0)
5478 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005479 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 return 0;
5481 }
5482 else {
5483 Py_XDECREF(type);
5484 Py_XDECREF(value);
5485 Py_XDECREF(traceback);
5486 return -1;
5487 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005488}
5489
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005490static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005491call_trace(Py_tracefunc func, PyObject *obj,
5492 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005493 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005494 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 int result;
5497 if (tstate->tracing)
5498 return 0;
5499 tstate->tracing++;
5500 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005501 if (frame->f_lasti < 0) {
5502 frame->f_lineno = frame->f_code->co_firstlineno;
5503 }
5504 else {
5505 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005507 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005508 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005509 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5510 || (tstate->c_profilefunc != NULL));
5511 tstate->tracing--;
5512 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005513}
5514
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005515PyObject *
5516_PyEval_CallTracing(PyObject *func, PyObject *args)
5517{
Victor Stinner50b48572018-11-01 01:51:40 +01005518 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005519 int save_tracing = tstate->tracing;
5520 int save_use_tracing = tstate->use_tracing;
5521 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 tstate->tracing = 0;
5524 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5525 || (tstate->c_profilefunc != NULL));
5526 result = PyObject_Call(func, args, NULL);
5527 tstate->tracing = save_tracing;
5528 tstate->use_tracing = save_use_tracing;
5529 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005530}
5531
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005532/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005533static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005534maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005535 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005536 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005539
Nick Coghlan5a851672017-09-08 10:14:16 +10005540 /* If the last instruction falls at the start of a line or if it
5541 represents a jump backwards, update the frame's line number and
5542 then call the trace function if we're tracing source lines.
5543 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005544 int lastline = bounds->ar_line;
5545 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5546 if (line != -1 && frame->f_trace_lines) {
5547 /* Trace backward edges or first instruction of a new line */
5548 if (frame->f_lasti < *instr_prev ||
5549 (line != lastline && frame->f_lasti == bounds->ar_start))
5550 {
Mark Shannon86433452021-01-07 16:49:02 +00005551 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 }
George King20faa682017-10-18 17:44:22 -07005554 /* Always emit an opcode event if we're tracing all opcodes. */
5555 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005556 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005557 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 *instr_prev = frame->f_lasti;
5559 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005560}
5561
Victor Stinner309d7cc2020-03-13 16:39:12 +01005562int
5563_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5564{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005565 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005566 /* The caller must hold the GIL */
5567 assert(PyGILState_Check());
5568
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005569 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005570 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005571 PyThreadState *current_tstate = _PyThreadState_GET();
5572 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005573 return -1;
5574 }
5575
5576 PyObject *profileobj = tstate->c_profileobj;
5577
5578 tstate->c_profilefunc = NULL;
5579 tstate->c_profileobj = NULL;
5580 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5581 tstate->use_tracing = tstate->c_tracefunc != NULL;
5582 Py_XDECREF(profileobj);
5583
5584 Py_XINCREF(arg);
5585 tstate->c_profileobj = arg;
5586 tstate->c_profilefunc = func;
5587
5588 /* Flag that tracing or profiling is turned on */
5589 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5590 return 0;
5591}
5592
Fred Drake5755ce62001-06-27 19:19:46 +00005593void
5594PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005595{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005596 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005597 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005598 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005599 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5600 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005601}
5602
5603int
5604_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5605{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005606 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005607 /* The caller must hold the GIL */
5608 assert(PyGILState_Check());
5609
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005610 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005611 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005612 PyThreadState *current_tstate = _PyThreadState_GET();
5613 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005614 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005615 }
5616
Victor Stinnerda2914d2020-03-20 09:29:08 +01005617 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005618 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005619 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005620
5621 tstate->c_tracefunc = NULL;
5622 tstate->c_traceobj = NULL;
5623 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5624 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5625 Py_XDECREF(traceobj);
5626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005628 tstate->c_traceobj = arg;
5629 tstate->c_tracefunc = func;
5630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005631 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005632 tstate->use_tracing = ((func != NULL)
5633 || (tstate->c_profilefunc != NULL));
5634
5635 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005636}
5637
5638void
5639PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5640{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005641 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005642 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005643 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005644 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5645 }
Fred Draked0838392001-06-16 21:02:31 +00005646}
5647
Victor Stinner309d7cc2020-03-13 16:39:12 +01005648
Yury Selivanov75445082015-05-11 22:57:16 -04005649void
Victor Stinner838f2642019-06-13 22:41:23 +02005650_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005651{
5652 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005653 tstate->coroutine_origin_tracking_depth = new_depth;
5654}
5655
5656int
5657_PyEval_GetCoroutineOriginTrackingDepth(void)
5658{
Victor Stinner50b48572018-11-01 01:51:40 +01005659 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005660 return tstate->coroutine_origin_tracking_depth;
5661}
5662
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005663int
Yury Selivanoveb636452016-09-08 22:01:51 -07005664_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5665{
Victor Stinner50b48572018-11-01 01:51:40 +01005666 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005667
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005668 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005669 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005670 }
5671
Yury Selivanoveb636452016-09-08 22:01:51 -07005672 Py_XINCREF(firstiter);
5673 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005674 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005675}
5676
5677PyObject *
5678_PyEval_GetAsyncGenFirstiter(void)
5679{
Victor Stinner50b48572018-11-01 01:51:40 +01005680 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005681 return tstate->async_gen_firstiter;
5682}
5683
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005684int
Yury Selivanoveb636452016-09-08 22:01:51 -07005685_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5686{
Victor Stinner50b48572018-11-01 01:51:40 +01005687 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005688
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005689 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005690 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005691 }
5692
Yury Selivanoveb636452016-09-08 22:01:51 -07005693 Py_XINCREF(finalizer);
5694 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005695 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005696}
5697
5698PyObject *
5699_PyEval_GetAsyncGenFinalizer(void)
5700{
Victor Stinner50b48572018-11-01 01:51:40 +01005701 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005702 return tstate->async_gen_finalizer;
5703}
5704
Victor Stinner438a12d2019-05-24 17:01:38 +02005705PyFrameObject *
5706PyEval_GetFrame(void)
5707{
5708 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005709 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005710}
5711
Guido van Rossumb209a111997-04-29 18:18:01 +00005712PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005713_PyEval_GetBuiltins(PyThreadState *tstate)
5714{
5715 PyFrameObject *frame = tstate->frame;
5716 if (frame != NULL) {
5717 return frame->f_builtins;
5718 }
5719 return tstate->interp->builtins;
5720}
5721
5722PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005723PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005724{
Victor Stinner438a12d2019-05-24 17:01:38 +02005725 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005726 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005727}
5728
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005729/* Convenience function to get a builtin from its name */
5730PyObject *
5731_PyEval_GetBuiltinId(_Py_Identifier *name)
5732{
Victor Stinner438a12d2019-05-24 17:01:38 +02005733 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005734 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5735 if (attr) {
5736 Py_INCREF(attr);
5737 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005738 else if (!_PyErr_Occurred(tstate)) {
5739 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005740 }
5741 return attr;
5742}
5743
Guido van Rossumb209a111997-04-29 18:18:01 +00005744PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005745PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005746{
Victor Stinner438a12d2019-05-24 17:01:38 +02005747 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005748 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005749 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005750 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005751 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005752 }
5753
Victor Stinner438a12d2019-05-24 17:01:38 +02005754 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005755 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005756 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005757
5758 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005759 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005760}
5761
Guido van Rossumb209a111997-04-29 18:18:01 +00005762PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005763PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005764{
Victor Stinner438a12d2019-05-24 17:01:38 +02005765 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005766 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005767 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005768 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005769 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005770
5771 assert(current_frame->f_globals != NULL);
5772 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005773}
5774
Guido van Rossum6135a871995-01-09 17:53:26 +00005775int
Tim Peters5ba58662001-07-16 02:29:45 +00005776PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005777{
Victor Stinner438a12d2019-05-24 17:01:38 +02005778 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005779 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005780 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005782 if (current_frame != NULL) {
5783 const int codeflags = current_frame->f_code->co_flags;
5784 const int compilerflags = codeflags & PyCF_MASK;
5785 if (compilerflags) {
5786 result = 1;
5787 cf->cf_flags |= compilerflags;
5788 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005789#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005790 if (codeflags & CO_GENERATOR_ALLOWED) {
5791 result = 1;
5792 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5793 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005794#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005795 }
5796 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005797}
5798
Guido van Rossum3f5da241990-12-20 15:06:42 +00005799
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005800const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005801PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005803 if (PyMethod_Check(func))
5804 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5805 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005806 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005807 else if (PyCFunction_Check(func))
5808 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5809 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005810 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005811}
5812
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005813const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005814PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005816 if (PyMethod_Check(func))
5817 return "()";
5818 else if (PyFunction_Check(func))
5819 return "()";
5820 else if (PyCFunction_Check(func))
5821 return "()";
5822 else
5823 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005824}
5825
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005826#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005827if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005828 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005829 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005830 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005831 x = NULL; \
5832 } \
5833 else { \
5834 x = call; \
5835 if (tstate->c_profilefunc != NULL) { \
5836 if (x == NULL) { \
5837 call_trace_protected(tstate->c_profilefunc, \
5838 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005839 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005840 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005841 /* XXX should pass (type, value, tb) */ \
5842 } else { \
5843 if (call_trace(tstate->c_profilefunc, \
5844 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005845 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005846 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005847 Py_DECREF(x); \
5848 x = NULL; \
5849 } \
5850 } \
5851 } \
5852 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005853} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005854 x = call; \
5855 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005856
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005857
5858static PyObject *
5859trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005860 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005861 PyObject *func,
5862 PyObject **args, Py_ssize_t nargs,
5863 PyObject *kwnames)
5864{
5865 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005866 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005867 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005868 return x;
5869 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005870 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005871 /* We need to create a temporary bound method as argument
5872 for profiling.
5873
5874 If nargs == 0, then this cannot work because we have no
5875 "self". In any case, the call itself would raise
5876 TypeError (foo needs an argument), so we just skip
5877 profiling. */
5878 PyObject *self = args[0];
5879 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5880 if (func == NULL) {
5881 return NULL;
5882 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005883 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005884 args+1, nargs-1,
5885 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005886 Py_DECREF(func);
5887 return x;
5888 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005889 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005890}
5891
Victor Stinner415c5102017-01-11 00:54:57 +01005892/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5893 to reduce the stack consumption. */
5894Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005895call_function(PyThreadState *tstate,
5896 PyCodeAddressRange *bounds,
5897 PyObject ***pp_stack,
5898 Py_ssize_t oparg,
5899 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005900{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005901 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005902 PyObject *func = *pfunc;
5903 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005904 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5905 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005906 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005907
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005908 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005909 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005910 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005911 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005912 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005913 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005914
Victor Stinner438a12d2019-05-24 17:01:38 +02005915 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005916
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005917 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005918 while ((*pp_stack) > pfunc) {
5919 w = EXT_POP(*pp_stack);
5920 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005921 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005924}
5925
Jeremy Hylton52820442001-01-03 23:52:36 +00005926static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005927do_call_core(PyThreadState *tstate,
5928 PyCodeAddressRange *bounds,
5929 PyObject *func,
5930 PyObject *callargs,
5931 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005932{
jdemeyere89de732018-09-19 12:06:20 +02005933 PyObject *result;
5934
scoder4c9ea092020-05-12 16:12:41 +02005935 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005936 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005937 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005938 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005939 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005940 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5941 if (nargs > 0 && tstate->use_tracing) {
5942 /* We need to create a temporary bound method as argument
5943 for profiling.
5944
5945 If nargs == 0, then this cannot work because we have no
5946 "self". In any case, the call itself would raise
5947 TypeError (foo needs an argument), so we just skip
5948 profiling. */
5949 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5950 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5951 if (func == NULL) {
5952 return NULL;
5953 }
5954
Victor Stinner4d231bc2019-11-14 13:36:21 +01005955 C_TRACE(result, _PyObject_FastCallDictTstate(
5956 tstate, func,
5957 &_PyTuple_ITEMS(callargs)[1],
5958 nargs - 1,
5959 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005960 Py_DECREF(func);
5961 return result;
5962 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005963 }
jdemeyere89de732018-09-19 12:06:20 +02005964 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005965}
5966
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005967/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005968 nb_index slot defined, and store in *pi.
5969 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005970 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005971 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005972*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005973int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005974_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005975{
Victor Stinner438a12d2019-05-24 17:01:38 +02005976 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005977 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005978 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005979 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005980 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005981 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005982 return 0;
5983 }
5984 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005985 _PyErr_SetString(tstate, PyExc_TypeError,
5986 "slice indices must be integers or "
5987 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005988 return 0;
5989 }
5990 *pi = x;
5991 }
5992 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005993}
5994
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005995int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005996_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005997{
Victor Stinner438a12d2019-05-24 17:01:38 +02005998 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005999 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02006000 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006001 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006002 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006003 return 0;
6004 }
6005 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006006 _PyErr_SetString(tstate, PyExc_TypeError,
6007 "slice indices must be integers or "
6008 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006009 return 0;
6010 }
6011 *pi = x;
6012 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006013}
6014
Thomas Wouters52152252000-08-17 22:55:00 +00006015static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006016import_name(PyThreadState *tstate, PyFrameObject *f,
6017 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006018{
6019 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006020 PyObject *import_func, *res;
6021 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006022
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006023 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006024 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006025 if (!_PyErr_Occurred(tstate)) {
6026 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006027 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006028 return NULL;
6029 }
6030
6031 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006032 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006033 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006034 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006035 return NULL;
6036 }
6037 res = PyImport_ImportModuleLevelObject(
6038 name,
6039 f->f_globals,
6040 f->f_locals == NULL ? Py_None : f->f_locals,
6041 fromlist,
6042 ilevel);
6043 return res;
6044 }
6045
6046 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006047
6048 stack[0] = name;
6049 stack[1] = f->f_globals;
6050 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6051 stack[3] = fromlist;
6052 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006053 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006054 Py_DECREF(import_func);
6055 return res;
6056}
6057
6058static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006059import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006061 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006062 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006063
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006064 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006065 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006066 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006067 /* Issue #17636: in case this failed because of a circular relative
6068 import, try to fallback on reading the module directly from
6069 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006070 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006071 if (pkgname == NULL) {
6072 goto error;
6073 }
Oren Milman6db70332017-09-19 14:23:01 +03006074 if (!PyUnicode_Check(pkgname)) {
6075 Py_CLEAR(pkgname);
6076 goto error;
6077 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006078 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006079 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006080 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006081 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006082 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006083 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006084 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006085 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006086 goto error;
6087 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006088 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006089 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006090 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006091 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006092 if (pkgname == NULL) {
6093 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6094 if (pkgname_or_unknown == NULL) {
6095 Py_XDECREF(pkgpath);
6096 return NULL;
6097 }
6098 } else {
6099 pkgname_or_unknown = pkgname;
6100 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006101
6102 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006103 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006104 errmsg = PyUnicode_FromFormat(
6105 "cannot import name %R from %R (unknown location)",
6106 name, pkgname_or_unknown
6107 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006108 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006109 PyErr_SetImportError(errmsg, pkgname, NULL);
6110 }
6111 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006112 _Py_IDENTIFIER(__spec__);
6113 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006114 const char *fmt =
6115 _PyModuleSpec_IsInitializing(spec) ?
6116 "cannot import name %R from partially initialized module %R "
6117 "(most likely due to a circular import) (%S)" :
6118 "cannot import name %R from %R (%S)";
6119 Py_XDECREF(spec);
6120
6121 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006122 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006123 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006124 }
6125
Xiang Zhang4830f582017-03-21 11:13:42 +08006126 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006127 Py_XDECREF(pkgname_or_unknown);
6128 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006129 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006130}
Guido van Rossumac7be682001-01-17 15:42:30 +00006131
Thomas Wouters52152252000-08-17 22:55:00 +00006132static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006133import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006134{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006135 _Py_IDENTIFIER(__all__);
6136 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006137 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006138 int skip_leading_underscores = 0;
6139 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006140
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006141 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6142 return -1; /* Unexpected error */
6143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006144 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006145 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6146 return -1;
6147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006148 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006149 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006150 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006151 return -1;
6152 }
6153 all = PyMapping_Keys(dict);
6154 Py_DECREF(dict);
6155 if (all == NULL)
6156 return -1;
6157 skip_leading_underscores = 1;
6158 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006160 for (pos = 0, err = 0; ; pos++) {
6161 name = PySequence_GetItem(all, pos);
6162 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006163 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006164 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006165 }
6166 else {
6167 _PyErr_Clear(tstate);
6168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169 break;
6170 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006171 if (!PyUnicode_Check(name)) {
6172 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6173 if (modname == NULL) {
6174 Py_DECREF(name);
6175 err = -1;
6176 break;
6177 }
6178 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006179 _PyErr_Format(tstate, PyExc_TypeError,
6180 "module __name__ must be a string, not %.100s",
6181 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006182 }
6183 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006184 _PyErr_Format(tstate, PyExc_TypeError,
6185 "%s in %U.%s must be str, not %.100s",
6186 skip_leading_underscores ? "Key" : "Item",
6187 modname,
6188 skip_leading_underscores ? "__dict__" : "__all__",
6189 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006190 }
6191 Py_DECREF(modname);
6192 Py_DECREF(name);
6193 err = -1;
6194 break;
6195 }
6196 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006197 if (PyUnicode_READY(name) == -1) {
6198 Py_DECREF(name);
6199 err = -1;
6200 break;
6201 }
6202 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6203 Py_DECREF(name);
6204 continue;
6205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006206 }
6207 value = PyObject_GetAttr(v, name);
6208 if (value == NULL)
6209 err = -1;
6210 else if (PyDict_CheckExact(locals))
6211 err = PyDict_SetItem(locals, name, value);
6212 else
6213 err = PyObject_SetItem(locals, name, value);
6214 Py_DECREF(name);
6215 Py_XDECREF(value);
6216 if (err != 0)
6217 break;
6218 }
6219 Py_DECREF(all);
6220 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006221}
6222
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006223static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006224check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006225{
Victor Stinnera102ed72020-02-07 02:24:48 +01006226 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006227 /* check_args_iterable() may be called with a live exception:
6228 * clear it to prevent calling _PyObject_FunctionStr() with an
6229 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006230 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006231 PyObject *funcstr = _PyObject_FunctionStr(func);
6232 if (funcstr != NULL) {
6233 _PyErr_Format(tstate, PyExc_TypeError,
6234 "%U argument after * must be an iterable, not %.200s",
6235 funcstr, Py_TYPE(args)->tp_name);
6236 Py_DECREF(funcstr);
6237 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006238 return -1;
6239 }
6240 return 0;
6241}
6242
6243static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006244format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006245{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006246 /* _PyDict_MergeEx raises attribute
6247 * error (percolated from an attempt
6248 * to get 'keys' attribute) instead of
6249 * a type error if its second argument
6250 * is not a mapping.
6251 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006252 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006253 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006254 PyObject *funcstr = _PyObject_FunctionStr(func);
6255 if (funcstr != NULL) {
6256 _PyErr_Format(
6257 tstate, PyExc_TypeError,
6258 "%U argument after ** must be a mapping, not %.200s",
6259 funcstr, Py_TYPE(kwargs)->tp_name);
6260 Py_DECREF(funcstr);
6261 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006262 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006263 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006264 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006265 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006266 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006267 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006268 PyObject *funcstr = _PyObject_FunctionStr(func);
6269 if (funcstr != NULL) {
6270 PyObject *key = PyTuple_GET_ITEM(val, 0);
6271 _PyErr_Format(
6272 tstate, PyExc_TypeError,
6273 "%U got multiple values for keyword argument '%S'",
6274 funcstr, key);
6275 Py_DECREF(funcstr);
6276 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006277 Py_XDECREF(exc);
6278 Py_XDECREF(val);
6279 Py_XDECREF(tb);
6280 }
6281 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006282 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006283 }
6284 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006285}
6286
Guido van Rossumac7be682001-01-17 15:42:30 +00006287static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006288format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6289 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006293 if (!obj)
6294 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006295
Serhiy Storchaka06515832016-11-20 09:13:07 +02006296 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006297 if (!obj_str)
6298 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006299
Victor Stinner438a12d2019-05-24 17:01:38 +02006300 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006301}
Guido van Rossum950361c1997-01-24 13:49:28 +00006302
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006303static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006304format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006305{
6306 PyObject *name;
6307 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006308 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006309 return;
6310 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6311 name = PyTuple_GET_ITEM(co->co_cellvars,
6312 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006313 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006314 PyExc_UnboundLocalError,
6315 UNBOUNDLOCAL_ERROR_MSG,
6316 name);
6317 } else {
6318 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6319 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006320 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006321 UNBOUNDFREE_ERROR_MSG, name);
6322 }
6323}
6324
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006325static void
Mark Shannonfee55262019-11-21 09:11:43 +00006326format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006327{
6328 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6329 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006330 _PyErr_Format(tstate, PyExc_TypeError,
6331 "'async with' received an object from __aenter__ "
6332 "that does not implement __await__: %.100s",
6333 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006334 }
Mark Shannonfee55262019-11-21 09:11:43 +00006335 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006336 _PyErr_Format(tstate, PyExc_TypeError,
6337 "'async with' received an object from __aexit__ "
6338 "that does not implement __await__: %.100s",
6339 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006340 }
6341 }
6342}
6343
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006344static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006345unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006346 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006347{
6348 PyObject *res;
6349 if (Py_REFCNT(v) == 2) {
6350 /* In the common case, there are 2 references to the value
6351 * stored in 'variable' when the += is performed: one on the
6352 * value stack (in 'v') and one still stored in the
6353 * 'variable'. We try to delete the variable now to reduce
6354 * the refcnt to 1.
6355 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006356 int opcode, oparg;
6357 NEXTOPARG();
6358 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006359 case STORE_FAST:
6360 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006361 PyObject **fastlocals = f->f_localsplus;
6362 if (GETLOCAL(oparg) == v)
6363 SETLOCAL(oparg, NULL);
6364 break;
6365 }
6366 case STORE_DEREF:
6367 {
6368 PyObject **freevars = (f->f_localsplus +
6369 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006370 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006371 if (PyCell_GET(c) == v) {
6372 PyCell_SET(c, NULL);
6373 Py_DECREF(v);
6374 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006375 break;
6376 }
6377 case STORE_NAME:
6378 {
6379 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006380 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006381 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006382 if (locals && PyDict_CheckExact(locals)) {
6383 PyObject *w = PyDict_GetItemWithError(locals, name);
6384 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006385 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006386 {
6387 Py_DECREF(v);
6388 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006389 }
6390 }
6391 break;
6392 }
6393 }
6394 }
6395 res = v;
6396 PyUnicode_Append(&res, w);
6397 return res;
6398}
6399
Guido van Rossum950361c1997-01-24 13:49:28 +00006400#ifdef DYNAMIC_EXECUTION_PROFILE
6401
Skip Montanarof118cb12001-10-15 20:51:38 +00006402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006403getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405 int i;
6406 PyObject *l = PyList_New(256);
6407 if (l == NULL) return NULL;
6408 for (i = 0; i < 256; i++) {
6409 PyObject *x = PyLong_FromLong(a[i]);
6410 if (x == NULL) {
6411 Py_DECREF(l);
6412 return NULL;
6413 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006414 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006415 }
6416 for (i = 0; i < 256; i++)
6417 a[i] = 0;
6418 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006419}
6420
6421PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006422_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006423{
6424#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006425 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006426#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 int i;
6428 PyObject *l = PyList_New(257);
6429 if (l == NULL) return NULL;
6430 for (i = 0; i < 257; i++) {
6431 PyObject *x = getarray(dxpairs[i]);
6432 if (x == NULL) {
6433 Py_DECREF(l);
6434 return NULL;
6435 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006436 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006437 }
6438 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006439#endif
6440}
6441
6442#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006443
6444Py_ssize_t
6445_PyEval_RequestCodeExtraIndex(freefunc free)
6446{
Victor Stinner81a7be32020-04-14 15:14:01 +02006447 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006448 Py_ssize_t new_index;
6449
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006450 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006451 return -1;
6452 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006453 new_index = interp->co_extra_user_count++;
6454 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006455 return new_index;
6456}
Łukasz Langaa785c872016-09-09 17:37:37 -07006457
6458static void
6459dtrace_function_entry(PyFrameObject *f)
6460{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006461 const char *filename;
6462 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006463 int lineno;
6464
Victor Stinner6d86a232020-04-29 00:56:58 +02006465 PyCodeObject *code = f->f_code;
6466 filename = PyUnicode_AsUTF8(code->co_filename);
6467 funcname = PyUnicode_AsUTF8(code->co_name);
6468 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006469
Andy Lestere6be9b52020-02-11 20:28:35 -06006470 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006471}
6472
6473static void
6474dtrace_function_return(PyFrameObject *f)
6475{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006476 const char *filename;
6477 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006478 int lineno;
6479
Victor Stinner6d86a232020-04-29 00:56:58 +02006480 PyCodeObject *code = f->f_code;
6481 filename = PyUnicode_AsUTF8(code->co_filename);
6482 funcname = PyUnicode_AsUTF8(code->co_name);
6483 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006484
Andy Lestere6be9b52020-02-11 20:28:35 -06006485 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006486}
6487
6488/* DTrace equivalent of maybe_call_line_trace. */
6489static void
6490maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00006491 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006492{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006493 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006494
6495 /* If the last instruction executed isn't in the current
6496 instruction window, reset the window.
6497 */
Mark Shannon877df852020-11-12 09:43:29 +00006498 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006499 /* If the last instruction falls at the start of a line or if
6500 it represents a jump backwards, update the frame's line
6501 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00006502 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
6503 if (line != -1) {
6504 frame->f_lineno = line;
6505 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6506 if (!co_filename)
6507 co_filename = "?";
6508 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6509 if (!co_name)
6510 co_name = "?";
6511 PyDTrace_LINE(co_filename, co_name, line);
6512 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006513 }
6514 *instr_prev = frame->f_lasti;
6515}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006516
6517
6518/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6519 for the limited API. */
6520
6521#undef Py_EnterRecursiveCall
6522
6523int Py_EnterRecursiveCall(const char *where)
6524{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006525 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006526}
6527
6528#undef Py_LeaveRecursiveCall
6529
6530void Py_LeaveRecursiveCall(void)
6531{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006532 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006533}