blob: 8ec00bc2400fb346dba35943de628d7d565d9c9c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020014#include "pycore_call.h" // _PyObject_FastCallDictTstate()
15#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
16#include "pycore_code.h" // _PyCode_InitOpcache()
17#include "pycore_initconfig.h" // _PyStatus_OK()
18#include "pycore_object.h" // _PyObject_GC_TRACK()
19#include "pycore_pyerrors.h" // _PyErr_Fetch()
20#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020023#include "pycore_sysmodule.h" // _PySys_Audit()
24#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum5c5a9382021-01-29 18:02:29 -080032#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossumc6004111993-11-05 10:22:19 +000034#include <ctype.h>
35
Guido van Rossum408027e1996-12-30 16:17:54 +000036#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000037/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038#define LLTRACE 1 /* Low-level trace feature */
39#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#endif
41
Victor Stinner5c75f372019-04-17 23:02:26 +020042#if !defined(Py_BUILD_CORE)
43# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
44#endif
45
Hai Shi46874c22020-01-30 17:20:25 -060046_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000047
Guido van Rossum374a9221991-04-04 10:40:29 +000048/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020049Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon86433452021-01-07 16:49:02 +000050 PyThreadState *tstate, PyCodeAddressRange *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020051 Py_ssize_t oparg, PyObject *kwnames);
52static PyObject * do_call_core(
Mark Shannon86433452021-01-07 16:49:02 +000053 PyThreadState *tstate, PyCodeAddressRange *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020054 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000055
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000057static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020058static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000059#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060static int call_trace(Py_tracefunc, PyObject *,
61 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000062 PyCodeAddressRange *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000064static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010065 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000066 PyCodeAddressRange *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010067 int, PyObject *);
68static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000069 PyThreadState *, PyFrameObject *,
70 PyCodeAddressRange *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000071static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060072 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000073 PyCodeAddressRange *, int *);
74static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070075static void dtrace_function_entry(PyFrameObject *);
76static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000077
Victor Stinner438a12d2019-05-24 17:01:38 +020078static PyObject * import_name(PyThreadState *, PyFrameObject *,
79 PyObject *, PyObject *, PyObject *);
80static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
81static int import_all_from(PyThreadState *, PyObject *, PyObject *);
82static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
83static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
84static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030085 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020086static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
87static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
88static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000089static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Paul Prescode68140d2000-08-30 20:25:01 +000091#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000093#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000095#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 "free variable '%.200s' referenced before assignment" \
97 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
Inada Naoki91234a12019-06-03 21:30:58 +0900109/* per opcode cache */
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000110static int opcache_min_runs = 1024; /* create opcache when code executed this many times */
Pablo Galindo109826c2020-10-20 06:22:44 +0100111#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_STATS 0 /* Enable stats */
113
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000114// This function allows to deactivate the opcode cache. As different cache mechanisms may hold
115// references, this can mess with the reference leak detector functionality so the cache needs
116// to be deactivated in such scenarios to avoid false positives. See bpo-3714 for more information.
117void
118_PyEval_DeactivateOpCache(void)
119{
120 opcache_min_runs = 0;
121}
122
Inada Naoki91234a12019-06-03 21:30:58 +0900123#if OPCACHE_STATS
124static size_t opcache_code_objects = 0;
125static size_t opcache_code_objects_extra_mem = 0;
126
127static size_t opcache_global_opts = 0;
128static size_t opcache_global_hits = 0;
129static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100130
131static size_t opcache_attr_opts = 0;
132static size_t opcache_attr_hits = 0;
133static size_t opcache_attr_misses = 0;
134static size_t opcache_attr_deopts = 0;
135static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900136#endif
137
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100138
Victor Stinnerda2914d2020-03-20 09:29:08 +0100139#ifndef NDEBUG
140/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
141 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
142 when a thread continues to run after Python finalization, especially
143 daemon threads. */
144static int
145is_tstate_valid(PyThreadState *tstate)
146{
147 assert(!_PyMem_IsPtrFreed(tstate));
148 assert(!_PyMem_IsPtrFreed(tstate->interp));
149 return 1;
150}
151#endif
152
153
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000154/* This can set eval_breaker to 0 even though gil_drop_request became
155 1. We believe this is all right because the eval loop will release
156 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100157static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200158COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200159 struct _ceval_runtime_state *ceval,
160 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100161{
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
163 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200164 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200165 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200166 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100167 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200168 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100169}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000170
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000171
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200173SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100174{
Victor Stinner299b8c62020-05-05 17:40:18 +0200175 struct _ceval_state *ceval2 = &interp->ceval;
176 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
177 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000179
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000180
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200182RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100183{
Victor Stinner299b8c62020-05-05 17:40:18 +0200184 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
185 struct _ceval_state *ceval2 = &interp->ceval;
186 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
187 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189
Eric Snowfdf282d2019-01-11 14:26:55 -0700190
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200192SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100193{
Victor Stinner299b8c62020-05-05 17:40:18 +0200194 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
195 struct _ceval_state *ceval2 = &interp->ceval;
196 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
197 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100198}
Eric Snowfdf282d2019-01-11 14:26:55 -0700199
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000200
Victor Stinnerda2914d2020-03-20 09:29:08 +0100201static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200202UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100203{
Victor Stinner299b8c62020-05-05 17:40:18 +0200204 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
205 struct _ceval_state *ceval2 = &interp->ceval;
206 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
207 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100208}
209
210
211static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100212SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100213{
Victor Stinner299b8c62020-05-05 17:40:18 +0200214 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
215 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200216 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100217 if (force) {
218 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
219 }
220 else {
221 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
222 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
223 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100224}
225
226
227static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200228UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100229{
Victor Stinner299b8c62020-05-05 17:40:18 +0200230 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
231 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200232 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200233 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100234}
235
236
237static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200238SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100239{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200240 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100241 ceval2->pending.async_exc = 1;
242 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
243}
244
245
246static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200247UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100248{
Victor Stinner299b8c62020-05-05 17:40:18 +0200249 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
250 struct _ceval_state *ceval2 = &interp->ceval;
251 ceval2->pending.async_exc = 0;
252 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100253}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254
255
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000258#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000260
Victor Stinner3026cad2020-06-01 16:02:40 +0200261void _Py_NO_RETURN
262_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100263{
Victor Stinner3026cad2020-06-01 16:02:40 +0200264 _Py_FatalErrorFunc(func,
265 "the function must be called with the GIL held, "
266 "but the GIL is released "
267 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100268}
269
Victor Stinner7be4e352020-05-05 20:27:47 +0200270#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
271int
272_PyEval_ThreadsInitialized(PyInterpreterState *interp)
273{
274 return gil_created(&interp->ceval.gil);
275}
276
277int
278PyEval_ThreadsInitialized(void)
279{
280 // Fatal error if there is no current interpreter
281 PyInterpreterState *interp = PyInterpreterState_Get();
282 return _PyEval_ThreadsInitialized(interp);
283}
284#else
Tim Peters7f468f22004-10-11 02:40:51 +0000285int
Victor Stinner175a7042020-03-10 00:37:48 +0100286_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
287{
288 return gil_created(&runtime->ceval.gil);
289}
290
291int
Tim Peters7f468f22004-10-11 02:40:51 +0000292PyEval_ThreadsInitialized(void)
293{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100294 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100295 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000296}
Victor Stinner7be4e352020-05-05 20:27:47 +0200297#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000298
Victor Stinner111e4ee2020-03-09 21:24:14 +0100299PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200300_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301{
Victor Stinner7be4e352020-05-05 20:27:47 +0200302#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100303 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200304 /* Currently, the GIL is shared by all interpreters,
305 and only the main interpreter is responsible to create
306 and destroy it. */
307 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100308 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100310
Victor Stinner7be4e352020-05-05 20:27:47 +0200311#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
312 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
313#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200314 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200315#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200316 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100317
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200318 PyThread_init_thread();
319 create_gil(gil);
320
321 take_gil(tstate);
322
323 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100324 return _PyStatus_OK();
325}
326
327void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100328_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200329{
Victor Stinner7be4e352020-05-05 20:27:47 +0200330#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100331 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200332 /* Currently, the GIL is shared by all interpreters,
333 and only the main interpreter is responsible to create
334 and destroy it. */
335 return;
336 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200338
Victor Stinner7be4e352020-05-05 20:27:47 +0200339#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100340 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200341#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100342 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200343#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200344 if (!gil_created(gil)) {
345 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
346 yet: do nothing. */
347 return;
348 }
349
350 destroy_gil(gil);
351 assert(!gil_created(gil));
352}
353
354void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100355PyEval_InitThreads(void)
356{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100357 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000359
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000360void
Inada Naoki91234a12019-06-03 21:30:58 +0900361_PyEval_Fini(void)
362{
363#if OPCACHE_STATS
364 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
365 opcache_code_objects);
366
367 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
368 opcache_code_objects_extra_mem);
369
370 fprintf(stderr, "\n");
371
372 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
373 opcache_global_hits,
374 (int) (100.0 * opcache_global_hits /
375 (opcache_global_hits + opcache_global_misses)));
376
377 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
378 opcache_global_misses,
379 (int) (100.0 * opcache_global_misses /
380 (opcache_global_hits + opcache_global_misses)));
381
382 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
383 opcache_global_opts);
384
385 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100386
387 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
388 opcache_attr_hits,
389 (int) (100.0 * opcache_attr_hits /
390 opcache_attr_total));
391
392 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
393 opcache_attr_misses,
394 (int) (100.0 * opcache_attr_misses /
395 opcache_attr_total));
396
397 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
398 opcache_attr_opts);
399
400 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
401 opcache_attr_deopts);
402
403 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
404 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900405#endif
406}
407
408void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000409PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000410{
Victor Stinner09532fe2019-05-10 23:39:09 +0200411 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200412 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200413 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100414
Victor Stinner85f5a692020-03-09 22:12:04 +0100415 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000416}
417
418void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000419PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000420{
Victor Stinner09532fe2019-05-10 23:39:09 +0200421 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200422 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100424 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100425 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200426 struct _ceval_runtime_state *ceval = &runtime->ceval;
427 struct _ceval_state *ceval2 = &tstate->interp->ceval;
428 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000429}
430
431void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100432_PyEval_ReleaseLock(PyThreadState *tstate)
433{
434 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200435 struct _ceval_state *ceval2 = &tstate->interp->ceval;
436 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100437}
438
439void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000441{
Victor Stinner3026cad2020-06-01 16:02:40 +0200442 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100443
Victor Stinner85f5a692020-03-09 22:12:04 +0100444 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200445
Victor Stinner85f5a692020-03-09 22:12:04 +0100446 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200447#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
448 (void)_PyThreadState_Swap(gilstate, tstate);
449#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100450 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100451 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200452 }
Victor Stinnere838a932020-05-05 19:56:48 +0200453#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000454}
455
456void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000457PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000458{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100459 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200460
Victor Stinner01b1cc12019-11-20 02:27:56 +0100461 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200462 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
463 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100464 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200465 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200466 struct _ceval_runtime_state *ceval = &runtime->ceval;
467 struct _ceval_state *ceval2 = &tstate->interp->ceval;
468 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000469}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000470
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900471#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200472/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200473 which are not running in the child process, and clear internal locks
474 which might be held by those threads. */
475PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200476_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000477{
Victor Stinner317bab02020-06-02 18:44:54 +0200478 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200479
480#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
481 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
482#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100483 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200484#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100485 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200486 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200487 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100488 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100489
490 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700491
Victor Stinner50e6e992020-03-19 02:41:21 +0100492 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900493 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200494 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700495 }
Jesse Nollera8513972008-07-17 16:49:17 +0000496
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200497 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100498 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200499 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000500}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900501#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000502
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600504 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000505
506void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100507_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000508{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100509 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000510}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000511
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000512PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000513PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000514{
Victor Stinner09532fe2019-05-10 23:39:09 +0200515 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200516#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
517 PyThreadState *old_tstate = _PyThreadState_GET();
518 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
519#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200520 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200521#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200522 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100523
Victor Stinner0b1e3302020-05-05 16:14:31 +0200524 struct _ceval_runtime_state *ceval = &runtime->ceval;
525 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200526#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
527 assert(gil_created(&ceval2->gil));
528#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200529 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200530#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200531 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000533}
534
535void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000536PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000537{
Victor Stinner3026cad2020-06-01 16:02:40 +0200538 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100539
Victor Stinner85f5a692020-03-09 22:12:04 +0100540 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100541
Victor Stinner85f5a692020-03-09 22:12:04 +0100542 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
543 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000544}
545
546
Guido van Rossuma9672091994-09-14 13:31:22 +0000547/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
548 signal handlers or Mac I/O completion routines) can schedule calls
549 to a function to be called synchronously.
550 The synchronous function is called with one void* argument.
551 It should return 0 for success or -1 for failure -- failure should
552 be accompanied by an exception.
553
554 If registry succeeds, the registry function returns 0; if it fails
555 (e.g. due to too many pending calls) it returns -1 (without setting
556 an exception condition).
557
558 Note that because registry may occur from within signal handlers,
559 or other asynchronous events, calling malloc() is unsafe!
560
Guido van Rossuma9672091994-09-14 13:31:22 +0000561 Any thread can schedule pending calls, but only the main thread
562 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000563 There is no facility to schedule calls to a particular thread, but
564 that should be easy to change, should that ever be required. In
565 that case, the static variables here should go into the python
566 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000567*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000568
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200569void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200570_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200571{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100572#ifdef MS_WINDOWS
573 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
574 // handler which can run in a thread different than the Python thread, in
575 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
576 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
577 //
578 // The next eval_frame_handle_pending() call will call
579 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
580 int force = 1;
581#else
582 int force = 0;
583#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200584 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100585 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200586 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100587 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200588}
589
Eric Snow5be45a62019-03-08 22:47:07 -0700590/* Push one item onto the queue while holding the lock. */
591static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200592_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600593 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700594{
Eric Snow842a2f02019-03-15 15:47:51 -0600595 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700596 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600597 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700598 return -1; /* Queue full */
599 }
Eric Snow842a2f02019-03-15 15:47:51 -0600600 pending->calls[i].func = func;
601 pending->calls[i].arg = arg;
602 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700603 return 0;
604}
605
606/* Pop one item off the queue while holding the lock. */
607static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200608_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600609 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700610{
Eric Snow842a2f02019-03-15 15:47:51 -0600611 int i = pending->first;
612 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700613 return; /* Queue empty */
614 }
615
Eric Snow842a2f02019-03-15 15:47:51 -0600616 *func = pending->calls[i].func;
617 *arg = pending->calls[i].arg;
618 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700619}
620
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200621/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000622 scheduling to be made from any thread, and even from an executing
623 callback.
624 */
625
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000626int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200627_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200628 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000629{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200630 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600631
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200632 /* Ensure that _PyEval_InitPendingCalls() was called
633 and that _PyEval_FiniPendingCalls() is not called yet. */
634 assert(pending->lock != NULL);
635
Eric Snow842a2f02019-03-15 15:47:51 -0600636 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200637 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600638 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700639
Victor Stinnere225beb2019-06-03 18:14:24 +0200640 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200641 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000643}
644
Victor Stinner09532fe2019-05-10 23:39:09 +0200645int
646Py_AddPendingCall(int (*func)(void *), void *arg)
647{
Victor Stinner50e6e992020-03-19 02:41:21 +0100648 /* Best-effort to support subinterpreters and calls with the GIL released.
649
650 First attempt _PyThreadState_GET() since it supports subinterpreters.
651
652 If the GIL is released, _PyThreadState_GET() returns NULL . In this
653 case, use PyGILState_GetThisThreadState() which works even if the GIL
654 is released.
655
656 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
657 see bpo-10915 and bpo-15751.
658
Victor Stinner8849e592020-03-18 19:28:53 +0100659 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100660 PyThreadState *tstate = _PyThreadState_GET();
661 if (tstate == NULL) {
662 tstate = PyGILState_GetThisThreadState();
663 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200664
665 PyInterpreterState *interp;
666 if (tstate != NULL) {
667 interp = tstate->interp;
668 }
669 else {
670 /* Last resort: use the main interpreter */
671 interp = _PyRuntime.interpreters.main;
672 }
673 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200674}
675
Eric Snowfdf282d2019-01-11 14:26:55 -0700676static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100677handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700678{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200679 assert(is_tstate_valid(tstate));
680 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700681 return 0;
682 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700683
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200684 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100685 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
686 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100687 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700688 return -1;
689 }
690 return 0;
691}
692
693static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100694make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000695{
Victor Stinnerd8316882020-03-20 14:50:35 +0100696 /* only execute pending calls on main thread */
697 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200698 return 0;
699 }
700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100702 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700703 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700705 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200706 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100707
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200708 /* unsignal before starting to call callbacks, so that any callback
709 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100710 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700711 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100714 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700715 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700716 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 void *arg = NULL;
718
719 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600720 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200721 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600722 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700723
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100724 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700725 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100726 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700727 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700728 res = func(arg);
729 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200730 goto error;
731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200733
Charles-François Natalif23339a2011-07-23 18:15:43 +0200734 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700735 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200736
737error:
738 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100739 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700740 return res;
741}
742
Eric Snow842a2f02019-03-15 15:47:51 -0600743void
Victor Stinner2b1df452020-01-13 18:46:59 +0100744_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600745{
Eric Snow842a2f02019-03-15 15:47:51 -0600746 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100747 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600748
Victor Stinner50e6e992020-03-19 02:41:21 +0100749 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200750
Eric Snow842a2f02019-03-15 15:47:51 -0600751 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
752 return;
753 }
754
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100755 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200756 PyObject *exc, *val, *tb;
757 _PyErr_Fetch(tstate, &exc, &val, &tb);
758 PyErr_BadInternalCall();
759 _PyErr_ChainExceptions(exc, val, tb);
760 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600761 }
762}
763
Eric Snowfdf282d2019-01-11 14:26:55 -0700764/* Py_MakePendingCalls() is a simple wrapper for the sake
765 of backward-compatibility. */
766int
767Py_MakePendingCalls(void)
768{
769 assert(PyGILState_Check());
770
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100772 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100773
Eric Snowfdf282d2019-01-11 14:26:55 -0700774 /* Python signal handler doesn't really queue a callback: it only signals
775 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100776 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700777 if (res != 0) {
778 return res;
779 }
780
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100781 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600782 if (res != 0) {
783 return res;
784 }
785
786 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000787}
788
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000789/* The interpreter's recursion limit */
790
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000791#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200792# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000793#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600794
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600795void
Victor Stinnerdab84232020-03-17 18:56:44 +0100796_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600797{
Victor Stinner7be4e352020-05-05 20:27:47 +0200798#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100799 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200800#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100801}
802
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200803int
Victor Stinnerdab84232020-03-17 18:56:44 +0100804_PyEval_InitState(struct _ceval_state *ceval)
805{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200806 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
807
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200808 struct _pending_calls *pending = &ceval->pending;
809 assert(pending->lock == NULL);
810
811 pending->lock = PyThread_allocate_lock();
812 if (pending->lock == NULL) {
813 return -1;
814 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200815
816#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
817 _gil_initialize(&ceval->gil);
818#endif
819
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200820 return 0;
821}
822
823void
824_PyEval_FiniState(struct _ceval_state *ceval)
825{
826 struct _pending_calls *pending = &ceval->pending;
827 if (pending->lock != NULL) {
828 PyThread_free_lock(pending->lock);
829 pending->lock = NULL;
830 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600831}
832
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000833int
834Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000835{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200836 PyInterpreterState *interp = _PyInterpreterState_GET();
837 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000838}
839
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000840void
841Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000842{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200843 PyThreadState *tstate = _PyThreadState_GET();
844 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000845}
846
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100847/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200848 if the recursion_depth reaches recursion_limit.
849 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000850 to guarantee that _Py_CheckRecursiveCall() is regularly called.
851 Without USE_STACKCHECK, there is no need for this. */
852int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100853_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000854{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200855 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000856
857#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700858 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (PyOS_CheckStack()) {
860 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200861 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 return -1;
863 }
pdox18967932017-10-25 23:03:01 -0700864#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000865 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (tstate->recursion_depth > recursion_limit + 50) {
867 /* Overflowing while handling an overflow. Give up. */
868 Py_FatalError("Cannot recover from stack overflow.");
869 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000871 else {
872 if (tstate->recursion_depth > recursion_limit) {
873 tstate->recursion_headroom++;
874 _PyErr_Format(tstate, PyExc_RecursionError,
875 "maximum recursion depth exceeded%s",
876 where);
877 tstate->recursion_headroom--;
878 --tstate->recursion_depth;
879 return -1;
880 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 }
882 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000883}
884
Brandt Bucher145bf262021-02-26 14:51:55 -0800885
886// PEP 634: Structural Pattern Matching
887
888
889// Return a tuple of values corresponding to keys, with error checks for
890// duplicate/missing keys.
891static PyObject*
892match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
893{
894 assert(PyTuple_CheckExact(keys));
895 Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
896 if (!nkeys) {
897 // No keys means no items.
898 return PyTuple_New(0);
899 }
900 PyObject *seen = NULL;
901 PyObject *dummy = NULL;
902 PyObject *values = NULL;
903 // We use the two argument form of map.get(key, default) for two reasons:
904 // - Atomically check for a key and get its value without error handling.
905 // - Don't cause key creation or resizing in dict subclasses like
906 // collections.defaultdict that define __missing__ (or similar).
907 _Py_IDENTIFIER(get);
908 PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
909 if (get == NULL) {
910 goto fail;
911 }
912 seen = PySet_New(NULL);
913 if (seen == NULL) {
914 goto fail;
915 }
916 // dummy = object()
917 dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
918 if (dummy == NULL) {
919 goto fail;
920 }
921 values = PyList_New(0);
922 if (values == NULL) {
923 goto fail;
924 }
925 for (Py_ssize_t i = 0; i < nkeys; i++) {
926 PyObject *key = PyTuple_GET_ITEM(keys, i);
927 if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
928 if (!_PyErr_Occurred(tstate)) {
929 // Seen it before!
930 _PyErr_Format(tstate, PyExc_ValueError,
931 "mapping pattern checks duplicate key (%R)", key);
932 }
933 goto fail;
934 }
935 PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
936 if (value == NULL) {
937 goto fail;
938 }
939 if (value == dummy) {
940 // key not in map!
941 Py_DECREF(value);
942 Py_DECREF(values);
943 // Return None:
944 Py_INCREF(Py_None);
945 values = Py_None;
946 goto done;
947 }
948 PyList_Append(values, value);
949 Py_DECREF(value);
950 }
951 Py_SETREF(values, PyList_AsTuple(values));
952 // Success:
953done:
954 Py_DECREF(get);
955 Py_DECREF(seen);
956 Py_DECREF(dummy);
957 return values;
958fail:
959 Py_XDECREF(get);
960 Py_XDECREF(seen);
961 Py_XDECREF(dummy);
962 Py_XDECREF(values);
963 return NULL;
964}
965
966// Extract a named attribute from the subject, with additional bookkeeping to
967// raise TypeErrors for repeated lookups. On failure, return NULL (with no
968// error set). Use _PyErr_Occurred(tstate) to disambiguate.
969static PyObject*
970match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
971 PyObject *name, PyObject *seen)
972{
973 assert(PyUnicode_CheckExact(name));
974 assert(PySet_CheckExact(seen));
975 if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
976 if (!_PyErr_Occurred(tstate)) {
977 // Seen it before!
978 _PyErr_Format(tstate, PyExc_TypeError,
979 "%s() got multiple sub-patterns for attribute %R",
980 ((PyTypeObject*)type)->tp_name, name);
981 }
982 return NULL;
983 }
984 PyObject *attr = PyObject_GetAttr(subject, name);
985 if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
986 _PyErr_Clear(tstate);
987 }
988 return attr;
989}
990
991// On success (match), return a tuple of extracted attributes. On failure (no
992// match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
993static PyObject*
994match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
995 Py_ssize_t nargs, PyObject *kwargs)
996{
997 if (!PyType_Check(type)) {
998 const char *e = "called match pattern must be a type";
999 _PyErr_Format(tstate, PyExc_TypeError, e);
1000 return NULL;
1001 }
1002 assert(PyTuple_CheckExact(kwargs));
1003 // First, an isinstance check:
1004 if (PyObject_IsInstance(subject, type) <= 0) {
1005 return NULL;
1006 }
1007 // So far so good:
1008 PyObject *seen = PySet_New(NULL);
1009 if (seen == NULL) {
1010 return NULL;
1011 }
1012 PyObject *attrs = PyList_New(0);
1013 if (attrs == NULL) {
1014 Py_DECREF(seen);
1015 return NULL;
1016 }
1017 // NOTE: From this point on, goto fail on failure:
1018 PyObject *match_args = NULL;
1019 // First, the positional subpatterns:
1020 if (nargs) {
1021 int match_self = 0;
1022 match_args = PyObject_GetAttrString(type, "__match_args__");
1023 if (match_args) {
1024 if (PyList_CheckExact(match_args)) {
1025 Py_SETREF(match_args, PyList_AsTuple(match_args));
1026 }
1027 if (match_args == NULL) {
1028 goto fail;
1029 }
1030 if (!PyTuple_CheckExact(match_args)) {
1031 const char *e = "%s.__match_args__ must be a list or tuple "
1032 "(got %s)";
1033 _PyErr_Format(tstate, PyExc_TypeError, e,
1034 ((PyTypeObject *)type)->tp_name,
1035 Py_TYPE(match_args)->tp_name);
1036 goto fail;
1037 }
1038 }
1039 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1040 _PyErr_Clear(tstate);
1041 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1042 // define __match_args__. This is natural behavior for subclasses:
1043 // it's as if __match_args__ is some "magic" value that is lost as
1044 // soon as they redefine it.
1045 match_args = PyTuple_New(0);
1046 match_self = PyType_HasFeature((PyTypeObject*)type,
1047 _Py_TPFLAGS_MATCH_SELF);
1048 }
1049 else {
1050 goto fail;
1051 }
1052 assert(PyTuple_CheckExact(match_args));
1053 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1054 if (allowed < nargs) {
1055 const char *plural = (allowed == 1) ? "" : "s";
1056 _PyErr_Format(tstate, PyExc_TypeError,
1057 "%s() accepts %d positional sub-pattern%s (%d given)",
1058 ((PyTypeObject*)type)->tp_name,
1059 allowed, plural, nargs);
1060 goto fail;
1061 }
1062 if (match_self) {
1063 // Easy. Copy the subject itself, and move on to kwargs.
1064 PyList_Append(attrs, subject);
1065 }
1066 else {
1067 for (Py_ssize_t i = 0; i < nargs; i++) {
1068 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1069 if (!PyUnicode_CheckExact(name)) {
1070 _PyErr_Format(tstate, PyExc_TypeError,
1071 "__match_args__ elements must be strings "
1072 "(got %s)", Py_TYPE(name)->tp_name);
1073 goto fail;
1074 }
1075 PyObject *attr = match_class_attr(tstate, subject, type, name,
1076 seen);
1077 if (attr == NULL) {
1078 goto fail;
1079 }
1080 PyList_Append(attrs, attr);
1081 Py_DECREF(attr);
1082 }
1083 }
1084 Py_CLEAR(match_args);
1085 }
1086 // Finally, the keyword subpatterns:
1087 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1088 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1089 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1090 if (attr == NULL) {
1091 goto fail;
1092 }
1093 PyList_Append(attrs, attr);
1094 Py_DECREF(attr);
1095 }
1096 Py_SETREF(attrs, PyList_AsTuple(attrs));
1097 Py_DECREF(seen);
1098 return attrs;
1099fail:
1100 // We really don't care whether an error was raised or not... that's our
1101 // caller's problem. All we know is that the match failed.
1102 Py_XDECREF(match_args);
1103 Py_DECREF(seen);
1104 Py_DECREF(attrs);
1105 return NULL;
1106}
1107
1108
Victor Stinner09532fe2019-05-10 23:39:09 +02001109static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001110static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001111
Victor Stinnere225beb2019-06-03 18:14:24 +02001112#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001113
Guido van Rossum374a9221991-04-04 10:40:29 +00001114
Guido van Rossumb209a111997-04-29 18:18:01 +00001115PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001116PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001117{
Victor Stinner46496f92021-02-20 15:17:18 +01001118 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001119 if (locals == NULL) {
1120 locals = globals;
1121 }
Victor Stinner46496f92021-02-20 15:17:18 +01001122 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
Mark Shannon0332e562021-02-01 10:42:03 +00001123 if (builtins == NULL) {
1124 return NULL;
1125 }
1126 PyFrameConstructor desc = {
1127 .fc_globals = globals,
1128 .fc_builtins = builtins,
1129 .fc_name = ((PyCodeObject *)co)->co_name,
1130 .fc_qualname = ((PyCodeObject *)co)->co_name,
1131 .fc_code = co,
1132 .fc_defaults = NULL,
1133 .fc_kwdefaults = NULL,
1134 .fc_closure = NULL
1135 };
Victor Stinner46496f92021-02-20 15:17:18 +01001136 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001137}
1138
1139
1140/* Interpreter main loop */
1141
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001142PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001143PyEval_EvalFrame(PyFrameObject *f)
1144{
Victor Stinner0b72b232020-03-12 23:18:39 +01001145 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001146 PyThreadState *tstate = _PyThreadState_GET();
1147 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001148}
1149
1150PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001151PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001152{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001153 PyThreadState *tstate = _PyThreadState_GET();
1154 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001155}
1156
Victor Stinnerda2914d2020-03-20 09:29:08 +01001157
1158/* Handle signals, pending calls, GIL drop request
1159 and asynchronous exception */
1160static int
1161eval_frame_handle_pending(PyThreadState *tstate)
1162{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001163 _PyRuntimeState * const runtime = &_PyRuntime;
1164 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001165
1166 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001167 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001168 if (handle_signals(tstate) != 0) {
1169 return -1;
1170 }
1171 }
1172
1173 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001174 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001175 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001176 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001177 return -1;
1178 }
1179 }
1180
1181 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001182 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001183 /* Give another thread a chance */
1184 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1185 Py_FatalError("tstate mix-up");
1186 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001187 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001188
1189 /* Other threads may run now */
1190
1191 take_gil(tstate);
1192
Victor Stinnere838a932020-05-05 19:56:48 +02001193#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1194 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1195#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001196 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1197 Py_FatalError("orphan tstate");
1198 }
Victor Stinnere838a932020-05-05 19:56:48 +02001199#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001200 }
1201
1202 /* Check for asynchronous exception. */
1203 if (tstate->async_exc != NULL) {
1204 PyObject *exc = tstate->async_exc;
1205 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001206 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001207 _PyErr_SetNone(tstate, exc);
1208 Py_DECREF(exc);
1209 return -1;
1210 }
1211
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001212#ifdef MS_WINDOWS
1213 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1214 // different thread than the Python thread, in which case
1215 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1216 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1217 // value. It prevents to interrupt the eval loop at every instruction if
1218 // the current Python thread cannot handle signals (if
1219 // _Py_ThreadCanHandleSignals() is false).
1220 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1221#endif
1222
Victor Stinnerda2914d2020-03-20 09:29:08 +01001223 return 0;
1224}
1225
Victor Stinnerc6944e72016-11-11 02:13:35 +01001226PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001227_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001228{
Victor Stinner3026cad2020-06-01 16:02:40 +02001229 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001230
Guido van Rossum950361c1997-01-24 13:49:28 +00001231#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001233#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001234 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001235 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001236 int opcode; /* Current opcode */
1237 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001238 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001240 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001241 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 is true when the line being executed has changed. The
1249 initial values are such as to make this false the first
1250 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001251
Serhiy Storchakaab874002016-09-11 13:48:15 +03001252 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 PyObject *names;
1254 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001255 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001256
Brett Cannon368b4b72012-04-02 12:17:59 -04001257#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001258 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001259#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001260
Antoine Pitroub52ec782009-01-25 16:34:23 +00001261/* Computed GOTOs, or
1262 the-optimization-commonly-but-improperly-known-as-"threaded code"
1263 using gcc's labels-as-values extension
1264 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1265
1266 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001268 combined with a lookup table of jump addresses. However, since the
1269 indirect jump instruction is shared by all opcodes, the CPU will have a
1270 hard time making the right prediction for where to jump next (actually,
1271 it will be always wrong except in the uncommon case of a sequence of
1272 several identical opcodes).
1273
1274 "Threaded code" in contrast, uses an explicit jump table and an explicit
1275 indirect jump instruction at the end of each opcode. Since the jump
1276 instruction is at a different address for each opcode, the CPU will make a
1277 separate prediction for each of these instructions, which is equivalent to
1278 predicting the second opcode of each opcode pair. These predictions have
1279 a much better chance to turn out valid, especially in small bytecode loops.
1280
1281 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001283 and potentially many more instructions (depending on the pipeline width).
1284 A correctly predicted branch, however, is nearly free.
1285
1286 At the time of this writing, the "threaded code" version is up to 15-20%
1287 faster than the normal "switch" version, depending on the compiler and the
1288 CPU architecture.
1289
1290 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1291 because it would render the measurements invalid.
1292
1293
1294 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1295 indirect jumps by sharing them between all opcodes. Such optimizations
1296 can be disabled on gcc by using the -fno-gcse flag (or possibly
1297 -fno-crossjumping).
1298*/
1299
Antoine Pitrou042b1282010-08-13 21:15:58 +00001300#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001302#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001303#endif
1304
Antoine Pitrou042b1282010-08-13 21:15:58 +00001305#ifdef HAVE_COMPUTED_GOTOS
1306 #ifndef USE_COMPUTED_GOTOS
1307 #define USE_COMPUTED_GOTOS 1
1308 #endif
1309#else
1310 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1311 #error "Computed gotos are not supported on this compiler."
1312 #endif
1313 #undef USE_COMPUTED_GOTOS
1314 #define USE_COMPUTED_GOTOS 0
1315#endif
1316
1317#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001318/* Import the static jump table */
1319#include "opcode_targets.h"
1320
Antoine Pitroub52ec782009-01-25 16:34:23 +00001321#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001322 op: \
1323 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001324
Antoine Pitroub52ec782009-01-25 16:34:23 +00001325#ifdef LLTRACE
1326#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001328 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001330 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001331 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 } \
1333 goto fast_next_opcode; \
1334 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335#else
1336#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001338 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001340 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001341 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 } \
1343 goto fast_next_opcode; \
1344 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001345#endif
1346
Victor Stinner09532fe2019-05-10 23:39:09 +02001347#define DISPATCH() \
1348 { \
1349 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1350 FAST_DISPATCH(); \
1351 } \
1352 continue; \
1353 }
1354
Antoine Pitroub52ec782009-01-25 16:34:23 +00001355#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001358#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001359#endif
1360
1361
Neal Norwitza81d2202002-07-14 00:27:26 +00001362/* Tuple access macros */
1363
1364#ifndef Py_DEBUG
1365#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1366#else
1367#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1368#endif
1369
Guido van Rossum374a9221991-04-04 10:40:29 +00001370/* Code access macros */
1371
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001372/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001373#define INSTR_OFFSET() \
1374 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001375#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001376 _Py_CODEUNIT word = *next_instr; \
1377 opcode = _Py_OPCODE(word); \
1378 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001379 next_instr++; \
1380 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001381#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1382#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001383
Raymond Hettingerf606f872003-03-16 03:11:04 +00001384/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Some opcodes tend to come in pairs thus making it possible to
1386 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001387 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 Verifying the prediction costs a single high-speed test of a register
1390 variable against a constant. If the pairing was good, then the
1391 processor's own internal branch predication has a high likelihood of
1392 success, resulting in a nearly zero-overhead transition to the
1393 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001394 including its unpredictable switch-case branch. Combined with the
1395 processor's internal branch prediction, a successful PREDICT has the
1396 effect of making the two opcodes run as if they were a single new opcode
1397 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001398
Georg Brandl86b2fb92008-07-16 03:43:04 +00001399 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 predictions turned-on and interpret the results as if some opcodes
1401 had been combined or turn-off predictions so that the opcode frequency
1402 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001403
1404 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 the CPU to record separate branch prediction information for each
1406 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001407
Raymond Hettingerf606f872003-03-16 03:11:04 +00001408*/
1409
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001410#define PREDICT_ID(op) PRED_##op
1411
Antoine Pitrou042b1282010-08-13 21:15:58 +00001412#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001413#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001414#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001415#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001416 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001417 _Py_CODEUNIT word = *next_instr; \
1418 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001419 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001420 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001421 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001422 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001423 } \
1424 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001425#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001426#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001427
Raymond Hettingerf606f872003-03-16 03:11:04 +00001428
Guido van Rossum374a9221991-04-04 10:40:29 +00001429/* Stack manipulation macros */
1430
Martin v. Löwis18e16552006-02-15 17:27:45 +00001431/* The stack can grow at most MAXINT deep, as co_nlocals and
1432 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001433#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1434#define EMPTY() (STACK_LEVEL() == 0)
1435#define TOP() (stack_pointer[-1])
1436#define SECOND() (stack_pointer[-2])
1437#define THIRD() (stack_pointer[-3])
1438#define FOURTH() (stack_pointer[-4])
1439#define PEEK(n) (stack_pointer[-(n)])
1440#define SET_TOP(v) (stack_pointer[-1] = (v))
1441#define SET_SECOND(v) (stack_pointer[-2] = (v))
1442#define SET_THIRD(v) (stack_pointer[-3] = (v))
1443#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001444#define BASIC_STACKADJ(n) (stack_pointer += n)
1445#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1446#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001447
Guido van Rossum96a42c81992-01-12 02:29:51 +00001448#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001450 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001451 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001452#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001453 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001454#define STACK_GROW(n) do { \
1455 assert(n >= 0); \
1456 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001457 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001458 assert(STACK_LEVEL() <= co->co_stacksize); \
1459 } while (0)
1460#define STACK_SHRINK(n) do { \
1461 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001462 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001463 (void)(BASIC_STACKADJ(-n)); \
1464 assert(STACK_LEVEL() <= co->co_stacksize); \
1465 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001466#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001467 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001468 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001469#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001470#define PUSH(v) BASIC_PUSH(v)
1471#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001472#define STACK_GROW(n) BASIC_STACKADJ(n)
1473#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001474#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001475#endif
1476
Guido van Rossum681d79a1995-07-18 14:51:37 +00001477/* Local variable macros */
1478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001480
1481/* The SETLOCAL() macro must not DECREF the local variable in-place and
1482 then store the new value; it must copy the old value to a temporary
1483 value, then store the new value, and then DECREF the temporary value.
1484 This is because it is possible that during the DECREF the frame is
1485 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1486 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001488 GETLOCAL(i) = value; \
1489 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001490
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001491
1492#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 while (STACK_LEVEL() > (b)->b_level) { \
1494 PyObject *v = POP(); \
1495 Py_XDECREF(v); \
1496 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001497
1498#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001499 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001501 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1503 while (STACK_LEVEL() > (b)->b_level + 3) { \
1504 value = POP(); \
1505 Py_XDECREF(value); \
1506 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001507 exc_info = tstate->exc_info; \
1508 type = exc_info->exc_type; \
1509 value = exc_info->exc_value; \
1510 traceback = exc_info->exc_traceback; \
1511 exc_info->exc_type = POP(); \
1512 exc_info->exc_value = POP(); \
1513 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_XDECREF(type); \
1515 Py_XDECREF(value); \
1516 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001517 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001518
Inada Naoki91234a12019-06-03 21:30:58 +09001519 /* macros for opcode cache */
1520#define OPCACHE_CHECK() \
1521 do { \
1522 co_opcache = NULL; \
1523 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001524 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001525 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001526 if (co_opcache_offset > 0) { \
1527 assert(co_opcache_offset <= co->co_opcache_size); \
1528 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001529 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001530 } \
1531 } \
1532 } while (0)
1533
Pablo Galindo109826c2020-10-20 06:22:44 +01001534#define OPCACHE_DEOPT() \
1535 do { \
1536 if (co_opcache != NULL) { \
1537 co_opcache->optimized = -1; \
1538 unsigned char co_opcache_offset = \
1539 co->co_opcache_map[next_instr - first_instr]; \
1540 assert(co_opcache_offset <= co->co_opcache_size); \
1541 co->co_opcache_map[co_opcache_offset] = 0; \
1542 co_opcache = NULL; \
1543 } \
1544 } while (0)
1545
1546#define OPCACHE_DEOPT_LOAD_ATTR() \
1547 do { \
1548 if (co_opcache != NULL) { \
1549 OPCACHE_STAT_ATTR_DEOPT(); \
1550 OPCACHE_DEOPT(); \
1551 } \
1552 } while (0)
1553
1554#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1555 do { \
1556 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1557 OPCACHE_DEOPT_LOAD_ATTR(); \
1558 } \
1559 } while (0)
1560
Inada Naoki91234a12019-06-03 21:30:58 +09001561#if OPCACHE_STATS
1562
1563#define OPCACHE_STAT_GLOBAL_HIT() \
1564 do { \
1565 if (co->co_opcache != NULL) opcache_global_hits++; \
1566 } while (0)
1567
1568#define OPCACHE_STAT_GLOBAL_MISS() \
1569 do { \
1570 if (co->co_opcache != NULL) opcache_global_misses++; \
1571 } while (0)
1572
1573#define OPCACHE_STAT_GLOBAL_OPT() \
1574 do { \
1575 if (co->co_opcache != NULL) opcache_global_opts++; \
1576 } while (0)
1577
Pablo Galindo109826c2020-10-20 06:22:44 +01001578#define OPCACHE_STAT_ATTR_HIT() \
1579 do { \
1580 if (co->co_opcache != NULL) opcache_attr_hits++; \
1581 } while (0)
1582
1583#define OPCACHE_STAT_ATTR_MISS() \
1584 do { \
1585 if (co->co_opcache != NULL) opcache_attr_misses++; \
1586 } while (0)
1587
1588#define OPCACHE_STAT_ATTR_OPT() \
1589 do { \
1590 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1591 } while (0)
1592
1593#define OPCACHE_STAT_ATTR_DEOPT() \
1594 do { \
1595 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1596 } while (0)
1597
1598#define OPCACHE_STAT_ATTR_TOTAL() \
1599 do { \
1600 if (co->co_opcache != NULL) opcache_attr_total++; \
1601 } while (0)
1602
Inada Naoki91234a12019-06-03 21:30:58 +09001603#else /* OPCACHE_STATS */
1604
1605#define OPCACHE_STAT_GLOBAL_HIT()
1606#define OPCACHE_STAT_GLOBAL_MISS()
1607#define OPCACHE_STAT_GLOBAL_OPT()
1608
Pablo Galindo109826c2020-10-20 06:22:44 +01001609#define OPCACHE_STAT_ATTR_HIT()
1610#define OPCACHE_STAT_ATTR_MISS()
1611#define OPCACHE_STAT_ATTR_OPT()
1612#define OPCACHE_STAT_ATTR_DEOPT()
1613#define OPCACHE_STAT_ATTR_TOTAL()
1614
Inada Naoki91234a12019-06-03 21:30:58 +09001615#endif
1616
Guido van Rossuma027efa1997-05-05 20:56:21 +00001617/* Start of code */
1618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001620 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001622 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001625 co = f->f_code;
1626 PyCodeAddressRange bounds;
1627 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (tstate->use_tracing) {
1630 if (tstate->c_tracefunc != NULL) {
1631 /* tstate->c_tracefunc, if defined, is a
1632 function that will be called on *every* entry
1633 to a code block. Its return value, if not
1634 None, is a function that will be called at
1635 the start of each executed line of code.
1636 (Actually, the function must return itself
1637 in order to continue tracing.) The trace
1638 functions are called with three arguments:
1639 a pointer to the current frame, a string
1640 indicating why the function is called, and
1641 an argument which depends on the situation.
1642 The global trace function is also called
1643 whenever an exception is detected. */
1644 if (call_trace_protected(tstate->c_tracefunc,
1645 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001646 tstate, f, &bounds,
1647 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 /* Trace function raised an error */
1649 goto exit_eval_frame;
1650 }
1651 }
1652 if (tstate->c_profilefunc != NULL) {
1653 /* Similar for c_profilefunc, except it needn't
1654 return itself and isn't called for "line" events */
1655 if (call_trace_protected(tstate->c_profilefunc,
1656 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001657 tstate, f, &bounds,
1658 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 /* Profile function raised an error */
1660 goto exit_eval_frame;
1661 }
1662 }
1663 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001664
Łukasz Langaa785c872016-09-09 17:37:37 -07001665 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1666 dtrace_function_entry(f);
1667
Mark Shannon877df852020-11-12 09:43:29 +00001668 int instr_prev = -1;
1669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 names = co->co_names;
1671 consts = co->co_consts;
1672 fastlocals = f->f_localsplus;
1673 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001674 assert(PyBytes_Check(co->co_code));
1675 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001676 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1677 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1678 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001679 /*
1680 f->f_lasti refers to the index of the last instruction,
1681 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001682
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001683 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001684 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 When the PREDICT() macros are enabled, some opcode pairs follow in
1687 direct succession without updating f->f_lasti. A successful
1688 prediction effectively links the two codes together as if they
1689 were a single new opcode; accordingly,f->f_lasti will point to
1690 the first code in the pair (for instance, GET_ITER followed by
1691 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001692 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001694 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001695 next_instr = first_instr;
1696 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001697 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1698 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001699 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001700 stack_pointer = f->f_valuestack + f->f_stackdepth;
1701 /* Set f->f_stackdepth to -1.
1702 * Update when returning or calling trace function.
1703 Having f_stackdepth <= 0 ensures that invalid
1704 values are not visible to the cycle GC.
1705 We choose -1 rather than 0 to assist debugging.
1706 */
1707 f->f_stackdepth = -1;
1708 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001709
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001710 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001711 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001712 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001713 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001714 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001715 }
1716#if OPCACHE_STATS
1717 opcache_code_objects_extra_mem +=
1718 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1719 sizeof(_PyOpcache) * co->co_opcache_size;
1720 opcache_code_objects++;
1721#endif
1722 }
1723 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001724
Tim Peters5ca576e2001-06-18 22:08:13 +00001725#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001726 {
1727 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1728 if (r < 0) {
1729 goto exit_eval_frame;
1730 }
1731 lltrace = r;
1732 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001733#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001735 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001737 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001738
Victor Stinnerace47d72013-07-18 01:41:08 +02001739#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001740 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001741 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001742 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001743 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001744#endif
1745
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1749 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001750 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* Do periodic things. Doing this every time through
1753 the loop would add too much overhead, so we do it
1754 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001755 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 event needs attention (e.g. a signal handler or
1757 async I/O handler); see Py_AddPendingCall() and
1758 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001759
Eric Snow7bda9de2019-03-08 17:25:54 -07001760 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001761 opcode = _Py_OPCODE(*next_instr);
1762 if (opcode == SETUP_FINALLY ||
1763 opcode == SETUP_WITH ||
1764 opcode == BEFORE_ASYNC_WITH ||
1765 opcode == YIELD_FROM) {
1766 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001767 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001768 - If we're about to enter the 'with:'. It will prevent
1769 emitting a resource warning in the common idiom
1770 'with open(path) as file:'.
1771 - If we're about to enter the 'async with:'.
1772 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001773 *very* useful, but might help in some cases and it's
1774 traditional)
1775 - If we're resuming a chain of nested 'yield from' or
1776 'await' calls, then each frame is parked with YIELD_FROM
1777 as its next opcode. If the user hit control-C we want to
1778 wait until we've reached the innermost frame before
1779 running the signal handler and raising KeyboardInterrupt
1780 (see bpo-30039).
1781 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 goto fast_next_opcode;
1783 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001784
Victor Stinnerda2914d2020-03-20 09:29:08 +01001785 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
1788 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 fast_next_opcode:
1791 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Łukasz Langaa785c872016-09-09 17:37:37 -07001793 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001794 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001797
Victor Stinnerdab84232020-03-17 18:56:44 +01001798 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001799 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001801 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001803 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 err = maybe_call_line_trace(tstate->c_tracefunc,
1806 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001807 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001808 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* Reload possibly changed frame fields */
1810 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001811 stack_pointer = f->f_valuestack+f->f_stackdepth;
1812 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001819
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001820 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001821 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001822#ifdef DYNAMIC_EXECUTION_PROFILE
1823#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 dxpairs[lastopcode][opcode]++;
1825 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001828#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001829
Guido van Rossum96a42c81992-01-12 02:29:51 +00001830#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 if (lltrace) {
1834 if (HAS_ARG(opcode)) {
1835 printf("%d: %d, %d\n",
1836 f->f_lasti, opcode, oparg);
1837 }
1838 else {
1839 printf("%d: %d\n",
1840 f->f_lasti, opcode);
1841 }
1842 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001843#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001848 It is essential that any operation that fails must goto error
1849 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Benjamin Petersonddd19492018-09-16 22:38:02 -07001851 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001854
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *value = GETLOCAL(oparg);
1857 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001858 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 UNBOUNDLOCAL_ERROR_MSG,
1860 PyTuple_GetItem(co->co_varnames, oparg));
1861 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 Py_INCREF(value);
1864 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 }
1867
Benjamin Petersonddd19492018-09-16 22:38:02 -07001868 case TARGET(LOAD_CONST): {
1869 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 PyObject *value = GETITEM(consts, oparg);
1871 Py_INCREF(value);
1872 PUSH(value);
1873 FAST_DISPATCH();
1874 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001875
Benjamin Petersonddd19492018-09-16 22:38:02 -07001876 case TARGET(STORE_FAST): {
1877 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *value = POP();
1879 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001882
Benjamin Petersonddd19492018-09-16 22:38:02 -07001883 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *value = POP();
1885 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Benjamin Petersonddd19492018-09-16 22:38:02 -07001889 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *top = TOP();
1891 PyObject *second = SECOND();
1892 SET_TOP(second);
1893 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Benjamin Petersonddd19492018-09-16 22:38:02 -07001897 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 PyObject *top = TOP();
1899 PyObject *second = SECOND();
1900 PyObject *third = THIRD();
1901 SET_TOP(second);
1902 SET_SECOND(third);
1903 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001908 PyObject *top = TOP();
1909 PyObject *second = SECOND();
1910 PyObject *third = THIRD();
1911 PyObject *fourth = FOURTH();
1912 SET_TOP(second);
1913 SET_SECOND(third);
1914 SET_THIRD(fourth);
1915 SET_FOURTH(top);
1916 FAST_DISPATCH();
1917 }
1918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 PyObject *top = TOP();
1921 Py_INCREF(top);
1922 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Benjamin Petersonddd19492018-09-16 22:38:02 -07001926 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001929 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001931 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 SET_TOP(top);
1933 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001934 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001936
Benjamin Petersonddd19492018-09-16 22:38:02 -07001937 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 PyObject *value = TOP();
1939 PyObject *res = PyNumber_Positive(value);
1940 Py_DECREF(value);
1941 SET_TOP(res);
1942 if (res == NULL)
1943 goto error;
1944 DISPATCH();
1945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 PyObject *value = TOP();
1949 PyObject *res = PyNumber_Negative(value);
1950 Py_DECREF(value);
1951 SET_TOP(res);
1952 if (res == NULL)
1953 goto error;
1954 DISPATCH();
1955 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Benjamin Petersonddd19492018-09-16 22:38:02 -07001957 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 PyObject *value = TOP();
1959 int err = PyObject_IsTrue(value);
1960 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (err == 0) {
1962 Py_INCREF(Py_True);
1963 SET_TOP(Py_True);
1964 DISPATCH();
1965 }
1966 else if (err > 0) {
1967 Py_INCREF(Py_False);
1968 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 DISPATCH();
1970 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001971 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 goto error;
1973 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001974
Benjamin Petersonddd19492018-09-16 22:38:02 -07001975 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 PyObject *value = TOP();
1977 PyObject *res = PyNumber_Invert(value);
1978 Py_DECREF(value);
1979 SET_TOP(res);
1980 if (res == NULL)
1981 goto error;
1982 DISPATCH();
1983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 PyObject *exp = POP();
1987 PyObject *base = TOP();
1988 PyObject *res = PyNumber_Power(base, exp, Py_None);
1989 Py_DECREF(base);
1990 Py_DECREF(exp);
1991 SET_TOP(res);
1992 if (res == NULL)
1993 goto error;
1994 DISPATCH();
1995 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Benjamin Petersonddd19492018-09-16 22:38:02 -07001997 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 PyObject *right = POP();
1999 PyObject *left = TOP();
2000 PyObject *res = PyNumber_Multiply(left, right);
2001 Py_DECREF(left);
2002 Py_DECREF(right);
2003 SET_TOP(res);
2004 if (res == NULL)
2005 goto error;
2006 DISPATCH();
2007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Benjamin Petersonddd19492018-09-16 22:38:02 -07002009 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002010 PyObject *right = POP();
2011 PyObject *left = TOP();
2012 PyObject *res = PyNumber_MatrixMultiply(left, right);
2013 Py_DECREF(left);
2014 Py_DECREF(right);
2015 SET_TOP(res);
2016 if (res == NULL)
2017 goto error;
2018 DISPATCH();
2019 }
2020
Benjamin Petersonddd19492018-09-16 22:38:02 -07002021 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 PyObject *divisor = POP();
2023 PyObject *dividend = TOP();
2024 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2025 Py_DECREF(dividend);
2026 Py_DECREF(divisor);
2027 SET_TOP(quotient);
2028 if (quotient == NULL)
2029 goto error;
2030 DISPATCH();
2031 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Benjamin Petersonddd19492018-09-16 22:38:02 -07002033 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 PyObject *divisor = POP();
2035 PyObject *dividend = TOP();
2036 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2037 Py_DECREF(dividend);
2038 Py_DECREF(divisor);
2039 SET_TOP(quotient);
2040 if (quotient == NULL)
2041 goto error;
2042 DISPATCH();
2043 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002044
Benjamin Petersonddd19492018-09-16 22:38:02 -07002045 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 PyObject *divisor = POP();
2047 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002048 PyObject *res;
2049 if (PyUnicode_CheckExact(dividend) && (
2050 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2051 // fast path; string formatting, but not if the RHS is a str subclass
2052 // (see issue28598)
2053 res = PyUnicode_Format(dividend, divisor);
2054 } else {
2055 res = PyNumber_Remainder(dividend, divisor);
2056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_DECREF(divisor);
2058 Py_DECREF(dividend);
2059 SET_TOP(res);
2060 if (res == NULL)
2061 goto error;
2062 DISPATCH();
2063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Benjamin Petersonddd19492018-09-16 22:38:02 -07002065 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 PyObject *right = POP();
2067 PyObject *left = TOP();
2068 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002069 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002070 CPython using bytecode, it is simply worthless.
2071 See http://bugs.python.org/issue21955 and
2072 http://bugs.python.org/issue10044 for the discussion. In short,
2073 no patch shown any impact on a realistic benchmark, only a minor
2074 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 if (PyUnicode_CheckExact(left) &&
2076 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002077 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002078 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002079 }
2080 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 sum = PyNumber_Add(left, right);
2082 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 Py_DECREF(right);
2085 SET_TOP(sum);
2086 if (sum == NULL)
2087 goto error;
2088 DISPATCH();
2089 }
2090
Benjamin Petersonddd19492018-09-16 22:38:02 -07002091 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 PyObject *right = POP();
2093 PyObject *left = TOP();
2094 PyObject *diff = PyNumber_Subtract(left, right);
2095 Py_DECREF(right);
2096 Py_DECREF(left);
2097 SET_TOP(diff);
2098 if (diff == NULL)
2099 goto error;
2100 DISPATCH();
2101 }
2102
Benjamin Petersonddd19492018-09-16 22:38:02 -07002103 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 PyObject *sub = POP();
2105 PyObject *container = TOP();
2106 PyObject *res = PyObject_GetItem(container, sub);
2107 Py_DECREF(container);
2108 Py_DECREF(sub);
2109 SET_TOP(res);
2110 if (res == NULL)
2111 goto error;
2112 DISPATCH();
2113 }
2114
Benjamin Petersonddd19492018-09-16 22:38:02 -07002115 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PyObject *right = POP();
2117 PyObject *left = TOP();
2118 PyObject *res = PyNumber_Lshift(left, right);
2119 Py_DECREF(left);
2120 Py_DECREF(right);
2121 SET_TOP(res);
2122 if (res == NULL)
2123 goto error;
2124 DISPATCH();
2125 }
2126
Benjamin Petersonddd19492018-09-16 22:38:02 -07002127 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 PyObject *right = POP();
2129 PyObject *left = TOP();
2130 PyObject *res = PyNumber_Rshift(left, right);
2131 Py_DECREF(left);
2132 Py_DECREF(right);
2133 SET_TOP(res);
2134 if (res == NULL)
2135 goto error;
2136 DISPATCH();
2137 }
2138
Benjamin Petersonddd19492018-09-16 22:38:02 -07002139 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 PyObject *right = POP();
2141 PyObject *left = TOP();
2142 PyObject *res = PyNumber_And(left, right);
2143 Py_DECREF(left);
2144 Py_DECREF(right);
2145 SET_TOP(res);
2146 if (res == NULL)
2147 goto error;
2148 DISPATCH();
2149 }
2150
Benjamin Petersonddd19492018-09-16 22:38:02 -07002151 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 PyObject *right = POP();
2153 PyObject *left = TOP();
2154 PyObject *res = PyNumber_Xor(left, right);
2155 Py_DECREF(left);
2156 Py_DECREF(right);
2157 SET_TOP(res);
2158 if (res == NULL)
2159 goto error;
2160 DISPATCH();
2161 }
2162
Benjamin Petersonddd19492018-09-16 22:38:02 -07002163 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 PyObject *right = POP();
2165 PyObject *left = TOP();
2166 PyObject *res = PyNumber_Or(left, right);
2167 Py_DECREF(left);
2168 Py_DECREF(right);
2169 SET_TOP(res);
2170 if (res == NULL)
2171 goto error;
2172 DISPATCH();
2173 }
2174
Benjamin Petersonddd19492018-09-16 22:38:02 -07002175 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 PyObject *v = POP();
2177 PyObject *list = PEEK(oparg);
2178 int err;
2179 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 if (err != 0)
2182 goto error;
2183 PREDICT(JUMP_ABSOLUTE);
2184 DISPATCH();
2185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002186
Benjamin Petersonddd19492018-09-16 22:38:02 -07002187 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002189 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 int err;
2191 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 if (err != 0)
2194 goto error;
2195 PREDICT(JUMP_ABSOLUTE);
2196 DISPATCH();
2197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 PyObject *exp = POP();
2201 PyObject *base = TOP();
2202 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2203 Py_DECREF(base);
2204 Py_DECREF(exp);
2205 SET_TOP(res);
2206 if (res == NULL)
2207 goto error;
2208 DISPATCH();
2209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002210
Benjamin Petersonddd19492018-09-16 22:38:02 -07002211 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 PyObject *right = POP();
2213 PyObject *left = TOP();
2214 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2215 Py_DECREF(left);
2216 Py_DECREF(right);
2217 SET_TOP(res);
2218 if (res == NULL)
2219 goto error;
2220 DISPATCH();
2221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002222
Benjamin Petersonddd19492018-09-16 22:38:02 -07002223 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002224 PyObject *right = POP();
2225 PyObject *left = TOP();
2226 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2227 Py_DECREF(left);
2228 Py_DECREF(right);
2229 SET_TOP(res);
2230 if (res == NULL)
2231 goto error;
2232 DISPATCH();
2233 }
2234
Benjamin Petersonddd19492018-09-16 22:38:02 -07002235 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 PyObject *divisor = POP();
2237 PyObject *dividend = TOP();
2238 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2239 Py_DECREF(dividend);
2240 Py_DECREF(divisor);
2241 SET_TOP(quotient);
2242 if (quotient == NULL)
2243 goto error;
2244 DISPATCH();
2245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002246
Benjamin Petersonddd19492018-09-16 22:38:02 -07002247 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 PyObject *divisor = POP();
2249 PyObject *dividend = TOP();
2250 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2251 Py_DECREF(dividend);
2252 Py_DECREF(divisor);
2253 SET_TOP(quotient);
2254 if (quotient == NULL)
2255 goto error;
2256 DISPATCH();
2257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002258
Benjamin Petersonddd19492018-09-16 22:38:02 -07002259 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 PyObject *right = POP();
2261 PyObject *left = TOP();
2262 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2263 Py_DECREF(left);
2264 Py_DECREF(right);
2265 SET_TOP(mod);
2266 if (mod == NULL)
2267 goto error;
2268 DISPATCH();
2269 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002270
Benjamin Petersonddd19492018-09-16 22:38:02 -07002271 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 PyObject *right = POP();
2273 PyObject *left = TOP();
2274 PyObject *sum;
2275 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002276 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002277 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002278 }
2279 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 sum = PyNumber_InPlaceAdd(left, right);
2281 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 Py_DECREF(right);
2284 SET_TOP(sum);
2285 if (sum == NULL)
2286 goto error;
2287 DISPATCH();
2288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Benjamin Petersonddd19492018-09-16 22:38:02 -07002290 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 PyObject *right = POP();
2292 PyObject *left = TOP();
2293 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2294 Py_DECREF(left);
2295 Py_DECREF(right);
2296 SET_TOP(diff);
2297 if (diff == NULL)
2298 goto error;
2299 DISPATCH();
2300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Benjamin Petersonddd19492018-09-16 22:38:02 -07002302 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 PyObject *right = POP();
2304 PyObject *left = TOP();
2305 PyObject *res = PyNumber_InPlaceLshift(left, right);
2306 Py_DECREF(left);
2307 Py_DECREF(right);
2308 SET_TOP(res);
2309 if (res == NULL)
2310 goto error;
2311 DISPATCH();
2312 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Benjamin Petersonddd19492018-09-16 22:38:02 -07002314 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 PyObject *right = POP();
2316 PyObject *left = TOP();
2317 PyObject *res = PyNumber_InPlaceRshift(left, right);
2318 Py_DECREF(left);
2319 Py_DECREF(right);
2320 SET_TOP(res);
2321 if (res == NULL)
2322 goto error;
2323 DISPATCH();
2324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Petersonddd19492018-09-16 22:38:02 -07002326 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 PyObject *right = POP();
2328 PyObject *left = TOP();
2329 PyObject *res = PyNumber_InPlaceAnd(left, right);
2330 Py_DECREF(left);
2331 Py_DECREF(right);
2332 SET_TOP(res);
2333 if (res == NULL)
2334 goto error;
2335 DISPATCH();
2336 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002337
Benjamin Petersonddd19492018-09-16 22:38:02 -07002338 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 PyObject *right = POP();
2340 PyObject *left = TOP();
2341 PyObject *res = PyNumber_InPlaceXor(left, right);
2342 Py_DECREF(left);
2343 Py_DECREF(right);
2344 SET_TOP(res);
2345 if (res == NULL)
2346 goto error;
2347 DISPATCH();
2348 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002349
Benjamin Petersonddd19492018-09-16 22:38:02 -07002350 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 PyObject *right = POP();
2352 PyObject *left = TOP();
2353 PyObject *res = PyNumber_InPlaceOr(left, right);
2354 Py_DECREF(left);
2355 Py_DECREF(right);
2356 SET_TOP(res);
2357 if (res == NULL)
2358 goto error;
2359 DISPATCH();
2360 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002361
Benjamin Petersonddd19492018-09-16 22:38:02 -07002362 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 PyObject *sub = TOP();
2364 PyObject *container = SECOND();
2365 PyObject *v = THIRD();
2366 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002367 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002368 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 Py_DECREF(container);
2372 Py_DECREF(sub);
2373 if (err != 0)
2374 goto error;
2375 DISPATCH();
2376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Benjamin Petersonddd19492018-09-16 22:38:02 -07002378 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 PyObject *sub = TOP();
2380 PyObject *container = SECOND();
2381 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002382 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002383 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 err = PyObject_DelItem(container, sub);
2385 Py_DECREF(container);
2386 Py_DECREF(sub);
2387 if (err != 0)
2388 goto error;
2389 DISPATCH();
2390 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002391
Benjamin Petersonddd19492018-09-16 22:38:02 -07002392 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002393 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002395 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002396 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002398 _PyErr_SetString(tstate, PyExc_RuntimeError,
2399 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 Py_DECREF(value);
2401 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002403 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 Py_DECREF(value);
2405 if (res == NULL)
2406 goto error;
2407 Py_DECREF(res);
2408 DISPATCH();
2409 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002410
Benjamin Petersonddd19492018-09-16 22:38:02 -07002411 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 switch (oparg) {
2414 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002416 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002419 /* fall through */
2420 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002421 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002422 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 break;
2425 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002426 _PyErr_SetString(tstate, PyExc_SystemError,
2427 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 break;
2429 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 goto error;
2431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002432
Benjamin Petersonddd19492018-09-16 22:38:02 -07002433 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002435 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002436 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002437 f->f_state = FRAME_RETURNED;
2438 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002439 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002441
Benjamin Petersonddd19492018-09-16 22:38:02 -07002442 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002443 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002444 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002445 PyObject *obj = TOP();
2446 PyTypeObject *type = Py_TYPE(obj);
2447
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002448 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002449 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002450 }
Yury Selivanov75445082015-05-11 22:57:16 -04002451
2452 if (getter != NULL) {
2453 iter = (*getter)(obj);
2454 Py_DECREF(obj);
2455 if (iter == NULL) {
2456 SET_TOP(NULL);
2457 goto error;
2458 }
2459 }
2460 else {
2461 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002462 _PyErr_Format(tstate, PyExc_TypeError,
2463 "'async for' requires an object with "
2464 "__aiter__ method, got %.100s",
2465 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002466 Py_DECREF(obj);
2467 goto error;
2468 }
2469
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002470 if (Py_TYPE(iter)->tp_as_async == NULL ||
2471 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002472
Yury Selivanov398ff912017-03-02 22:20:00 -05002473 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 _PyErr_Format(tstate, PyExc_TypeError,
2475 "'async for' received an object from __aiter__ "
2476 "that does not implement __anext__: %.100s",
2477 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002478 Py_DECREF(iter);
2479 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002480 }
2481
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002482 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002483 DISPATCH();
2484 }
2485
Benjamin Petersonddd19492018-09-16 22:38:02 -07002486 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002487 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002488 PyObject *next_iter = NULL;
2489 PyObject *awaitable = NULL;
2490 PyObject *aiter = TOP();
2491 PyTypeObject *type = Py_TYPE(aiter);
2492
Yury Selivanoveb636452016-09-08 22:01:51 -07002493 if (PyAsyncGen_CheckExact(aiter)) {
2494 awaitable = type->tp_as_async->am_anext(aiter);
2495 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002496 goto error;
2497 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002498 } else {
2499 if (type->tp_as_async != NULL){
2500 getter = type->tp_as_async->am_anext;
2501 }
Yury Selivanov75445082015-05-11 22:57:16 -04002502
Yury Selivanoveb636452016-09-08 22:01:51 -07002503 if (getter != NULL) {
2504 next_iter = (*getter)(aiter);
2505 if (next_iter == NULL) {
2506 goto error;
2507 }
2508 }
2509 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002510 _PyErr_Format(tstate, PyExc_TypeError,
2511 "'async for' requires an iterator with "
2512 "__anext__ method, got %.100s",
2513 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002514 goto error;
2515 }
Yury Selivanov75445082015-05-11 22:57:16 -04002516
Yury Selivanoveb636452016-09-08 22:01:51 -07002517 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2518 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002519 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002520 PyExc_TypeError,
2521 "'async for' received an invalid object "
2522 "from __anext__: %.100s",
2523 Py_TYPE(next_iter)->tp_name);
2524
2525 Py_DECREF(next_iter);
2526 goto error;
2527 } else {
2528 Py_DECREF(next_iter);
2529 }
2530 }
Yury Selivanov75445082015-05-11 22:57:16 -04002531
2532 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002533 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002534 DISPATCH();
2535 }
2536
Benjamin Petersonddd19492018-09-16 22:38:02 -07002537 case TARGET(GET_AWAITABLE): {
2538 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002539 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002540 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002541
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002542 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002543 int opcode_at_minus_3 = 0;
2544 if ((next_instr - first_instr) > 2) {
2545 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2546 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002547 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002548 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002549 _Py_OPCODE(next_instr[-2]));
2550 }
2551
Yury Selivanov75445082015-05-11 22:57:16 -04002552 Py_DECREF(iterable);
2553
Yury Selivanovc724bae2016-03-02 11:30:46 -05002554 if (iter != NULL && PyCoro_CheckExact(iter)) {
2555 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2556 if (yf != NULL) {
2557 /* `iter` is a coroutine object that is being
2558 awaited, `yf` is a pointer to the current awaitable
2559 being awaited on. */
2560 Py_DECREF(yf);
2561 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002562 _PyErr_SetString(tstate, PyExc_RuntimeError,
2563 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002564 /* The code below jumps to `error` if `iter` is NULL. */
2565 }
2566 }
2567
Yury Selivanov75445082015-05-11 22:57:16 -04002568 SET_TOP(iter); /* Even if it's NULL */
2569
2570 if (iter == NULL) {
2571 goto error;
2572 }
2573
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002574 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002575 DISPATCH();
2576 }
2577
Benjamin Petersonddd19492018-09-16 22:38:02 -07002578 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002580 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002581 PySendResult gen_status;
2582 if (tstate->c_tracefunc == NULL) {
2583 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002584 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002585 _Py_IDENTIFIER(send);
2586 if (v == Py_None && PyIter_Check(receiver)) {
2587 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002588 }
2589 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002590 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002591 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002592 if (retval == NULL) {
2593 if (tstate->c_tracefunc != NULL
2594 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002595 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002596 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2597 gen_status = PYGEN_RETURN;
2598 }
2599 else {
2600 gen_status = PYGEN_ERROR;
2601 }
2602 }
2603 else {
2604 gen_status = PYGEN_NEXT;
2605 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002606 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002608 if (gen_status == PYGEN_ERROR) {
2609 assert (retval == NULL);
2610 goto error;
2611 }
2612 if (gen_status == PYGEN_RETURN) {
2613 assert (retval != NULL);
2614
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002615 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002616 SET_TOP(retval);
2617 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002619 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002620 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002621 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002622 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002623 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002624 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002625 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002626 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002627 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002629
Benjamin Petersonddd19492018-09-16 22:38:02 -07002630 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002632
2633 if (co->co_flags & CO_ASYNC_GENERATOR) {
2634 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2635 Py_DECREF(retval);
2636 if (w == NULL) {
2637 retval = NULL;
2638 goto error;
2639 }
2640 retval = w;
2641 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002642 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002643 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002644 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002646
Benjamin Petersonddd19492018-09-16 22:38:02 -07002647 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002648 PyObject *type, *value, *traceback;
2649 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 PyTryBlock *b = PyFrame_BlockPop(f);
2651 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002652 _PyErr_SetString(tstate, PyExc_SystemError,
2653 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002656 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2657 STACK_LEVEL() <= (b)->b_level + 4);
2658 exc_info = tstate->exc_info;
2659 type = exc_info->exc_type;
2660 value = exc_info->exc_value;
2661 traceback = exc_info->exc_traceback;
2662 exc_info->exc_type = POP();
2663 exc_info->exc_value = POP();
2664 exc_info->exc_traceback = POP();
2665 Py_XDECREF(type);
2666 Py_XDECREF(value);
2667 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002670
Benjamin Petersonddd19492018-09-16 22:38:02 -07002671 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002672 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002675
Mark Shannonfee55262019-11-21 09:11:43 +00002676 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002677 assert(f->f_iblock > 0);
2678 if (oparg) {
2679 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2680 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002681 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002682 PyObject *val = POP();
2683 PyObject *tb = POP();
2684 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002685 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002686 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002688
Benjamin Petersonddd19492018-09-16 22:38:02 -07002689 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002690 PyObject *exc = POP();
2691 assert(PyExceptionClass_Check(exc));
2692 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2693 PyTryBlock *b = PyFrame_BlockPop(f);
2694 assert(b->b_type == EXCEPT_HANDLER);
2695 Py_DECREF(exc);
2696 UNWIND_EXCEPT_HANDLER(b);
2697 Py_DECREF(POP());
2698 JUMPBY(oparg);
2699 FAST_DISPATCH();
2700 }
2701 else {
2702 PyObject *val = POP();
2703 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002704 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002705 goto exception_unwind;
2706 }
2707 }
2708
Zackery Spytzce6a0702019-08-25 03:44:09 -06002709 case TARGET(LOAD_ASSERTION_ERROR): {
2710 PyObject *value = PyExc_AssertionError;
2711 Py_INCREF(value);
2712 PUSH(value);
2713 FAST_DISPATCH();
2714 }
2715
Benjamin Petersonddd19492018-09-16 22:38:02 -07002716 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002717 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002718
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002720 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002721 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002723 if (!_PyErr_Occurred(tstate)) {
2724 _PyErr_SetString(tstate, PyExc_NameError,
2725 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002726 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002730 }
2731 else {
2732 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2733 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002734 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2736 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002737 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2738 _PyErr_SetString(tstate, PyExc_NameError,
2739 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002741 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002744 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002746
Benjamin Petersonddd19492018-09-16 22:38:02 -07002747 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 PyObject *name = GETITEM(names, oparg);
2749 PyObject *v = POP();
2750 PyObject *ns = f->f_locals;
2751 int err;
2752 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002753 _PyErr_Format(tstate, PyExc_SystemError,
2754 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 if (PyDict_CheckExact(ns))
2759 err = PyDict_SetItem(ns, name, v);
2760 else
2761 err = PyObject_SetItem(ns, name, v);
2762 Py_DECREF(v);
2763 if (err != 0)
2764 goto error;
2765 DISPATCH();
2766 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002767
Benjamin Petersonddd19492018-09-16 22:38:02 -07002768 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 PyObject *name = GETITEM(names, oparg);
2770 PyObject *ns = f->f_locals;
2771 int err;
2772 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002773 _PyErr_Format(tstate, PyExc_SystemError,
2774 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 err = PyObject_DelItem(ns, name);
2778 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002779 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 NAME_ERROR_MSG,
2781 name);
2782 goto error;
2783 }
2784 DISPATCH();
2785 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002786
Benjamin Petersonddd19492018-09-16 22:38:02 -07002787 case TARGET(UNPACK_SEQUENCE): {
2788 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 PyObject *seq = POP(), *item, **items;
2790 if (PyTuple_CheckExact(seq) &&
2791 PyTuple_GET_SIZE(seq) == oparg) {
2792 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 item = items[oparg];
2795 Py_INCREF(item);
2796 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 } else if (PyList_CheckExact(seq) &&
2799 PyList_GET_SIZE(seq) == oparg) {
2800 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 item = items[oparg];
2803 Py_INCREF(item);
2804 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002806 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002808 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 } else {
2810 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 Py_DECREF(seq);
2812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002815 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002817
Benjamin Petersonddd19492018-09-16 22:38:02 -07002818 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2820 PyObject *seq = POP();
2821
Victor Stinner438a12d2019-05-24 17:01:38 +02002822 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 stack_pointer + totalargs)) {
2824 stack_pointer += totalargs;
2825 } else {
2826 Py_DECREF(seq);
2827 goto error;
2828 }
2829 Py_DECREF(seq);
2830 DISPATCH();
2831 }
2832
Benjamin Petersonddd19492018-09-16 22:38:02 -07002833 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 PyObject *name = GETITEM(names, oparg);
2835 PyObject *owner = TOP();
2836 PyObject *v = SECOND();
2837 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002838 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 Py_DECREF(owner);
2842 if (err != 0)
2843 goto error;
2844 DISPATCH();
2845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002846
Benjamin Petersonddd19492018-09-16 22:38:02 -07002847 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 PyObject *name = GETITEM(names, oparg);
2849 PyObject *owner = POP();
2850 int err;
2851 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2852 Py_DECREF(owner);
2853 if (err != 0)
2854 goto error;
2855 DISPATCH();
2856 }
2857
Benjamin Petersonddd19492018-09-16 22:38:02 -07002858 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *name = GETITEM(names, oparg);
2860 PyObject *v = POP();
2861 int err;
2862 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 if (err != 0)
2865 goto error;
2866 DISPATCH();
2867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002868
Benjamin Petersonddd19492018-09-16 22:38:02 -07002869 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 PyObject *name = GETITEM(names, oparg);
2871 int err;
2872 err = PyDict_DelItem(f->f_globals, name);
2873 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002874 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2875 format_exc_check_arg(tstate, PyExc_NameError,
2876 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002879 }
2880 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Benjamin Petersonddd19492018-09-16 22:38:02 -07002883 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 PyObject *name = GETITEM(names, oparg);
2885 PyObject *locals = f->f_locals;
2886 PyObject *v;
2887 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002888 _PyErr_Format(tstate, PyExc_SystemError,
2889 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002893 v = PyDict_GetItemWithError(locals, name);
2894 if (v != NULL) {
2895 Py_INCREF(v);
2896 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002897 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002898 goto error;
2899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 }
2901 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002903 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002904 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002905 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002906 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 }
2908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002910 v = PyDict_GetItemWithError(f->f_globals, name);
2911 if (v != NULL) {
2912 Py_INCREF(v);
2913 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002914 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002915 goto error;
2916 }
2917 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002918 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002919 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002921 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002922 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002923 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002925 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002929 }
2930 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 v = PyObject_GetItem(f->f_builtins, name);
2932 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002933 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002934 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002935 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002937 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002939 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002946
Benjamin Petersonddd19492018-09-16 22:38:02 -07002947 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002948 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002950 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002951 && PyDict_CheckExact(f->f_builtins))
2952 {
Inada Naoki91234a12019-06-03 21:30:58 +09002953 OPCACHE_CHECK();
2954 if (co_opcache != NULL && co_opcache->optimized > 0) {
2955 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2956
2957 if (lg->globals_ver ==
2958 ((PyDictObject *)f->f_globals)->ma_version_tag
2959 && lg->builtins_ver ==
2960 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2961 {
2962 PyObject *ptr = lg->ptr;
2963 OPCACHE_STAT_GLOBAL_HIT();
2964 assert(ptr != NULL);
2965 Py_INCREF(ptr);
2966 PUSH(ptr);
2967 DISPATCH();
2968 }
2969 }
2970
2971 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002973 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 name);
2975 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002976 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002977 /* _PyDict_LoadGlobal() returns NULL without raising
2978 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002979 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002980 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 }
Inada Naoki91234a12019-06-03 21:30:58 +09002984
2985 if (co_opcache != NULL) {
2986 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2987
2988 if (co_opcache->optimized == 0) {
2989 /* Wasn't optimized before. */
2990 OPCACHE_STAT_GLOBAL_OPT();
2991 } else {
2992 OPCACHE_STAT_GLOBAL_MISS();
2993 }
2994
2995 co_opcache->optimized = 1;
2996 lg->globals_ver =
2997 ((PyDictObject *)f->f_globals)->ma_version_tag;
2998 lg->builtins_ver =
2999 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3000 lg->ptr = v; /* borrowed */
3001 }
3002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003005 else {
3006 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003007
3008 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003009 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 v = PyObject_GetItem(f->f_globals, name);
3011 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003012 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003013 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003014 }
3015 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003016
Victor Stinnerb4efc962015-11-20 09:24:02 +01003017 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 v = PyObject_GetItem(f->f_builtins, name);
3019 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003020 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003021 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003022 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003023 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003026 }
3027 }
3028 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003032
Benjamin Petersonddd19492018-09-16 22:38:02 -07003033 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 PyObject *v = GETLOCAL(oparg);
3035 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 SETLOCAL(oparg, NULL);
3037 DISPATCH();
3038 }
3039 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003040 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 UNBOUNDLOCAL_ERROR_MSG,
3042 PyTuple_GetItem(co->co_varnames, oparg)
3043 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 goto error;
3045 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003046
Benjamin Petersonddd19492018-09-16 22:38:02 -07003047 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003049 PyObject *oldobj = PyCell_GET(cell);
3050 if (oldobj != NULL) {
3051 PyCell_SET(cell, NULL);
3052 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003053 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003054 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003055 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 goto error;
3057 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003058
Benjamin Petersonddd19492018-09-16 22:38:02 -07003059 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 PyObject *cell = freevars[oparg];
3061 Py_INCREF(cell);
3062 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003065
Benjamin Petersonddd19492018-09-16 22:38:02 -07003066 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003067 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003068 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003069 assert(locals);
3070 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3071 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3072 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3073 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3074 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003075 value = PyDict_GetItemWithError(locals, name);
3076 if (value != NULL) {
3077 Py_INCREF(value);
3078 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003079 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003080 goto error;
3081 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003082 }
3083 else {
3084 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003085 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003086 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003087 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003088 }
3089 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003090 }
3091 }
3092 if (!value) {
3093 PyObject *cell = freevars[oparg];
3094 value = PyCell_GET(cell);
3095 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003096 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003097 goto error;
3098 }
3099 Py_INCREF(value);
3100 }
3101 PUSH(value);
3102 DISPATCH();
3103 }
3104
Benjamin Petersonddd19492018-09-16 22:38:02 -07003105 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 PyObject *cell = freevars[oparg];
3107 PyObject *value = PyCell_GET(cell);
3108 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003109 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 Py_INCREF(value);
3113 PUSH(value);
3114 DISPATCH();
3115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *v = POP();
3119 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003120 PyObject *oldobj = PyCell_GET(cell);
3121 PyCell_SET(cell, v);
3122 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 DISPATCH();
3124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003125
Benjamin Petersonddd19492018-09-16 22:38:02 -07003126 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003127 PyObject *str;
3128 PyObject *empty = PyUnicode_New(0, 0);
3129 if (empty == NULL) {
3130 goto error;
3131 }
3132 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3133 Py_DECREF(empty);
3134 if (str == NULL)
3135 goto error;
3136 while (--oparg >= 0) {
3137 PyObject *item = POP();
3138 Py_DECREF(item);
3139 }
3140 PUSH(str);
3141 DISPATCH();
3142 }
3143
Benjamin Petersonddd19492018-09-16 22:38:02 -07003144 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 PyObject *tup = PyTuple_New(oparg);
3146 if (tup == NULL)
3147 goto error;
3148 while (--oparg >= 0) {
3149 PyObject *item = POP();
3150 PyTuple_SET_ITEM(tup, oparg, item);
3151 }
3152 PUSH(tup);
3153 DISPATCH();
3154 }
3155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PyObject *list = PyList_New(oparg);
3158 if (list == NULL)
3159 goto error;
3160 while (--oparg >= 0) {
3161 PyObject *item = POP();
3162 PyList_SET_ITEM(list, oparg, item);
3163 }
3164 PUSH(list);
3165 DISPATCH();
3166 }
3167
Mark Shannon13bc1392020-01-23 09:25:17 +00003168 case TARGET(LIST_TO_TUPLE): {
3169 PyObject *list = POP();
3170 PyObject *tuple = PyList_AsTuple(list);
3171 Py_DECREF(list);
3172 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003173 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003174 }
3175 PUSH(tuple);
3176 DISPATCH();
3177 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003178
Mark Shannon13bc1392020-01-23 09:25:17 +00003179 case TARGET(LIST_EXTEND): {
3180 PyObject *iterable = POP();
3181 PyObject *list = PEEK(oparg);
3182 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3183 if (none_val == NULL) {
3184 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003185 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003186 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003187 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003188 _PyErr_Format(tstate, PyExc_TypeError,
3189 "Value after * must be an iterable, not %.200s",
3190 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003191 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003192 Py_DECREF(iterable);
3193 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003194 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003195 Py_DECREF(none_val);
3196 Py_DECREF(iterable);
3197 DISPATCH();
3198 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003199
Mark Shannon13bc1392020-01-23 09:25:17 +00003200 case TARGET(SET_UPDATE): {
3201 PyObject *iterable = POP();
3202 PyObject *set = PEEK(oparg);
3203 int err = _PySet_Update(set, iterable);
3204 Py_DECREF(iterable);
3205 if (err < 0) {
3206 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003207 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003208 DISPATCH();
3209 }
3210
Benjamin Petersonddd19492018-09-16 22:38:02 -07003211 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 PyObject *set = PySet_New(NULL);
3213 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003214 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 if (set == NULL)
3216 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003217 for (i = oparg; i > 0; i--) {
3218 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 if (err == 0)
3220 err = PySet_Add(set, item);
3221 Py_DECREF(item);
3222 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003223 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 if (err != 0) {
3225 Py_DECREF(set);
3226 goto error;
3227 }
3228 PUSH(set);
3229 DISPATCH();
3230 }
3231
Benjamin Petersonddd19492018-09-16 22:38:02 -07003232 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003233 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3235 if (map == NULL)
3236 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003237 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003238 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003239 PyObject *key = PEEK(2*i);
3240 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003241 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003242 if (err != 0) {
3243 Py_DECREF(map);
3244 goto error;
3245 }
3246 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003247
3248 while (oparg--) {
3249 Py_DECREF(POP());
3250 Py_DECREF(POP());
3251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 PUSH(map);
3253 DISPATCH();
3254 }
3255
Benjamin Petersonddd19492018-09-16 22:38:02 -07003256 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003257 _Py_IDENTIFIER(__annotations__);
3258 int err;
3259 PyObject *ann_dict;
3260 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003261 _PyErr_Format(tstate, PyExc_SystemError,
3262 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003263 goto error;
3264 }
3265 /* check if __annotations__ in locals()... */
3266 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003267 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003268 &PyId___annotations__);
3269 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003270 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003271 goto error;
3272 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003273 /* ...if not, create a new one */
3274 ann_dict = PyDict_New();
3275 if (ann_dict == NULL) {
3276 goto error;
3277 }
3278 err = _PyDict_SetItemId(f->f_locals,
3279 &PyId___annotations__, ann_dict);
3280 Py_DECREF(ann_dict);
3281 if (err != 0) {
3282 goto error;
3283 }
3284 }
3285 }
3286 else {
3287 /* do the same if locals() is not a dict */
3288 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3289 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003290 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003291 }
3292 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3293 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003294 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003295 goto error;
3296 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003297 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003298 ann_dict = PyDict_New();
3299 if (ann_dict == NULL) {
3300 goto error;
3301 }
3302 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3303 Py_DECREF(ann_dict);
3304 if (err != 0) {
3305 goto error;
3306 }
3307 }
3308 else {
3309 Py_DECREF(ann_dict);
3310 }
3311 }
3312 DISPATCH();
3313 }
3314
Benjamin Petersonddd19492018-09-16 22:38:02 -07003315 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003316 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003317 PyObject *map;
3318 PyObject *keys = TOP();
3319 if (!PyTuple_CheckExact(keys) ||
3320 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003321 _PyErr_SetString(tstate, PyExc_SystemError,
3322 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003323 goto error;
3324 }
3325 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3326 if (map == NULL) {
3327 goto error;
3328 }
3329 for (i = oparg; i > 0; i--) {
3330 int err;
3331 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3332 PyObject *value = PEEK(i + 1);
3333 err = PyDict_SetItem(map, key, value);
3334 if (err != 0) {
3335 Py_DECREF(map);
3336 goto error;
3337 }
3338 }
3339
3340 Py_DECREF(POP());
3341 while (oparg--) {
3342 Py_DECREF(POP());
3343 }
3344 PUSH(map);
3345 DISPATCH();
3346 }
3347
Mark Shannon8a4cd702020-01-27 09:57:45 +00003348 case TARGET(DICT_UPDATE): {
3349 PyObject *update = POP();
3350 PyObject *dict = PEEK(oparg);
3351 if (PyDict_Update(dict, update) < 0) {
3352 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3353 _PyErr_Format(tstate, PyExc_TypeError,
3354 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003355 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003356 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003357 Py_DECREF(update);
3358 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003359 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003360 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361 DISPATCH();
3362 }
3363
Mark Shannon8a4cd702020-01-27 09:57:45 +00003364 case TARGET(DICT_MERGE): {
3365 PyObject *update = POP();
3366 PyObject *dict = PEEK(oparg);
3367
3368 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3369 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3370 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003371 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003372 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003373 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003374 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003375 DISPATCH();
3376 }
3377
Benjamin Petersonddd19492018-09-16 22:38:02 -07003378 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003379 PyObject *value = TOP();
3380 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 PyObject *map;
3382 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003383 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003384 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003386 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 Py_DECREF(value);
3388 Py_DECREF(key);
3389 if (err != 0)
3390 goto error;
3391 PREDICT(JUMP_ABSOLUTE);
3392 DISPATCH();
3393 }
3394
Benjamin Petersonddd19492018-09-16 22:38:02 -07003395 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 PyObject *name = GETITEM(names, oparg);
3397 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003398
3399 PyTypeObject *type = Py_TYPE(owner);
3400 PyObject *res;
3401 PyObject **dictptr;
3402 PyObject *dict;
3403 _PyOpCodeOpt_LoadAttr *la;
3404
3405 OPCACHE_STAT_ATTR_TOTAL();
3406
3407 OPCACHE_CHECK();
3408 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3409 {
3410 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003411 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003412 la = &co_opcache->u.la;
3413 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3414 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003415 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3416 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3417 // so ~offset is strictly < -1 (assuming 2's complement).
3418 if (la->hint < -1) {
3419 // Even faster path -- slot hint.
3420 Py_ssize_t offset = ~la->hint;
3421 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3422 char *addr = (char *)owner + offset;
3423 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003424 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003425 Py_INCREF(res);
3426 SET_TOP(res);
3427 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003428 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003429 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003430 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3431 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003432 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003433 // Fast path for dict.
3434 assert(type->tp_dict != NULL);
3435 assert(type->tp_dictoffset > 0);
3436
3437 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3438 dict = *dictptr;
3439 if (dict != NULL && PyDict_CheckExact(dict)) {
3440 Py_ssize_t hint = la->hint;
3441 Py_INCREF(dict);
3442 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003443 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003444 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003445 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003446 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003447 if (la->hint == hint && hint >= 0) {
3448 // Our hint has helped -- cache hit.
3449 OPCACHE_STAT_ATTR_HIT();
3450 } else {
3451 // The hint we provided didn't work.
3452 // Maybe next time?
3453 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3454 }
3455
3456 Py_INCREF(res);
3457 SET_TOP(res);
3458 Py_DECREF(owner);
3459 Py_DECREF(dict);
3460 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003461 }
3462 else {
3463 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003464 // This attribute can be missing sometimes;
3465 // we don't want to optimize this lookup.
3466 OPCACHE_DEOPT_LOAD_ATTR();
3467 Py_DECREF(dict);
3468 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003469 }
3470 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003471 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3472 OPCACHE_DEOPT_LOAD_ATTR();
3473 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003474 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003475 }
3476 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003477 // The type of the object has either been updated,
3478 // or is different. Maybe it will stabilize?
3479 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3480 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003481 OPCACHE_STAT_ATTR_MISS();
3482 }
3483
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003484 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003485 type->tp_getattro == PyObject_GenericGetAttr)
3486 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003487 if (type->tp_dict == NULL) {
3488 if (PyType_Ready(type) < 0) {
3489 Py_DECREF(owner);
3490 SET_TOP(NULL);
3491 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003492 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003493 }
3494 PyObject *descr = _PyType_Lookup(type, name);
3495 if (descr != NULL) {
3496 // We found an attribute with a data-like descriptor.
3497 PyTypeObject *dtype = Py_TYPE(descr);
3498 if (dtype == &PyMemberDescr_Type) { // It's a slot
3499 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3500 struct PyMemberDef *dmem = member->d_member;
3501 if (dmem->type == T_OBJECT_EX) {
3502 Py_ssize_t offset = dmem->offset;
3503 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003504
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003505 if (co_opcache->optimized == 0) {
3506 // First time we optimize this opcode.
3507 OPCACHE_STAT_ATTR_OPT();
3508 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3509 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3510 }
3511
3512 la = &co_opcache->u.la;
3513 la->type = type;
3514 la->tp_version_tag = type->tp_version_tag;
3515 la->hint = ~offset;
3516
3517 char *addr = (char *)owner + offset;
3518 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003519 if (res != NULL) {
3520 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003521 Py_DECREF(owner);
3522 SET_TOP(res);
3523
Pablo Galindo109826c2020-10-20 06:22:44 +01003524 DISPATCH();
3525 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003526 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003527 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003528 // Else it's a slot of a different type. We don't handle those.
3529 }
3530 // Else it's some other kind of descriptor that we don't handle.
3531 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003532 }
3533 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003534 // We found an instance with a __dict__.
3535 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3536 dict = *dictptr;
3537
3538 if (dict != NULL && PyDict_CheckExact(dict)) {
3539 Py_INCREF(dict);
3540 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003541 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003542 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3543 if (res != NULL) {
3544 Py_INCREF(res);
3545 Py_DECREF(dict);
3546 Py_DECREF(owner);
3547 SET_TOP(res);
3548
3549 if (co_opcache->optimized == 0) {
3550 // First time we optimize this opcode.
3551 OPCACHE_STAT_ATTR_OPT();
3552 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3553 }
3554
3555 la = &co_opcache->u.la;
3556 la->type = type;
3557 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003558 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003559 la->hint = hint;
3560
3561 DISPATCH();
3562 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003563 else {
3564 _PyErr_Clear(tstate);
3565 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003566 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003567 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003568 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003569 OPCACHE_DEOPT_LOAD_ATTR();
3570 }
3571 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003572 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003573 OPCACHE_DEOPT_LOAD_ATTR();
3574 }
3575 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3576 OPCACHE_DEOPT_LOAD_ATTR();
3577 }
3578 }
3579
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003580 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003581 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 Py_DECREF(owner);
3583 SET_TOP(res);
3584 if (res == NULL)
3585 goto error;
3586 DISPATCH();
3587 }
3588
Benjamin Petersonddd19492018-09-16 22:38:02 -07003589 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003590 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 PyObject *right = POP();
3592 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003593 PyObject *res = PyObject_RichCompare(left, right, oparg);
3594 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003595 Py_DECREF(left);
3596 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003597 if (res == NULL)
3598 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 PREDICT(POP_JUMP_IF_FALSE);
3600 PREDICT(POP_JUMP_IF_TRUE);
3601 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003603
Mark Shannon9af0e472020-01-14 10:12:45 +00003604 case TARGET(IS_OP): {
3605 PyObject *right = POP();
3606 PyObject *left = TOP();
3607 int res = (left == right)^oparg;
3608 PyObject *b = res ? Py_True : Py_False;
3609 Py_INCREF(b);
3610 SET_TOP(b);
3611 Py_DECREF(left);
3612 Py_DECREF(right);
3613 PREDICT(POP_JUMP_IF_FALSE);
3614 PREDICT(POP_JUMP_IF_TRUE);
3615 FAST_DISPATCH();
3616 }
3617
3618 case TARGET(CONTAINS_OP): {
3619 PyObject *right = POP();
3620 PyObject *left = POP();
3621 int res = PySequence_Contains(right, left);
3622 Py_DECREF(left);
3623 Py_DECREF(right);
3624 if (res < 0) {
3625 goto error;
3626 }
3627 PyObject *b = (res^oparg) ? Py_True : Py_False;
3628 Py_INCREF(b);
3629 PUSH(b);
3630 PREDICT(POP_JUMP_IF_FALSE);
3631 PREDICT(POP_JUMP_IF_TRUE);
3632 FAST_DISPATCH();
3633 }
3634
3635#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3636 "BaseException is not allowed"
3637
3638 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3639 PyObject *right = POP();
3640 PyObject *left = POP();
3641 if (PyTuple_Check(right)) {
3642 Py_ssize_t i, length;
3643 length = PyTuple_GET_SIZE(right);
3644 for (i = 0; i < length; i++) {
3645 PyObject *exc = PyTuple_GET_ITEM(right, i);
3646 if (!PyExceptionClass_Check(exc)) {
3647 _PyErr_SetString(tstate, PyExc_TypeError,
3648 CANNOT_CATCH_MSG);
3649 Py_DECREF(left);
3650 Py_DECREF(right);
3651 goto error;
3652 }
3653 }
3654 }
3655 else {
3656 if (!PyExceptionClass_Check(right)) {
3657 _PyErr_SetString(tstate, PyExc_TypeError,
3658 CANNOT_CATCH_MSG);
3659 Py_DECREF(left);
3660 Py_DECREF(right);
3661 goto error;
3662 }
3663 }
3664 int res = PyErr_GivenExceptionMatches(left, right);
3665 Py_DECREF(left);
3666 Py_DECREF(right);
3667 if (res > 0) {
3668 /* Exception matches -- Do nothing */;
3669 }
3670 else if (res == 0) {
3671 JUMPTO(oparg);
3672 }
3673 else {
3674 goto error;
3675 }
3676 DISPATCH();
3677 }
3678
Benjamin Petersonddd19492018-09-16 22:38:02 -07003679 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003680 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003681 PyObject *fromlist = POP();
3682 PyObject *level = TOP();
3683 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003684 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003685 Py_DECREF(level);
3686 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003687 SET_TOP(res);
3688 if (res == NULL)
3689 goto error;
3690 DISPATCH();
3691 }
3692
Benjamin Petersonddd19492018-09-16 22:38:02 -07003693 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003694 PyObject *from = POP(), *locals;
3695 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003696 if (PyFrame_FastToLocalsWithError(f) < 0) {
3697 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003698 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003699 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 locals = f->f_locals;
3702 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003703 _PyErr_SetString(tstate, PyExc_SystemError,
3704 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003705 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003708 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003710 Py_DECREF(from);
3711 if (err != 0)
3712 goto error;
3713 DISPATCH();
3714 }
Guido van Rossum25831651993-05-19 14:50:45 +00003715
Benjamin Petersonddd19492018-09-16 22:38:02 -07003716 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003717 PyObject *name = GETITEM(names, oparg);
3718 PyObject *from = TOP();
3719 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003720 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003721 PUSH(res);
3722 if (res == NULL)
3723 goto error;
3724 DISPATCH();
3725 }
Thomas Wouters52152252000-08-17 22:55:00 +00003726
Benjamin Petersonddd19492018-09-16 22:38:02 -07003727 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 JUMPBY(oparg);
3729 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003731
Benjamin Petersonddd19492018-09-16 22:38:02 -07003732 case TARGET(POP_JUMP_IF_FALSE): {
3733 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003734 PyObject *cond = POP();
3735 int err;
3736 if (cond == Py_True) {
3737 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 FAST_DISPATCH();
3739 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003740 if (cond == Py_False) {
3741 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 JUMPTO(oparg);
3743 FAST_DISPATCH();
3744 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003745 err = PyObject_IsTrue(cond);
3746 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003748 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 else if (err == 0)
3750 JUMPTO(oparg);
3751 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003752 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003754 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003755
Benjamin Petersonddd19492018-09-16 22:38:02 -07003756 case TARGET(POP_JUMP_IF_TRUE): {
3757 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003758 PyObject *cond = POP();
3759 int err;
3760 if (cond == Py_False) {
3761 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 FAST_DISPATCH();
3763 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003764 if (cond == Py_True) {
3765 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 JUMPTO(oparg);
3767 FAST_DISPATCH();
3768 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003769 err = PyObject_IsTrue(cond);
3770 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 JUMPTO(oparg);
3773 }
3774 else if (err == 0)
3775 ;
3776 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003777 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003779 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003780
Benjamin Petersonddd19492018-09-16 22:38:02 -07003781 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003782 PyObject *cond = TOP();
3783 int err;
3784 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003785 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003786 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 FAST_DISPATCH();
3788 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003789 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 JUMPTO(oparg);
3791 FAST_DISPATCH();
3792 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003793 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003795 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 else if (err == 0)
3799 JUMPTO(oparg);
3800 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003803 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003804
Benjamin Petersonddd19492018-09-16 22:38:02 -07003805 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003806 PyObject *cond = TOP();
3807 int err;
3808 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003809 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003810 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 FAST_DISPATCH();
3812 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003813 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 JUMPTO(oparg);
3815 FAST_DISPATCH();
3816 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003817 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 JUMPTO(oparg);
3820 }
3821 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003822 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003823 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 }
3825 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003826 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003829
Benjamin Petersonddd19492018-09-16 22:38:02 -07003830 case TARGET(JUMP_ABSOLUTE): {
3831 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003833#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 /* Enabling this path speeds-up all while and for-loops by bypassing
3835 the per-loop checks for signals. By default, this should be turned-off
3836 because it prevents detection of a control-break in tight loops like
3837 "while 1: pass". Compile with this option turned-on when you need
3838 the speed-up and do not need break checking inside tight loops (ones
3839 that contain only instructions ending with FAST_DISPATCH).
3840 */
3841 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003842#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003844#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003846
Brandt Bucher145bf262021-02-26 14:51:55 -08003847 case TARGET(GET_LEN): {
3848 // PUSH(len(TOS))
3849 Py_ssize_t len_i = PyObject_Length(TOP());
3850 if (len_i < 0) {
3851 goto error;
3852 }
3853 PyObject *len_o = PyLong_FromSsize_t(len_i);
3854 if (len_o == NULL) {
3855 goto error;
3856 }
3857 PUSH(len_o);
3858 DISPATCH();
3859 }
3860
3861 case TARGET(MATCH_CLASS): {
3862 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3863 // attributes. On failure, set TOS to False.
3864 PyObject *names = POP();
3865 PyObject *type = TOP();
3866 PyObject *subject = SECOND();
3867 assert(PyTuple_CheckExact(names));
3868 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3869 Py_DECREF(names);
3870 if (attrs) {
3871 // Success!
3872 assert(PyTuple_CheckExact(attrs));
3873 Py_DECREF(subject);
3874 SET_SECOND(attrs);
3875 }
3876 else if (_PyErr_Occurred(tstate)) {
3877 goto error;
3878 }
3879 Py_DECREF(type);
3880 SET_TOP(PyBool_FromLong(!!attrs));
3881 DISPATCH();
3882 }
3883
3884 case TARGET(MATCH_MAPPING): {
3885 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3886 PyObject *subject = TOP();
3887 // Fast path for dicts:
3888 if (PyDict_Check(subject)) {
3889 Py_INCREF(Py_True);
3890 PUSH(Py_True);
3891 DISPATCH();
3892 }
3893 // Lazily import _collections_abc.Mapping, and keep it handy on the
3894 // PyInterpreterState struct (it gets cleaned up at exit):
3895 PyInterpreterState *interp = PyInterpreterState_Get();
3896 if (interp->map_abc == NULL) {
3897 PyObject *abc = PyImport_ImportModule("_collections_abc");
3898 if (abc == NULL) {
3899 goto error;
3900 }
3901 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3902 if (interp->map_abc == NULL) {
3903 goto error;
3904 }
3905 }
3906 int match = PyObject_IsInstance(subject, interp->map_abc);
3907 if (match < 0) {
3908 goto error;
3909 }
3910 PUSH(PyBool_FromLong(match));
3911 DISPATCH();
3912 }
3913
3914 case TARGET(MATCH_SEQUENCE): {
3915 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3916 // and isinstance(TOS, _collections_abc.Sequence))
3917 PyObject *subject = TOP();
3918 // Fast path for lists and tuples:
3919 if (PyType_FastSubclass(Py_TYPE(subject),
3920 Py_TPFLAGS_LIST_SUBCLASS |
3921 Py_TPFLAGS_TUPLE_SUBCLASS))
3922 {
3923 Py_INCREF(Py_True);
3924 PUSH(Py_True);
3925 DISPATCH();
3926 }
3927 // Bail on some possible Sequences that we intentionally exclude:
3928 if (PyType_FastSubclass(Py_TYPE(subject),
3929 Py_TPFLAGS_BYTES_SUBCLASS |
3930 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3931 PyByteArray_Check(subject))
3932 {
3933 Py_INCREF(Py_False);
3934 PUSH(Py_False);
3935 DISPATCH();
3936 }
3937 // Lazily import _collections_abc.Sequence, and keep it handy on the
3938 // PyInterpreterState struct (it gets cleaned up at exit):
3939 PyInterpreterState *interp = PyInterpreterState_Get();
3940 if (interp->seq_abc == NULL) {
3941 PyObject *abc = PyImport_ImportModule("_collections_abc");
3942 if (abc == NULL) {
3943 goto error;
3944 }
3945 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3946 if (interp->seq_abc == NULL) {
3947 goto error;
3948 }
3949 }
3950 int match = PyObject_IsInstance(subject, interp->seq_abc);
3951 if (match < 0) {
3952 goto error;
3953 }
3954 PUSH(PyBool_FromLong(match));
3955 DISPATCH();
3956 }
3957
3958 case TARGET(MATCH_KEYS): {
3959 // On successful match for all keys, PUSH(values) and PUSH(True).
3960 // Otherwise, PUSH(None) and PUSH(False).
3961 PyObject *keys = TOP();
3962 PyObject *subject = SECOND();
3963 PyObject *values_or_none = match_keys(tstate, subject, keys);
3964 if (values_or_none == NULL) {
3965 goto error;
3966 }
3967 PUSH(values_or_none);
3968 if (values_or_none == Py_None) {
3969 Py_INCREF(Py_False);
3970 PUSH(Py_False);
3971 DISPATCH();
3972 }
3973 assert(PyTuple_CheckExact(values_or_none));
3974 Py_INCREF(Py_True);
3975 PUSH(Py_True);
3976 DISPATCH();
3977 }
3978
3979 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3980 // rest = dict(TOS1)
3981 // for key in TOS:
3982 // del rest[key]
3983 // SET_TOP(rest)
3984 PyObject *keys = TOP();
3985 PyObject *subject = SECOND();
3986 PyObject *rest = PyDict_New();
3987 if (rest == NULL || PyDict_Update(rest, subject)) {
3988 Py_XDECREF(rest);
3989 goto error;
3990 }
3991 // This may seem a bit inefficient, but keys is rarely big enough to
3992 // actually impact runtime.
3993 assert(PyTuple_CheckExact(keys));
3994 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3995 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3996 Py_DECREF(rest);
3997 goto error;
3998 }
3999 }
4000 Py_DECREF(keys);
4001 SET_TOP(rest);
4002 DISPATCH();
4003 }
4004
Benjamin Petersonddd19492018-09-16 22:38:02 -07004005 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004007 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004008 PyObject *iter = PyObject_GetIter(iterable);
4009 Py_DECREF(iterable);
4010 SET_TOP(iter);
4011 if (iter == NULL)
4012 goto error;
4013 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004014 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004015 DISPATCH();
4016 }
4017
Benjamin Petersonddd19492018-09-16 22:38:02 -07004018 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004019 /* before: [obj]; after [getiter(obj)] */
4020 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004021 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004022 if (PyCoro_CheckExact(iterable)) {
4023 /* `iterable` is a coroutine */
4024 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4025 /* and it is used in a 'yield from' expression of a
4026 regular generator. */
4027 Py_DECREF(iterable);
4028 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004029 _PyErr_SetString(tstate, PyExc_TypeError,
4030 "cannot 'yield from' a coroutine object "
4031 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004032 goto error;
4033 }
4034 }
4035 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004036 /* `iterable` is not a generator. */
4037 iter = PyObject_GetIter(iterable);
4038 Py_DECREF(iterable);
4039 SET_TOP(iter);
4040 if (iter == NULL)
4041 goto error;
4042 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004043 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004044 DISPATCH();
4045 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004046
Benjamin Petersonddd19492018-09-16 22:38:02 -07004047 case TARGET(FOR_ITER): {
4048 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004050 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004051 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004052 if (next != NULL) {
4053 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PREDICT(STORE_FAST);
4055 PREDICT(UNPACK_SEQUENCE);
4056 DISPATCH();
4057 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004058 if (_PyErr_Occurred(tstate)) {
4059 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004060 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004061 }
4062 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00004063 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02004064 }
4065 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 }
4067 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004068 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004069 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 JUMPBY(oparg);
4071 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004072 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004073
Benjamin Petersonddd19492018-09-16 22:38:02 -07004074 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004075 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 STACK_LEVEL());
4077 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004078 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004079
Benjamin Petersonddd19492018-09-16 22:38:02 -07004080 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004081 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004082 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004083 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004084 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004085 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004086 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004087 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004088 }
4089 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4090 if (exit == NULL) {
4091 Py_DECREF(enter);
4092 goto error;
4093 }
Yury Selivanov75445082015-05-11 22:57:16 -04004094 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004095 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004096 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004097 Py_DECREF(enter);
4098 if (res == NULL)
4099 goto error;
4100 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004101 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004102 DISPATCH();
4103 }
4104
Benjamin Petersonddd19492018-09-16 22:38:02 -07004105 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004106 PyObject *res = POP();
4107 /* Setup the finally block before pushing the result
4108 of __aenter__ on the stack. */
4109 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4110 STACK_LEVEL());
4111 PUSH(res);
4112 DISPATCH();
4113 }
4114
Benjamin Petersonddd19492018-09-16 22:38:02 -07004115 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004116 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004117 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004118 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004119 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004120 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004121 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004122 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004123 }
4124 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004125 if (exit == NULL) {
4126 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004127 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004129 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004130 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004131 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004132 Py_DECREF(enter);
4133 if (res == NULL)
4134 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 /* Setup the finally block before pushing the result
4136 of __enter__ on the stack. */
4137 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4138 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004139
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004140 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 DISPATCH();
4142 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004143
Mark Shannonfee55262019-11-21 09:11:43 +00004144 case TARGET(WITH_EXCEPT_START): {
4145 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004147 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4148 - SEVENTH: the context.__exit__ bound method
4149 We call SEVENTH(TOP, SECOND, THIRD).
4150 Then we push again the TOP exception and the __exit__
4151 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004154 PyObject *exc, *val, *tb, *res;
4155
Victor Stinner842cfff2016-12-01 14:45:31 +01004156 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004157 val = SECOND();
4158 tb = THIRD();
4159 assert(exc != Py_None);
4160 assert(!PyLong_Check(exc));
4161 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004162 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004163 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004164 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004165 if (res == NULL)
4166 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004167
Yury Selivanov75445082015-05-11 22:57:16 -04004168 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004169 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004171
Benjamin Petersonddd19492018-09-16 22:38:02 -07004172 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004173 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004174 PyObject *name = GETITEM(names, oparg);
4175 PyObject *obj = TOP();
4176 PyObject *meth = NULL;
4177
4178 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4179
Yury Selivanovf2392132016-12-13 19:03:51 -05004180 if (meth == NULL) {
4181 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004182 goto error;
4183 }
4184
4185 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004186 /* We can bypass temporary bound method object.
4187 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004188
INADA Naoki015bce62017-01-16 17:23:30 +09004189 meth | self | arg1 | ... | argN
4190 */
4191 SET_TOP(meth);
4192 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004193 }
4194 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004195 /* meth is not an unbound method (but a regular attr, or
4196 something was returned by a descriptor protocol). Set
4197 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004198 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004199
4200 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004201 */
INADA Naoki015bce62017-01-16 17:23:30 +09004202 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004203 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004204 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004205 }
4206 DISPATCH();
4207 }
4208
Benjamin Petersonddd19492018-09-16 22:38:02 -07004209 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004210 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004211 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004212
4213 sp = stack_pointer;
4214
INADA Naoki015bce62017-01-16 17:23:30 +09004215 meth = PEEK(oparg + 2);
4216 if (meth == NULL) {
4217 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4218 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004219
4220 Stack layout:
4221
INADA Naoki015bce62017-01-16 17:23:30 +09004222 ... | NULL | callable | arg1 | ... | argN
4223 ^- TOP()
4224 ^- (-oparg)
4225 ^- (-oparg-1)
4226 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004227
Ville Skyttä49b27342017-08-03 09:00:59 +03004228 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004229 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004230 */
Mark Shannon86433452021-01-07 16:49:02 +00004231 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004232 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004233 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004234 }
4235 else {
4236 /* This is a method call. Stack layout:
4237
INADA Naoki015bce62017-01-16 17:23:30 +09004238 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004239 ^- TOP()
4240 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004241 ^- (-oparg-1)
4242 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004243
INADA Naoki015bce62017-01-16 17:23:30 +09004244 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004245 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004246 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004247 */
Mark Shannon86433452021-01-07 16:49:02 +00004248 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004249 stack_pointer = sp;
4250 }
4251
4252 PUSH(res);
4253 if (res == NULL)
4254 goto error;
4255 DISPATCH();
4256 }
4257
Benjamin Petersonddd19492018-09-16 22:38:02 -07004258 case TARGET(CALL_FUNCTION): {
4259 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004260 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004262 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004264 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004265 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004266 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004268 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004270
Benjamin Petersonddd19492018-09-16 22:38:02 -07004271 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004272 PyObject **sp, *res, *names;
4273
4274 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004275 assert(PyTuple_Check(names));
4276 assert(PyTuple_GET_SIZE(names) <= oparg);
4277 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00004279 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004281 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004282 Py_DECREF(names);
4283
4284 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004285 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004286 }
4287 DISPATCH();
4288 }
4289
Benjamin Petersonddd19492018-09-16 22:38:02 -07004290 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004291 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004292 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004293 if (oparg & 0x01) {
4294 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004295 if (!PyDict_CheckExact(kwargs)) {
4296 PyObject *d = PyDict_New();
4297 if (d == NULL)
4298 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004299 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004300 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004301 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004302 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004303 goto error;
4304 }
4305 Py_DECREF(kwargs);
4306 kwargs = d;
4307 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004308 assert(PyDict_CheckExact(kwargs));
4309 }
4310 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004311 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004312 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004313 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004314 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004315 goto error;
4316 }
4317 Py_SETREF(callargs, PySequence_Tuple(callargs));
4318 if (callargs == NULL) {
4319 goto error;
4320 }
4321 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004322 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004323
Mark Shannon86433452021-01-07 16:49:02 +00004324 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004325 Py_DECREF(func);
4326 Py_DECREF(callargs);
4327 Py_XDECREF(kwargs);
4328
4329 SET_TOP(result);
4330 if (result == NULL) {
4331 goto error;
4332 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004333 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004335
Benjamin Petersonddd19492018-09-16 22:38:02 -07004336 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004337 PyObject *qualname = POP();
4338 PyObject *codeobj = POP();
4339 PyFunctionObject *func = (PyFunctionObject *)
4340 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004341
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004342 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004343 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004344 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004345 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004347
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004348 if (oparg & 0x08) {
4349 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004350 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004351 }
4352 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004353 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004354 func->func_annotations = POP();
4355 }
4356 if (oparg & 0x02) {
4357 assert(PyDict_CheckExact(TOP()));
4358 func->func_kwdefaults = POP();
4359 }
4360 if (oparg & 0x01) {
4361 assert(PyTuple_CheckExact(TOP()));
4362 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004364
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004365 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004366 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004368
Benjamin Petersonddd19492018-09-16 22:38:02 -07004369 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004372 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004374 step = NULL;
4375 stop = POP();
4376 start = TOP();
4377 slice = PySlice_New(start, stop, step);
4378 Py_DECREF(start);
4379 Py_DECREF(stop);
4380 Py_XDECREF(step);
4381 SET_TOP(slice);
4382 if (slice == NULL)
4383 goto error;
4384 DISPATCH();
4385 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004386
Benjamin Petersonddd19492018-09-16 22:38:02 -07004387 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004388 /* Handles f-string value formatting. */
4389 PyObject *result;
4390 PyObject *fmt_spec;
4391 PyObject *value;
4392 PyObject *(*conv_fn)(PyObject *);
4393 int which_conversion = oparg & FVC_MASK;
4394 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4395
4396 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004397 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004398
4399 /* See if any conversion is specified. */
4400 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004401 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004402 case FVC_STR: conv_fn = PyObject_Str; break;
4403 case FVC_REPR: conv_fn = PyObject_Repr; break;
4404 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004405 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004406 _PyErr_Format(tstate, PyExc_SystemError,
4407 "unexpected conversion flag %d",
4408 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004409 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004410 }
4411
4412 /* If there's a conversion function, call it and replace
4413 value with that result. Otherwise, just use value,
4414 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004415 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004416 result = conv_fn(value);
4417 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004418 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004419 Py_XDECREF(fmt_spec);
4420 goto error;
4421 }
4422 value = result;
4423 }
4424
4425 /* If value is a unicode object, and there's no fmt_spec,
4426 then we know the result of format(value) is value
4427 itself. In that case, skip calling format(). I plan to
4428 move this optimization in to PyObject_Format()
4429 itself. */
4430 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4431 /* Do nothing, just transfer ownership to result. */
4432 result = value;
4433 } else {
4434 /* Actually call format(). */
4435 result = PyObject_Format(value, fmt_spec);
4436 Py_DECREF(value);
4437 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004438 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004439 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004440 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004441 }
4442
Eric V. Smith135d5f42016-02-05 18:23:08 -05004443 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004444 DISPATCH();
4445 }
4446
Benjamin Petersonddd19492018-09-16 22:38:02 -07004447 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004448 int oldoparg = oparg;
4449 NEXTOPARG();
4450 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004452 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004453
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004454
Antoine Pitrou042b1282010-08-13 21:15:58 +00004455#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 default:
4459 fprintf(stderr,
4460 "XXX lineno: %d, opcode: %d\n",
4461 PyFrame_GetLineNumber(f),
4462 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004463 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004464 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004468 /* This should never be reached. Every opcode should end with DISPATCH()
4469 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004470 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004471
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004472error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004473 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004474#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 if (!_PyErr_Occurred(tstate)) {
4476 _PyErr_SetString(tstate, PyExc_SystemError,
4477 "error return without exception set");
4478 }
Victor Stinner365b6932013-07-12 00:11:58 +02004479#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004481#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004483 /* Log traceback info. */
4484 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004485
Mark Shannoncb9879b2020-07-17 11:44:23 +01004486 if (tstate->c_tracefunc != NULL) {
4487 /* Make sure state is set to FRAME_EXECUTING for tracing */
4488 assert(f->f_state == FRAME_EXECUTING);
4489 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004490 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004491 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004492 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004493exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004494 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004495 /* Unwind stacks if an exception occurred */
4496 while (f->f_iblock > 0) {
4497 /* Pop the current block. */
4498 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (b->b_type == EXCEPT_HANDLER) {
4501 UNWIND_EXCEPT_HANDLER(b);
4502 continue;
4503 }
4504 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004505 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 PyObject *exc, *val, *tb;
4507 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004508 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004510 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004511 PUSH(exc_info->exc_traceback);
4512 PUSH(exc_info->exc_value);
4513 if (exc_info->exc_type != NULL) {
4514 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 }
4516 else {
4517 Py_INCREF(Py_None);
4518 PUSH(Py_None);
4519 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004520 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 /* Make the raw exception data
4522 available to the handler,
4523 so a program can emulate the
4524 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004525 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004526 if (tb != NULL)
4527 PyException_SetTraceback(val, tb);
4528 else
4529 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004531 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004533 exc_info->exc_value = val;
4534 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 if (tb == NULL)
4536 tb = Py_None;
4537 Py_INCREF(tb);
4538 PUSH(tb);
4539 PUSH(val);
4540 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004542 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004543 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004544 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004545 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004546 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004547 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 }
4549 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004550
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004551 /* End the loop as we still have an error */
4552 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004554
Pablo Galindof00828a2019-05-09 16:52:02 +01004555 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004556 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 /* Pop remaining stack entries. */
4559 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004560 PyObject *o = POP();
4561 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004563 f->f_stackdepth = 0;
4564 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004565exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004567 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004568 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004569 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004570 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 }
4572 }
4573 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004574 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004575 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004576 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 }
4578 }
4579 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004582exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004583 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4584 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004585 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004587
Victor Stinner0b72b232020-03-12 23:18:39 +01004588 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004589}
4590
Benjamin Petersonb204a422011-06-05 22:04:07 -05004591static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004592format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004593 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004594{
4595 int err;
4596 Py_ssize_t len = PyList_GET_SIZE(names);
4597 PyObject *name_str, *comma, *tail, *tmp;
4598
4599 assert(PyList_CheckExact(names));
4600 assert(len >= 1);
4601 /* Deal with the joys of natural language. */
4602 switch (len) {
4603 case 1:
4604 name_str = PyList_GET_ITEM(names, 0);
4605 Py_INCREF(name_str);
4606 break;
4607 case 2:
4608 name_str = PyUnicode_FromFormat("%U and %U",
4609 PyList_GET_ITEM(names, len - 2),
4610 PyList_GET_ITEM(names, len - 1));
4611 break;
4612 default:
4613 tail = PyUnicode_FromFormat(", %U, and %U",
4614 PyList_GET_ITEM(names, len - 2),
4615 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004616 if (tail == NULL)
4617 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004618 /* Chop off the last two objects in the list. This shouldn't actually
4619 fail, but we can't be too careful. */
4620 err = PyList_SetSlice(names, len - 2, len, NULL);
4621 if (err == -1) {
4622 Py_DECREF(tail);
4623 return;
4624 }
4625 /* Stitch everything up into a nice comma-separated list. */
4626 comma = PyUnicode_FromString(", ");
4627 if (comma == NULL) {
4628 Py_DECREF(tail);
4629 return;
4630 }
4631 tmp = PyUnicode_Join(comma, names);
4632 Py_DECREF(comma);
4633 if (tmp == NULL) {
4634 Py_DECREF(tail);
4635 return;
4636 }
4637 name_str = PyUnicode_Concat(tmp, tail);
4638 Py_DECREF(tmp);
4639 Py_DECREF(tail);
4640 break;
4641 }
4642 if (name_str == NULL)
4643 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004644 _PyErr_Format(tstate, PyExc_TypeError,
4645 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004646 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004647 len,
4648 kind,
4649 len == 1 ? "" : "s",
4650 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004651 Py_DECREF(name_str);
4652}
4653
4654static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004655missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4656 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004657 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004658{
Victor Stinner74319ae2016-08-25 00:04:09 +02004659 Py_ssize_t i, j = 0;
4660 Py_ssize_t start, end;
4661 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004662 const char *kind = positional ? "positional" : "keyword-only";
4663 PyObject *missing_names;
4664
4665 /* Compute the names of the arguments that are missing. */
4666 missing_names = PyList_New(missing);
4667 if (missing_names == NULL)
4668 return;
4669 if (positional) {
4670 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004671 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004672 }
4673 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004674 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004675 end = start + co->co_kwonlyargcount;
4676 }
4677 for (i = start; i < end; i++) {
4678 if (GETLOCAL(i) == NULL) {
4679 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4680 PyObject *name = PyObject_Repr(raw);
4681 if (name == NULL) {
4682 Py_DECREF(missing_names);
4683 return;
4684 }
4685 PyList_SET_ITEM(missing_names, j++, name);
4686 }
4687 }
4688 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004689 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004690 Py_DECREF(missing_names);
4691}
4692
4693static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004694too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004695 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004696 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004697{
4698 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004699 Py_ssize_t kwonly_given = 0;
4700 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004701 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004702 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004703
Benjamin Petersone109c702011-06-24 09:37:26 -05004704 assert((co->co_flags & CO_VARARGS) == 0);
4705 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004706 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004707 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004708 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004709 }
4710 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004711 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004712 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004713 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004714 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004715 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004716 }
4717 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004718 plural = (co_argcount != 1);
4719 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004720 }
4721 if (sig == NULL)
4722 return;
4723 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004724 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4725 kwonly_sig = PyUnicode_FromFormat(format,
4726 given != 1 ? "s" : "",
4727 kwonly_given,
4728 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004729 if (kwonly_sig == NULL) {
4730 Py_DECREF(sig);
4731 return;
4732 }
4733 }
4734 else {
4735 /* This will not fail. */
4736 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004737 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004738 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004739 _PyErr_Format(tstate, PyExc_TypeError,
4740 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004741 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004742 sig,
4743 plural ? "s" : "",
4744 given,
4745 kwonly_sig,
4746 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004747 Py_DECREF(sig);
4748 Py_DECREF(kwonly_sig);
4749}
4750
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004751static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004752positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004753 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004754 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004755{
4756 int posonly_conflicts = 0;
4757 PyObject* posonly_names = PyList_New(0);
4758
4759 for(int k=0; k < co->co_posonlyargcount; k++){
4760 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4761
4762 for (int k2=0; k2<kwcount; k2++){
4763 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004764 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004765 if (kwname == posonly_name){
4766 if(PyList_Append(posonly_names, kwname) != 0) {
4767 goto fail;
4768 }
4769 posonly_conflicts++;
4770 continue;
4771 }
4772
4773 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4774
4775 if ( cmp > 0) {
4776 if(PyList_Append(posonly_names, kwname) != 0) {
4777 goto fail;
4778 }
4779 posonly_conflicts++;
4780 } else if (cmp < 0) {
4781 goto fail;
4782 }
4783
4784 }
4785 }
4786 if (posonly_conflicts) {
4787 PyObject* comma = PyUnicode_FromString(", ");
4788 if (comma == NULL) {
4789 goto fail;
4790 }
4791 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4792 Py_DECREF(comma);
4793 if (error_names == NULL) {
4794 goto fail;
4795 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004796 _PyErr_Format(tstate, PyExc_TypeError,
4797 "%U() got some positional-only arguments passed"
4798 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004799 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004800 Py_DECREF(error_names);
4801 goto fail;
4802 }
4803
4804 Py_DECREF(posonly_names);
4805 return 0;
4806
4807fail:
4808 Py_XDECREF(posonly_names);
4809 return 1;
4810
4811}
4812
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004813
Mark Shannon0332e562021-02-01 10:42:03 +00004814PyFrameObject *
4815_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004816 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004817 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004818 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004819{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004820 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004821
Mark Shannond6c33fb2021-01-29 13:24:55 +00004822 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4823 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004824 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004825
Victor Stinnerc7020012016-08-16 23:40:29 +02004826 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004827 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004828 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004830 }
Victor Stinner232dda62020-06-04 15:19:02 +02004831 PyObject **fastlocals = f->f_localsplus;
4832 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004833
Victor Stinnerc7020012016-08-16 23:40:29 +02004834 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004835 PyObject *kwdict;
4836 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004837 if (co->co_flags & CO_VARKEYWORDS) {
4838 kwdict = PyDict_New();
4839 if (kwdict == NULL)
4840 goto fail;
4841 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004842 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004843 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004844 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004845 SETLOCAL(i, kwdict);
4846 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004847 else {
4848 kwdict = NULL;
4849 }
4850
Pablo Galindocd74e662019-06-01 18:08:04 +01004851 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004852 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004853 if (argcount > co->co_argcount) {
4854 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004855 }
4856 else {
4857 n = argcount;
4858 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004859 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004860 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004861 Py_INCREF(x);
4862 SETLOCAL(j, x);
4863 }
4864
Victor Stinnerc7020012016-08-16 23:40:29 +02004865 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004866 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004867 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004868 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004869 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004870 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004871 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004872 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004873
Mark Shannon0332e562021-02-01 10:42:03 +00004874 /* Handle keyword arguments */
4875 if (kwnames != NULL) {
4876 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4877 for (i = 0; i < kwcount; i++) {
4878 PyObject **co_varnames;
4879 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4880 PyObject *value = args[i+argcount];
4881 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004882
Mark Shannon0332e562021-02-01 10:42:03 +00004883 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4884 _PyErr_Format(tstate, PyExc_TypeError,
4885 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004886 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004888 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004889
Mark Shannon0332e562021-02-01 10:42:03 +00004890 /* Speed hack: do raw pointer compares. As names are
4891 normally interned this should almost always hit. */
4892 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4893 for (j = co->co_posonlyargcount; j < total_args; j++) {
4894 PyObject *varname = co_varnames[j];
4895 if (varname == keyword) {
4896 goto kw_found;
4897 }
4898 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004899
Mark Shannon0332e562021-02-01 10:42:03 +00004900 /* Slow fallback, just in case */
4901 for (j = co->co_posonlyargcount; j < total_args; j++) {
4902 PyObject *varname = co_varnames[j];
4903 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4904 if (cmp > 0) {
4905 goto kw_found;
4906 }
4907 else if (cmp < 0) {
4908 goto fail;
4909 }
4910 }
4911
4912 assert(j >= total_args);
4913 if (kwdict == NULL) {
4914
4915 if (co->co_posonlyargcount
4916 && positional_only_passed_as_keyword(tstate, co,
4917 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004918 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004919 {
4920 goto fail;
4921 }
4922
4923 _PyErr_Format(tstate, PyExc_TypeError,
4924 "%U() got an unexpected keyword argument '%S'",
4925 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004926 goto fail;
4927 }
4928
Mark Shannon0332e562021-02-01 10:42:03 +00004929 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4930 goto fail;
4931 }
4932 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004933
Mark Shannon0332e562021-02-01 10:42:03 +00004934 kw_found:
4935 if (GETLOCAL(j) != NULL) {
4936 _PyErr_Format(tstate, PyExc_TypeError,
4937 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004938 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004939 goto fail;
4940 }
4941 Py_INCREF(value);
4942 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004945
4946 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004947 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004948 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4949 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 goto fail;
4951 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004952
4953 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004954 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004955 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004956 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004957 Py_ssize_t missing = 0;
4958 for (i = argcount; i < m; i++) {
4959 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004960 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004961 }
4962 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004963 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004964 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004965 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004966 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004967 }
4968 if (n > m)
4969 i = n - m;
4970 else
4971 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004972 if (defcount) {
4973 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4974 for (; i < defcount; i++) {
4975 if (GETLOCAL(m+i) == NULL) {
4976 PyObject *def = defs[i];
4977 Py_INCREF(def);
4978 SETLOCAL(m+i, def);
4979 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004980 }
4981 }
4982 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004983
4984 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004985 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004986 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004987 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004988 if (GETLOCAL(i) != NULL)
4989 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004990 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004991 if (con->fc_kwdefaults != NULL) {
4992 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004993 if (def) {
4994 Py_INCREF(def);
4995 SETLOCAL(i, def);
4996 continue;
4997 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004998 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004999 goto fail;
5000 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05005001 }
Benjamin Petersone109c702011-06-24 09:37:26 -05005002 missing++;
5003 }
5004 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005005 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005006 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005007 goto fail;
5008 }
5009 }
5010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005012 vars into frame. */
5013 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005015 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005016 /* Possibly account for the cell variable being an argument. */
5017 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005018 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005019 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005020 /* Clear the local copy. */
5021 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005022 }
5023 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005024 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005025 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005026 if (c == NULL)
5027 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005028 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005030
5031 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005032 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005033 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005034 Py_INCREF(o);
5035 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005037
Mark Shannon0332e562021-02-01 10:42:03 +00005038 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005039
Thomas Woutersce272b62007-09-19 21:19:28 +00005040fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 /* decref'ing the frame can cause __del__ methods to get invoked,
5043 which can call back into Python. While we're done with the
5044 current Python frame (f), the associated C stack is still in use,
5045 so recursion_depth must be boosted for the duration.
5046 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005047 if (Py_REFCNT(f) > 1) {
5048 Py_DECREF(f);
5049 _PyObject_GC_TRACK(f);
5050 }
5051 else {
5052 ++tstate->recursion_depth;
5053 Py_DECREF(f);
5054 --tstate->recursion_depth;
5055 }
Mark Shannon0332e562021-02-01 10:42:03 +00005056 return NULL;
5057}
5058
5059static PyObject *
5060make_coro(PyFrameConstructor *con, PyFrameObject *f)
5061{
5062 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5063 PyObject *gen;
5064 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5065
5066 /* Don't need to keep the reference to f_back, it will be set
5067 * when the generator is resumed. */
5068 Py_CLEAR(f->f_back);
5069
5070 /* Create a new generator that owns the ready to run frame
5071 * and return that as the value. */
5072 if (is_coro) {
5073 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5074 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5075 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5076 } else {
5077 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5078 }
5079 if (gen == NULL) {
5080 return NULL;
5081 }
5082
5083 _PyObject_GC_TRACK(f);
5084
5085 return gen;
5086}
5087
5088PyObject *
5089_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5090 PyObject *locals,
5091 PyObject* const* args, size_t argcount,
5092 PyObject *kwnames)
5093{
5094 PyFrameObject *f = _PyEval_MakeFrameVector(
5095 tstate, con, locals, args, argcount, kwnames);
5096 if (f == NULL) {
5097 return NULL;
5098 }
5099 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5100 return make_coro(con, f);
5101 }
5102 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5103
5104 /* decref'ing the frame can cause __del__ methods to get invoked,
5105 which can call back into Python. While we're done with the
5106 current Python frame (f), the associated C stack is still in use,
5107 so recursion_depth must be boosted for the duration.
5108 */
5109 if (Py_REFCNT(f) > 1) {
5110 Py_DECREF(f);
5111 _PyObject_GC_TRACK(f);
5112 }
5113 else {
5114 ++tstate->recursion_depth;
5115 Py_DECREF(f);
5116 --tstate->recursion_depth;
5117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005119}
5120
Mark Shannond6c33fb2021-01-29 13:24:55 +00005121/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005122PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005123PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5124 PyObject *const *args, int argcount,
5125 PyObject *const *kws, int kwcount,
5126 PyObject *const *defs, int defcount,
5127 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005128{
Victor Stinner46496f92021-02-20 15:17:18 +01005129 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005130 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005131 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5132 if (defaults == NULL) {
5133 return NULL;
5134 }
Victor Stinner46496f92021-02-20 15:17:18 +01005135 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005136 if (builtins == NULL) {
5137 Py_DECREF(defaults);
5138 return NULL;
5139 }
Dong-hee Na3cf08332021-02-14 15:54:39 +09005140 assert ((((PyCodeObject *)_co)->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
Mark Shannon0332e562021-02-01 10:42:03 +00005141 if (locals == NULL) {
5142 locals = globals;
5143 }
5144 PyObject *kwnames;
5145 PyObject *const *allargs;
5146 PyObject **newargs;
5147 if (kwcount == 0) {
5148 allargs = args;
5149 kwnames = NULL;
5150 }
5151 else {
5152 kwnames = PyTuple_New(kwcount);
5153 if (kwnames == NULL) {
5154 res = NULL;
5155 goto fail;
5156 }
5157 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5158 if (newargs == NULL) {
5159 res = NULL;
5160 Py_DECREF(kwnames);
5161 goto fail;
5162 }
5163 for (int i = 0; i < argcount; i++) {
5164 newargs[i] = args[i];
5165 }
5166 for (int i = 0; i < kwcount; i++) {
5167 Py_INCREF(kws[2*i]);
5168 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5169 newargs[argcount+i] = kws[2*i+1];
5170 }
5171 allargs = newargs;
5172 }
5173 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5174 if (kwargs == NULL) {
5175 res = NULL;
5176 Py_DECREF(kwnames);
5177 goto fail;
5178 }
5179 for (int i = 0; i < kwcount; i++) {
5180 Py_INCREF(kws[2*i]);
5181 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5182 kwargs[i] = kws[2*i+1];
5183 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005184 PyFrameConstructor constr = {
5185 .fc_globals = globals,
5186 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005187 .fc_name = ((PyCodeObject *)_co)->co_name,
5188 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005189 .fc_code = _co,
5190 .fc_defaults = defaults,
5191 .fc_kwdefaults = kwdefs,
5192 .fc_closure = closure
5193 };
Mark Shannon0332e562021-02-01 10:42:03 +00005194 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005195 allargs, argcount,
5196 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005197 if (kwcount) {
5198 Py_DECREF(kwnames);
5199 PyMem_Free(newargs);
5200 }
5201fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005202 Py_DECREF(defaults);
5203 Py_DECREF(builtins);
5204 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005205}
5206
Tim Peters5ca576e2001-06-18 22:08:13 +00005207
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005208static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005209special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005212 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005213 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005214 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 return NULL;
5216 }
5217 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005218}
5219
5220
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005221/* Logic for the raise statement (too complicated for inlining).
5222 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005223static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005224do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005226 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 if (exc == NULL) {
5229 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005230 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005231 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005232 type = exc_info->exc_type;
5233 value = exc_info->exc_value;
5234 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005235 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005236 _PyErr_SetString(tstate, PyExc_RuntimeError,
5237 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005238 return 0;
5239 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 Py_XINCREF(type);
5241 Py_XINCREF(value);
5242 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005243 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005244 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 /* We support the following forms of raise:
5248 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005249 raise <instance>
5250 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 if (PyExceptionClass_Check(exc)) {
5253 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005254 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 if (value == NULL)
5256 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005257 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005258 _PyErr_Format(tstate, PyExc_TypeError,
5259 "calling %R should have returned an instance of "
5260 "BaseException, not %R",
5261 type, Py_TYPE(value));
5262 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005263 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 }
5265 else if (PyExceptionInstance_Check(exc)) {
5266 value = exc;
5267 type = PyExceptionInstance_Class(exc);
5268 Py_INCREF(type);
5269 }
5270 else {
5271 /* Not something you can raise. You get an exception
5272 anyway, just not what you specified :-) */
5273 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005274 _PyErr_SetString(tstate, PyExc_TypeError,
5275 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 goto raise_error;
5277 }
Collin Winter828f04a2007-08-31 00:04:24 +00005278
Serhiy Storchakac0191582016-09-27 11:37:10 +03005279 assert(type != NULL);
5280 assert(value != NULL);
5281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 if (cause) {
5283 PyObject *fixed_cause;
5284 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005285 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 if (fixed_cause == NULL)
5287 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005288 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005290 else if (PyExceptionInstance_Check(cause)) {
5291 fixed_cause = cause;
5292 }
5293 else if (cause == Py_None) {
5294 Py_DECREF(cause);
5295 fixed_cause = NULL;
5296 }
5297 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005298 _PyErr_SetString(tstate, PyExc_TypeError,
5299 "exception causes must derive from "
5300 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 goto raise_error;
5302 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005303 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 }
Collin Winter828f04a2007-08-31 00:04:24 +00005305
Victor Stinner438a12d2019-05-24 17:01:38 +02005306 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005307 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005308 Py_DECREF(value);
5309 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005310 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005311
5312raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 Py_XDECREF(value);
5314 Py_XDECREF(type);
5315 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005316 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005317}
5318
Tim Petersd6d010b2001-06-21 02:49:55 +00005319/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005320 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005321
Guido van Rossum0368b722007-05-11 16:50:42 +00005322 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5323 with a variable target.
5324*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005325
Barry Warsawe42b18f1997-08-25 22:13:04 +00005326static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005327unpack_iterable(PyThreadState *tstate, PyObject *v,
5328 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 int i = 0, j = 0;
5331 Py_ssize_t ll = 0;
5332 PyObject *it; /* iter(v) */
5333 PyObject *w;
5334 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005339 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005340 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005341 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005342 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005343 _PyErr_Format(tstate, PyExc_TypeError,
5344 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005345 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005346 }
5347 return 0;
5348 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 for (; i < argcnt; i++) {
5351 w = PyIter_Next(it);
5352 if (w == NULL) {
5353 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005355 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 _PyErr_Format(tstate, PyExc_ValueError,
5357 "not enough values to unpack "
5358 "(expected %d, got %d)",
5359 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005360 }
5361 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005362 _PyErr_Format(tstate, PyExc_ValueError,
5363 "not enough values to unpack "
5364 "(expected at least %d, got %d)",
5365 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 }
5368 goto Error;
5369 }
5370 *--sp = w;
5371 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 if (argcntafter == -1) {
5374 /* We better have exhausted the iterator now. */
5375 w = PyIter_Next(it);
5376 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005377 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 goto Error;
5379 Py_DECREF(it);
5380 return 1;
5381 }
5382 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005383 _PyErr_Format(tstate, PyExc_ValueError,
5384 "too many values to unpack (expected %d)",
5385 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 goto Error;
5387 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 l = PySequence_List(it);
5390 if (l == NULL)
5391 goto Error;
5392 *--sp = l;
5393 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005395 ll = PyList_GET_SIZE(l);
5396 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005397 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005398 "not enough values to unpack (expected at least %d, got %zd)",
5399 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 goto Error;
5401 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 /* Pop the "after-variable" args off the list. */
5404 for (j = argcntafter; j > 0; j--, i++) {
5405 *--sp = PyList_GET_ITEM(l, ll - j);
5406 }
5407 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005408 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 Py_DECREF(it);
5410 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005411
Tim Petersd6d010b2001-06-21 02:49:55 +00005412Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 for (; i > 0; i--, sp++)
5414 Py_DECREF(*sp);
5415 Py_XDECREF(it);
5416 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005417}
5418
5419
Guido van Rossum96a42c81992-01-12 02:29:51 +00005420#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005421static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005422prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005425 if (PyObject_Print(v, stdout, 0) != 0) {
5426 /* Don't know what else to do */
5427 _PyErr_Clear(tstate);
5428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 printf("\n");
5430 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005431}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005432#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005433
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005434static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005435call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005436 PyThreadState *tstate,
5437 PyFrameObject *f,
5438 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005439{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005440 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005442 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 if (value == NULL) {
5444 value = Py_None;
5445 Py_INCREF(value);
5446 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005447 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005448 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 arg = PyTuple_Pack(3, type, value, traceback);
5450 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005451 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 return;
5453 }
Mark Shannon86433452021-01-07 16:49:02 +00005454 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005456 if (err == 0) {
5457 _PyErr_Restore(tstate, type, value, orig_traceback);
5458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005459 else {
5460 Py_XDECREF(type);
5461 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005462 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005464}
5465
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005466static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005467call_trace_protected(Py_tracefunc func, PyObject *obj,
5468 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005469 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005472 PyObject *type, *value, *traceback;
5473 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005474 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00005475 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 if (err == 0)
5477 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005478 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 return 0;
5480 }
5481 else {
5482 Py_XDECREF(type);
5483 Py_XDECREF(value);
5484 Py_XDECREF(traceback);
5485 return -1;
5486 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005487}
5488
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005489static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005490call_trace(Py_tracefunc func, PyObject *obj,
5491 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005492 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005495 int result;
5496 if (tstate->tracing)
5497 return 0;
5498 tstate->tracing++;
5499 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005500 if (frame->f_lasti < 0) {
5501 frame->f_lineno = frame->f_code->co_firstlineno;
5502 }
5503 else {
5504 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5505 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005506 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005507 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005508 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5509 || (tstate->c_profilefunc != NULL));
5510 tstate->tracing--;
5511 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005512}
5513
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005514PyObject *
5515_PyEval_CallTracing(PyObject *func, PyObject *args)
5516{
Victor Stinner50b48572018-11-01 01:51:40 +01005517 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 int save_tracing = tstate->tracing;
5519 int save_use_tracing = tstate->use_tracing;
5520 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005522 tstate->tracing = 0;
5523 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5524 || (tstate->c_profilefunc != NULL));
5525 result = PyObject_Call(func, args, NULL);
5526 tstate->tracing = save_tracing;
5527 tstate->use_tracing = save_use_tracing;
5528 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005529}
5530
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005531/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005532static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005533maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005534 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005535 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005537 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005538
Nick Coghlan5a851672017-09-08 10:14:16 +10005539 /* If the last instruction falls at the start of a line or if it
5540 represents a jump backwards, update the frame's line number and
5541 then call the trace function if we're tracing source lines.
5542 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005543 int lastline = bounds->ar_line;
5544 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5545 if (line != -1 && frame->f_trace_lines) {
5546 /* Trace backward edges or first instruction of a new line */
5547 if (frame->f_lasti < *instr_prev ||
5548 (line != lastline && frame->f_lasti == bounds->ar_start))
5549 {
Mark Shannon86433452021-01-07 16:49:02 +00005550 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005552 }
George King20faa682017-10-18 17:44:22 -07005553 /* Always emit an opcode event if we're tracing all opcodes. */
5554 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005555 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005556 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 *instr_prev = frame->f_lasti;
5558 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005559}
5560
Victor Stinner309d7cc2020-03-13 16:39:12 +01005561int
5562_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5563{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005564 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005565 /* The caller must hold the GIL */
5566 assert(PyGILState_Check());
5567
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005568 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005569 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005570 PyThreadState *current_tstate = _PyThreadState_GET();
5571 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005572 return -1;
5573 }
5574
5575 PyObject *profileobj = tstate->c_profileobj;
5576
5577 tstate->c_profilefunc = NULL;
5578 tstate->c_profileobj = NULL;
5579 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5580 tstate->use_tracing = tstate->c_tracefunc != NULL;
5581 Py_XDECREF(profileobj);
5582
5583 Py_XINCREF(arg);
5584 tstate->c_profileobj = arg;
5585 tstate->c_profilefunc = func;
5586
5587 /* Flag that tracing or profiling is turned on */
5588 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5589 return 0;
5590}
5591
Fred Drake5755ce62001-06-27 19:19:46 +00005592void
5593PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005594{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005595 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005596 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005597 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005598 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5599 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005600}
5601
5602int
5603_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5604{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005605 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005606 /* The caller must hold the GIL */
5607 assert(PyGILState_Check());
5608
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005609 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005610 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005611 PyThreadState *current_tstate = _PyThreadState_GET();
5612 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005613 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005614 }
5615
Victor Stinnerda2914d2020-03-20 09:29:08 +01005616 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005617 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005618 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005619
5620 tstate->c_tracefunc = NULL;
5621 tstate->c_traceobj = NULL;
5622 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5623 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5624 Py_XDECREF(traceobj);
5625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005626 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005627 tstate->c_traceobj = arg;
5628 tstate->c_tracefunc = func;
5629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005630 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005631 tstate->use_tracing = ((func != NULL)
5632 || (tstate->c_profilefunc != NULL));
5633
5634 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005635}
5636
5637void
5638PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5639{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005640 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005641 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005642 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005643 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5644 }
Fred Draked0838392001-06-16 21:02:31 +00005645}
5646
Victor Stinner309d7cc2020-03-13 16:39:12 +01005647
Yury Selivanov75445082015-05-11 22:57:16 -04005648void
Victor Stinner838f2642019-06-13 22:41:23 +02005649_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005650{
5651 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005652 tstate->coroutine_origin_tracking_depth = new_depth;
5653}
5654
5655int
5656_PyEval_GetCoroutineOriginTrackingDepth(void)
5657{
Victor Stinner50b48572018-11-01 01:51:40 +01005658 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005659 return tstate->coroutine_origin_tracking_depth;
5660}
5661
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005662int
Yury Selivanoveb636452016-09-08 22:01:51 -07005663_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5664{
Victor Stinner50b48572018-11-01 01:51:40 +01005665 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005666
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005667 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005668 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005669 }
5670
Yury Selivanoveb636452016-09-08 22:01:51 -07005671 Py_XINCREF(firstiter);
5672 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005673 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005674}
5675
5676PyObject *
5677_PyEval_GetAsyncGenFirstiter(void)
5678{
Victor Stinner50b48572018-11-01 01:51:40 +01005679 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005680 return tstate->async_gen_firstiter;
5681}
5682
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005683int
Yury Selivanoveb636452016-09-08 22:01:51 -07005684_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5685{
Victor Stinner50b48572018-11-01 01:51:40 +01005686 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005687
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005688 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005689 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005690 }
5691
Yury Selivanoveb636452016-09-08 22:01:51 -07005692 Py_XINCREF(finalizer);
5693 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005694 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005695}
5696
5697PyObject *
5698_PyEval_GetAsyncGenFinalizer(void)
5699{
Victor Stinner50b48572018-11-01 01:51:40 +01005700 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005701 return tstate->async_gen_finalizer;
5702}
5703
Victor Stinner438a12d2019-05-24 17:01:38 +02005704PyFrameObject *
5705PyEval_GetFrame(void)
5706{
5707 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005708 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005709}
5710
Guido van Rossumb209a111997-04-29 18:18:01 +00005711PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005712_PyEval_GetBuiltins(PyThreadState *tstate)
5713{
5714 PyFrameObject *frame = tstate->frame;
5715 if (frame != NULL) {
5716 return frame->f_builtins;
5717 }
5718 return tstate->interp->builtins;
5719}
5720
5721PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005722PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005723{
Victor Stinner438a12d2019-05-24 17:01:38 +02005724 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005725 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005726}
5727
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005728/* Convenience function to get a builtin from its name */
5729PyObject *
5730_PyEval_GetBuiltinId(_Py_Identifier *name)
5731{
Victor Stinner438a12d2019-05-24 17:01:38 +02005732 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005733 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5734 if (attr) {
5735 Py_INCREF(attr);
5736 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005737 else if (!_PyErr_Occurred(tstate)) {
5738 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005739 }
5740 return attr;
5741}
5742
Guido van Rossumb209a111997-04-29 18:18:01 +00005743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005744PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005745{
Victor Stinner438a12d2019-05-24 17:01:38 +02005746 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005747 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005748 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005749 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005751 }
5752
Victor Stinner438a12d2019-05-24 17:01:38 +02005753 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005754 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005755 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005756
5757 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005759}
5760
Guido van Rossumb209a111997-04-29 18:18:01 +00005761PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005762PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005763{
Victor Stinner438a12d2019-05-24 17:01:38 +02005764 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005765 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005766 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005767 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005768 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005769
5770 assert(current_frame->f_globals != NULL);
5771 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005772}
5773
Guido van Rossum6135a871995-01-09 17:53:26 +00005774int
Tim Peters5ba58662001-07-16 02:29:45 +00005775PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005776{
Victor Stinner438a12d2019-05-24 17:01:38 +02005777 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005778 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005779 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005781 if (current_frame != NULL) {
5782 const int codeflags = current_frame->f_code->co_flags;
5783 const int compilerflags = codeflags & PyCF_MASK;
5784 if (compilerflags) {
5785 result = 1;
5786 cf->cf_flags |= compilerflags;
5787 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005788#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005789 if (codeflags & CO_GENERATOR_ALLOWED) {
5790 result = 1;
5791 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5792 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005793#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005794 }
5795 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005796}
5797
Guido van Rossum3f5da241990-12-20 15:06:42 +00005798
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005799const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005800PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005802 if (PyMethod_Check(func))
5803 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5804 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005805 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 else if (PyCFunction_Check(func))
5807 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5808 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005809 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005810}
5811
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005812const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005813PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 if (PyMethod_Check(func))
5816 return "()";
5817 else if (PyFunction_Check(func))
5818 return "()";
5819 else if (PyCFunction_Check(func))
5820 return "()";
5821 else
5822 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005823}
5824
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005825#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005826if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005827 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005828 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005829 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005830 x = NULL; \
5831 } \
5832 else { \
5833 x = call; \
5834 if (tstate->c_profilefunc != NULL) { \
5835 if (x == NULL) { \
5836 call_trace_protected(tstate->c_profilefunc, \
5837 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005838 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005839 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005840 /* XXX should pass (type, value, tb) */ \
5841 } else { \
5842 if (call_trace(tstate->c_profilefunc, \
5843 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005844 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005845 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005846 Py_DECREF(x); \
5847 x = NULL; \
5848 } \
5849 } \
5850 } \
5851 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005852} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005853 x = call; \
5854 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005855
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005856
5857static PyObject *
5858trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005859 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005860 PyObject *func,
5861 PyObject **args, Py_ssize_t nargs,
5862 PyObject *kwnames)
5863{
5864 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005865 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005866 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005867 return x;
5868 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005869 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005870 /* We need to create a temporary bound method as argument
5871 for profiling.
5872
5873 If nargs == 0, then this cannot work because we have no
5874 "self". In any case, the call itself would raise
5875 TypeError (foo needs an argument), so we just skip
5876 profiling. */
5877 PyObject *self = args[0];
5878 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5879 if (func == NULL) {
5880 return NULL;
5881 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005882 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005883 args+1, nargs-1,
5884 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005885 Py_DECREF(func);
5886 return x;
5887 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005888 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005889}
5890
Victor Stinner415c5102017-01-11 00:54:57 +01005891/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5892 to reduce the stack consumption. */
5893Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005894call_function(PyThreadState *tstate,
5895 PyCodeAddressRange *bounds,
5896 PyObject ***pp_stack,
5897 Py_ssize_t oparg,
5898 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005899{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005900 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005901 PyObject *func = *pfunc;
5902 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005903 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5904 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005905 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005906
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005907 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005908 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005909 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005910 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005911 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005912 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005913
Victor Stinner438a12d2019-05-24 17:01:38 +02005914 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005915
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005916 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005917 while ((*pp_stack) > pfunc) {
5918 w = EXT_POP(*pp_stack);
5919 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005920 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005922 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005923}
5924
Jeremy Hylton52820442001-01-03 23:52:36 +00005925static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005926do_call_core(PyThreadState *tstate,
5927 PyCodeAddressRange *bounds,
5928 PyObject *func,
5929 PyObject *callargs,
5930 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005931{
jdemeyere89de732018-09-19 12:06:20 +02005932 PyObject *result;
5933
scoder4c9ea092020-05-12 16:12:41 +02005934 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005935 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005936 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005937 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005938 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005939 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5940 if (nargs > 0 && tstate->use_tracing) {
5941 /* We need to create a temporary bound method as argument
5942 for profiling.
5943
5944 If nargs == 0, then this cannot work because we have no
5945 "self". In any case, the call itself would raise
5946 TypeError (foo needs an argument), so we just skip
5947 profiling. */
5948 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5949 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5950 if (func == NULL) {
5951 return NULL;
5952 }
5953
Victor Stinner4d231bc2019-11-14 13:36:21 +01005954 C_TRACE(result, _PyObject_FastCallDictTstate(
5955 tstate, func,
5956 &_PyTuple_ITEMS(callargs)[1],
5957 nargs - 1,
5958 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005959 Py_DECREF(func);
5960 return result;
5961 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005962 }
jdemeyere89de732018-09-19 12:06:20 +02005963 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005964}
5965
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005966/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005967 nb_index slot defined, and store in *pi.
5968 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005969 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005970 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005971*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005972int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005973_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005974{
Victor Stinner438a12d2019-05-24 17:01:38 +02005975 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005976 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005977 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005978 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005980 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005981 return 0;
5982 }
5983 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005984 _PyErr_SetString(tstate, PyExc_TypeError,
5985 "slice indices must be integers or "
5986 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005987 return 0;
5988 }
5989 *pi = x;
5990 }
5991 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005992}
5993
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005994int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005995_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005996{
Victor Stinner438a12d2019-05-24 17:01:38 +02005997 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005998 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005999 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006000 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006001 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006002 return 0;
6003 }
6004 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006005 _PyErr_SetString(tstate, PyExc_TypeError,
6006 "slice indices must be integers or "
6007 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006008 return 0;
6009 }
6010 *pi = x;
6011 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006012}
6013
Thomas Wouters52152252000-08-17 22:55:00 +00006014static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006015import_name(PyThreadState *tstate, PyFrameObject *f,
6016 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006017{
6018 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006019 PyObject *import_func, *res;
6020 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006021
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006022 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006023 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006024 if (!_PyErr_Occurred(tstate)) {
6025 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006026 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006027 return NULL;
6028 }
6029
6030 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006031 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006032 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006033 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006034 return NULL;
6035 }
6036 res = PyImport_ImportModuleLevelObject(
6037 name,
6038 f->f_globals,
6039 f->f_locals == NULL ? Py_None : f->f_locals,
6040 fromlist,
6041 ilevel);
6042 return res;
6043 }
6044
6045 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006046
6047 stack[0] = name;
6048 stack[1] = f->f_globals;
6049 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6050 stack[3] = fromlist;
6051 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006052 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006053 Py_DECREF(import_func);
6054 return res;
6055}
6056
6057static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006058import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006060 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006061 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006062
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006063 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006064 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006065 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006066 /* Issue #17636: in case this failed because of a circular relative
6067 import, try to fallback on reading the module directly from
6068 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006069 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006070 if (pkgname == NULL) {
6071 goto error;
6072 }
Oren Milman6db70332017-09-19 14:23:01 +03006073 if (!PyUnicode_Check(pkgname)) {
6074 Py_CLEAR(pkgname);
6075 goto error;
6076 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006077 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006078 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006079 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006080 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006081 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006082 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006083 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006084 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006085 goto error;
6086 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006087 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006088 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006089 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006090 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006091 if (pkgname == NULL) {
6092 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6093 if (pkgname_or_unknown == NULL) {
6094 Py_XDECREF(pkgpath);
6095 return NULL;
6096 }
6097 } else {
6098 pkgname_or_unknown = pkgname;
6099 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006100
6101 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006102 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006103 errmsg = PyUnicode_FromFormat(
6104 "cannot import name %R from %R (unknown location)",
6105 name, pkgname_or_unknown
6106 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006107 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006108 PyErr_SetImportError(errmsg, pkgname, NULL);
6109 }
6110 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006111 _Py_IDENTIFIER(__spec__);
6112 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006113 const char *fmt =
6114 _PyModuleSpec_IsInitializing(spec) ?
6115 "cannot import name %R from partially initialized module %R "
6116 "(most likely due to a circular import) (%S)" :
6117 "cannot import name %R from %R (%S)";
6118 Py_XDECREF(spec);
6119
6120 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006121 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006122 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006123 }
6124
Xiang Zhang4830f582017-03-21 11:13:42 +08006125 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006126 Py_XDECREF(pkgname_or_unknown);
6127 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006128 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006129}
Guido van Rossumac7be682001-01-17 15:42:30 +00006130
Thomas Wouters52152252000-08-17 22:55:00 +00006131static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006132import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006133{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006134 _Py_IDENTIFIER(__all__);
6135 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006136 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006137 int skip_leading_underscores = 0;
6138 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006139
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006140 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6141 return -1; /* Unexpected error */
6142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006143 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006144 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6145 return -1;
6146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006147 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006148 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006149 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006150 return -1;
6151 }
6152 all = PyMapping_Keys(dict);
6153 Py_DECREF(dict);
6154 if (all == NULL)
6155 return -1;
6156 skip_leading_underscores = 1;
6157 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159 for (pos = 0, err = 0; ; pos++) {
6160 name = PySequence_GetItem(all, pos);
6161 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006162 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006163 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006164 }
6165 else {
6166 _PyErr_Clear(tstate);
6167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006168 break;
6169 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006170 if (!PyUnicode_Check(name)) {
6171 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6172 if (modname == NULL) {
6173 Py_DECREF(name);
6174 err = -1;
6175 break;
6176 }
6177 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006178 _PyErr_Format(tstate, PyExc_TypeError,
6179 "module __name__ must be a string, not %.100s",
6180 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006181 }
6182 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006183 _PyErr_Format(tstate, PyExc_TypeError,
6184 "%s in %U.%s must be str, not %.100s",
6185 skip_leading_underscores ? "Key" : "Item",
6186 modname,
6187 skip_leading_underscores ? "__dict__" : "__all__",
6188 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006189 }
6190 Py_DECREF(modname);
6191 Py_DECREF(name);
6192 err = -1;
6193 break;
6194 }
6195 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006196 if (PyUnicode_READY(name) == -1) {
6197 Py_DECREF(name);
6198 err = -1;
6199 break;
6200 }
6201 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6202 Py_DECREF(name);
6203 continue;
6204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006205 }
6206 value = PyObject_GetAttr(v, name);
6207 if (value == NULL)
6208 err = -1;
6209 else if (PyDict_CheckExact(locals))
6210 err = PyDict_SetItem(locals, name, value);
6211 else
6212 err = PyObject_SetItem(locals, name, value);
6213 Py_DECREF(name);
6214 Py_XDECREF(value);
6215 if (err != 0)
6216 break;
6217 }
6218 Py_DECREF(all);
6219 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006220}
6221
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006222static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006223check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006224{
Victor Stinnera102ed72020-02-07 02:24:48 +01006225 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006226 /* check_args_iterable() may be called with a live exception:
6227 * clear it to prevent calling _PyObject_FunctionStr() with an
6228 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006229 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006230 PyObject *funcstr = _PyObject_FunctionStr(func);
6231 if (funcstr != NULL) {
6232 _PyErr_Format(tstate, PyExc_TypeError,
6233 "%U argument after * must be an iterable, not %.200s",
6234 funcstr, Py_TYPE(args)->tp_name);
6235 Py_DECREF(funcstr);
6236 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006237 return -1;
6238 }
6239 return 0;
6240}
6241
6242static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006243format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006244{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006245 /* _PyDict_MergeEx raises attribute
6246 * error (percolated from an attempt
6247 * to get 'keys' attribute) instead of
6248 * a type error if its second argument
6249 * is not a mapping.
6250 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006251 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006252 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006253 PyObject *funcstr = _PyObject_FunctionStr(func);
6254 if (funcstr != NULL) {
6255 _PyErr_Format(
6256 tstate, PyExc_TypeError,
6257 "%U argument after ** must be a mapping, not %.200s",
6258 funcstr, Py_TYPE(kwargs)->tp_name);
6259 Py_DECREF(funcstr);
6260 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006261 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006262 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006263 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006264 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006265 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006266 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006267 PyObject *funcstr = _PyObject_FunctionStr(func);
6268 if (funcstr != NULL) {
6269 PyObject *key = PyTuple_GET_ITEM(val, 0);
6270 _PyErr_Format(
6271 tstate, PyExc_TypeError,
6272 "%U got multiple values for keyword argument '%S'",
6273 funcstr, key);
6274 Py_DECREF(funcstr);
6275 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006276 Py_XDECREF(exc);
6277 Py_XDECREF(val);
6278 Py_XDECREF(tb);
6279 }
6280 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006281 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006282 }
6283 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006284}
6285
Guido van Rossumac7be682001-01-17 15:42:30 +00006286static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006287format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6288 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006290 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006292 if (!obj)
6293 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006294
Serhiy Storchaka06515832016-11-20 09:13:07 +02006295 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006296 if (!obj_str)
6297 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006298
Victor Stinner438a12d2019-05-24 17:01:38 +02006299 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006300}
Guido van Rossum950361c1997-01-24 13:49:28 +00006301
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006302static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006303format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006304{
6305 PyObject *name;
6306 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006307 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006308 return;
6309 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6310 name = PyTuple_GET_ITEM(co->co_cellvars,
6311 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006312 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006313 PyExc_UnboundLocalError,
6314 UNBOUNDLOCAL_ERROR_MSG,
6315 name);
6316 } else {
6317 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6318 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006319 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006320 UNBOUNDFREE_ERROR_MSG, name);
6321 }
6322}
6323
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006324static void
Mark Shannonfee55262019-11-21 09:11:43 +00006325format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006326{
6327 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6328 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006329 _PyErr_Format(tstate, PyExc_TypeError,
6330 "'async with' received an object from __aenter__ "
6331 "that does not implement __await__: %.100s",
6332 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006333 }
Mark Shannonfee55262019-11-21 09:11:43 +00006334 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006335 _PyErr_Format(tstate, PyExc_TypeError,
6336 "'async with' received an object from __aexit__ "
6337 "that does not implement __await__: %.100s",
6338 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006339 }
6340 }
6341}
6342
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006343static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006344unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006345 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006346{
6347 PyObject *res;
6348 if (Py_REFCNT(v) == 2) {
6349 /* In the common case, there are 2 references to the value
6350 * stored in 'variable' when the += is performed: one on the
6351 * value stack (in 'v') and one still stored in the
6352 * 'variable'. We try to delete the variable now to reduce
6353 * the refcnt to 1.
6354 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006355 int opcode, oparg;
6356 NEXTOPARG();
6357 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006358 case STORE_FAST:
6359 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006360 PyObject **fastlocals = f->f_localsplus;
6361 if (GETLOCAL(oparg) == v)
6362 SETLOCAL(oparg, NULL);
6363 break;
6364 }
6365 case STORE_DEREF:
6366 {
6367 PyObject **freevars = (f->f_localsplus +
6368 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006369 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006370 if (PyCell_GET(c) == v) {
6371 PyCell_SET(c, NULL);
6372 Py_DECREF(v);
6373 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006374 break;
6375 }
6376 case STORE_NAME:
6377 {
6378 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006379 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006380 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006381 if (locals && PyDict_CheckExact(locals)) {
6382 PyObject *w = PyDict_GetItemWithError(locals, name);
6383 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006384 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006385 {
6386 Py_DECREF(v);
6387 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006388 }
6389 }
6390 break;
6391 }
6392 }
6393 }
6394 res = v;
6395 PyUnicode_Append(&res, w);
6396 return res;
6397}
6398
Guido van Rossum950361c1997-01-24 13:49:28 +00006399#ifdef DYNAMIC_EXECUTION_PROFILE
6400
Skip Montanarof118cb12001-10-15 20:51:38 +00006401static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006402getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006404 int i;
6405 PyObject *l = PyList_New(256);
6406 if (l == NULL) return NULL;
6407 for (i = 0; i < 256; i++) {
6408 PyObject *x = PyLong_FromLong(a[i]);
6409 if (x == NULL) {
6410 Py_DECREF(l);
6411 return NULL;
6412 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006413 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006414 }
6415 for (i = 0; i < 256; i++)
6416 a[i] = 0;
6417 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006418}
6419
6420PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006421_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006422{
6423#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006424 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006425#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006426 int i;
6427 PyObject *l = PyList_New(257);
6428 if (l == NULL) return NULL;
6429 for (i = 0; i < 257; i++) {
6430 PyObject *x = getarray(dxpairs[i]);
6431 if (x == NULL) {
6432 Py_DECREF(l);
6433 return NULL;
6434 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006435 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006436 }
6437 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006438#endif
6439}
6440
6441#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006442
6443Py_ssize_t
6444_PyEval_RequestCodeExtraIndex(freefunc free)
6445{
Victor Stinner81a7be32020-04-14 15:14:01 +02006446 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006447 Py_ssize_t new_index;
6448
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006449 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006450 return -1;
6451 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006452 new_index = interp->co_extra_user_count++;
6453 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006454 return new_index;
6455}
Łukasz Langaa785c872016-09-09 17:37:37 -07006456
6457static void
6458dtrace_function_entry(PyFrameObject *f)
6459{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006460 const char *filename;
6461 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006462 int lineno;
6463
Victor Stinner6d86a232020-04-29 00:56:58 +02006464 PyCodeObject *code = f->f_code;
6465 filename = PyUnicode_AsUTF8(code->co_filename);
6466 funcname = PyUnicode_AsUTF8(code->co_name);
6467 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006468
Andy Lestere6be9b52020-02-11 20:28:35 -06006469 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006470}
6471
6472static void
6473dtrace_function_return(PyFrameObject *f)
6474{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006475 const char *filename;
6476 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006477 int lineno;
6478
Victor Stinner6d86a232020-04-29 00:56:58 +02006479 PyCodeObject *code = f->f_code;
6480 filename = PyUnicode_AsUTF8(code->co_filename);
6481 funcname = PyUnicode_AsUTF8(code->co_name);
6482 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006483
Andy Lestere6be9b52020-02-11 20:28:35 -06006484 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006485}
6486
6487/* DTrace equivalent of maybe_call_line_trace. */
6488static void
6489maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00006490 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006491{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006492 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006493
6494 /* If the last instruction executed isn't in the current
6495 instruction window, reset the window.
6496 */
Mark Shannon877df852020-11-12 09:43:29 +00006497 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006498 /* If the last instruction falls at the start of a line or if
6499 it represents a jump backwards, update the frame's line
6500 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00006501 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
6502 if (line != -1) {
6503 frame->f_lineno = line;
6504 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6505 if (!co_filename)
6506 co_filename = "?";
6507 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6508 if (!co_name)
6509 co_name = "?";
6510 PyDTrace_LINE(co_filename, co_name, line);
6511 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006512 }
6513 *instr_prev = frame->f_lasti;
6514}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006515
6516
6517/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6518 for the limited API. */
6519
6520#undef Py_EnterRecursiveCall
6521
6522int Py_EnterRecursiveCall(const char *where)
6523{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006524 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006525}
6526
6527#undef Py_LeaveRecursiveCall
6528
6529void Py_LeaveRecursiveCall(void)
6530{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006531 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006532}