blob: 7ccb8fcf5ae5413fb261f3733d7c410c74728756 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020014#include "pycore_call.h" // _PyObject_FastCallDictTstate()
15#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
16#include "pycore_code.h" // _PyCode_InitOpcache()
17#include "pycore_initconfig.h" // _PyStatus_OK()
18#include "pycore_object.h" // _PyObject_GC_TRACK()
19#include "pycore_pyerrors.h" // _PyErr_Fetch()
20#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020023#include "pycore_sysmodule.h" // _PySys_Audit()
24#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum5c5a9382021-01-29 18:02:29 -080032#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossumc6004111993-11-05 10:22:19 +000034#include <ctype.h>
35
Guido van Rossum408027e1996-12-30 16:17:54 +000036#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000037/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038#define LLTRACE 1 /* Low-level trace feature */
39#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#endif
41
Victor Stinner5c75f372019-04-17 23:02:26 +020042#if !defined(Py_BUILD_CORE)
43# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
44#endif
45
Hai Shi46874c22020-01-30 17:20:25 -060046_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000047
Guido van Rossum374a9221991-04-04 10:40:29 +000048/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020049Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon86433452021-01-07 16:49:02 +000050 PyThreadState *tstate, PyCodeAddressRange *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020051 Py_ssize_t oparg, PyObject *kwnames);
52static PyObject * do_call_core(
Mark Shannon86433452021-01-07 16:49:02 +000053 PyThreadState *tstate, PyCodeAddressRange *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020054 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000055
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000057static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020058static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000059#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060static int call_trace(Py_tracefunc, PyObject *,
61 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000062 PyCodeAddressRange *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000064static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010065 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000066 PyCodeAddressRange *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010067 int, PyObject *);
68static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000069 PyThreadState *, PyFrameObject *,
70 PyCodeAddressRange *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000071static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060072 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000073 PyCodeAddressRange *, int *);
74static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070075static void dtrace_function_entry(PyFrameObject *);
76static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000077
Victor Stinner438a12d2019-05-24 17:01:38 +020078static PyObject * import_name(PyThreadState *, PyFrameObject *,
79 PyObject *, PyObject *, PyObject *);
80static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
81static int import_all_from(PyThreadState *, PyObject *, PyObject *);
82static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
83static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
84static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030085 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020086static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
87static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
88static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000089static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Paul Prescode68140d2000-08-30 20:25:01 +000091#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000093#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000095#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 "free variable '%.200s' referenced before assignment" \
97 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
Inada Naoki91234a12019-06-03 21:30:58 +0900109/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900110#ifdef Py_DEBUG
111// --with-pydebug is used to find memory leak. opcache makes it harder.
112// So we disable opcache when Py_DEBUG is defined.
113// See bpo-37146
114#define OPCACHE_MIN_RUNS 0 /* disable opcache */
115#else
Inada Naoki91234a12019-06-03 21:30:58 +0900116#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900117#endif
Pablo Galindo109826c2020-10-20 06:22:44 +0100118#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900119#define OPCACHE_STATS 0 /* Enable stats */
120
121#if OPCACHE_STATS
122static size_t opcache_code_objects = 0;
123static size_t opcache_code_objects_extra_mem = 0;
124
125static size_t opcache_global_opts = 0;
126static size_t opcache_global_hits = 0;
127static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100128
129static size_t opcache_attr_opts = 0;
130static size_t opcache_attr_hits = 0;
131static size_t opcache_attr_misses = 0;
132static size_t opcache_attr_deopts = 0;
133static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900134#endif
135
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100136
Victor Stinnerda2914d2020-03-20 09:29:08 +0100137#ifndef NDEBUG
138/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
139 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
140 when a thread continues to run after Python finalization, especially
141 daemon threads. */
142static int
143is_tstate_valid(PyThreadState *tstate)
144{
145 assert(!_PyMem_IsPtrFreed(tstate));
146 assert(!_PyMem_IsPtrFreed(tstate->interp));
147 return 1;
148}
149#endif
150
151
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000152/* This can set eval_breaker to 0 even though gil_drop_request became
153 1. We believe this is all right because the eval loop will release
154 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200156COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200157 struct _ceval_runtime_state *ceval,
158 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159{
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
161 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200162 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200163 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200164 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100165 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200166 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100167}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000169
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200171SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172{
Victor Stinner299b8c62020-05-05 17:40:18 +0200173 struct _ceval_state *ceval2 = &interp->ceval;
174 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
175 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100176}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Victor Stinnerda2914d2020-03-20 09:29:08 +0100179static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200180RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181{
Victor Stinner299b8c62020-05-05 17:40:18 +0200182 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
183 struct _ceval_state *ceval2 = &interp->ceval;
184 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
185 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100186}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Eric Snowfdf282d2019-01-11 14:26:55 -0700188
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200190SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191{
Victor Stinner299b8c62020-05-05 17:40:18 +0200192 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
193 struct _ceval_state *ceval2 = &interp->ceval;
194 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
195 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100196}
Eric Snowfdf282d2019-01-11 14:26:55 -0700197
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000198
Victor Stinnerda2914d2020-03-20 09:29:08 +0100199static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200200UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100201{
Victor Stinner299b8c62020-05-05 17:40:18 +0200202 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
203 struct _ceval_state *ceval2 = &interp->ceval;
204 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
207
208
209static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100210SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200214 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100215 if (force) {
216 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
217 }
218 else {
219 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
220 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
221 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100222}
223
224
225static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200226UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100227{
Victor Stinner299b8c62020-05-05 17:40:18 +0200228 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
229 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200230 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100232}
233
234
235static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200236SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100237{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200238 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100239 ceval2->pending.async_exc = 1;
240 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
241}
242
243
244static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200245UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100246{
Victor Stinner299b8c62020-05-05 17:40:18 +0200247 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
248 struct _ceval_state *ceval2 = &interp->ceval;
249 ceval2->pending.async_exc = 0;
250 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100251}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
253
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000255#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000256#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000257#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000258
Victor Stinner3026cad2020-06-01 16:02:40 +0200259void _Py_NO_RETURN
260_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100261{
Victor Stinner3026cad2020-06-01 16:02:40 +0200262 _Py_FatalErrorFunc(func,
263 "the function must be called with the GIL held, "
264 "but the GIL is released "
265 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100266}
267
Victor Stinner7be4e352020-05-05 20:27:47 +0200268#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
269int
270_PyEval_ThreadsInitialized(PyInterpreterState *interp)
271{
272 return gil_created(&interp->ceval.gil);
273}
274
275int
276PyEval_ThreadsInitialized(void)
277{
278 // Fatal error if there is no current interpreter
279 PyInterpreterState *interp = PyInterpreterState_Get();
280 return _PyEval_ThreadsInitialized(interp);
281}
282#else
Tim Peters7f468f22004-10-11 02:40:51 +0000283int
Victor Stinner175a7042020-03-10 00:37:48 +0100284_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
285{
286 return gil_created(&runtime->ceval.gil);
287}
288
289int
Tim Peters7f468f22004-10-11 02:40:51 +0000290PyEval_ThreadsInitialized(void)
291{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100292 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100293 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000294}
Victor Stinner7be4e352020-05-05 20:27:47 +0200295#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000296
Victor Stinner111e4ee2020-03-09 21:24:14 +0100297PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200298_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299{
Victor Stinner7be4e352020-05-05 20:27:47 +0200300#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100301 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200302 /* Currently, the GIL is shared by all interpreters,
303 and only the main interpreter is responsible to create
304 and destroy it. */
305 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100306 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200307#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100308
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
310 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
311#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200312 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200313#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200314 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100315
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200316 PyThread_init_thread();
317 create_gil(gil);
318
319 take_gil(tstate);
320
321 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100322 return _PyStatus_OK();
323}
324
325void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100326_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200327{
Victor Stinner7be4e352020-05-05 20:27:47 +0200328#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100329 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200330 /* Currently, the GIL is shared by all interpreters,
331 and only the main interpreter is responsible to create
332 and destroy it. */
333 return;
334 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200335#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200336
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100338 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200339#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100340 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200341#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200342 if (!gil_created(gil)) {
343 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
344 yet: do nothing. */
345 return;
346 }
347
348 destroy_gil(gil);
349 assert(!gil_created(gil));
350}
351
352void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100353PyEval_InitThreads(void)
354{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100355 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000357
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358void
Inada Naoki91234a12019-06-03 21:30:58 +0900359_PyEval_Fini(void)
360{
361#if OPCACHE_STATS
362 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
363 opcache_code_objects);
364
365 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
366 opcache_code_objects_extra_mem);
367
368 fprintf(stderr, "\n");
369
370 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
371 opcache_global_hits,
372 (int) (100.0 * opcache_global_hits /
373 (opcache_global_hits + opcache_global_misses)));
374
375 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
376 opcache_global_misses,
377 (int) (100.0 * opcache_global_misses /
378 (opcache_global_hits + opcache_global_misses)));
379
380 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
381 opcache_global_opts);
382
383 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100384
385 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
386 opcache_attr_hits,
387 (int) (100.0 * opcache_attr_hits /
388 opcache_attr_total));
389
390 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
391 opcache_attr_misses,
392 (int) (100.0 * opcache_attr_misses /
393 opcache_attr_total));
394
395 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
396 opcache_attr_opts);
397
398 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
399 opcache_attr_deopts);
400
401 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
402 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900403#endif
404}
405
406void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000408{
Victor Stinner09532fe2019-05-10 23:39:09 +0200409 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200410 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200411 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100412
Victor Stinner85f5a692020-03-09 22:12:04 +0100413 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414}
415
416void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418{
Victor Stinner09532fe2019-05-10 23:39:09 +0200419 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200420 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100422 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100423 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200424 struct _ceval_runtime_state *ceval = &runtime->ceval;
425 struct _ceval_state *ceval2 = &tstate->interp->ceval;
426 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427}
428
429void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100430_PyEval_ReleaseLock(PyThreadState *tstate)
431{
432 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200433 struct _ceval_state *ceval2 = &tstate->interp->ceval;
434 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100435}
436
437void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000438PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000439{
Victor Stinner3026cad2020-06-01 16:02:40 +0200440 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100441
Victor Stinner85f5a692020-03-09 22:12:04 +0100442 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200443
Victor Stinner85f5a692020-03-09 22:12:04 +0100444 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200445#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
446 (void)_PyThreadState_Swap(gilstate, tstate);
447#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100448 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100449 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200450 }
Victor Stinnere838a932020-05-05 19:56:48 +0200451#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000452}
453
454void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000456{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100457 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200458
Victor Stinner01b1cc12019-11-20 02:27:56 +0100459 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200460 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
461 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100462 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200463 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200464 struct _ceval_runtime_state *ceval = &runtime->ceval;
465 struct _ceval_state *ceval2 = &tstate->interp->ceval;
466 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000467}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000468
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900469#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200470/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200471 which are not running in the child process, and clear internal locks
472 which might be held by those threads. */
473PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200474_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000475{
Victor Stinner317bab02020-06-02 18:44:54 +0200476 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200477
478#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
479 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
480#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100481 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200482#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100483 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200484 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200485 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100486 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100487
488 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700489
Victor Stinner50e6e992020-03-19 02:41:21 +0100490 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900491 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200492 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700493 }
Jesse Nollera8513972008-07-17 16:49:17 +0000494
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200495 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100496 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200497 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000498}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900499#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000500
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000501/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600502 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000503
504void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100505_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000506{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100507 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000508}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000510PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512{
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200514#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
515 PyThreadState *old_tstate = _PyThreadState_GET();
516 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
517#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200518 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200519#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200520 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100521
Victor Stinner0b1e3302020-05-05 16:14:31 +0200522 struct _ceval_runtime_state *ceval = &runtime->ceval;
523 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200524#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
525 assert(gil_created(&ceval2->gil));
526#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200528#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200529 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000531}
532
533void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535{
Victor Stinner3026cad2020-06-01 16:02:40 +0200536 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100537
Victor Stinner85f5a692020-03-09 22:12:04 +0100538 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100539
Victor Stinner85f5a692020-03-09 22:12:04 +0100540 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
541 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000542}
543
544
Guido van Rossuma9672091994-09-14 13:31:22 +0000545/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
546 signal handlers or Mac I/O completion routines) can schedule calls
547 to a function to be called synchronously.
548 The synchronous function is called with one void* argument.
549 It should return 0 for success or -1 for failure -- failure should
550 be accompanied by an exception.
551
552 If registry succeeds, the registry function returns 0; if it fails
553 (e.g. due to too many pending calls) it returns -1 (without setting
554 an exception condition).
555
556 Note that because registry may occur from within signal handlers,
557 or other asynchronous events, calling malloc() is unsafe!
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559 Any thread can schedule pending calls, but only the main thread
560 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000561 There is no facility to schedule calls to a particular thread, but
562 that should be easy to change, should that ever be required. In
563 that case, the static variables here should go into the python
564 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000566
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200567void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200568_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200569{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100570#ifdef MS_WINDOWS
571 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
572 // handler which can run in a thread different than the Python thread, in
573 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
574 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
575 //
576 // The next eval_frame_handle_pending() call will call
577 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
578 int force = 1;
579#else
580 int force = 0;
581#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200582 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100583 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200584 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100585 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200586}
587
Eric Snow5be45a62019-03-08 22:47:07 -0700588/* Push one item onto the queue while holding the lock. */
589static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200590_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600591 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700592{
Eric Snow842a2f02019-03-15 15:47:51 -0600593 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700594 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600595 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700596 return -1; /* Queue full */
597 }
Eric Snow842a2f02019-03-15 15:47:51 -0600598 pending->calls[i].func = func;
599 pending->calls[i].arg = arg;
600 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700601 return 0;
602}
603
604/* Pop one item off the queue while holding the lock. */
605static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200606_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600607 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700608{
Eric Snow842a2f02019-03-15 15:47:51 -0600609 int i = pending->first;
610 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700611 return; /* Queue empty */
612 }
613
Eric Snow842a2f02019-03-15 15:47:51 -0600614 *func = pending->calls[i].func;
615 *arg = pending->calls[i].arg;
616 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700617}
618
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200619/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000620 scheduling to be made from any thread, and even from an executing
621 callback.
622 */
623
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000624int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200625_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200626 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000627{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200628 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600629
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200630 /* Ensure that _PyEval_InitPendingCalls() was called
631 and that _PyEval_FiniPendingCalls() is not called yet. */
632 assert(pending->lock != NULL);
633
Eric Snow842a2f02019-03-15 15:47:51 -0600634 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600636 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700637
Victor Stinnere225beb2019-06-03 18:14:24 +0200638 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000641}
642
Victor Stinner09532fe2019-05-10 23:39:09 +0200643int
644Py_AddPendingCall(int (*func)(void *), void *arg)
645{
Victor Stinner50e6e992020-03-19 02:41:21 +0100646 /* Best-effort to support subinterpreters and calls with the GIL released.
647
648 First attempt _PyThreadState_GET() since it supports subinterpreters.
649
650 If the GIL is released, _PyThreadState_GET() returns NULL . In this
651 case, use PyGILState_GetThisThreadState() which works even if the GIL
652 is released.
653
654 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
655 see bpo-10915 and bpo-15751.
656
Victor Stinner8849e592020-03-18 19:28:53 +0100657 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100658 PyThreadState *tstate = _PyThreadState_GET();
659 if (tstate == NULL) {
660 tstate = PyGILState_GetThisThreadState();
661 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200662
663 PyInterpreterState *interp;
664 if (tstate != NULL) {
665 interp = tstate->interp;
666 }
667 else {
668 /* Last resort: use the main interpreter */
669 interp = _PyRuntime.interpreters.main;
670 }
671 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200672}
673
Eric Snowfdf282d2019-01-11 14:26:55 -0700674static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100675handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700676{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200677 assert(is_tstate_valid(tstate));
678 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700679 return 0;
680 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700681
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200682 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100683 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
684 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100685 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700686 return -1;
687 }
688 return 0;
689}
690
691static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100692make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000693{
Victor Stinnerd8316882020-03-20 14:50:35 +0100694 /* only execute pending calls on main thread */
695 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200696 return 0;
697 }
698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100700 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700701 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700703 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200704 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100705
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200706 /* unsignal before starting to call callbacks, so that any callback
707 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100708 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700709 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100712 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700713 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700714 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 void *arg = NULL;
716
717 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600718 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200719 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600720 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700721
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100722 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700723 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100724 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700725 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700726 res = func(arg);
727 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200728 goto error;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200731
Charles-François Natalif23339a2011-07-23 18:15:43 +0200732 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700733 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200734
735error:
736 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100737 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700738 return res;
739}
740
Eric Snow842a2f02019-03-15 15:47:51 -0600741void
Victor Stinner2b1df452020-01-13 18:46:59 +0100742_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600743{
Eric Snow842a2f02019-03-15 15:47:51 -0600744 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100745 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600746
Victor Stinner50e6e992020-03-19 02:41:21 +0100747 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200748
Eric Snow842a2f02019-03-15 15:47:51 -0600749 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
750 return;
751 }
752
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100753 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200754 PyObject *exc, *val, *tb;
755 _PyErr_Fetch(tstate, &exc, &val, &tb);
756 PyErr_BadInternalCall();
757 _PyErr_ChainExceptions(exc, val, tb);
758 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600759 }
760}
761
Eric Snowfdf282d2019-01-11 14:26:55 -0700762/* Py_MakePendingCalls() is a simple wrapper for the sake
763 of backward-compatibility. */
764int
765Py_MakePendingCalls(void)
766{
767 assert(PyGILState_Check());
768
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100769 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100770 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771
Eric Snowfdf282d2019-01-11 14:26:55 -0700772 /* Python signal handler doesn't really queue a callback: it only signals
773 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100774 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700775 if (res != 0) {
776 return res;
777 }
778
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100779 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600780 if (res != 0) {
781 return res;
782 }
783
784 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000785}
786
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000787/* The interpreter's recursion limit */
788
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000789#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200790# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000791#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600792
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600793void
Victor Stinnerdab84232020-03-17 18:56:44 +0100794_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600795{
Victor Stinner7be4e352020-05-05 20:27:47 +0200796#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100797 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200798#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100799}
800
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200801int
Victor Stinnerdab84232020-03-17 18:56:44 +0100802_PyEval_InitState(struct _ceval_state *ceval)
803{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200804 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
805
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200806 struct _pending_calls *pending = &ceval->pending;
807 assert(pending->lock == NULL);
808
809 pending->lock = PyThread_allocate_lock();
810 if (pending->lock == NULL) {
811 return -1;
812 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200813
814#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
815 _gil_initialize(&ceval->gil);
816#endif
817
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200818 return 0;
819}
820
821void
822_PyEval_FiniState(struct _ceval_state *ceval)
823{
824 struct _pending_calls *pending = &ceval->pending;
825 if (pending->lock != NULL) {
826 PyThread_free_lock(pending->lock);
827 pending->lock = NULL;
828 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600829}
830
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000831int
832Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000833{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200834 PyInterpreterState *interp = _PyInterpreterState_GET();
835 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000836}
837
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000838void
839Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000840{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200841 PyThreadState *tstate = _PyThreadState_GET();
842 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000843}
844
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100845/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200846 if the recursion_depth reaches recursion_limit.
847 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000848 to guarantee that _Py_CheckRecursiveCall() is regularly called.
849 Without USE_STACKCHECK, there is no need for this. */
850int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100851_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000852{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200853 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000854
855#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700856 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (PyOS_CheckStack()) {
858 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200859 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return -1;
861 }
pdox18967932017-10-25 23:03:01 -0700862#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000863 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (tstate->recursion_depth > recursion_limit + 50) {
865 /* Overflowing while handling an overflow. Give up. */
866 Py_FatalError("Cannot recover from stack overflow.");
867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000869 else {
870 if (tstate->recursion_depth > recursion_limit) {
871 tstate->recursion_headroom++;
872 _PyErr_Format(tstate, PyExc_RecursionError,
873 "maximum recursion depth exceeded%s",
874 where);
875 tstate->recursion_headroom--;
876 --tstate->recursion_depth;
877 return -1;
878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 }
880 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000881}
882
Victor Stinner09532fe2019-05-10 23:39:09 +0200883static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200884static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000885
Victor Stinnere225beb2019-06-03 18:14:24 +0200886#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000887
Guido van Rossum374a9221991-04-04 10:40:29 +0000888
Guido van Rossumb209a111997-04-29 18:18:01 +0000889PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000890PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000891{
Mark Shannon0332e562021-02-01 10:42:03 +0000892 if (locals == NULL) {
893 locals = globals;
894 }
895 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
896 if (builtins == NULL) {
897 return NULL;
898 }
899 PyFrameConstructor desc = {
900 .fc_globals = globals,
901 .fc_builtins = builtins,
902 .fc_name = ((PyCodeObject *)co)->co_name,
903 .fc_qualname = ((PyCodeObject *)co)->co_name,
904 .fc_code = co,
905 .fc_defaults = NULL,
906 .fc_kwdefaults = NULL,
907 .fc_closure = NULL
908 };
909 PyThreadState *tstate = PyThreadState_GET();
Victor Stinner44085a32021-02-18 19:20:16 +0100910 PyObject *res = _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Mark Shannon0332e562021-02-01 10:42:03 +0000911 Py_DECREF(builtins);
912 return res;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000913}
914
915
916/* Interpreter main loop */
917
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000918PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100919PyEval_EvalFrame(PyFrameObject *f)
920{
Victor Stinner0b72b232020-03-12 23:18:39 +0100921 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100922 PyThreadState *tstate = _PyThreadState_GET();
923 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000924}
925
926PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000928{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100929 PyThreadState *tstate = _PyThreadState_GET();
930 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700931}
932
Victor Stinnerda2914d2020-03-20 09:29:08 +0100933
934/* Handle signals, pending calls, GIL drop request
935 and asynchronous exception */
936static int
937eval_frame_handle_pending(PyThreadState *tstate)
938{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100939 _PyRuntimeState * const runtime = &_PyRuntime;
940 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200941
942 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200943 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100944 if (handle_signals(tstate) != 0) {
945 return -1;
946 }
947 }
948
949 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200950 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100951 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100952 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100953 return -1;
954 }
955 }
956
957 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200958 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100959 /* Give another thread a chance */
960 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
961 Py_FatalError("tstate mix-up");
962 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200963 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100964
965 /* Other threads may run now */
966
967 take_gil(tstate);
968
Victor Stinnere838a932020-05-05 19:56:48 +0200969#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
970 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
971#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100972 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
973 Py_FatalError("orphan tstate");
974 }
Victor Stinnere838a932020-05-05 19:56:48 +0200975#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100976 }
977
978 /* Check for asynchronous exception. */
979 if (tstate->async_exc != NULL) {
980 PyObject *exc = tstate->async_exc;
981 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200982 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100983 _PyErr_SetNone(tstate, exc);
984 Py_DECREF(exc);
985 return -1;
986 }
987
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100988#ifdef MS_WINDOWS
989 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
990 // different thread than the Python thread, in which case
991 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
992 // current Python thread with the correct _Py_ThreadCanHandleSignals()
993 // value. It prevents to interrupt the eval loop at every instruction if
994 // the current Python thread cannot handle signals (if
995 // _Py_ThreadCanHandleSignals() is false).
996 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
997#endif
998
Victor Stinnerda2914d2020-03-20 09:29:08 +0100999 return 0;
1000}
1001
Victor Stinnerc6944e72016-11-11 02:13:35 +01001002PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001003_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001004{
Victor Stinner3026cad2020-06-01 16:02:40 +02001005 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001006
Guido van Rossum950361c1997-01-24 13:49:28 +00001007#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001009#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001010 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001011 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001012 int opcode; /* Current opcode */
1013 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001014 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001016 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001017 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 is true when the line being executed has changed. The
1025 initial values are such as to make this false the first
1026 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001027
Serhiy Storchakaab874002016-09-11 13:48:15 +03001028 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 PyObject *names;
1030 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001031 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001032
Brett Cannon368b4b72012-04-02 12:17:59 -04001033#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001034 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001035#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001036
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037/* Computed GOTOs, or
1038 the-optimization-commonly-but-improperly-known-as-"threaded code"
1039 using gcc's labels-as-values extension
1040 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1041
1042 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001044 combined with a lookup table of jump addresses. However, since the
1045 indirect jump instruction is shared by all opcodes, the CPU will have a
1046 hard time making the right prediction for where to jump next (actually,
1047 it will be always wrong except in the uncommon case of a sequence of
1048 several identical opcodes).
1049
1050 "Threaded code" in contrast, uses an explicit jump table and an explicit
1051 indirect jump instruction at the end of each opcode. Since the jump
1052 instruction is at a different address for each opcode, the CPU will make a
1053 separate prediction for each of these instructions, which is equivalent to
1054 predicting the second opcode of each opcode pair. These predictions have
1055 a much better chance to turn out valid, especially in small bytecode loops.
1056
1057 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001059 and potentially many more instructions (depending on the pipeline width).
1060 A correctly predicted branch, however, is nearly free.
1061
1062 At the time of this writing, the "threaded code" version is up to 15-20%
1063 faster than the normal "switch" version, depending on the compiler and the
1064 CPU architecture.
1065
1066 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1067 because it would render the measurements invalid.
1068
1069
1070 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1071 indirect jumps by sharing them between all opcodes. Such optimizations
1072 can be disabled on gcc by using the -fno-gcse flag (or possibly
1073 -fno-crossjumping).
1074*/
1075
Antoine Pitrou042b1282010-08-13 21:15:58 +00001076#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001077#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001078#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001079#endif
1080
Antoine Pitrou042b1282010-08-13 21:15:58 +00001081#ifdef HAVE_COMPUTED_GOTOS
1082 #ifndef USE_COMPUTED_GOTOS
1083 #define USE_COMPUTED_GOTOS 1
1084 #endif
1085#else
1086 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1087 #error "Computed gotos are not supported on this compiler."
1088 #endif
1089 #undef USE_COMPUTED_GOTOS
1090 #define USE_COMPUTED_GOTOS 0
1091#endif
1092
1093#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001094/* Import the static jump table */
1095#include "opcode_targets.h"
1096
Antoine Pitroub52ec782009-01-25 16:34:23 +00001097#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001098 op: \
1099 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001100
Antoine Pitroub52ec782009-01-25 16:34:23 +00001101#ifdef LLTRACE
1102#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001104 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001106 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001107 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 } \
1109 goto fast_next_opcode; \
1110 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001111#else
1112#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001114 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001116 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001117 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 } \
1119 goto fast_next_opcode; \
1120 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001121#endif
1122
Victor Stinner09532fe2019-05-10 23:39:09 +02001123#define DISPATCH() \
1124 { \
1125 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1126 FAST_DISPATCH(); \
1127 } \
1128 continue; \
1129 }
1130
Antoine Pitroub52ec782009-01-25 16:34:23 +00001131#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001132#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001133#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001134#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001135#endif
1136
1137
Neal Norwitza81d2202002-07-14 00:27:26 +00001138/* Tuple access macros */
1139
1140#ifndef Py_DEBUG
1141#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1142#else
1143#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1144#endif
1145
Guido van Rossum374a9221991-04-04 10:40:29 +00001146/* Code access macros */
1147
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001148/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001149#define INSTR_OFFSET() \
1150 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001151#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001152 _Py_CODEUNIT word = *next_instr; \
1153 opcode = _Py_OPCODE(word); \
1154 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001155 next_instr++; \
1156 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001157#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1158#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001159
Raymond Hettingerf606f872003-03-16 03:11:04 +00001160/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 Some opcodes tend to come in pairs thus making it possible to
1162 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001163 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 Verifying the prediction costs a single high-speed test of a register
1166 variable against a constant. If the pairing was good, then the
1167 processor's own internal branch predication has a high likelihood of
1168 success, resulting in a nearly zero-overhead transition to the
1169 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001170 including its unpredictable switch-case branch. Combined with the
1171 processor's internal branch prediction, a successful PREDICT has the
1172 effect of making the two opcodes run as if they were a single new opcode
1173 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001174
Georg Brandl86b2fb92008-07-16 03:43:04 +00001175 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 predictions turned-on and interpret the results as if some opcodes
1177 had been combined or turn-off predictions so that the opcode frequency
1178 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001179
1180 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 the CPU to record separate branch prediction information for each
1182 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001183
Raymond Hettingerf606f872003-03-16 03:11:04 +00001184*/
1185
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001186#define PREDICT_ID(op) PRED_##op
1187
Antoine Pitrou042b1282010-08-13 21:15:58 +00001188#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001189#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001190#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001191#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001192 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001193 _Py_CODEUNIT word = *next_instr; \
1194 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001195 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001196 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001197 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001198 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001199 } \
1200 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001201#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001202#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001203
Raymond Hettingerf606f872003-03-16 03:11:04 +00001204
Guido van Rossum374a9221991-04-04 10:40:29 +00001205/* Stack manipulation macros */
1206
Martin v. Löwis18e16552006-02-15 17:27:45 +00001207/* The stack can grow at most MAXINT deep, as co_nlocals and
1208 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001209#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1210#define EMPTY() (STACK_LEVEL() == 0)
1211#define TOP() (stack_pointer[-1])
1212#define SECOND() (stack_pointer[-2])
1213#define THIRD() (stack_pointer[-3])
1214#define FOURTH() (stack_pointer[-4])
1215#define PEEK(n) (stack_pointer[-(n)])
1216#define SET_TOP(v) (stack_pointer[-1] = (v))
1217#define SET_SECOND(v) (stack_pointer[-2] = (v))
1218#define SET_THIRD(v) (stack_pointer[-3] = (v))
1219#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001220#define BASIC_STACKADJ(n) (stack_pointer += n)
1221#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1222#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001223
Guido van Rossum96a42c81992-01-12 02:29:51 +00001224#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001226 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001227 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001228#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001229 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001230#define STACK_GROW(n) do { \
1231 assert(n >= 0); \
1232 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001233 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001234 assert(STACK_LEVEL() <= co->co_stacksize); \
1235 } while (0)
1236#define STACK_SHRINK(n) do { \
1237 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001238 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001239 (void)(BASIC_STACKADJ(-n)); \
1240 assert(STACK_LEVEL() <= co->co_stacksize); \
1241 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001242#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001243 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001244 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001245#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001246#define PUSH(v) BASIC_PUSH(v)
1247#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001248#define STACK_GROW(n) BASIC_STACKADJ(n)
1249#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001250#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001251#endif
1252
Guido van Rossum681d79a1995-07-18 14:51:37 +00001253/* Local variable macros */
1254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001256
1257/* The SETLOCAL() macro must not DECREF the local variable in-place and
1258 then store the new value; it must copy the old value to a temporary
1259 value, then store the new value, and then DECREF the temporary value.
1260 This is because it is possible that during the DECREF the frame is
1261 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1262 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001264 GETLOCAL(i) = value; \
1265 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001266
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001267
1268#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 while (STACK_LEVEL() > (b)->b_level) { \
1270 PyObject *v = POP(); \
1271 Py_XDECREF(v); \
1272 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001273
1274#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001275 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001277 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1279 while (STACK_LEVEL() > (b)->b_level + 3) { \
1280 value = POP(); \
1281 Py_XDECREF(value); \
1282 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001283 exc_info = tstate->exc_info; \
1284 type = exc_info->exc_type; \
1285 value = exc_info->exc_value; \
1286 traceback = exc_info->exc_traceback; \
1287 exc_info->exc_type = POP(); \
1288 exc_info->exc_value = POP(); \
1289 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 Py_XDECREF(type); \
1291 Py_XDECREF(value); \
1292 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001293 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001294
Inada Naoki91234a12019-06-03 21:30:58 +09001295 /* macros for opcode cache */
1296#define OPCACHE_CHECK() \
1297 do { \
1298 co_opcache = NULL; \
1299 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001300 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001301 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001302 if (co_opcache_offset > 0) { \
1303 assert(co_opcache_offset <= co->co_opcache_size); \
1304 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001305 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001306 } \
1307 } \
1308 } while (0)
1309
Pablo Galindo109826c2020-10-20 06:22:44 +01001310#define OPCACHE_DEOPT() \
1311 do { \
1312 if (co_opcache != NULL) { \
1313 co_opcache->optimized = -1; \
1314 unsigned char co_opcache_offset = \
1315 co->co_opcache_map[next_instr - first_instr]; \
1316 assert(co_opcache_offset <= co->co_opcache_size); \
1317 co->co_opcache_map[co_opcache_offset] = 0; \
1318 co_opcache = NULL; \
1319 } \
1320 } while (0)
1321
1322#define OPCACHE_DEOPT_LOAD_ATTR() \
1323 do { \
1324 if (co_opcache != NULL) { \
1325 OPCACHE_STAT_ATTR_DEOPT(); \
1326 OPCACHE_DEOPT(); \
1327 } \
1328 } while (0)
1329
1330#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1331 do { \
1332 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1333 OPCACHE_DEOPT_LOAD_ATTR(); \
1334 } \
1335 } while (0)
1336
Inada Naoki91234a12019-06-03 21:30:58 +09001337#if OPCACHE_STATS
1338
1339#define OPCACHE_STAT_GLOBAL_HIT() \
1340 do { \
1341 if (co->co_opcache != NULL) opcache_global_hits++; \
1342 } while (0)
1343
1344#define OPCACHE_STAT_GLOBAL_MISS() \
1345 do { \
1346 if (co->co_opcache != NULL) opcache_global_misses++; \
1347 } while (0)
1348
1349#define OPCACHE_STAT_GLOBAL_OPT() \
1350 do { \
1351 if (co->co_opcache != NULL) opcache_global_opts++; \
1352 } while (0)
1353
Pablo Galindo109826c2020-10-20 06:22:44 +01001354#define OPCACHE_STAT_ATTR_HIT() \
1355 do { \
1356 if (co->co_opcache != NULL) opcache_attr_hits++; \
1357 } while (0)
1358
1359#define OPCACHE_STAT_ATTR_MISS() \
1360 do { \
1361 if (co->co_opcache != NULL) opcache_attr_misses++; \
1362 } while (0)
1363
1364#define OPCACHE_STAT_ATTR_OPT() \
1365 do { \
1366 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1367 } while (0)
1368
1369#define OPCACHE_STAT_ATTR_DEOPT() \
1370 do { \
1371 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1372 } while (0)
1373
1374#define OPCACHE_STAT_ATTR_TOTAL() \
1375 do { \
1376 if (co->co_opcache != NULL) opcache_attr_total++; \
1377 } while (0)
1378
Inada Naoki91234a12019-06-03 21:30:58 +09001379#else /* OPCACHE_STATS */
1380
1381#define OPCACHE_STAT_GLOBAL_HIT()
1382#define OPCACHE_STAT_GLOBAL_MISS()
1383#define OPCACHE_STAT_GLOBAL_OPT()
1384
Pablo Galindo109826c2020-10-20 06:22:44 +01001385#define OPCACHE_STAT_ATTR_HIT()
1386#define OPCACHE_STAT_ATTR_MISS()
1387#define OPCACHE_STAT_ATTR_OPT()
1388#define OPCACHE_STAT_ATTR_DEOPT()
1389#define OPCACHE_STAT_ATTR_TOTAL()
1390
Inada Naoki91234a12019-06-03 21:30:58 +09001391#endif
1392
Guido van Rossuma027efa1997-05-05 20:56:21 +00001393/* Start of code */
1394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001396 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001398 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001401 co = f->f_code;
1402 PyCodeAddressRange bounds;
1403 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (tstate->use_tracing) {
1406 if (tstate->c_tracefunc != NULL) {
1407 /* tstate->c_tracefunc, if defined, is a
1408 function that will be called on *every* entry
1409 to a code block. Its return value, if not
1410 None, is a function that will be called at
1411 the start of each executed line of code.
1412 (Actually, the function must return itself
1413 in order to continue tracing.) The trace
1414 functions are called with three arguments:
1415 a pointer to the current frame, a string
1416 indicating why the function is called, and
1417 an argument which depends on the situation.
1418 The global trace function is also called
1419 whenever an exception is detected. */
1420 if (call_trace_protected(tstate->c_tracefunc,
1421 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001422 tstate, f, &bounds,
1423 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 /* Trace function raised an error */
1425 goto exit_eval_frame;
1426 }
1427 }
1428 if (tstate->c_profilefunc != NULL) {
1429 /* Similar for c_profilefunc, except it needn't
1430 return itself and isn't called for "line" events */
1431 if (call_trace_protected(tstate->c_profilefunc,
1432 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001433 tstate, f, &bounds,
1434 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 /* Profile function raised an error */
1436 goto exit_eval_frame;
1437 }
1438 }
1439 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001440
Łukasz Langaa785c872016-09-09 17:37:37 -07001441 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1442 dtrace_function_entry(f);
1443
Mark Shannon877df852020-11-12 09:43:29 +00001444 int instr_prev = -1;
1445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 names = co->co_names;
1447 consts = co->co_consts;
1448 fastlocals = f->f_localsplus;
1449 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001450 assert(PyBytes_Check(co->co_code));
1451 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001452 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1453 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1454 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001455 /*
1456 f->f_lasti refers to the index of the last instruction,
1457 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001458
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001459 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001460 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 When the PREDICT() macros are enabled, some opcode pairs follow in
1463 direct succession without updating f->f_lasti. A successful
1464 prediction effectively links the two codes together as if they
1465 were a single new opcode; accordingly,f->f_lasti will point to
1466 the first code in the pair (for instance, GET_ITER followed by
1467 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001468 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001470 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001471 next_instr = first_instr;
1472 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001473 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1474 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001475 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001476 stack_pointer = f->f_valuestack + f->f_stackdepth;
1477 /* Set f->f_stackdepth to -1.
1478 * Update when returning or calling trace function.
1479 Having f_stackdepth <= 0 ensures that invalid
1480 values are not visible to the cycle GC.
1481 We choose -1 rather than 0 to assist debugging.
1482 */
1483 f->f_stackdepth = -1;
1484 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001485
Inada Naoki91234a12019-06-03 21:30:58 +09001486 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1487 co->co_opcache_flag++;
1488 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1489 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001490 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001491 }
1492#if OPCACHE_STATS
1493 opcache_code_objects_extra_mem +=
1494 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1495 sizeof(_PyOpcache) * co->co_opcache_size;
1496 opcache_code_objects++;
1497#endif
1498 }
1499 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001500
Tim Peters5ca576e2001-06-18 22:08:13 +00001501#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001502 {
1503 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1504 if (r < 0) {
1505 goto exit_eval_frame;
1506 }
1507 lltrace = r;
1508 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001509#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001511 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001513 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001514
Victor Stinnerace47d72013-07-18 01:41:08 +02001515#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001516 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001517 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001518 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001519 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001520#endif
1521
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001522main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1525 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001526 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 /* Do periodic things. Doing this every time through
1529 the loop would add too much overhead, so we do it
1530 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001531 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 event needs attention (e.g. a signal handler or
1533 async I/O handler); see Py_AddPendingCall() and
1534 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Eric Snow7bda9de2019-03-08 17:25:54 -07001536 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001537 opcode = _Py_OPCODE(*next_instr);
1538 if (opcode == SETUP_FINALLY ||
1539 opcode == SETUP_WITH ||
1540 opcode == BEFORE_ASYNC_WITH ||
1541 opcode == YIELD_FROM) {
1542 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001543 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001544 - If we're about to enter the 'with:'. It will prevent
1545 emitting a resource warning in the common idiom
1546 'with open(path) as file:'.
1547 - If we're about to enter the 'async with:'.
1548 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001549 *very* useful, but might help in some cases and it's
1550 traditional)
1551 - If we're resuming a chain of nested 'yield from' or
1552 'await' calls, then each frame is parked with YIELD_FROM
1553 as its next opcode. If the user hit control-C we want to
1554 wait until we've reached the innermost frame before
1555 running the signal handler and raising KeyboardInterrupt
1556 (see bpo-30039).
1557 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 goto fast_next_opcode;
1559 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001560
Victor Stinnerda2914d2020-03-20 09:29:08 +01001561 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 }
1564 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 fast_next_opcode:
1567 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Łukasz Langaa785c872016-09-09 17:37:37 -07001569 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001570 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001573
Victor Stinnerdab84232020-03-17 18:56:44 +01001574 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001575 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001577 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001579 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 err = maybe_call_line_trace(tstate->c_tracefunc,
1582 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001583 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001584 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /* Reload possibly changed frame fields */
1586 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001587 stack_pointer = f->f_valuestack+f->f_stackdepth;
1588 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001595
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001596 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001597 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001598#ifdef DYNAMIC_EXECUTION_PROFILE
1599#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 dxpairs[lastopcode][opcode]++;
1601 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001602#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001604#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001605
Guido van Rossum96a42c81992-01-12 02:29:51 +00001606#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (lltrace) {
1610 if (HAS_ARG(opcode)) {
1611 printf("%d: %d, %d\n",
1612 f->f_lasti, opcode, oparg);
1613 }
1614 else {
1615 printf("%d: %d\n",
1616 f->f_lasti, opcode);
1617 }
1618 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001619#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001624 It is essential that any operation that fails must goto error
1625 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001629 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001630
Benjamin Petersonddd19492018-09-16 22:38:02 -07001631 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 PyObject *value = GETLOCAL(oparg);
1633 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001634 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 UNBOUNDLOCAL_ERROR_MSG,
1636 PyTuple_GetItem(co->co_varnames, oparg));
1637 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 Py_INCREF(value);
1640 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 }
1643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(LOAD_CONST): {
1645 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 PyObject *value = GETITEM(consts, oparg);
1647 Py_INCREF(value);
1648 PUSH(value);
1649 FAST_DISPATCH();
1650 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(STORE_FAST): {
1653 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *value = POP();
1655 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001658
Benjamin Petersonddd19492018-09-16 22:38:02 -07001659 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 PyObject *value = POP();
1661 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Benjamin Petersonddd19492018-09-16 22:38:02 -07001665 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 PyObject *top = TOP();
1667 PyObject *second = SECOND();
1668 SET_TOP(second);
1669 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *top = TOP();
1675 PyObject *second = SECOND();
1676 PyObject *third = THIRD();
1677 SET_TOP(second);
1678 SET_SECOND(third);
1679 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Benjamin Petersonddd19492018-09-16 22:38:02 -07001683 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001684 PyObject *top = TOP();
1685 PyObject *second = SECOND();
1686 PyObject *third = THIRD();
1687 PyObject *fourth = FOURTH();
1688 SET_TOP(second);
1689 SET_SECOND(third);
1690 SET_THIRD(fourth);
1691 SET_FOURTH(top);
1692 FAST_DISPATCH();
1693 }
1694
Benjamin Petersonddd19492018-09-16 22:38:02 -07001695 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 PyObject *top = TOP();
1697 Py_INCREF(top);
1698 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Petersonddd19492018-09-16 22:38:02 -07001702 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001705 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001707 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 SET_TOP(top);
1709 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001710 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001712
Benjamin Petersonddd19492018-09-16 22:38:02 -07001713 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001714 PyObject *value = TOP();
1715 PyObject *res = PyNumber_Positive(value);
1716 Py_DECREF(value);
1717 SET_TOP(res);
1718 if (res == NULL)
1719 goto error;
1720 DISPATCH();
1721 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Benjamin Petersonddd19492018-09-16 22:38:02 -07001723 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 PyObject *value = TOP();
1725 PyObject *res = PyNumber_Negative(value);
1726 Py_DECREF(value);
1727 SET_TOP(res);
1728 if (res == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *value = TOP();
1735 int err = PyObject_IsTrue(value);
1736 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (err == 0) {
1738 Py_INCREF(Py_True);
1739 SET_TOP(Py_True);
1740 DISPATCH();
1741 }
1742 else if (err > 0) {
1743 Py_INCREF(Py_False);
1744 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 DISPATCH();
1746 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001747 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 goto error;
1749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 PyObject *value = TOP();
1753 PyObject *res = PyNumber_Invert(value);
1754 Py_DECREF(value);
1755 SET_TOP(res);
1756 if (res == NULL)
1757 goto error;
1758 DISPATCH();
1759 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Benjamin Petersonddd19492018-09-16 22:38:02 -07001761 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 PyObject *exp = POP();
1763 PyObject *base = TOP();
1764 PyObject *res = PyNumber_Power(base, exp, Py_None);
1765 Py_DECREF(base);
1766 Py_DECREF(exp);
1767 SET_TOP(res);
1768 if (res == NULL)
1769 goto error;
1770 DISPATCH();
1771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Benjamin Petersonddd19492018-09-16 22:38:02 -07001773 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 PyObject *right = POP();
1775 PyObject *left = TOP();
1776 PyObject *res = PyNumber_Multiply(left, right);
1777 Py_DECREF(left);
1778 Py_DECREF(right);
1779 SET_TOP(res);
1780 if (res == NULL)
1781 goto error;
1782 DISPATCH();
1783 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001784
Benjamin Petersonddd19492018-09-16 22:38:02 -07001785 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001786 PyObject *right = POP();
1787 PyObject *left = TOP();
1788 PyObject *res = PyNumber_MatrixMultiply(left, right);
1789 Py_DECREF(left);
1790 Py_DECREF(right);
1791 SET_TOP(res);
1792 if (res == NULL)
1793 goto error;
1794 DISPATCH();
1795 }
1796
Benjamin Petersonddd19492018-09-16 22:38:02 -07001797 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 PyObject *divisor = POP();
1799 PyObject *dividend = TOP();
1800 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1801 Py_DECREF(dividend);
1802 Py_DECREF(divisor);
1803 SET_TOP(quotient);
1804 if (quotient == NULL)
1805 goto error;
1806 DISPATCH();
1807 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001808
Benjamin Petersonddd19492018-09-16 22:38:02 -07001809 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 PyObject *divisor = POP();
1811 PyObject *dividend = TOP();
1812 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1813 Py_DECREF(dividend);
1814 Py_DECREF(divisor);
1815 SET_TOP(quotient);
1816 if (quotient == NULL)
1817 goto error;
1818 DISPATCH();
1819 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001820
Benjamin Petersonddd19492018-09-16 22:38:02 -07001821 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 PyObject *divisor = POP();
1823 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001824 PyObject *res;
1825 if (PyUnicode_CheckExact(dividend) && (
1826 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1827 // fast path; string formatting, but not if the RHS is a str subclass
1828 // (see issue28598)
1829 res = PyUnicode_Format(dividend, divisor);
1830 } else {
1831 res = PyNumber_Remainder(dividend, divisor);
1832 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 Py_DECREF(divisor);
1834 Py_DECREF(dividend);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001845 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001846 CPython using bytecode, it is simply worthless.
1847 See http://bugs.python.org/issue21955 and
1848 http://bugs.python.org/issue10044 for the discussion. In short,
1849 no patch shown any impact on a realistic benchmark, only a minor
1850 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 if (PyUnicode_CheckExact(left) &&
1852 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001853 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001854 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001855 }
1856 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 sum = PyNumber_Add(left, right);
1858 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001859 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 Py_DECREF(right);
1861 SET_TOP(sum);
1862 if (sum == NULL)
1863 goto error;
1864 DISPATCH();
1865 }
1866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *right = POP();
1869 PyObject *left = TOP();
1870 PyObject *diff = PyNumber_Subtract(left, right);
1871 Py_DECREF(right);
1872 Py_DECREF(left);
1873 SET_TOP(diff);
1874 if (diff == NULL)
1875 goto error;
1876 DISPATCH();
1877 }
1878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *sub = POP();
1881 PyObject *container = TOP();
1882 PyObject *res = PyObject_GetItem(container, sub);
1883 Py_DECREF(container);
1884 Py_DECREF(sub);
1885 SET_TOP(res);
1886 if (res == NULL)
1887 goto error;
1888 DISPATCH();
1889 }
1890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *right = POP();
1893 PyObject *left = TOP();
1894 PyObject *res = PyNumber_Lshift(left, right);
1895 Py_DECREF(left);
1896 Py_DECREF(right);
1897 SET_TOP(res);
1898 if (res == NULL)
1899 goto error;
1900 DISPATCH();
1901 }
1902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 PyObject *right = POP();
1905 PyObject *left = TOP();
1906 PyObject *res = PyNumber_Rshift(left, right);
1907 Py_DECREF(left);
1908 Py_DECREF(right);
1909 SET_TOP(res);
1910 if (res == NULL)
1911 goto error;
1912 DISPATCH();
1913 }
1914
Benjamin Petersonddd19492018-09-16 22:38:02 -07001915 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 PyObject *right = POP();
1917 PyObject *left = TOP();
1918 PyObject *res = PyNumber_And(left, right);
1919 Py_DECREF(left);
1920 Py_DECREF(right);
1921 SET_TOP(res);
1922 if (res == NULL)
1923 goto error;
1924 DISPATCH();
1925 }
1926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 PyObject *right = POP();
1929 PyObject *left = TOP();
1930 PyObject *res = PyNumber_Xor(left, right);
1931 Py_DECREF(left);
1932 Py_DECREF(right);
1933 SET_TOP(res);
1934 if (res == NULL)
1935 goto error;
1936 DISPATCH();
1937 }
1938
Benjamin Petersonddd19492018-09-16 22:38:02 -07001939 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 PyObject *right = POP();
1941 PyObject *left = TOP();
1942 PyObject *res = PyNumber_Or(left, right);
1943 Py_DECREF(left);
1944 Py_DECREF(right);
1945 SET_TOP(res);
1946 if (res == NULL)
1947 goto error;
1948 DISPATCH();
1949 }
1950
Benjamin Petersonddd19492018-09-16 22:38:02 -07001951 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 PyObject *v = POP();
1953 PyObject *list = PEEK(oparg);
1954 int err;
1955 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 if (err != 0)
1958 goto error;
1959 PREDICT(JUMP_ABSOLUTE);
1960 DISPATCH();
1961 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Benjamin Petersonddd19492018-09-16 22:38:02 -07001963 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001965 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 int err;
1967 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 if (err != 0)
1970 goto error;
1971 PREDICT(JUMP_ABSOLUTE);
1972 DISPATCH();
1973 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001974
Benjamin Petersonddd19492018-09-16 22:38:02 -07001975 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 PyObject *exp = POP();
1977 PyObject *base = TOP();
1978 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1979 Py_DECREF(base);
1980 Py_DECREF(exp);
1981 SET_TOP(res);
1982 if (res == NULL)
1983 goto error;
1984 DISPATCH();
1985 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001986
Benjamin Petersonddd19492018-09-16 22:38:02 -07001987 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 PyObject *right = POP();
1989 PyObject *left = TOP();
1990 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1991 Py_DECREF(left);
1992 Py_DECREF(right);
1993 SET_TOP(res);
1994 if (res == NULL)
1995 goto error;
1996 DISPATCH();
1997 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Benjamin Petersonddd19492018-09-16 22:38:02 -07001999 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002000 PyObject *right = POP();
2001 PyObject *left = TOP();
2002 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2003 Py_DECREF(left);
2004 Py_DECREF(right);
2005 SET_TOP(res);
2006 if (res == NULL)
2007 goto error;
2008 DISPATCH();
2009 }
2010
Benjamin Petersonddd19492018-09-16 22:38:02 -07002011 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 PyObject *divisor = POP();
2013 PyObject *dividend = TOP();
2014 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2015 Py_DECREF(dividend);
2016 Py_DECREF(divisor);
2017 SET_TOP(quotient);
2018 if (quotient == NULL)
2019 goto error;
2020 DISPATCH();
2021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Benjamin Petersonddd19492018-09-16 22:38:02 -07002023 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 PyObject *divisor = POP();
2025 PyObject *dividend = TOP();
2026 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2027 Py_DECREF(dividend);
2028 Py_DECREF(divisor);
2029 SET_TOP(quotient);
2030 if (quotient == NULL)
2031 goto error;
2032 DISPATCH();
2033 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Benjamin Petersonddd19492018-09-16 22:38:02 -07002035 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 PyObject *right = POP();
2037 PyObject *left = TOP();
2038 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2039 Py_DECREF(left);
2040 Py_DECREF(right);
2041 SET_TOP(mod);
2042 if (mod == NULL)
2043 goto error;
2044 DISPATCH();
2045 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Benjamin Petersonddd19492018-09-16 22:38:02 -07002047 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 PyObject *right = POP();
2049 PyObject *left = TOP();
2050 PyObject *sum;
2051 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002052 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002053 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002054 }
2055 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 sum = PyNumber_InPlaceAdd(left, right);
2057 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 Py_DECREF(right);
2060 SET_TOP(sum);
2061 if (sum == NULL)
2062 goto error;
2063 DISPATCH();
2064 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002065
Benjamin Petersonddd19492018-09-16 22:38:02 -07002066 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 PyObject *right = POP();
2068 PyObject *left = TOP();
2069 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2070 Py_DECREF(left);
2071 Py_DECREF(right);
2072 SET_TOP(diff);
2073 if (diff == NULL)
2074 goto error;
2075 DISPATCH();
2076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Benjamin Petersonddd19492018-09-16 22:38:02 -07002078 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 PyObject *right = POP();
2080 PyObject *left = TOP();
2081 PyObject *res = PyNumber_InPlaceLshift(left, right);
2082 Py_DECREF(left);
2083 Py_DECREF(right);
2084 SET_TOP(res);
2085 if (res == NULL)
2086 goto error;
2087 DISPATCH();
2088 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002089
Benjamin Petersonddd19492018-09-16 22:38:02 -07002090 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 PyObject *right = POP();
2092 PyObject *left = TOP();
2093 PyObject *res = PyNumber_InPlaceRshift(left, right);
2094 Py_DECREF(left);
2095 Py_DECREF(right);
2096 SET_TOP(res);
2097 if (res == NULL)
2098 goto error;
2099 DISPATCH();
2100 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Benjamin Petersonddd19492018-09-16 22:38:02 -07002102 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 PyObject *right = POP();
2104 PyObject *left = TOP();
2105 PyObject *res = PyNumber_InPlaceAnd(left, right);
2106 Py_DECREF(left);
2107 Py_DECREF(right);
2108 SET_TOP(res);
2109 if (res == NULL)
2110 goto error;
2111 DISPATCH();
2112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Benjamin Petersonddd19492018-09-16 22:38:02 -07002114 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 PyObject *right = POP();
2116 PyObject *left = TOP();
2117 PyObject *res = PyNumber_InPlaceXor(left, right);
2118 Py_DECREF(left);
2119 Py_DECREF(right);
2120 SET_TOP(res);
2121 if (res == NULL)
2122 goto error;
2123 DISPATCH();
2124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 PyObject *right = POP();
2128 PyObject *left = TOP();
2129 PyObject *res = PyNumber_InPlaceOr(left, right);
2130 Py_DECREF(left);
2131 Py_DECREF(right);
2132 SET_TOP(res);
2133 if (res == NULL)
2134 goto error;
2135 DISPATCH();
2136 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002137
Benjamin Petersonddd19492018-09-16 22:38:02 -07002138 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 PyObject *sub = TOP();
2140 PyObject *container = SECOND();
2141 PyObject *v = THIRD();
2142 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002143 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002144 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 Py_DECREF(container);
2148 Py_DECREF(sub);
2149 if (err != 0)
2150 goto error;
2151 DISPATCH();
2152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002153
Benjamin Petersonddd19492018-09-16 22:38:02 -07002154 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 PyObject *sub = TOP();
2156 PyObject *container = SECOND();
2157 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002158 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002159 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 err = PyObject_DelItem(container, sub);
2161 Py_DECREF(container);
2162 Py_DECREF(sub);
2163 if (err != 0)
2164 goto error;
2165 DISPATCH();
2166 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002169 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002171 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002172 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002174 _PyErr_SetString(tstate, PyExc_RuntimeError,
2175 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(value);
2177 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002179 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 Py_DECREF(value);
2181 if (res == NULL)
2182 goto error;
2183 Py_DECREF(res);
2184 DISPATCH();
2185 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002186
Benjamin Petersonddd19492018-09-16 22:38:02 -07002187 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 switch (oparg) {
2190 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002192 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002195 /* fall through */
2196 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002197 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002198 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 break;
2201 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002202 _PyErr_SetString(tstate, PyExc_SystemError,
2203 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 break;
2205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 goto error;
2207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Petersonddd19492018-09-16 22:38:02 -07002209 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002211 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002212 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002213 f->f_state = FRAME_RETURNED;
2214 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002215 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002219 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002220 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002221 PyObject *obj = TOP();
2222 PyTypeObject *type = Py_TYPE(obj);
2223
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002224 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002225 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002226 }
Yury Selivanov75445082015-05-11 22:57:16 -04002227
2228 if (getter != NULL) {
2229 iter = (*getter)(obj);
2230 Py_DECREF(obj);
2231 if (iter == NULL) {
2232 SET_TOP(NULL);
2233 goto error;
2234 }
2235 }
2236 else {
2237 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002238 _PyErr_Format(tstate, PyExc_TypeError,
2239 "'async for' requires an object with "
2240 "__aiter__ method, got %.100s",
2241 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002242 Py_DECREF(obj);
2243 goto error;
2244 }
2245
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002246 if (Py_TYPE(iter)->tp_as_async == NULL ||
2247 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002248
Yury Selivanov398ff912017-03-02 22:20:00 -05002249 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002250 _PyErr_Format(tstate, PyExc_TypeError,
2251 "'async for' received an object from __aiter__ "
2252 "that does not implement __anext__: %.100s",
2253 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002254 Py_DECREF(iter);
2255 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002256 }
2257
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002258 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002259 DISPATCH();
2260 }
2261
Benjamin Petersonddd19492018-09-16 22:38:02 -07002262 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002263 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002264 PyObject *next_iter = NULL;
2265 PyObject *awaitable = NULL;
2266 PyObject *aiter = TOP();
2267 PyTypeObject *type = Py_TYPE(aiter);
2268
Yury Selivanoveb636452016-09-08 22:01:51 -07002269 if (PyAsyncGen_CheckExact(aiter)) {
2270 awaitable = type->tp_as_async->am_anext(aiter);
2271 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002272 goto error;
2273 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002274 } else {
2275 if (type->tp_as_async != NULL){
2276 getter = type->tp_as_async->am_anext;
2277 }
Yury Selivanov75445082015-05-11 22:57:16 -04002278
Yury Selivanoveb636452016-09-08 22:01:51 -07002279 if (getter != NULL) {
2280 next_iter = (*getter)(aiter);
2281 if (next_iter == NULL) {
2282 goto error;
2283 }
2284 }
2285 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002286 _PyErr_Format(tstate, PyExc_TypeError,
2287 "'async for' requires an iterator with "
2288 "__anext__ method, got %.100s",
2289 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002290 goto error;
2291 }
Yury Selivanov75445082015-05-11 22:57:16 -04002292
Yury Selivanoveb636452016-09-08 22:01:51 -07002293 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2294 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002295 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002296 PyExc_TypeError,
2297 "'async for' received an invalid object "
2298 "from __anext__: %.100s",
2299 Py_TYPE(next_iter)->tp_name);
2300
2301 Py_DECREF(next_iter);
2302 goto error;
2303 } else {
2304 Py_DECREF(next_iter);
2305 }
2306 }
Yury Selivanov75445082015-05-11 22:57:16 -04002307
2308 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002309 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002310 DISPATCH();
2311 }
2312
Benjamin Petersonddd19492018-09-16 22:38:02 -07002313 case TARGET(GET_AWAITABLE): {
2314 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002315 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002316 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002317
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002318 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002319 int opcode_at_minus_3 = 0;
2320 if ((next_instr - first_instr) > 2) {
2321 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2322 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002323 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002324 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002325 _Py_OPCODE(next_instr[-2]));
2326 }
2327
Yury Selivanov75445082015-05-11 22:57:16 -04002328 Py_DECREF(iterable);
2329
Yury Selivanovc724bae2016-03-02 11:30:46 -05002330 if (iter != NULL && PyCoro_CheckExact(iter)) {
2331 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2332 if (yf != NULL) {
2333 /* `iter` is a coroutine object that is being
2334 awaited, `yf` is a pointer to the current awaitable
2335 being awaited on. */
2336 Py_DECREF(yf);
2337 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002338 _PyErr_SetString(tstate, PyExc_RuntimeError,
2339 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002340 /* The code below jumps to `error` if `iter` is NULL. */
2341 }
2342 }
2343
Yury Selivanov75445082015-05-11 22:57:16 -04002344 SET_TOP(iter); /* Even if it's NULL */
2345
2346 if (iter == NULL) {
2347 goto error;
2348 }
2349
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002350 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002351 DISPATCH();
2352 }
2353
Benjamin Petersonddd19492018-09-16 22:38:02 -07002354 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002356 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002357 PySendResult gen_status;
2358 if (tstate->c_tracefunc == NULL) {
2359 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002360 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002361 _Py_IDENTIFIER(send);
2362 if (v == Py_None && PyIter_Check(receiver)) {
2363 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002364 }
2365 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002366 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002367 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002368 if (retval == NULL) {
2369 if (tstate->c_tracefunc != NULL
2370 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002371 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002372 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2373 gen_status = PYGEN_RETURN;
2374 }
2375 else {
2376 gen_status = PYGEN_ERROR;
2377 }
2378 }
2379 else {
2380 gen_status = PYGEN_NEXT;
2381 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002382 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002384 if (gen_status == PYGEN_ERROR) {
2385 assert (retval == NULL);
2386 goto error;
2387 }
2388 if (gen_status == PYGEN_RETURN) {
2389 assert (retval != NULL);
2390
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002391 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002392 SET_TOP(retval);
2393 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002395 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002396 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002397 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002398 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002399 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002400 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002401 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002402 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002403 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002408
2409 if (co->co_flags & CO_ASYNC_GENERATOR) {
2410 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2411 Py_DECREF(retval);
2412 if (w == NULL) {
2413 retval = NULL;
2414 goto error;
2415 }
2416 retval = w;
2417 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002418 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002419 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002420 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002422
Benjamin Petersonddd19492018-09-16 22:38:02 -07002423 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002424 PyObject *type, *value, *traceback;
2425 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 PyTryBlock *b = PyFrame_BlockPop(f);
2427 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002428 _PyErr_SetString(tstate, PyExc_SystemError,
2429 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002432 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2433 STACK_LEVEL() <= (b)->b_level + 4);
2434 exc_info = tstate->exc_info;
2435 type = exc_info->exc_type;
2436 value = exc_info->exc_value;
2437 traceback = exc_info->exc_traceback;
2438 exc_info->exc_type = POP();
2439 exc_info->exc_value = POP();
2440 exc_info->exc_traceback = POP();
2441 Py_XDECREF(type);
2442 Py_XDECREF(value);
2443 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002446
Benjamin Petersonddd19492018-09-16 22:38:02 -07002447 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002448 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Mark Shannonfee55262019-11-21 09:11:43 +00002452 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002453 assert(f->f_iblock > 0);
2454 if (oparg) {
2455 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2456 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002457 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002458 PyObject *val = POP();
2459 PyObject *tb = POP();
2460 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002461 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002462 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Benjamin Petersonddd19492018-09-16 22:38:02 -07002465 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002466 PyObject *exc = POP();
2467 assert(PyExceptionClass_Check(exc));
2468 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2469 PyTryBlock *b = PyFrame_BlockPop(f);
2470 assert(b->b_type == EXCEPT_HANDLER);
2471 Py_DECREF(exc);
2472 UNWIND_EXCEPT_HANDLER(b);
2473 Py_DECREF(POP());
2474 JUMPBY(oparg);
2475 FAST_DISPATCH();
2476 }
2477 else {
2478 PyObject *val = POP();
2479 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002480 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002481 goto exception_unwind;
2482 }
2483 }
2484
Zackery Spytzce6a0702019-08-25 03:44:09 -06002485 case TARGET(LOAD_ASSERTION_ERROR): {
2486 PyObject *value = PyExc_AssertionError;
2487 Py_INCREF(value);
2488 PUSH(value);
2489 FAST_DISPATCH();
2490 }
2491
Benjamin Petersonddd19492018-09-16 22:38:02 -07002492 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002493 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002496 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002497 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002499 if (!_PyErr_Occurred(tstate)) {
2500 _PyErr_SetString(tstate, PyExc_NameError,
2501 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002502 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002506 }
2507 else {
2508 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2509 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002510 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2512 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002513 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2514 _PyErr_SetString(tstate, PyExc_NameError,
2515 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002520 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002522
Benjamin Petersonddd19492018-09-16 22:38:02 -07002523 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 PyObject *name = GETITEM(names, oparg);
2525 PyObject *v = POP();
2526 PyObject *ns = f->f_locals;
2527 int err;
2528 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 _PyErr_Format(tstate, PyExc_SystemError,
2530 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 if (PyDict_CheckExact(ns))
2535 err = PyDict_SetItem(ns, name, v);
2536 else
2537 err = PyObject_SetItem(ns, name, v);
2538 Py_DECREF(v);
2539 if (err != 0)
2540 goto error;
2541 DISPATCH();
2542 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002543
Benjamin Petersonddd19492018-09-16 22:38:02 -07002544 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 PyObject *name = GETITEM(names, oparg);
2546 PyObject *ns = f->f_locals;
2547 int err;
2548 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002549 _PyErr_Format(tstate, PyExc_SystemError,
2550 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 err = PyObject_DelItem(ns, name);
2554 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002555 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 NAME_ERROR_MSG,
2557 name);
2558 goto error;
2559 }
2560 DISPATCH();
2561 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002562
Benjamin Petersonddd19492018-09-16 22:38:02 -07002563 case TARGET(UNPACK_SEQUENCE): {
2564 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 PyObject *seq = POP(), *item, **items;
2566 if (PyTuple_CheckExact(seq) &&
2567 PyTuple_GET_SIZE(seq) == oparg) {
2568 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 item = items[oparg];
2571 Py_INCREF(item);
2572 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 } else if (PyList_CheckExact(seq) &&
2575 PyList_GET_SIZE(seq) == oparg) {
2576 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 item = items[oparg];
2579 Py_INCREF(item);
2580 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002582 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002584 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 } else {
2586 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 Py_DECREF(seq);
2588 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002591 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002593
Benjamin Petersonddd19492018-09-16 22:38:02 -07002594 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2596 PyObject *seq = POP();
2597
Victor Stinner438a12d2019-05-24 17:01:38 +02002598 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 stack_pointer + totalargs)) {
2600 stack_pointer += totalargs;
2601 } else {
2602 Py_DECREF(seq);
2603 goto error;
2604 }
2605 Py_DECREF(seq);
2606 DISPATCH();
2607 }
2608
Benjamin Petersonddd19492018-09-16 22:38:02 -07002609 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PyObject *name = GETITEM(names, oparg);
2611 PyObject *owner = TOP();
2612 PyObject *v = SECOND();
2613 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002614 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 Py_DECREF(owner);
2618 if (err != 0)
2619 goto error;
2620 DISPATCH();
2621 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002622
Benjamin Petersonddd19492018-09-16 22:38:02 -07002623 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 PyObject *name = GETITEM(names, oparg);
2625 PyObject *owner = POP();
2626 int err;
2627 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2628 Py_DECREF(owner);
2629 if (err != 0)
2630 goto error;
2631 DISPATCH();
2632 }
2633
Benjamin Petersonddd19492018-09-16 22:38:02 -07002634 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 PyObject *name = GETITEM(names, oparg);
2636 PyObject *v = POP();
2637 int err;
2638 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 if (err != 0)
2641 goto error;
2642 DISPATCH();
2643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002644
Benjamin Petersonddd19492018-09-16 22:38:02 -07002645 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 PyObject *name = GETITEM(names, oparg);
2647 int err;
2648 err = PyDict_DelItem(f->f_globals, name);
2649 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002650 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2651 format_exc_check_arg(tstate, PyExc_NameError,
2652 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002653 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002655 }
2656 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002658
Benjamin Petersonddd19492018-09-16 22:38:02 -07002659 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 PyObject *name = GETITEM(names, oparg);
2661 PyObject *locals = f->f_locals;
2662 PyObject *v;
2663 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002664 _PyErr_Format(tstate, PyExc_SystemError,
2665 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002669 v = PyDict_GetItemWithError(locals, name);
2670 if (v != NULL) {
2671 Py_INCREF(v);
2672 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002673 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002674 goto error;
2675 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 }
2677 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002679 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002680 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002681 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002682 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 }
2684 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002686 v = PyDict_GetItemWithError(f->f_globals, name);
2687 if (v != NULL) {
2688 Py_INCREF(v);
2689 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002690 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002691 goto error;
2692 }
2693 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002694 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002695 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002697 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002698 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002699 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002700 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002703 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002705 }
2706 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002707 v = PyObject_GetItem(f->f_builtins, name);
2708 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002709 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002710 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002711 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002713 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002715 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002716 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002724 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002726 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002727 && PyDict_CheckExact(f->f_builtins))
2728 {
Inada Naoki91234a12019-06-03 21:30:58 +09002729 OPCACHE_CHECK();
2730 if (co_opcache != NULL && co_opcache->optimized > 0) {
2731 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2732
2733 if (lg->globals_ver ==
2734 ((PyDictObject *)f->f_globals)->ma_version_tag
2735 && lg->builtins_ver ==
2736 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2737 {
2738 PyObject *ptr = lg->ptr;
2739 OPCACHE_STAT_GLOBAL_HIT();
2740 assert(ptr != NULL);
2741 Py_INCREF(ptr);
2742 PUSH(ptr);
2743 DISPATCH();
2744 }
2745 }
2746
2747 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002749 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 name);
2751 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002752 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002753 /* _PyDict_LoadGlobal() returns NULL without raising
2754 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002755 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002756 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
Inada Naoki91234a12019-06-03 21:30:58 +09002760
2761 if (co_opcache != NULL) {
2762 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2763
2764 if (co_opcache->optimized == 0) {
2765 /* Wasn't optimized before. */
2766 OPCACHE_STAT_GLOBAL_OPT();
2767 } else {
2768 OPCACHE_STAT_GLOBAL_MISS();
2769 }
2770
2771 co_opcache->optimized = 1;
2772 lg->globals_ver =
2773 ((PyDictObject *)f->f_globals)->ma_version_tag;
2774 lg->builtins_ver =
2775 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2776 lg->ptr = v; /* borrowed */
2777 }
2778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002781 else {
2782 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002783
2784 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002785 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 v = PyObject_GetItem(f->f_globals, name);
2787 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002788 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002789 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002790 }
2791 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002792
Victor Stinnerb4efc962015-11-20 09:24:02 +01002793 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 v = PyObject_GetItem(f->f_builtins, name);
2795 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002796 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002797 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002798 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002799 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002800 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002802 }
2803 }
2804 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002808
Benjamin Petersonddd19492018-09-16 22:38:02 -07002809 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 PyObject *v = GETLOCAL(oparg);
2811 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 SETLOCAL(oparg, NULL);
2813 DISPATCH();
2814 }
2815 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002816 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 UNBOUNDLOCAL_ERROR_MSG,
2818 PyTuple_GetItem(co->co_varnames, oparg)
2819 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 goto error;
2821 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002822
Benjamin Petersonddd19492018-09-16 22:38:02 -07002823 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002825 PyObject *oldobj = PyCell_GET(cell);
2826 if (oldobj != NULL) {
2827 PyCell_SET(cell, NULL);
2828 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002829 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002830 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002831 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 goto error;
2833 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002834
Benjamin Petersonddd19492018-09-16 22:38:02 -07002835 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 PyObject *cell = freevars[oparg];
2837 Py_INCREF(cell);
2838 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002841
Benjamin Petersonddd19492018-09-16 22:38:02 -07002842 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002843 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002844 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002845 assert(locals);
2846 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2847 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2848 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2849 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2850 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002851 value = PyDict_GetItemWithError(locals, name);
2852 if (value != NULL) {
2853 Py_INCREF(value);
2854 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002855 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002856 goto error;
2857 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002858 }
2859 else {
2860 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002861 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002862 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002863 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002864 }
2865 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002866 }
2867 }
2868 if (!value) {
2869 PyObject *cell = freevars[oparg];
2870 value = PyCell_GET(cell);
2871 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002872 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002873 goto error;
2874 }
2875 Py_INCREF(value);
2876 }
2877 PUSH(value);
2878 DISPATCH();
2879 }
2880
Benjamin Petersonddd19492018-09-16 22:38:02 -07002881 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 PyObject *cell = freevars[oparg];
2883 PyObject *value = PyCell_GET(cell);
2884 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002885 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 Py_INCREF(value);
2889 PUSH(value);
2890 DISPATCH();
2891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Benjamin Petersonddd19492018-09-16 22:38:02 -07002893 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 PyObject *v = POP();
2895 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002896 PyObject *oldobj = PyCell_GET(cell);
2897 PyCell_SET(cell, v);
2898 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 DISPATCH();
2900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002901
Benjamin Petersonddd19492018-09-16 22:38:02 -07002902 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002903 PyObject *str;
2904 PyObject *empty = PyUnicode_New(0, 0);
2905 if (empty == NULL) {
2906 goto error;
2907 }
2908 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2909 Py_DECREF(empty);
2910 if (str == NULL)
2911 goto error;
2912 while (--oparg >= 0) {
2913 PyObject *item = POP();
2914 Py_DECREF(item);
2915 }
2916 PUSH(str);
2917 DISPATCH();
2918 }
2919
Benjamin Petersonddd19492018-09-16 22:38:02 -07002920 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 PyObject *tup = PyTuple_New(oparg);
2922 if (tup == NULL)
2923 goto error;
2924 while (--oparg >= 0) {
2925 PyObject *item = POP();
2926 PyTuple_SET_ITEM(tup, oparg, item);
2927 }
2928 PUSH(tup);
2929 DISPATCH();
2930 }
2931
Benjamin Petersonddd19492018-09-16 22:38:02 -07002932 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 PyObject *list = PyList_New(oparg);
2934 if (list == NULL)
2935 goto error;
2936 while (--oparg >= 0) {
2937 PyObject *item = POP();
2938 PyList_SET_ITEM(list, oparg, item);
2939 }
2940 PUSH(list);
2941 DISPATCH();
2942 }
2943
Mark Shannon13bc1392020-01-23 09:25:17 +00002944 case TARGET(LIST_TO_TUPLE): {
2945 PyObject *list = POP();
2946 PyObject *tuple = PyList_AsTuple(list);
2947 Py_DECREF(list);
2948 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002949 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002950 }
2951 PUSH(tuple);
2952 DISPATCH();
2953 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002954
Mark Shannon13bc1392020-01-23 09:25:17 +00002955 case TARGET(LIST_EXTEND): {
2956 PyObject *iterable = POP();
2957 PyObject *list = PEEK(oparg);
2958 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2959 if (none_val == NULL) {
2960 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002961 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002962 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002963 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002964 _PyErr_Format(tstate, PyExc_TypeError,
2965 "Value after * must be an iterable, not %.200s",
2966 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002967 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002968 Py_DECREF(iterable);
2969 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002970 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002971 Py_DECREF(none_val);
2972 Py_DECREF(iterable);
2973 DISPATCH();
2974 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002975
Mark Shannon13bc1392020-01-23 09:25:17 +00002976 case TARGET(SET_UPDATE): {
2977 PyObject *iterable = POP();
2978 PyObject *set = PEEK(oparg);
2979 int err = _PySet_Update(set, iterable);
2980 Py_DECREF(iterable);
2981 if (err < 0) {
2982 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002983 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002984 DISPATCH();
2985 }
2986
Benjamin Petersonddd19492018-09-16 22:38:02 -07002987 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 PyObject *set = PySet_New(NULL);
2989 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002990 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 if (set == NULL)
2992 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002993 for (i = oparg; i > 0; i--) {
2994 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 if (err == 0)
2996 err = PySet_Add(set, item);
2997 Py_DECREF(item);
2998 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002999 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 if (err != 0) {
3001 Py_DECREF(set);
3002 goto error;
3003 }
3004 PUSH(set);
3005 DISPATCH();
3006 }
3007
Benjamin Petersonddd19492018-09-16 22:38:02 -07003008 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003009 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3011 if (map == NULL)
3012 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003013 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003014 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003015 PyObject *key = PEEK(2*i);
3016 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003017 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003018 if (err != 0) {
3019 Py_DECREF(map);
3020 goto error;
3021 }
3022 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003023
3024 while (oparg--) {
3025 Py_DECREF(POP());
3026 Py_DECREF(POP());
3027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 PUSH(map);
3029 DISPATCH();
3030 }
3031
Benjamin Petersonddd19492018-09-16 22:38:02 -07003032 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003033 _Py_IDENTIFIER(__annotations__);
3034 int err;
3035 PyObject *ann_dict;
3036 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003037 _PyErr_Format(tstate, PyExc_SystemError,
3038 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003039 goto error;
3040 }
3041 /* check if __annotations__ in locals()... */
3042 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003043 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003044 &PyId___annotations__);
3045 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003046 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003047 goto error;
3048 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003049 /* ...if not, create a new one */
3050 ann_dict = PyDict_New();
3051 if (ann_dict == NULL) {
3052 goto error;
3053 }
3054 err = _PyDict_SetItemId(f->f_locals,
3055 &PyId___annotations__, ann_dict);
3056 Py_DECREF(ann_dict);
3057 if (err != 0) {
3058 goto error;
3059 }
3060 }
3061 }
3062 else {
3063 /* do the same if locals() is not a dict */
3064 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3065 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003066 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003067 }
3068 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3069 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003070 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003071 goto error;
3072 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003073 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003074 ann_dict = PyDict_New();
3075 if (ann_dict == NULL) {
3076 goto error;
3077 }
3078 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3079 Py_DECREF(ann_dict);
3080 if (err != 0) {
3081 goto error;
3082 }
3083 }
3084 else {
3085 Py_DECREF(ann_dict);
3086 }
3087 }
3088 DISPATCH();
3089 }
3090
Benjamin Petersonddd19492018-09-16 22:38:02 -07003091 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003092 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003093 PyObject *map;
3094 PyObject *keys = TOP();
3095 if (!PyTuple_CheckExact(keys) ||
3096 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003097 _PyErr_SetString(tstate, PyExc_SystemError,
3098 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003099 goto error;
3100 }
3101 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3102 if (map == NULL) {
3103 goto error;
3104 }
3105 for (i = oparg; i > 0; i--) {
3106 int err;
3107 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3108 PyObject *value = PEEK(i + 1);
3109 err = PyDict_SetItem(map, key, value);
3110 if (err != 0) {
3111 Py_DECREF(map);
3112 goto error;
3113 }
3114 }
3115
3116 Py_DECREF(POP());
3117 while (oparg--) {
3118 Py_DECREF(POP());
3119 }
3120 PUSH(map);
3121 DISPATCH();
3122 }
3123
Mark Shannon8a4cd702020-01-27 09:57:45 +00003124 case TARGET(DICT_UPDATE): {
3125 PyObject *update = POP();
3126 PyObject *dict = PEEK(oparg);
3127 if (PyDict_Update(dict, update) < 0) {
3128 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3129 _PyErr_Format(tstate, PyExc_TypeError,
3130 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003131 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003132 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003133 Py_DECREF(update);
3134 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003135 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003136 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003137 DISPATCH();
3138 }
3139
Mark Shannon8a4cd702020-01-27 09:57:45 +00003140 case TARGET(DICT_MERGE): {
3141 PyObject *update = POP();
3142 PyObject *dict = PEEK(oparg);
3143
3144 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3145 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3146 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003147 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003148 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003149 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003150 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003151 DISPATCH();
3152 }
3153
Benjamin Petersonddd19492018-09-16 22:38:02 -07003154 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003155 PyObject *value = TOP();
3156 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PyObject *map;
3158 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003159 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003160 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003162 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 Py_DECREF(value);
3164 Py_DECREF(key);
3165 if (err != 0)
3166 goto error;
3167 PREDICT(JUMP_ABSOLUTE);
3168 DISPATCH();
3169 }
3170
Benjamin Petersonddd19492018-09-16 22:38:02 -07003171 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 PyObject *name = GETITEM(names, oparg);
3173 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003174
3175 PyTypeObject *type = Py_TYPE(owner);
3176 PyObject *res;
3177 PyObject **dictptr;
3178 PyObject *dict;
3179 _PyOpCodeOpt_LoadAttr *la;
3180
3181 OPCACHE_STAT_ATTR_TOTAL();
3182
3183 OPCACHE_CHECK();
3184 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3185 {
3186 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003187 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003188 la = &co_opcache->u.la;
3189 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3190 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003191 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3192 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3193 // so ~offset is strictly < -1 (assuming 2's complement).
3194 if (la->hint < -1) {
3195 // Even faster path -- slot hint.
3196 Py_ssize_t offset = ~la->hint;
3197 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3198 char *addr = (char *)owner + offset;
3199 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003200 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003201 Py_INCREF(res);
3202 SET_TOP(res);
3203 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003204 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003205 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003206 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3207 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003208 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003209 // Fast path for dict.
3210 assert(type->tp_dict != NULL);
3211 assert(type->tp_dictoffset > 0);
3212
3213 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3214 dict = *dictptr;
3215 if (dict != NULL && PyDict_CheckExact(dict)) {
3216 Py_ssize_t hint = la->hint;
3217 Py_INCREF(dict);
3218 res = NULL;
3219 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3220
3221 if (res != NULL) {
3222 if (la->hint == hint && hint >= 0) {
3223 // Our hint has helped -- cache hit.
3224 OPCACHE_STAT_ATTR_HIT();
3225 } else {
3226 // The hint we provided didn't work.
3227 // Maybe next time?
3228 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3229 }
3230
3231 Py_INCREF(res);
3232 SET_TOP(res);
3233 Py_DECREF(owner);
3234 Py_DECREF(dict);
3235 DISPATCH();
3236 } else {
3237 // This attribute can be missing sometimes;
3238 // we don't want to optimize this lookup.
3239 OPCACHE_DEOPT_LOAD_ATTR();
3240 Py_DECREF(dict);
3241 }
3242 } else {
3243 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3244 OPCACHE_DEOPT_LOAD_ATTR();
3245 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003246 }
3247 } else {
3248 // The type of the object has either been updated,
3249 // or is different. Maybe it will stabilize?
3250 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3251 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003252 OPCACHE_STAT_ATTR_MISS();
3253 }
3254
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003255 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003256 type->tp_getattro == PyObject_GenericGetAttr)
3257 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003258 if (type->tp_dict == NULL) {
3259 if (PyType_Ready(type) < 0) {
3260 Py_DECREF(owner);
3261 SET_TOP(NULL);
3262 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003263 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003264 }
3265 PyObject *descr = _PyType_Lookup(type, name);
3266 if (descr != NULL) {
3267 // We found an attribute with a data-like descriptor.
3268 PyTypeObject *dtype = Py_TYPE(descr);
3269 if (dtype == &PyMemberDescr_Type) { // It's a slot
3270 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3271 struct PyMemberDef *dmem = member->d_member;
3272 if (dmem->type == T_OBJECT_EX) {
3273 Py_ssize_t offset = dmem->offset;
3274 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003275
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003276 if (co_opcache->optimized == 0) {
3277 // First time we optimize this opcode.
3278 OPCACHE_STAT_ATTR_OPT();
3279 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3280 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3281 }
3282
3283 la = &co_opcache->u.la;
3284 la->type = type;
3285 la->tp_version_tag = type->tp_version_tag;
3286 la->hint = ~offset;
3287
3288 char *addr = (char *)owner + offset;
3289 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003290 if (res != NULL) {
3291 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003292 Py_DECREF(owner);
3293 SET_TOP(res);
3294
Pablo Galindo109826c2020-10-20 06:22:44 +01003295 DISPATCH();
3296 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003297 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003298 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003299 // Else it's a slot of a different type. We don't handle those.
3300 }
3301 // Else it's some other kind of descriptor that we don't handle.
3302 OPCACHE_DEOPT_LOAD_ATTR();
3303 } else if (type->tp_dictoffset > 0) {
3304 // We found an instance with a __dict__.
3305 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3306 dict = *dictptr;
3307
3308 if (dict != NULL && PyDict_CheckExact(dict)) {
3309 Py_INCREF(dict);
3310 res = NULL;
3311 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3312 if (res != NULL) {
3313 Py_INCREF(res);
3314 Py_DECREF(dict);
3315 Py_DECREF(owner);
3316 SET_TOP(res);
3317
3318 if (co_opcache->optimized == 0) {
3319 // First time we optimize this opcode.
3320 OPCACHE_STAT_ATTR_OPT();
3321 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3322 }
3323
3324 la = &co_opcache->u.la;
3325 la->type = type;
3326 la->tp_version_tag = type->tp_version_tag;
3327 la->hint = hint;
3328
3329 DISPATCH();
3330 }
3331 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003332 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003333 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003334 OPCACHE_DEOPT_LOAD_ATTR();
3335 }
3336 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003337 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003338 OPCACHE_DEOPT_LOAD_ATTR();
3339 }
3340 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3341 OPCACHE_DEOPT_LOAD_ATTR();
3342 }
3343 }
3344
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003345 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003346 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 Py_DECREF(owner);
3348 SET_TOP(res);
3349 if (res == NULL)
3350 goto error;
3351 DISPATCH();
3352 }
3353
Benjamin Petersonddd19492018-09-16 22:38:02 -07003354 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003355 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 PyObject *right = POP();
3357 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003358 PyObject *res = PyObject_RichCompare(left, right, oparg);
3359 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 Py_DECREF(left);
3361 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003362 if (res == NULL)
3363 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 PREDICT(POP_JUMP_IF_FALSE);
3365 PREDICT(POP_JUMP_IF_TRUE);
3366 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003368
Mark Shannon9af0e472020-01-14 10:12:45 +00003369 case TARGET(IS_OP): {
3370 PyObject *right = POP();
3371 PyObject *left = TOP();
3372 int res = (left == right)^oparg;
3373 PyObject *b = res ? Py_True : Py_False;
3374 Py_INCREF(b);
3375 SET_TOP(b);
3376 Py_DECREF(left);
3377 Py_DECREF(right);
3378 PREDICT(POP_JUMP_IF_FALSE);
3379 PREDICT(POP_JUMP_IF_TRUE);
3380 FAST_DISPATCH();
3381 }
3382
3383 case TARGET(CONTAINS_OP): {
3384 PyObject *right = POP();
3385 PyObject *left = POP();
3386 int res = PySequence_Contains(right, left);
3387 Py_DECREF(left);
3388 Py_DECREF(right);
3389 if (res < 0) {
3390 goto error;
3391 }
3392 PyObject *b = (res^oparg) ? Py_True : Py_False;
3393 Py_INCREF(b);
3394 PUSH(b);
3395 PREDICT(POP_JUMP_IF_FALSE);
3396 PREDICT(POP_JUMP_IF_TRUE);
3397 FAST_DISPATCH();
3398 }
3399
3400#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3401 "BaseException is not allowed"
3402
3403 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3404 PyObject *right = POP();
3405 PyObject *left = POP();
3406 if (PyTuple_Check(right)) {
3407 Py_ssize_t i, length;
3408 length = PyTuple_GET_SIZE(right);
3409 for (i = 0; i < length; i++) {
3410 PyObject *exc = PyTuple_GET_ITEM(right, i);
3411 if (!PyExceptionClass_Check(exc)) {
3412 _PyErr_SetString(tstate, PyExc_TypeError,
3413 CANNOT_CATCH_MSG);
3414 Py_DECREF(left);
3415 Py_DECREF(right);
3416 goto error;
3417 }
3418 }
3419 }
3420 else {
3421 if (!PyExceptionClass_Check(right)) {
3422 _PyErr_SetString(tstate, PyExc_TypeError,
3423 CANNOT_CATCH_MSG);
3424 Py_DECREF(left);
3425 Py_DECREF(right);
3426 goto error;
3427 }
3428 }
3429 int res = PyErr_GivenExceptionMatches(left, right);
3430 Py_DECREF(left);
3431 Py_DECREF(right);
3432 if (res > 0) {
3433 /* Exception matches -- Do nothing */;
3434 }
3435 else if (res == 0) {
3436 JUMPTO(oparg);
3437 }
3438 else {
3439 goto error;
3440 }
3441 DISPATCH();
3442 }
3443
Benjamin Petersonddd19492018-09-16 22:38:02 -07003444 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003445 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003446 PyObject *fromlist = POP();
3447 PyObject *level = TOP();
3448 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003449 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003450 Py_DECREF(level);
3451 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 SET_TOP(res);
3453 if (res == NULL)
3454 goto error;
3455 DISPATCH();
3456 }
3457
Benjamin Petersonddd19492018-09-16 22:38:02 -07003458 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 PyObject *from = POP(), *locals;
3460 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003461 if (PyFrame_FastToLocalsWithError(f) < 0) {
3462 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003463 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003464 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003465
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 locals = f->f_locals;
3467 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003468 _PyErr_SetString(tstate, PyExc_SystemError,
3469 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003470 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003471 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003473 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 Py_DECREF(from);
3476 if (err != 0)
3477 goto error;
3478 DISPATCH();
3479 }
Guido van Rossum25831651993-05-19 14:50:45 +00003480
Benjamin Petersonddd19492018-09-16 22:38:02 -07003481 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003482 PyObject *name = GETITEM(names, oparg);
3483 PyObject *from = TOP();
3484 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003485 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 PUSH(res);
3487 if (res == NULL)
3488 goto error;
3489 DISPATCH();
3490 }
Thomas Wouters52152252000-08-17 22:55:00 +00003491
Benjamin Petersonddd19492018-09-16 22:38:02 -07003492 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 JUMPBY(oparg);
3494 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003496
Benjamin Petersonddd19492018-09-16 22:38:02 -07003497 case TARGET(POP_JUMP_IF_FALSE): {
3498 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 PyObject *cond = POP();
3500 int err;
3501 if (cond == Py_True) {
3502 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 FAST_DISPATCH();
3504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 if (cond == Py_False) {
3506 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 JUMPTO(oparg);
3508 FAST_DISPATCH();
3509 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 err = PyObject_IsTrue(cond);
3511 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003513 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 else if (err == 0)
3515 JUMPTO(oparg);
3516 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003517 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003520
Benjamin Petersonddd19492018-09-16 22:38:02 -07003521 case TARGET(POP_JUMP_IF_TRUE): {
3522 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003523 PyObject *cond = POP();
3524 int err;
3525 if (cond == Py_False) {
3526 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 FAST_DISPATCH();
3528 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003529 if (cond == Py_True) {
3530 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 JUMPTO(oparg);
3532 FAST_DISPATCH();
3533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003534 err = PyObject_IsTrue(cond);
3535 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 JUMPTO(oparg);
3538 }
3539 else if (err == 0)
3540 ;
3541 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003542 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003544 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003545
Benjamin Petersonddd19492018-09-16 22:38:02 -07003546 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 PyObject *cond = TOP();
3548 int err;
3549 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003550 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 FAST_DISPATCH();
3553 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003554 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 JUMPTO(oparg);
3556 FAST_DISPATCH();
3557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003558 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003560 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 }
3563 else if (err == 0)
3564 JUMPTO(oparg);
3565 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003569
Benjamin Petersonddd19492018-09-16 22:38:02 -07003570 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003571 PyObject *cond = TOP();
3572 int err;
3573 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003574 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 FAST_DISPATCH();
3577 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 JUMPTO(oparg);
3580 FAST_DISPATCH();
3581 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 JUMPTO(oparg);
3585 }
3586 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003587 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003588 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 }
3590 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003594
Benjamin Petersonddd19492018-09-16 22:38:02 -07003595 case TARGET(JUMP_ABSOLUTE): {
3596 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003598#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 /* Enabling this path speeds-up all while and for-loops by bypassing
3600 the per-loop checks for signals. By default, this should be turned-off
3601 because it prevents detection of a control-break in tight loops like
3602 "while 1: pass". Compile with this option turned-on when you need
3603 the speed-up and do not need break checking inside tight loops (ones
3604 that contain only instructions ending with FAST_DISPATCH).
3605 */
3606 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003607#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003609#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003611
Benjamin Petersonddd19492018-09-16 22:38:02 -07003612 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003615 PyObject *iter = PyObject_GetIter(iterable);
3616 Py_DECREF(iterable);
3617 SET_TOP(iter);
3618 if (iter == NULL)
3619 goto error;
3620 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003621 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003622 DISPATCH();
3623 }
3624
Benjamin Petersonddd19492018-09-16 22:38:02 -07003625 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003626 /* before: [obj]; after [getiter(obj)] */
3627 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003628 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003629 if (PyCoro_CheckExact(iterable)) {
3630 /* `iterable` is a coroutine */
3631 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3632 /* and it is used in a 'yield from' expression of a
3633 regular generator. */
3634 Py_DECREF(iterable);
3635 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003636 _PyErr_SetString(tstate, PyExc_TypeError,
3637 "cannot 'yield from' a coroutine object "
3638 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003639 goto error;
3640 }
3641 }
3642 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003643 /* `iterable` is not a generator. */
3644 iter = PyObject_GetIter(iterable);
3645 Py_DECREF(iterable);
3646 SET_TOP(iter);
3647 if (iter == NULL)
3648 goto error;
3649 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003650 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003651 DISPATCH();
3652 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003653
Benjamin Petersonddd19492018-09-16 22:38:02 -07003654 case TARGET(FOR_ITER): {
3655 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003657 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003658 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003659 if (next != NULL) {
3660 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 PREDICT(STORE_FAST);
3662 PREDICT(UNPACK_SEQUENCE);
3663 DISPATCH();
3664 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003665 if (_PyErr_Occurred(tstate)) {
3666 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003667 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003668 }
3669 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00003670 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02003671 }
3672 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 }
3674 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003675 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003676 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 JUMPBY(oparg);
3678 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003679 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003680
Benjamin Petersonddd19492018-09-16 22:38:02 -07003681 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003682 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 STACK_LEVEL());
3684 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003686
Benjamin Petersonddd19492018-09-16 22:38:02 -07003687 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003688 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003689 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003690 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003691 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003692 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003693 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003694 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003695 }
3696 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3697 if (exit == NULL) {
3698 Py_DECREF(enter);
3699 goto error;
3700 }
Yury Selivanov75445082015-05-11 22:57:16 -04003701 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003702 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003703 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003704 Py_DECREF(enter);
3705 if (res == NULL)
3706 goto error;
3707 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003708 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003709 DISPATCH();
3710 }
3711
Benjamin Petersonddd19492018-09-16 22:38:02 -07003712 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003713 PyObject *res = POP();
3714 /* Setup the finally block before pushing the result
3715 of __aenter__ on the stack. */
3716 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3717 STACK_LEVEL());
3718 PUSH(res);
3719 DISPATCH();
3720 }
3721
Benjamin Petersonddd19492018-09-16 22:38:02 -07003722 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003723 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003724 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003725 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003726 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003727 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003728 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003729 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003730 }
3731 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003732 if (exit == NULL) {
3733 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003734 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003735 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003736 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003737 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003738 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003739 Py_DECREF(enter);
3740 if (res == NULL)
3741 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 /* Setup the finally block before pushing the result
3743 of __enter__ on the stack. */
3744 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3745 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003746
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 DISPATCH();
3749 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003750
Mark Shannonfee55262019-11-21 09:11:43 +00003751 case TARGET(WITH_EXCEPT_START): {
3752 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003754 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3755 - SEVENTH: the context.__exit__ bound method
3756 We call SEVENTH(TOP, SECOND, THIRD).
3757 Then we push again the TOP exception and the __exit__
3758 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003761 PyObject *exc, *val, *tb, *res;
3762
Victor Stinner842cfff2016-12-01 14:45:31 +01003763 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003764 val = SECOND();
3765 tb = THIRD();
3766 assert(exc != Py_None);
3767 assert(!PyLong_Check(exc));
3768 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003769 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003770 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003771 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003772 if (res == NULL)
3773 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003774
Yury Selivanov75445082015-05-11 22:57:16 -04003775 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003776 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003778
Benjamin Petersonddd19492018-09-16 22:38:02 -07003779 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003780 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003781 PyObject *name = GETITEM(names, oparg);
3782 PyObject *obj = TOP();
3783 PyObject *meth = NULL;
3784
3785 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3786
Yury Selivanovf2392132016-12-13 19:03:51 -05003787 if (meth == NULL) {
3788 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003789 goto error;
3790 }
3791
3792 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003793 /* We can bypass temporary bound method object.
3794 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003795
INADA Naoki015bce62017-01-16 17:23:30 +09003796 meth | self | arg1 | ... | argN
3797 */
3798 SET_TOP(meth);
3799 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003800 }
3801 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003802 /* meth is not an unbound method (but a regular attr, or
3803 something was returned by a descriptor protocol). Set
3804 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003805 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003806
3807 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003808 */
INADA Naoki015bce62017-01-16 17:23:30 +09003809 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003810 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003811 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003812 }
3813 DISPATCH();
3814 }
3815
Benjamin Petersonddd19492018-09-16 22:38:02 -07003816 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003817 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003818 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003819
3820 sp = stack_pointer;
3821
INADA Naoki015bce62017-01-16 17:23:30 +09003822 meth = PEEK(oparg + 2);
3823 if (meth == NULL) {
3824 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3825 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003826
3827 Stack layout:
3828
INADA Naoki015bce62017-01-16 17:23:30 +09003829 ... | NULL | callable | arg1 | ... | argN
3830 ^- TOP()
3831 ^- (-oparg)
3832 ^- (-oparg-1)
3833 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003834
Ville Skyttä49b27342017-08-03 09:00:59 +03003835 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003836 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003837 */
Mark Shannon86433452021-01-07 16:49:02 +00003838 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003839 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003840 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003841 }
3842 else {
3843 /* This is a method call. Stack layout:
3844
INADA Naoki015bce62017-01-16 17:23:30 +09003845 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003846 ^- TOP()
3847 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003848 ^- (-oparg-1)
3849 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003850
INADA Naoki015bce62017-01-16 17:23:30 +09003851 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003852 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003853 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003854 */
Mark Shannon86433452021-01-07 16:49:02 +00003855 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003856 stack_pointer = sp;
3857 }
3858
3859 PUSH(res);
3860 if (res == NULL)
3861 goto error;
3862 DISPATCH();
3863 }
3864
Benjamin Petersonddd19492018-09-16 22:38:02 -07003865 case TARGET(CALL_FUNCTION): {
3866 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003867 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003869 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003871 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003872 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003873 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003875 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003877
Benjamin Petersonddd19492018-09-16 22:38:02 -07003878 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003879 PyObject **sp, *res, *names;
3880
3881 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003882 assert(PyTuple_Check(names));
3883 assert(PyTuple_GET_SIZE(names) <= oparg);
3884 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003886 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003888 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003889 Py_DECREF(names);
3890
3891 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003892 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003893 }
3894 DISPATCH();
3895 }
3896
Benjamin Petersonddd19492018-09-16 22:38:02 -07003897 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003898 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003899 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003900 if (oparg & 0x01) {
3901 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003902 if (!PyDict_CheckExact(kwargs)) {
3903 PyObject *d = PyDict_New();
3904 if (d == NULL)
3905 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003906 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003907 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003908 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003909 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003910 goto error;
3911 }
3912 Py_DECREF(kwargs);
3913 kwargs = d;
3914 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003915 assert(PyDict_CheckExact(kwargs));
3916 }
3917 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003918 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003919 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003920 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003921 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003922 goto error;
3923 }
3924 Py_SETREF(callargs, PySequence_Tuple(callargs));
3925 if (callargs == NULL) {
3926 goto error;
3927 }
3928 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003929 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003930
Mark Shannon86433452021-01-07 16:49:02 +00003931 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003932 Py_DECREF(func);
3933 Py_DECREF(callargs);
3934 Py_XDECREF(kwargs);
3935
3936 SET_TOP(result);
3937 if (result == NULL) {
3938 goto error;
3939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003940 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003942
Benjamin Petersonddd19492018-09-16 22:38:02 -07003943 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003944 PyObject *qualname = POP();
3945 PyObject *codeobj = POP();
3946 PyFunctionObject *func = (PyFunctionObject *)
3947 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003948
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003949 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003950 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003951 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003952 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003954
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003955 if (oparg & 0x08) {
3956 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00003957 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003958 }
3959 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02003960 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003961 func->func_annotations = POP();
3962 }
3963 if (oparg & 0x02) {
3964 assert(PyDict_CheckExact(TOP()));
3965 func->func_kwdefaults = POP();
3966 }
3967 if (oparg & 0x01) {
3968 assert(PyTuple_CheckExact(TOP()));
3969 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003971
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003972 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003973 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003975
Benjamin Petersonddd19492018-09-16 22:38:02 -07003976 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003977 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003979 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003981 step = NULL;
3982 stop = POP();
3983 start = TOP();
3984 slice = PySlice_New(start, stop, step);
3985 Py_DECREF(start);
3986 Py_DECREF(stop);
3987 Py_XDECREF(step);
3988 SET_TOP(slice);
3989 if (slice == NULL)
3990 goto error;
3991 DISPATCH();
3992 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003993
Benjamin Petersonddd19492018-09-16 22:38:02 -07003994 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003995 /* Handles f-string value formatting. */
3996 PyObject *result;
3997 PyObject *fmt_spec;
3998 PyObject *value;
3999 PyObject *(*conv_fn)(PyObject *);
4000 int which_conversion = oparg & FVC_MASK;
4001 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4002
4003 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004004 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004005
4006 /* See if any conversion is specified. */
4007 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004008 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004009 case FVC_STR: conv_fn = PyObject_Str; break;
4010 case FVC_REPR: conv_fn = PyObject_Repr; break;
4011 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004012 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004013 _PyErr_Format(tstate, PyExc_SystemError,
4014 "unexpected conversion flag %d",
4015 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004016 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004017 }
4018
4019 /* If there's a conversion function, call it and replace
4020 value with that result. Otherwise, just use value,
4021 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004022 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004023 result = conv_fn(value);
4024 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004025 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004026 Py_XDECREF(fmt_spec);
4027 goto error;
4028 }
4029 value = result;
4030 }
4031
4032 /* If value is a unicode object, and there's no fmt_spec,
4033 then we know the result of format(value) is value
4034 itself. In that case, skip calling format(). I plan to
4035 move this optimization in to PyObject_Format()
4036 itself. */
4037 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4038 /* Do nothing, just transfer ownership to result. */
4039 result = value;
4040 } else {
4041 /* Actually call format(). */
4042 result = PyObject_Format(value, fmt_spec);
4043 Py_DECREF(value);
4044 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004045 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004046 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004047 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004048 }
4049
Eric V. Smith135d5f42016-02-05 18:23:08 -05004050 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004051 DISPATCH();
4052 }
4053
Benjamin Petersonddd19492018-09-16 22:38:02 -07004054 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004055 int oldoparg = oparg;
4056 NEXTOPARG();
4057 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004059 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004060
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004061
Antoine Pitrou042b1282010-08-13 21:15:58 +00004062#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 default:
4066 fprintf(stderr,
4067 "XXX lineno: %d, opcode: %d\n",
4068 PyFrame_GetLineNumber(f),
4069 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004070 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004071 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004074
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004075 /* This should never be reached. Every opcode should end with DISPATCH()
4076 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004077 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004078
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004079error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004080 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004081#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004082 if (!_PyErr_Occurred(tstate)) {
4083 _PyErr_SetString(tstate, PyExc_SystemError,
4084 "error return without exception set");
4085 }
Victor Stinner365b6932013-07-12 00:11:58 +02004086#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004087 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004088#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004090 /* Log traceback info. */
4091 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004092
Mark Shannoncb9879b2020-07-17 11:44:23 +01004093 if (tstate->c_tracefunc != NULL) {
4094 /* Make sure state is set to FRAME_EXECUTING for tracing */
4095 assert(f->f_state == FRAME_EXECUTING);
4096 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004097 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004098 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004099 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004100exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004101 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004102 /* Unwind stacks if an exception occurred */
4103 while (f->f_iblock > 0) {
4104 /* Pop the current block. */
4105 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 if (b->b_type == EXCEPT_HANDLER) {
4108 UNWIND_EXCEPT_HANDLER(b);
4109 continue;
4110 }
4111 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004112 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 PyObject *exc, *val, *tb;
4114 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004115 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004117 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004118 PUSH(exc_info->exc_traceback);
4119 PUSH(exc_info->exc_value);
4120 if (exc_info->exc_type != NULL) {
4121 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 }
4123 else {
4124 Py_INCREF(Py_None);
4125 PUSH(Py_None);
4126 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004127 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 /* Make the raw exception data
4129 available to the handler,
4130 so a program can emulate the
4131 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004132 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004133 if (tb != NULL)
4134 PyException_SetTraceback(val, tb);
4135 else
4136 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004138 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004140 exc_info->exc_value = val;
4141 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 if (tb == NULL)
4143 tb = Py_None;
4144 Py_INCREF(tb);
4145 PUSH(tb);
4146 PUSH(val);
4147 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004149 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004150 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004151 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004152 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004153 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004154 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 }
4156 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004157
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004158 /* End the loop as we still have an error */
4159 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004161
Pablo Galindof00828a2019-05-09 16:52:02 +01004162 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004163 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* Pop remaining stack entries. */
4166 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004167 PyObject *o = POP();
4168 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004170 f->f_stackdepth = 0;
4171 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004172exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004174 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004175 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004176 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004177 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 }
4179 }
4180 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004181 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004182 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004183 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 }
4185 }
4186 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004189exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004190 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4191 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004192 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004194
Victor Stinner0b72b232020-03-12 23:18:39 +01004195 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004196}
4197
Benjamin Petersonb204a422011-06-05 22:04:07 -05004198static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004199format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004200 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004201{
4202 int err;
4203 Py_ssize_t len = PyList_GET_SIZE(names);
4204 PyObject *name_str, *comma, *tail, *tmp;
4205
4206 assert(PyList_CheckExact(names));
4207 assert(len >= 1);
4208 /* Deal with the joys of natural language. */
4209 switch (len) {
4210 case 1:
4211 name_str = PyList_GET_ITEM(names, 0);
4212 Py_INCREF(name_str);
4213 break;
4214 case 2:
4215 name_str = PyUnicode_FromFormat("%U and %U",
4216 PyList_GET_ITEM(names, len - 2),
4217 PyList_GET_ITEM(names, len - 1));
4218 break;
4219 default:
4220 tail = PyUnicode_FromFormat(", %U, and %U",
4221 PyList_GET_ITEM(names, len - 2),
4222 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004223 if (tail == NULL)
4224 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004225 /* Chop off the last two objects in the list. This shouldn't actually
4226 fail, but we can't be too careful. */
4227 err = PyList_SetSlice(names, len - 2, len, NULL);
4228 if (err == -1) {
4229 Py_DECREF(tail);
4230 return;
4231 }
4232 /* Stitch everything up into a nice comma-separated list. */
4233 comma = PyUnicode_FromString(", ");
4234 if (comma == NULL) {
4235 Py_DECREF(tail);
4236 return;
4237 }
4238 tmp = PyUnicode_Join(comma, names);
4239 Py_DECREF(comma);
4240 if (tmp == NULL) {
4241 Py_DECREF(tail);
4242 return;
4243 }
4244 name_str = PyUnicode_Concat(tmp, tail);
4245 Py_DECREF(tmp);
4246 Py_DECREF(tail);
4247 break;
4248 }
4249 if (name_str == NULL)
4250 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004251 _PyErr_Format(tstate, PyExc_TypeError,
4252 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004253 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004254 len,
4255 kind,
4256 len == 1 ? "" : "s",
4257 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004258 Py_DECREF(name_str);
4259}
4260
4261static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004262missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4263 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004264 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004265{
Victor Stinner74319ae2016-08-25 00:04:09 +02004266 Py_ssize_t i, j = 0;
4267 Py_ssize_t start, end;
4268 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004269 const char *kind = positional ? "positional" : "keyword-only";
4270 PyObject *missing_names;
4271
4272 /* Compute the names of the arguments that are missing. */
4273 missing_names = PyList_New(missing);
4274 if (missing_names == NULL)
4275 return;
4276 if (positional) {
4277 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004278 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004279 }
4280 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004281 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004282 end = start + co->co_kwonlyargcount;
4283 }
4284 for (i = start; i < end; i++) {
4285 if (GETLOCAL(i) == NULL) {
4286 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4287 PyObject *name = PyObject_Repr(raw);
4288 if (name == NULL) {
4289 Py_DECREF(missing_names);
4290 return;
4291 }
4292 PyList_SET_ITEM(missing_names, j++, name);
4293 }
4294 }
4295 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004296 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004297 Py_DECREF(missing_names);
4298}
4299
4300static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004301too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004302 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004303 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004304{
4305 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004306 Py_ssize_t kwonly_given = 0;
4307 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004308 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004309 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004310
Benjamin Petersone109c702011-06-24 09:37:26 -05004311 assert((co->co_flags & CO_VARARGS) == 0);
4312 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004313 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004314 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004315 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004316 }
4317 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004318 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004319 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004320 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004321 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004322 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004323 }
4324 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004325 plural = (co_argcount != 1);
4326 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004327 }
4328 if (sig == NULL)
4329 return;
4330 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004331 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4332 kwonly_sig = PyUnicode_FromFormat(format,
4333 given != 1 ? "s" : "",
4334 kwonly_given,
4335 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004336 if (kwonly_sig == NULL) {
4337 Py_DECREF(sig);
4338 return;
4339 }
4340 }
4341 else {
4342 /* This will not fail. */
4343 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004344 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004345 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004346 _PyErr_Format(tstate, PyExc_TypeError,
4347 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004348 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004349 sig,
4350 plural ? "s" : "",
4351 given,
4352 kwonly_sig,
4353 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004354 Py_DECREF(sig);
4355 Py_DECREF(kwonly_sig);
4356}
4357
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004358static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004359positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004360 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004361 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004362{
4363 int posonly_conflicts = 0;
4364 PyObject* posonly_names = PyList_New(0);
4365
4366 for(int k=0; k < co->co_posonlyargcount; k++){
4367 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4368
4369 for (int k2=0; k2<kwcount; k2++){
4370 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004371 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004372 if (kwname == posonly_name){
4373 if(PyList_Append(posonly_names, kwname) != 0) {
4374 goto fail;
4375 }
4376 posonly_conflicts++;
4377 continue;
4378 }
4379
4380 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4381
4382 if ( cmp > 0) {
4383 if(PyList_Append(posonly_names, kwname) != 0) {
4384 goto fail;
4385 }
4386 posonly_conflicts++;
4387 } else if (cmp < 0) {
4388 goto fail;
4389 }
4390
4391 }
4392 }
4393 if (posonly_conflicts) {
4394 PyObject* comma = PyUnicode_FromString(", ");
4395 if (comma == NULL) {
4396 goto fail;
4397 }
4398 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4399 Py_DECREF(comma);
4400 if (error_names == NULL) {
4401 goto fail;
4402 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004403 _PyErr_Format(tstate, PyExc_TypeError,
4404 "%U() got some positional-only arguments passed"
4405 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004406 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004407 Py_DECREF(error_names);
4408 goto fail;
4409 }
4410
4411 Py_DECREF(posonly_names);
4412 return 0;
4413
4414fail:
4415 Py_XDECREF(posonly_names);
4416 return 1;
4417
4418}
4419
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004420
Mark Shannon0332e562021-02-01 10:42:03 +00004421PyFrameObject *
4422_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004423 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004424 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004425 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004426{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004427 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004428
Mark Shannond6c33fb2021-01-29 13:24:55 +00004429 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4430 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004431 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004432
Victor Stinnerc7020012016-08-16 23:40:29 +02004433 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004434 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004435 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004437 }
Victor Stinner232dda62020-06-04 15:19:02 +02004438 PyObject **fastlocals = f->f_localsplus;
4439 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004440
Victor Stinnerc7020012016-08-16 23:40:29 +02004441 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004442 PyObject *kwdict;
4443 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004444 if (co->co_flags & CO_VARKEYWORDS) {
4445 kwdict = PyDict_New();
4446 if (kwdict == NULL)
4447 goto fail;
4448 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004449 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004450 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004451 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004452 SETLOCAL(i, kwdict);
4453 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004454 else {
4455 kwdict = NULL;
4456 }
4457
Pablo Galindocd74e662019-06-01 18:08:04 +01004458 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004459 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004460 if (argcount > co->co_argcount) {
4461 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004462 }
4463 else {
4464 n = argcount;
4465 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004466 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004467 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004468 Py_INCREF(x);
4469 SETLOCAL(j, x);
4470 }
4471
Victor Stinnerc7020012016-08-16 23:40:29 +02004472 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004473 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004474 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004475 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004476 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004477 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004478 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004479 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004480
Mark Shannon0332e562021-02-01 10:42:03 +00004481 /* Handle keyword arguments */
4482 if (kwnames != NULL) {
4483 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4484 for (i = 0; i < kwcount; i++) {
4485 PyObject **co_varnames;
4486 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4487 PyObject *value = args[i+argcount];
4488 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004489
Mark Shannon0332e562021-02-01 10:42:03 +00004490 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4491 _PyErr_Format(tstate, PyExc_TypeError,
4492 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004493 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004495 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004496
Mark Shannon0332e562021-02-01 10:42:03 +00004497 /* Speed hack: do raw pointer compares. As names are
4498 normally interned this should almost always hit. */
4499 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4500 for (j = co->co_posonlyargcount; j < total_args; j++) {
4501 PyObject *varname = co_varnames[j];
4502 if (varname == keyword) {
4503 goto kw_found;
4504 }
4505 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004506
Mark Shannon0332e562021-02-01 10:42:03 +00004507 /* Slow fallback, just in case */
4508 for (j = co->co_posonlyargcount; j < total_args; j++) {
4509 PyObject *varname = co_varnames[j];
4510 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4511 if (cmp > 0) {
4512 goto kw_found;
4513 }
4514 else if (cmp < 0) {
4515 goto fail;
4516 }
4517 }
4518
4519 assert(j >= total_args);
4520 if (kwdict == NULL) {
4521
4522 if (co->co_posonlyargcount
4523 && positional_only_passed_as_keyword(tstate, co,
4524 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004525 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004526 {
4527 goto fail;
4528 }
4529
4530 _PyErr_Format(tstate, PyExc_TypeError,
4531 "%U() got an unexpected keyword argument '%S'",
4532 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004533 goto fail;
4534 }
4535
Mark Shannon0332e562021-02-01 10:42:03 +00004536 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4537 goto fail;
4538 }
4539 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004540
Mark Shannon0332e562021-02-01 10:42:03 +00004541 kw_found:
4542 if (GETLOCAL(j) != NULL) {
4543 _PyErr_Format(tstate, PyExc_TypeError,
4544 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004545 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004546 goto fail;
4547 }
4548 Py_INCREF(value);
4549 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004552
4553 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004554 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004555 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4556 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 goto fail;
4558 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004559
4560 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004561 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004562 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004563 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004564 Py_ssize_t missing = 0;
4565 for (i = argcount; i < m; i++) {
4566 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004567 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004568 }
4569 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004570 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004571 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004572 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004573 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004574 }
4575 if (n > m)
4576 i = n - m;
4577 else
4578 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004579 if (defcount) {
4580 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4581 for (; i < defcount; i++) {
4582 if (GETLOCAL(m+i) == NULL) {
4583 PyObject *def = defs[i];
4584 Py_INCREF(def);
4585 SETLOCAL(m+i, def);
4586 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004587 }
4588 }
4589 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004590
4591 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004592 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004593 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004594 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004595 if (GETLOCAL(i) != NULL)
4596 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004597 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004598 if (con->fc_kwdefaults != NULL) {
4599 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004600 if (def) {
4601 Py_INCREF(def);
4602 SETLOCAL(i, def);
4603 continue;
4604 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004605 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004606 goto fail;
4607 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004608 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004609 missing++;
4610 }
4611 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004612 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004613 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004614 goto fail;
4615 }
4616 }
4617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004619 vars into frame. */
4620 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004622 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004623 /* Possibly account for the cell variable being an argument. */
4624 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004625 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004626 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004627 /* Clear the local copy. */
4628 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004629 }
4630 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004631 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004632 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004633 if (c == NULL)
4634 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004635 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004637
4638 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004639 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004640 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05004641 Py_INCREF(o);
4642 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004644
Mark Shannon0332e562021-02-01 10:42:03 +00004645 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00004646
Thomas Woutersce272b62007-09-19 21:19:28 +00004647fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 /* decref'ing the frame can cause __del__ methods to get invoked,
4650 which can call back into Python. While we're done with the
4651 current Python frame (f), the associated C stack is still in use,
4652 so recursion_depth must be boosted for the duration.
4653 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004654 if (Py_REFCNT(f) > 1) {
4655 Py_DECREF(f);
4656 _PyObject_GC_TRACK(f);
4657 }
4658 else {
4659 ++tstate->recursion_depth;
4660 Py_DECREF(f);
4661 --tstate->recursion_depth;
4662 }
Mark Shannon0332e562021-02-01 10:42:03 +00004663 return NULL;
4664}
4665
4666static PyObject *
4667make_coro(PyFrameConstructor *con, PyFrameObject *f)
4668{
4669 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
4670 PyObject *gen;
4671 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
4672
4673 /* Don't need to keep the reference to f_back, it will be set
4674 * when the generator is resumed. */
4675 Py_CLEAR(f->f_back);
4676
4677 /* Create a new generator that owns the ready to run frame
4678 * and return that as the value. */
4679 if (is_coro) {
4680 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
4681 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
4682 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
4683 } else {
4684 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
4685 }
4686 if (gen == NULL) {
4687 return NULL;
4688 }
4689
4690 _PyObject_GC_TRACK(f);
4691
4692 return gen;
4693}
4694
4695PyObject *
4696_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
4697 PyObject *locals,
4698 PyObject* const* args, size_t argcount,
4699 PyObject *kwnames)
4700{
4701 PyFrameObject *f = _PyEval_MakeFrameVector(
4702 tstate, con, locals, args, argcount, kwnames);
4703 if (f == NULL) {
4704 return NULL;
4705 }
4706 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
4707 return make_coro(con, f);
4708 }
4709 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
4710
4711 /* decref'ing the frame can cause __del__ methods to get invoked,
4712 which can call back into Python. While we're done with the
4713 current Python frame (f), the associated C stack is still in use,
4714 so recursion_depth must be boosted for the duration.
4715 */
4716 if (Py_REFCNT(f) > 1) {
4717 Py_DECREF(f);
4718 _PyObject_GC_TRACK(f);
4719 }
4720 else {
4721 ++tstate->recursion_depth;
4722 Py_DECREF(f);
4723 --tstate->recursion_depth;
4724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004726}
4727
Mark Shannond6c33fb2021-01-29 13:24:55 +00004728/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004729PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00004730PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4731 PyObject *const *args, int argcount,
4732 PyObject *const *kws, int kwcount,
4733 PyObject *const *defs, int defcount,
4734 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004735{
Mark Shannon0332e562021-02-01 10:42:03 +00004736 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004737 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
4738 if (defaults == NULL) {
4739 return NULL;
4740 }
4741 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
4742 if (builtins == NULL) {
4743 Py_DECREF(defaults);
4744 return NULL;
4745 }
Dong-hee Na3cf08332021-02-14 15:54:39 +09004746 assert ((((PyCodeObject *)_co)->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
Mark Shannon0332e562021-02-01 10:42:03 +00004747 if (locals == NULL) {
4748 locals = globals;
4749 }
4750 PyObject *kwnames;
4751 PyObject *const *allargs;
4752 PyObject **newargs;
4753 if (kwcount == 0) {
4754 allargs = args;
4755 kwnames = NULL;
4756 }
4757 else {
4758 kwnames = PyTuple_New(kwcount);
4759 if (kwnames == NULL) {
4760 res = NULL;
4761 goto fail;
4762 }
4763 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
4764 if (newargs == NULL) {
4765 res = NULL;
4766 Py_DECREF(kwnames);
4767 goto fail;
4768 }
4769 for (int i = 0; i < argcount; i++) {
4770 newargs[i] = args[i];
4771 }
4772 for (int i = 0; i < kwcount; i++) {
4773 Py_INCREF(kws[2*i]);
4774 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
4775 newargs[argcount+i] = kws[2*i+1];
4776 }
4777 allargs = newargs;
4778 }
4779 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
4780 if (kwargs == NULL) {
4781 res = NULL;
4782 Py_DECREF(kwnames);
4783 goto fail;
4784 }
4785 for (int i = 0; i < kwcount; i++) {
4786 Py_INCREF(kws[2*i]);
4787 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
4788 kwargs[i] = kws[2*i+1];
4789 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004790 PyFrameConstructor constr = {
4791 .fc_globals = globals,
4792 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00004793 .fc_name = ((PyCodeObject *)_co)->co_name,
4794 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004795 .fc_code = _co,
4796 .fc_defaults = defaults,
4797 .fc_kwdefaults = kwdefs,
4798 .fc_closure = closure
4799 };
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004800 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00004801 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01004802 allargs, argcount,
4803 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00004804 if (kwcount) {
4805 Py_DECREF(kwnames);
4806 PyMem_Free(newargs);
4807 }
4808fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00004809 Py_DECREF(defaults);
4810 Py_DECREF(builtins);
4811 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004812}
4813
Tim Peters5ca576e2001-06-18 22:08:13 +00004814
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004815static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004816special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004819 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004820 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004821 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 return NULL;
4823 }
4824 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004825}
4826
4827
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004828/* Logic for the raise statement (too complicated for inlining).
4829 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004830static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004831do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 if (exc == NULL) {
4836 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004837 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004839 type = exc_info->exc_type;
4840 value = exc_info->exc_value;
4841 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004842 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004843 _PyErr_SetString(tstate, PyExc_RuntimeError,
4844 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004845 return 0;
4846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 Py_XINCREF(type);
4848 Py_XINCREF(value);
4849 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004850 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004851 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 /* We support the following forms of raise:
4855 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004856 raise <instance>
4857 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 if (PyExceptionClass_Check(exc)) {
4860 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004861 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 if (value == NULL)
4863 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004864 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004865 _PyErr_Format(tstate, PyExc_TypeError,
4866 "calling %R should have returned an instance of "
4867 "BaseException, not %R",
4868 type, Py_TYPE(value));
4869 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 }
4872 else if (PyExceptionInstance_Check(exc)) {
4873 value = exc;
4874 type = PyExceptionInstance_Class(exc);
4875 Py_INCREF(type);
4876 }
4877 else {
4878 /* Not something you can raise. You get an exception
4879 anyway, just not what you specified :-) */
4880 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004881 _PyErr_SetString(tstate, PyExc_TypeError,
4882 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 goto raise_error;
4884 }
Collin Winter828f04a2007-08-31 00:04:24 +00004885
Serhiy Storchakac0191582016-09-27 11:37:10 +03004886 assert(type != NULL);
4887 assert(value != NULL);
4888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 if (cause) {
4890 PyObject *fixed_cause;
4891 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004892 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 if (fixed_cause == NULL)
4894 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004895 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004897 else if (PyExceptionInstance_Check(cause)) {
4898 fixed_cause = cause;
4899 }
4900 else if (cause == Py_None) {
4901 Py_DECREF(cause);
4902 fixed_cause = NULL;
4903 }
4904 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004905 _PyErr_SetString(tstate, PyExc_TypeError,
4906 "exception causes must derive from "
4907 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 goto raise_error;
4909 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004910 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 }
Collin Winter828f04a2007-08-31 00:04:24 +00004912
Victor Stinner438a12d2019-05-24 17:01:38 +02004913 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004914 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004915 Py_DECREF(value);
4916 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004917 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004918
4919raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 Py_XDECREF(value);
4921 Py_XDECREF(type);
4922 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004923 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004924}
4925
Tim Petersd6d010b2001-06-21 02:49:55 +00004926/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004927 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004928
Guido van Rossum0368b722007-05-11 16:50:42 +00004929 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4930 with a variable target.
4931*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004932
Barry Warsawe42b18f1997-08-25 22:13:04 +00004933static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004934unpack_iterable(PyThreadState *tstate, PyObject *v,
4935 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 int i = 0, j = 0;
4938 Py_ssize_t ll = 0;
4939 PyObject *it; /* iter(v) */
4940 PyObject *w;
4941 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004946 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004947 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004948 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004949 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004950 _PyErr_Format(tstate, PyExc_TypeError,
4951 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004952 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004953 }
4954 return 0;
4955 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 for (; i < argcnt; i++) {
4958 w = PyIter_Next(it);
4959 if (w == NULL) {
4960 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004962 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004963 _PyErr_Format(tstate, PyExc_ValueError,
4964 "not enough values to unpack "
4965 "(expected %d, got %d)",
4966 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004967 }
4968 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004969 _PyErr_Format(tstate, PyExc_ValueError,
4970 "not enough values to unpack "
4971 "(expected at least %d, got %d)",
4972 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 }
4975 goto Error;
4976 }
4977 *--sp = w;
4978 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 if (argcntafter == -1) {
4981 /* We better have exhausted the iterator now. */
4982 w = PyIter_Next(it);
4983 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004984 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 goto Error;
4986 Py_DECREF(it);
4987 return 1;
4988 }
4989 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004990 _PyErr_Format(tstate, PyExc_ValueError,
4991 "too many values to unpack (expected %d)",
4992 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 goto Error;
4994 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 l = PySequence_List(it);
4997 if (l == NULL)
4998 goto Error;
4999 *--sp = l;
5000 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 ll = PyList_GET_SIZE(l);
5003 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005004 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005005 "not enough values to unpack (expected at least %d, got %zd)",
5006 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 goto Error;
5008 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 /* Pop the "after-variable" args off the list. */
5011 for (j = argcntafter; j > 0; j--, i++) {
5012 *--sp = PyList_GET_ITEM(l, ll - j);
5013 }
5014 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005015 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 Py_DECREF(it);
5017 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005018
Tim Petersd6d010b2001-06-21 02:49:55 +00005019Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 for (; i > 0; i--, sp++)
5021 Py_DECREF(*sp);
5022 Py_XDECREF(it);
5023 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005024}
5025
5026
Guido van Rossum96a42c81992-01-12 02:29:51 +00005027#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005028static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005029prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005032 if (PyObject_Print(v, stdout, 0) != 0) {
5033 /* Don't know what else to do */
5034 _PyErr_Clear(tstate);
5035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 printf("\n");
5037 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005038}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005039#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005040
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005041static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005042call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005043 PyThreadState *tstate,
5044 PyFrameObject *f,
5045 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005046{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005047 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005049 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 if (value == NULL) {
5051 value = Py_None;
5052 Py_INCREF(value);
5053 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005054 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005055 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 arg = PyTuple_Pack(3, type, value, traceback);
5057 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005058 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 return;
5060 }
Mark Shannon86433452021-01-07 16:49:02 +00005061 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005063 if (err == 0) {
5064 _PyErr_Restore(tstate, type, value, orig_traceback);
5065 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 else {
5067 Py_XDECREF(type);
5068 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005069 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005071}
5072
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005073static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005074call_trace_protected(Py_tracefunc func, PyObject *obj,
5075 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005076 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 PyObject *type, *value, *traceback;
5080 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005081 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00005082 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 if (err == 0)
5084 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005085 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 return 0;
5087 }
5088 else {
5089 Py_XDECREF(type);
5090 Py_XDECREF(value);
5091 Py_XDECREF(traceback);
5092 return -1;
5093 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005094}
5095
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005096static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005097call_trace(Py_tracefunc func, PyObject *obj,
5098 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005099 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 int result;
5103 if (tstate->tracing)
5104 return 0;
5105 tstate->tracing++;
5106 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005107 if (frame->f_lasti < 0) {
5108 frame->f_lineno = frame->f_code->co_firstlineno;
5109 }
5110 else {
5111 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005114 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5116 || (tstate->c_profilefunc != NULL));
5117 tstate->tracing--;
5118 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005119}
5120
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005121PyObject *
5122_PyEval_CallTracing(PyObject *func, PyObject *args)
5123{
Victor Stinner50b48572018-11-01 01:51:40 +01005124 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 int save_tracing = tstate->tracing;
5126 int save_use_tracing = tstate->use_tracing;
5127 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 tstate->tracing = 0;
5130 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5131 || (tstate->c_profilefunc != NULL));
5132 result = PyObject_Call(func, args, NULL);
5133 tstate->tracing = save_tracing;
5134 tstate->use_tracing = save_use_tracing;
5135 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005136}
5137
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005138/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005139static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005140maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005141 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005142 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005145
Nick Coghlan5a851672017-09-08 10:14:16 +10005146 /* If the last instruction falls at the start of a line or if it
5147 represents a jump backwards, update the frame's line number and
5148 then call the trace function if we're tracing source lines.
5149 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005150 int lastline = bounds->ar_line;
5151 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5152 if (line != -1 && frame->f_trace_lines) {
5153 /* Trace backward edges or first instruction of a new line */
5154 if (frame->f_lasti < *instr_prev ||
5155 (line != lastline && frame->f_lasti == bounds->ar_start))
5156 {
Mark Shannon86433452021-01-07 16:49:02 +00005157 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 }
George King20faa682017-10-18 17:44:22 -07005160 /* Always emit an opcode event if we're tracing all opcodes. */
5161 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005162 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005163 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 *instr_prev = frame->f_lasti;
5165 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005166}
5167
Victor Stinner309d7cc2020-03-13 16:39:12 +01005168int
5169_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5170{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005171 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005172 /* The caller must hold the GIL */
5173 assert(PyGILState_Check());
5174
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005175 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005176 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005177 PyThreadState *current_tstate = _PyThreadState_GET();
5178 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005179 return -1;
5180 }
5181
5182 PyObject *profileobj = tstate->c_profileobj;
5183
5184 tstate->c_profilefunc = NULL;
5185 tstate->c_profileobj = NULL;
5186 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5187 tstate->use_tracing = tstate->c_tracefunc != NULL;
5188 Py_XDECREF(profileobj);
5189
5190 Py_XINCREF(arg);
5191 tstate->c_profileobj = arg;
5192 tstate->c_profilefunc = func;
5193
5194 /* Flag that tracing or profiling is turned on */
5195 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5196 return 0;
5197}
5198
Fred Drake5755ce62001-06-27 19:19:46 +00005199void
5200PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005201{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005202 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005203 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005204 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005205 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5206 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005207}
5208
5209int
5210_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5211{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005212 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005213 /* The caller must hold the GIL */
5214 assert(PyGILState_Check());
5215
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005216 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005217 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005218 PyThreadState *current_tstate = _PyThreadState_GET();
5219 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005220 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005221 }
5222
Victor Stinnerda2914d2020-03-20 09:29:08 +01005223 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005224 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005225 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005226
5227 tstate->c_tracefunc = NULL;
5228 tstate->c_traceobj = NULL;
5229 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5230 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5231 Py_XDECREF(traceobj);
5232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005234 tstate->c_traceobj = arg;
5235 tstate->c_tracefunc = func;
5236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005238 tstate->use_tracing = ((func != NULL)
5239 || (tstate->c_profilefunc != NULL));
5240
5241 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005242}
5243
5244void
5245PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5246{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005247 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005248 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005249 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005250 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5251 }
Fred Draked0838392001-06-16 21:02:31 +00005252}
5253
Victor Stinner309d7cc2020-03-13 16:39:12 +01005254
Yury Selivanov75445082015-05-11 22:57:16 -04005255void
Victor Stinner838f2642019-06-13 22:41:23 +02005256_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005257{
5258 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005259 tstate->coroutine_origin_tracking_depth = new_depth;
5260}
5261
5262int
5263_PyEval_GetCoroutineOriginTrackingDepth(void)
5264{
Victor Stinner50b48572018-11-01 01:51:40 +01005265 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005266 return tstate->coroutine_origin_tracking_depth;
5267}
5268
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005269int
Yury Selivanoveb636452016-09-08 22:01:51 -07005270_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5271{
Victor Stinner50b48572018-11-01 01:51:40 +01005272 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005273
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005274 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005275 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005276 }
5277
Yury Selivanoveb636452016-09-08 22:01:51 -07005278 Py_XINCREF(firstiter);
5279 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005280 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005281}
5282
5283PyObject *
5284_PyEval_GetAsyncGenFirstiter(void)
5285{
Victor Stinner50b48572018-11-01 01:51:40 +01005286 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005287 return tstate->async_gen_firstiter;
5288}
5289
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005290int
Yury Selivanoveb636452016-09-08 22:01:51 -07005291_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5292{
Victor Stinner50b48572018-11-01 01:51:40 +01005293 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005294
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005295 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005296 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005297 }
5298
Yury Selivanoveb636452016-09-08 22:01:51 -07005299 Py_XINCREF(finalizer);
5300 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005301 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005302}
5303
5304PyObject *
5305_PyEval_GetAsyncGenFinalizer(void)
5306{
Victor Stinner50b48572018-11-01 01:51:40 +01005307 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005308 return tstate->async_gen_finalizer;
5309}
5310
Victor Stinner438a12d2019-05-24 17:01:38 +02005311PyFrameObject *
5312PyEval_GetFrame(void)
5313{
5314 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005315 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005316}
5317
Guido van Rossumb209a111997-04-29 18:18:01 +00005318PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005319PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005320{
Victor Stinner438a12d2019-05-24 17:01:38 +02005321 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005322 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005323 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005324 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 else
5326 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005327}
5328
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005329/* Convenience function to get a builtin from its name */
5330PyObject *
5331_PyEval_GetBuiltinId(_Py_Identifier *name)
5332{
Victor Stinner438a12d2019-05-24 17:01:38 +02005333 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005334 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5335 if (attr) {
5336 Py_INCREF(attr);
5337 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005338 else if (!_PyErr_Occurred(tstate)) {
5339 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005340 }
5341 return attr;
5342}
5343
Guido van Rossumb209a111997-04-29 18:18:01 +00005344PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005345PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005346{
Victor Stinner438a12d2019-05-24 17:01:38 +02005347 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005348 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005349 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005350 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005352 }
5353
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005355 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005357
5358 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005359 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005360}
5361
Guido van Rossumb209a111997-04-29 18:18:01 +00005362PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005363PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005364{
Victor Stinner438a12d2019-05-24 17:01:38 +02005365 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005366 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005367 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005369 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005370
5371 assert(current_frame->f_globals != NULL);
5372 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005373}
5374
Guido van Rossum6135a871995-01-09 17:53:26 +00005375int
Tim Peters5ba58662001-07-16 02:29:45 +00005376PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005377{
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005379 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 if (current_frame != NULL) {
5383 const int codeflags = current_frame->f_code->co_flags;
5384 const int compilerflags = codeflags & PyCF_MASK;
5385 if (compilerflags) {
5386 result = 1;
5387 cf->cf_flags |= compilerflags;
5388 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005389#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 if (codeflags & CO_GENERATOR_ALLOWED) {
5391 result = 1;
5392 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5393 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005395 }
5396 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005397}
5398
Guido van Rossum3f5da241990-12-20 15:06:42 +00005399
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005400const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005401PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 if (PyMethod_Check(func))
5404 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5405 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005406 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 else if (PyCFunction_Check(func))
5408 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5409 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005410 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005411}
5412
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005413const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005414PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005416 if (PyMethod_Check(func))
5417 return "()";
5418 else if (PyFunction_Check(func))
5419 return "()";
5420 else if (PyCFunction_Check(func))
5421 return "()";
5422 else
5423 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005424}
5425
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005426#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005427if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005428 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005429 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005430 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005431 x = NULL; \
5432 } \
5433 else { \
5434 x = call; \
5435 if (tstate->c_profilefunc != NULL) { \
5436 if (x == NULL) { \
5437 call_trace_protected(tstate->c_profilefunc, \
5438 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005439 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005440 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 /* XXX should pass (type, value, tb) */ \
5442 } else { \
5443 if (call_trace(tstate->c_profilefunc, \
5444 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005445 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005446 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 Py_DECREF(x); \
5448 x = NULL; \
5449 } \
5450 } \
5451 } \
5452 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005453} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005454 x = call; \
5455 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005456
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005457
5458static PyObject *
5459trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005460 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005461 PyObject *func,
5462 PyObject **args, Py_ssize_t nargs,
5463 PyObject *kwnames)
5464{
5465 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005466 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005467 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005468 return x;
5469 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005470 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005471 /* We need to create a temporary bound method as argument
5472 for profiling.
5473
5474 If nargs == 0, then this cannot work because we have no
5475 "self". In any case, the call itself would raise
5476 TypeError (foo needs an argument), so we just skip
5477 profiling. */
5478 PyObject *self = args[0];
5479 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5480 if (func == NULL) {
5481 return NULL;
5482 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005483 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005484 args+1, nargs-1,
5485 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005486 Py_DECREF(func);
5487 return x;
5488 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005489 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005490}
5491
Victor Stinner415c5102017-01-11 00:54:57 +01005492/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5493 to reduce the stack consumption. */
5494Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005495call_function(PyThreadState *tstate,
5496 PyCodeAddressRange *bounds,
5497 PyObject ***pp_stack,
5498 Py_ssize_t oparg,
5499 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005500{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005501 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005502 PyObject *func = *pfunc;
5503 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005504 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5505 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005506 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005507
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005508 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005509 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005510 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005511 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005512 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005513 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005514
Victor Stinner438a12d2019-05-24 17:01:38 +02005515 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005516
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005517 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 while ((*pp_stack) > pfunc) {
5519 w = EXT_POP(*pp_stack);
5520 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005521 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005524}
5525
Jeremy Hylton52820442001-01-03 23:52:36 +00005526static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005527do_call_core(PyThreadState *tstate,
5528 PyCodeAddressRange *bounds,
5529 PyObject *func,
5530 PyObject *callargs,
5531 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005532{
jdemeyere89de732018-09-19 12:06:20 +02005533 PyObject *result;
5534
scoder4c9ea092020-05-12 16:12:41 +02005535 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005536 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005537 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005539 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005540 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5541 if (nargs > 0 && tstate->use_tracing) {
5542 /* We need to create a temporary bound method as argument
5543 for profiling.
5544
5545 If nargs == 0, then this cannot work because we have no
5546 "self". In any case, the call itself would raise
5547 TypeError (foo needs an argument), so we just skip
5548 profiling. */
5549 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5550 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5551 if (func == NULL) {
5552 return NULL;
5553 }
5554
Victor Stinner4d231bc2019-11-14 13:36:21 +01005555 C_TRACE(result, _PyObject_FastCallDictTstate(
5556 tstate, func,
5557 &_PyTuple_ITEMS(callargs)[1],
5558 nargs - 1,
5559 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005560 Py_DECREF(func);
5561 return result;
5562 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005563 }
jdemeyere89de732018-09-19 12:06:20 +02005564 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005565}
5566
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005567/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005568 nb_index slot defined, and store in *pi.
5569 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005570 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005571 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005572*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005573int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005574_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005575{
Victor Stinner438a12d2019-05-24 17:01:38 +02005576 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005577 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005578 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005579 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005581 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005582 return 0;
5583 }
5584 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005585 _PyErr_SetString(tstate, PyExc_TypeError,
5586 "slice indices must be integers or "
5587 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005588 return 0;
5589 }
5590 *pi = x;
5591 }
5592 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005593}
5594
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005595int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005596_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005597{
Victor Stinner438a12d2019-05-24 17:01:38 +02005598 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005599 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005600 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005601 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005602 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005603 return 0;
5604 }
5605 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005606 _PyErr_SetString(tstate, PyExc_TypeError,
5607 "slice indices must be integers or "
5608 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005609 return 0;
5610 }
5611 *pi = x;
5612 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005613}
5614
Thomas Wouters52152252000-08-17 22:55:00 +00005615static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005616import_name(PyThreadState *tstate, PyFrameObject *f,
5617 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005618{
5619 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005620 PyObject *import_func, *res;
5621 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005622
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005623 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005624 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005625 if (!_PyErr_Occurred(tstate)) {
5626 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005627 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005628 return NULL;
5629 }
5630
5631 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005632 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005633 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005634 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005635 return NULL;
5636 }
5637 res = PyImport_ImportModuleLevelObject(
5638 name,
5639 f->f_globals,
5640 f->f_locals == NULL ? Py_None : f->f_locals,
5641 fromlist,
5642 ilevel);
5643 return res;
5644 }
5645
5646 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005647
5648 stack[0] = name;
5649 stack[1] = f->f_globals;
5650 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5651 stack[3] = fromlist;
5652 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005653 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005654 Py_DECREF(import_func);
5655 return res;
5656}
5657
5658static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005659import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005661 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005662 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005663
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005664 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005665 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005666 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005667 /* Issue #17636: in case this failed because of a circular relative
5668 import, try to fallback on reading the module directly from
5669 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005670 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005671 if (pkgname == NULL) {
5672 goto error;
5673 }
Oren Milman6db70332017-09-19 14:23:01 +03005674 if (!PyUnicode_Check(pkgname)) {
5675 Py_CLEAR(pkgname);
5676 goto error;
5677 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005678 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005679 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005680 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005681 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005682 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005683 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005684 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005685 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005686 goto error;
5687 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005688 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005689 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005690 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005691 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005692 if (pkgname == NULL) {
5693 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5694 if (pkgname_or_unknown == NULL) {
5695 Py_XDECREF(pkgpath);
5696 return NULL;
5697 }
5698 } else {
5699 pkgname_or_unknown = pkgname;
5700 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005701
5702 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005703 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005704 errmsg = PyUnicode_FromFormat(
5705 "cannot import name %R from %R (unknown location)",
5706 name, pkgname_or_unknown
5707 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005708 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005709 PyErr_SetImportError(errmsg, pkgname, NULL);
5710 }
5711 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005712 _Py_IDENTIFIER(__spec__);
5713 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005714 const char *fmt =
5715 _PyModuleSpec_IsInitializing(spec) ?
5716 "cannot import name %R from partially initialized module %R "
5717 "(most likely due to a circular import) (%S)" :
5718 "cannot import name %R from %R (%S)";
5719 Py_XDECREF(spec);
5720
5721 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005722 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005723 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005724 }
5725
Xiang Zhang4830f582017-03-21 11:13:42 +08005726 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005727 Py_XDECREF(pkgname_or_unknown);
5728 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005729 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005730}
Guido van Rossumac7be682001-01-17 15:42:30 +00005731
Thomas Wouters52152252000-08-17 22:55:00 +00005732static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005733import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005734{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005735 _Py_IDENTIFIER(__all__);
5736 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005737 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005738 int skip_leading_underscores = 0;
5739 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005740
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005741 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5742 return -1; /* Unexpected error */
5743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005744 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005745 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5746 return -1;
5747 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005748 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005749 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005750 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005751 return -1;
5752 }
5753 all = PyMapping_Keys(dict);
5754 Py_DECREF(dict);
5755 if (all == NULL)
5756 return -1;
5757 skip_leading_underscores = 1;
5758 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 for (pos = 0, err = 0; ; pos++) {
5761 name = PySequence_GetItem(all, pos);
5762 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005763 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005764 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005765 }
5766 else {
5767 _PyErr_Clear(tstate);
5768 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005769 break;
5770 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005771 if (!PyUnicode_Check(name)) {
5772 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5773 if (modname == NULL) {
5774 Py_DECREF(name);
5775 err = -1;
5776 break;
5777 }
5778 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005779 _PyErr_Format(tstate, PyExc_TypeError,
5780 "module __name__ must be a string, not %.100s",
5781 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005782 }
5783 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005784 _PyErr_Format(tstate, PyExc_TypeError,
5785 "%s in %U.%s must be str, not %.100s",
5786 skip_leading_underscores ? "Key" : "Item",
5787 modname,
5788 skip_leading_underscores ? "__dict__" : "__all__",
5789 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005790 }
5791 Py_DECREF(modname);
5792 Py_DECREF(name);
5793 err = -1;
5794 break;
5795 }
5796 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005797 if (PyUnicode_READY(name) == -1) {
5798 Py_DECREF(name);
5799 err = -1;
5800 break;
5801 }
5802 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5803 Py_DECREF(name);
5804 continue;
5805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 }
5807 value = PyObject_GetAttr(v, name);
5808 if (value == NULL)
5809 err = -1;
5810 else if (PyDict_CheckExact(locals))
5811 err = PyDict_SetItem(locals, name, value);
5812 else
5813 err = PyObject_SetItem(locals, name, value);
5814 Py_DECREF(name);
5815 Py_XDECREF(value);
5816 if (err != 0)
5817 break;
5818 }
5819 Py_DECREF(all);
5820 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005821}
5822
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005823static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005824check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005825{
Victor Stinnera102ed72020-02-07 02:24:48 +01005826 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005827 /* check_args_iterable() may be called with a live exception:
5828 * clear it to prevent calling _PyObject_FunctionStr() with an
5829 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005830 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005831 PyObject *funcstr = _PyObject_FunctionStr(func);
5832 if (funcstr != NULL) {
5833 _PyErr_Format(tstate, PyExc_TypeError,
5834 "%U argument after * must be an iterable, not %.200s",
5835 funcstr, Py_TYPE(args)->tp_name);
5836 Py_DECREF(funcstr);
5837 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005838 return -1;
5839 }
5840 return 0;
5841}
5842
5843static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005844format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005845{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005846 /* _PyDict_MergeEx raises attribute
5847 * error (percolated from an attempt
5848 * to get 'keys' attribute) instead of
5849 * a type error if its second argument
5850 * is not a mapping.
5851 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005852 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005853 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005854 PyObject *funcstr = _PyObject_FunctionStr(func);
5855 if (funcstr != NULL) {
5856 _PyErr_Format(
5857 tstate, PyExc_TypeError,
5858 "%U argument after ** must be a mapping, not %.200s",
5859 funcstr, Py_TYPE(kwargs)->tp_name);
5860 Py_DECREF(funcstr);
5861 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005862 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005863 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005864 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005865 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005866 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005867 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005868 PyObject *funcstr = _PyObject_FunctionStr(func);
5869 if (funcstr != NULL) {
5870 PyObject *key = PyTuple_GET_ITEM(val, 0);
5871 _PyErr_Format(
5872 tstate, PyExc_TypeError,
5873 "%U got multiple values for keyword argument '%S'",
5874 funcstr, key);
5875 Py_DECREF(funcstr);
5876 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005877 Py_XDECREF(exc);
5878 Py_XDECREF(val);
5879 Py_XDECREF(tb);
5880 }
5881 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005882 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005883 }
5884 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005885}
5886
Guido van Rossumac7be682001-01-17 15:42:30 +00005887static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005888format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5889 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005893 if (!obj)
5894 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005895
Serhiy Storchaka06515832016-11-20 09:13:07 +02005896 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005897 if (!obj_str)
5898 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005899
Victor Stinner438a12d2019-05-24 17:01:38 +02005900 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005901}
Guido van Rossum950361c1997-01-24 13:49:28 +00005902
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005903static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005904format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005905{
5906 PyObject *name;
5907 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005908 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005909 return;
5910 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5911 name = PyTuple_GET_ITEM(co->co_cellvars,
5912 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005913 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005914 PyExc_UnboundLocalError,
5915 UNBOUNDLOCAL_ERROR_MSG,
5916 name);
5917 } else {
5918 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5919 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005920 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005921 UNBOUNDFREE_ERROR_MSG, name);
5922 }
5923}
5924
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005925static void
Mark Shannonfee55262019-11-21 09:11:43 +00005926format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005927{
5928 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5929 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005930 _PyErr_Format(tstate, PyExc_TypeError,
5931 "'async with' received an object from __aenter__ "
5932 "that does not implement __await__: %.100s",
5933 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005934 }
Mark Shannonfee55262019-11-21 09:11:43 +00005935 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005936 _PyErr_Format(tstate, PyExc_TypeError,
5937 "'async with' received an object from __aexit__ "
5938 "that does not implement __await__: %.100s",
5939 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005940 }
5941 }
5942}
5943
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005944static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005945unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005946 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005947{
5948 PyObject *res;
5949 if (Py_REFCNT(v) == 2) {
5950 /* In the common case, there are 2 references to the value
5951 * stored in 'variable' when the += is performed: one on the
5952 * value stack (in 'v') and one still stored in the
5953 * 'variable'. We try to delete the variable now to reduce
5954 * the refcnt to 1.
5955 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005956 int opcode, oparg;
5957 NEXTOPARG();
5958 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005959 case STORE_FAST:
5960 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005961 PyObject **fastlocals = f->f_localsplus;
5962 if (GETLOCAL(oparg) == v)
5963 SETLOCAL(oparg, NULL);
5964 break;
5965 }
5966 case STORE_DEREF:
5967 {
5968 PyObject **freevars = (f->f_localsplus +
5969 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005970 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005971 if (PyCell_GET(c) == v) {
5972 PyCell_SET(c, NULL);
5973 Py_DECREF(v);
5974 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005975 break;
5976 }
5977 case STORE_NAME:
5978 {
5979 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005980 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005981 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005982 if (locals && PyDict_CheckExact(locals)) {
5983 PyObject *w = PyDict_GetItemWithError(locals, name);
5984 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005985 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005986 {
5987 Py_DECREF(v);
5988 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005989 }
5990 }
5991 break;
5992 }
5993 }
5994 }
5995 res = v;
5996 PyUnicode_Append(&res, w);
5997 return res;
5998}
5999
Guido van Rossum950361c1997-01-24 13:49:28 +00006000#ifdef DYNAMIC_EXECUTION_PROFILE
6001
Skip Montanarof118cb12001-10-15 20:51:38 +00006002static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006003getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006005 int i;
6006 PyObject *l = PyList_New(256);
6007 if (l == NULL) return NULL;
6008 for (i = 0; i < 256; i++) {
6009 PyObject *x = PyLong_FromLong(a[i]);
6010 if (x == NULL) {
6011 Py_DECREF(l);
6012 return NULL;
6013 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006014 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006015 }
6016 for (i = 0; i < 256; i++)
6017 a[i] = 0;
6018 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006019}
6020
6021PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006022_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006023{
6024#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006025 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006026#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006027 int i;
6028 PyObject *l = PyList_New(257);
6029 if (l == NULL) return NULL;
6030 for (i = 0; i < 257; i++) {
6031 PyObject *x = getarray(dxpairs[i]);
6032 if (x == NULL) {
6033 Py_DECREF(l);
6034 return NULL;
6035 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006036 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006037 }
6038 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006039#endif
6040}
6041
6042#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006043
6044Py_ssize_t
6045_PyEval_RequestCodeExtraIndex(freefunc free)
6046{
Victor Stinner81a7be32020-04-14 15:14:01 +02006047 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006048 Py_ssize_t new_index;
6049
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006050 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006051 return -1;
6052 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006053 new_index = interp->co_extra_user_count++;
6054 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006055 return new_index;
6056}
Łukasz Langaa785c872016-09-09 17:37:37 -07006057
6058static void
6059dtrace_function_entry(PyFrameObject *f)
6060{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006061 const char *filename;
6062 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006063 int lineno;
6064
Victor Stinner6d86a232020-04-29 00:56:58 +02006065 PyCodeObject *code = f->f_code;
6066 filename = PyUnicode_AsUTF8(code->co_filename);
6067 funcname = PyUnicode_AsUTF8(code->co_name);
6068 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006069
Andy Lestere6be9b52020-02-11 20:28:35 -06006070 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006071}
6072
6073static void
6074dtrace_function_return(PyFrameObject *f)
6075{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006076 const char *filename;
6077 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006078 int lineno;
6079
Victor Stinner6d86a232020-04-29 00:56:58 +02006080 PyCodeObject *code = f->f_code;
6081 filename = PyUnicode_AsUTF8(code->co_filename);
6082 funcname = PyUnicode_AsUTF8(code->co_name);
6083 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006084
Andy Lestere6be9b52020-02-11 20:28:35 -06006085 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006086}
6087
6088/* DTrace equivalent of maybe_call_line_trace. */
6089static void
6090maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00006091 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006092{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006093 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006094
6095 /* If the last instruction executed isn't in the current
6096 instruction window, reset the window.
6097 */
Mark Shannon877df852020-11-12 09:43:29 +00006098 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006099 /* If the last instruction falls at the start of a line or if
6100 it represents a jump backwards, update the frame's line
6101 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00006102 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
6103 if (line != -1) {
6104 frame->f_lineno = line;
6105 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6106 if (!co_filename)
6107 co_filename = "?";
6108 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6109 if (!co_name)
6110 co_name = "?";
6111 PyDTrace_LINE(co_filename, co_name, line);
6112 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006113 }
6114 *instr_prev = frame->f_lasti;
6115}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006116
6117
6118/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6119 for the limited API. */
6120
6121#undef Py_EnterRecursiveCall
6122
6123int Py_EnterRecursiveCall(const char *where)
6124{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006125 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006126}
6127
6128#undef Py_LeaveRecursiveCall
6129
6130void Py_LeaveRecursiveCall(void)
6131{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006132 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006133}