blob: 3b67a6b79bfb76530509b9f81cba7c46afc10f32 [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 Stinnerdda5d6e2020-04-08 17:54:59 +0200301 if (!_Py_IsMainInterpreter(tstate)) {
302 /* 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 Stinnerdda5d6e2020-04-08 17:54:59 +0200326_PyEval_FiniGIL(PyThreadState *tstate)
327{
Victor Stinner7be4e352020-05-05 20:27:47 +0200328#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200329 if (!_Py_IsMainInterpreter(tstate)) {
330 /* 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
338 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
339#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200340 struct _gil_runtime_state *gil = &tstate->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 Stinner56bfdeb2020-03-18 09:26:25 +0100505_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000506{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200507 assert(is_tstate_valid(tstate));
508 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000509}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000510
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000511PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000512PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000513{
Victor Stinner09532fe2019-05-10 23:39:09 +0200514 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200515#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
516 PyThreadState *old_tstate = _PyThreadState_GET();
517 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
518#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200519 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200520#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200521 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100522
Victor Stinner0b1e3302020-05-05 16:14:31 +0200523 struct _ceval_runtime_state *ceval = &runtime->ceval;
524 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200525#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
526 assert(gil_created(&ceval2->gil));
527#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200528 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200529#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200530 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532}
533
534void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000536{
Victor Stinner3026cad2020-06-01 16:02:40 +0200537 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100538
Victor Stinner85f5a692020-03-09 22:12:04 +0100539 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100540
Victor Stinner85f5a692020-03-09 22:12:04 +0100541 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
542 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000543}
544
545
Guido van Rossuma9672091994-09-14 13:31:22 +0000546/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
547 signal handlers or Mac I/O completion routines) can schedule calls
548 to a function to be called synchronously.
549 The synchronous function is called with one void* argument.
550 It should return 0 for success or -1 for failure -- failure should
551 be accompanied by an exception.
552
553 If registry succeeds, the registry function returns 0; if it fails
554 (e.g. due to too many pending calls) it returns -1 (without setting
555 an exception condition).
556
557 Note that because registry may occur from within signal handlers,
558 or other asynchronous events, calling malloc() is unsafe!
559
Guido van Rossuma9672091994-09-14 13:31:22 +0000560 Any thread can schedule pending calls, but only the main thread
561 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000562 There is no facility to schedule calls to a particular thread, but
563 that should be easy to change, should that ever be required. In
564 that case, the static variables here should go into the python
565 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000566*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000567
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200568void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200569_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200570{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100571#ifdef MS_WINDOWS
572 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
573 // handler which can run in a thread different than the Python thread, in
574 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
575 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
576 //
577 // The next eval_frame_handle_pending() call will call
578 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
579 int force = 1;
580#else
581 int force = 0;
582#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200583 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100584 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200585 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100586 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200587}
588
Eric Snow5be45a62019-03-08 22:47:07 -0700589/* Push one item onto the queue while holding the lock. */
590static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200591_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600592 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700593{
Eric Snow842a2f02019-03-15 15:47:51 -0600594 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700595 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600596 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700597 return -1; /* Queue full */
598 }
Eric Snow842a2f02019-03-15 15:47:51 -0600599 pending->calls[i].func = func;
600 pending->calls[i].arg = arg;
601 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700602 return 0;
603}
604
605/* Pop one item off the queue while holding the lock. */
606static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200607_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600608 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700609{
Eric Snow842a2f02019-03-15 15:47:51 -0600610 int i = pending->first;
611 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700612 return; /* Queue empty */
613 }
614
Eric Snow842a2f02019-03-15 15:47:51 -0600615 *func = pending->calls[i].func;
616 *arg = pending->calls[i].arg;
617 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700618}
619
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200620/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000621 scheduling to be made from any thread, and even from an executing
622 callback.
623 */
624
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000625int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200626_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200627 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000628{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200629 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600630
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200631 /* Ensure that _PyEval_InitPendingCalls() was called
632 and that _PyEval_FiniPendingCalls() is not called yet. */
633 assert(pending->lock != NULL);
634
Eric Snow842a2f02019-03-15 15:47:51 -0600635 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200636 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600637 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700638
Victor Stinnere225beb2019-06-03 18:14:24 +0200639 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200640 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000642}
643
Victor Stinner09532fe2019-05-10 23:39:09 +0200644int
645Py_AddPendingCall(int (*func)(void *), void *arg)
646{
Victor Stinner50e6e992020-03-19 02:41:21 +0100647 /* Best-effort to support subinterpreters and calls with the GIL released.
648
649 First attempt _PyThreadState_GET() since it supports subinterpreters.
650
651 If the GIL is released, _PyThreadState_GET() returns NULL . In this
652 case, use PyGILState_GetThisThreadState() which works even if the GIL
653 is released.
654
655 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
656 see bpo-10915 and bpo-15751.
657
Victor Stinner8849e592020-03-18 19:28:53 +0100658 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100659 PyThreadState *tstate = _PyThreadState_GET();
660 if (tstate == NULL) {
661 tstate = PyGILState_GetThisThreadState();
662 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200663
664 PyInterpreterState *interp;
665 if (tstate != NULL) {
666 interp = tstate->interp;
667 }
668 else {
669 /* Last resort: use the main interpreter */
670 interp = _PyRuntime.interpreters.main;
671 }
672 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200673}
674
Eric Snowfdf282d2019-01-11 14:26:55 -0700675static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100676handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700677{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200678 assert(is_tstate_valid(tstate));
679 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700680 return 0;
681 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700682
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200683 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100684 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
685 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100686 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700687 return -1;
688 }
689 return 0;
690}
691
692static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100693make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000694{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200695 assert(is_tstate_valid(tstate));
696
Victor Stinnerd8316882020-03-20 14:50:35 +0100697 /* only execute pending calls on main thread */
698 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200699 return 0;
700 }
701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100703 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700704 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700706 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200707 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100708
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200709 /* unsignal before starting to call callbacks, so that any callback
710 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200711 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700712 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100715 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700716 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700717 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 void *arg = NULL;
719
720 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600721 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200722 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600723 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700724
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100725 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700726 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100727 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700728 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700729 res = func(arg);
730 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200731 goto error;
732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200734
Charles-François Natalif23339a2011-07-23 18:15:43 +0200735 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700736 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200737
738error:
739 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200740 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700741 return res;
742}
743
Eric Snow842a2f02019-03-15 15:47:51 -0600744void
Victor Stinner2b1df452020-01-13 18:46:59 +0100745_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600746{
Eric Snow842a2f02019-03-15 15:47:51 -0600747 assert(PyGILState_Check());
748
Victor Stinner50e6e992020-03-19 02:41:21 +0100749 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200750
Eric Snow842a2f02019-03-15 15:47:51 -0600751 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
752 return;
753 }
754
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100755 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200756 PyObject *exc, *val, *tb;
757 _PyErr_Fetch(tstate, &exc, &val, &tb);
758 PyErr_BadInternalCall();
759 _PyErr_ChainExceptions(exc, val, tb);
760 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600761 }
762}
763
Eric Snowfdf282d2019-01-11 14:26:55 -0700764/* Py_MakePendingCalls() is a simple wrapper for the sake
765 of backward-compatibility. */
766int
767Py_MakePendingCalls(void)
768{
769 assert(PyGILState_Check());
770
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771 PyThreadState *tstate = _PyThreadState_GET();
772
Eric Snowfdf282d2019-01-11 14:26:55 -0700773 /* Python signal handler doesn't really queue a callback: it only signals
774 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100775 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700776 if (res != 0) {
777 return res;
778 }
779
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100780 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600781 if (res != 0) {
782 return res;
783 }
784
785 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000786}
787
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000788/* The interpreter's recursion limit */
789
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000790#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200791# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000792#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600793
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600794void
Victor Stinnerdab84232020-03-17 18:56:44 +0100795_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600796{
Victor Stinner7be4e352020-05-05 20:27:47 +0200797#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100798 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200799#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100800}
801
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200802int
Victor Stinnerdab84232020-03-17 18:56:44 +0100803_PyEval_InitState(struct _ceval_state *ceval)
804{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200805 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
806
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200807 struct _pending_calls *pending = &ceval->pending;
808 assert(pending->lock == NULL);
809
810 pending->lock = PyThread_allocate_lock();
811 if (pending->lock == NULL) {
812 return -1;
813 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200814
815#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
816 _gil_initialize(&ceval->gil);
817#endif
818
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200819 return 0;
820}
821
822void
823_PyEval_FiniState(struct _ceval_state *ceval)
824{
825 struct _pending_calls *pending = &ceval->pending;
826 if (pending->lock != NULL) {
827 PyThread_free_lock(pending->lock);
828 pending->lock = NULL;
829 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600830}
831
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000832int
833Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000834{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200835 PyInterpreterState *interp = _PyInterpreterState_GET();
836 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000837}
838
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000839void
840Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000841{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200842 PyThreadState *tstate = _PyThreadState_GET();
843 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000844}
845
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100846/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200847 if the recursion_depth reaches recursion_limit.
848 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000849 to guarantee that _Py_CheckRecursiveCall() is regularly called.
850 Without USE_STACKCHECK, there is no need for this. */
851int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100852_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000853{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200854 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000855
856#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700857 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (PyOS_CheckStack()) {
859 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200860 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 return -1;
862 }
pdox18967932017-10-25 23:03:01 -0700863#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000864 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (tstate->recursion_depth > recursion_limit + 50) {
866 /* Overflowing while handling an overflow. Give up. */
867 Py_FatalError("Cannot recover from stack overflow.");
868 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000870 else {
871 if (tstate->recursion_depth > recursion_limit) {
872 tstate->recursion_headroom++;
873 _PyErr_Format(tstate, PyExc_RecursionError,
874 "maximum recursion depth exceeded%s",
875 where);
876 tstate->recursion_headroom--;
877 --tstate->recursion_depth;
878 return -1;
879 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
881 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000882}
883
Victor Stinner09532fe2019-05-10 23:39:09 +0200884static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200885static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000886
Victor Stinnere225beb2019-06-03 18:14:24 +0200887#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889
Guido van Rossumb209a111997-04-29 18:18:01 +0000890PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000891PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000892{
Mark Shannon0332e562021-02-01 10:42:03 +0000893 if (locals == NULL) {
894 locals = globals;
895 }
896 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
897 if (builtins == NULL) {
898 return NULL;
899 }
900 PyFrameConstructor desc = {
901 .fc_globals = globals,
902 .fc_builtins = builtins,
903 .fc_name = ((PyCodeObject *)co)->co_name,
904 .fc_qualname = ((PyCodeObject *)co)->co_name,
905 .fc_code = co,
906 .fc_defaults = NULL,
907 .fc_kwdefaults = NULL,
908 .fc_closure = NULL
909 };
910 PyThreadState *tstate = PyThreadState_GET();
911 PyObject *res =_PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
912 Py_DECREF(builtins);
913 return res;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000914}
915
916
917/* Interpreter main loop */
918
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000919PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100920PyEval_EvalFrame(PyFrameObject *f)
921{
Victor Stinner0b72b232020-03-12 23:18:39 +0100922 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100923 PyThreadState *tstate = _PyThreadState_GET();
924 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000925}
926
927PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000929{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100930 PyThreadState *tstate = _PyThreadState_GET();
931 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700932}
933
Victor Stinnerda2914d2020-03-20 09:29:08 +0100934
935/* Handle signals, pending calls, GIL drop request
936 and asynchronous exception */
937static int
938eval_frame_handle_pending(PyThreadState *tstate)
939{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100940 _PyRuntimeState * const runtime = &_PyRuntime;
941 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200942
943 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200944 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100945 if (handle_signals(tstate) != 0) {
946 return -1;
947 }
948 }
949
950 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200951 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100952 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
953 if (make_pending_calls(tstate) != 0) {
954 return -1;
955 }
956 }
957
958 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200959 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100960 /* Give another thread a chance */
961 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
962 Py_FatalError("tstate mix-up");
963 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200964 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100965
966 /* Other threads may run now */
967
968 take_gil(tstate);
969
Victor Stinnere838a932020-05-05 19:56:48 +0200970#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
971 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
972#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100973 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
974 Py_FatalError("orphan tstate");
975 }
Victor Stinnere838a932020-05-05 19:56:48 +0200976#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100977 }
978
979 /* Check for asynchronous exception. */
980 if (tstate->async_exc != NULL) {
981 PyObject *exc = tstate->async_exc;
982 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200983 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100984 _PyErr_SetNone(tstate, exc);
985 Py_DECREF(exc);
986 return -1;
987 }
988
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100989#ifdef MS_WINDOWS
990 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
991 // different thread than the Python thread, in which case
992 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
993 // current Python thread with the correct _Py_ThreadCanHandleSignals()
994 // value. It prevents to interrupt the eval loop at every instruction if
995 // the current Python thread cannot handle signals (if
996 // _Py_ThreadCanHandleSignals() is false).
997 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
998#endif
999
Victor Stinnerda2914d2020-03-20 09:29:08 +01001000 return 0;
1001}
1002
Victor Stinnerc6944e72016-11-11 02:13:35 +01001003PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001004_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001005{
Victor Stinner3026cad2020-06-01 16:02:40 +02001006 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001007
Guido van Rossum950361c1997-01-24 13:49:28 +00001008#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001010#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001011 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001012 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001013 int opcode; /* Current opcode */
1014 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001015 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001017 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001018 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 is true when the line being executed has changed. The
1026 initial values are such as to make this false the first
1027 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001028
Serhiy Storchakaab874002016-09-11 13:48:15 +03001029 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyObject *names;
1031 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001032 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001033
Brett Cannon368b4b72012-04-02 12:17:59 -04001034#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001035 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001036#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001037
Antoine Pitroub52ec782009-01-25 16:34:23 +00001038/* Computed GOTOs, or
1039 the-optimization-commonly-but-improperly-known-as-"threaded code"
1040 using gcc's labels-as-values extension
1041 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1042
1043 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001045 combined with a lookup table of jump addresses. However, since the
1046 indirect jump instruction is shared by all opcodes, the CPU will have a
1047 hard time making the right prediction for where to jump next (actually,
1048 it will be always wrong except in the uncommon case of a sequence of
1049 several identical opcodes).
1050
1051 "Threaded code" in contrast, uses an explicit jump table and an explicit
1052 indirect jump instruction at the end of each opcode. Since the jump
1053 instruction is at a different address for each opcode, the CPU will make a
1054 separate prediction for each of these instructions, which is equivalent to
1055 predicting the second opcode of each opcode pair. These predictions have
1056 a much better chance to turn out valid, especially in small bytecode loops.
1057
1058 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001060 and potentially many more instructions (depending on the pipeline width).
1061 A correctly predicted branch, however, is nearly free.
1062
1063 At the time of this writing, the "threaded code" version is up to 15-20%
1064 faster than the normal "switch" version, depending on the compiler and the
1065 CPU architecture.
1066
1067 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1068 because it would render the measurements invalid.
1069
1070
1071 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1072 indirect jumps by sharing them between all opcodes. Such optimizations
1073 can be disabled on gcc by using the -fno-gcse flag (or possibly
1074 -fno-crossjumping).
1075*/
1076
Antoine Pitrou042b1282010-08-13 21:15:58 +00001077#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001078#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001079#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001080#endif
1081
Antoine Pitrou042b1282010-08-13 21:15:58 +00001082#ifdef HAVE_COMPUTED_GOTOS
1083 #ifndef USE_COMPUTED_GOTOS
1084 #define USE_COMPUTED_GOTOS 1
1085 #endif
1086#else
1087 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1088 #error "Computed gotos are not supported on this compiler."
1089 #endif
1090 #undef USE_COMPUTED_GOTOS
1091 #define USE_COMPUTED_GOTOS 0
1092#endif
1093
1094#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001095/* Import the static jump table */
1096#include "opcode_targets.h"
1097
Antoine Pitroub52ec782009-01-25 16:34:23 +00001098#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001099 op: \
1100 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001101
Antoine Pitroub52ec782009-01-25 16:34:23 +00001102#ifdef LLTRACE
1103#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001105 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001107 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001108 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 } \
1110 goto fast_next_opcode; \
1111 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001112#else
1113#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001115 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001117 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001118 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 } \
1120 goto fast_next_opcode; \
1121 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001122#endif
1123
Victor Stinner09532fe2019-05-10 23:39:09 +02001124#define DISPATCH() \
1125 { \
1126 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1127 FAST_DISPATCH(); \
1128 } \
1129 continue; \
1130 }
1131
Antoine Pitroub52ec782009-01-25 16:34:23 +00001132#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001133#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001134#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001135#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001136#endif
1137
1138
Neal Norwitza81d2202002-07-14 00:27:26 +00001139/* Tuple access macros */
1140
1141#ifndef Py_DEBUG
1142#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1143#else
1144#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1145#endif
1146
Guido van Rossum374a9221991-04-04 10:40:29 +00001147/* Code access macros */
1148
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001149/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001150#define INSTR_OFFSET() \
1151 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001152#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001153 _Py_CODEUNIT word = *next_instr; \
1154 opcode = _Py_OPCODE(word); \
1155 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001156 next_instr++; \
1157 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001158#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1159#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001160
Raymond Hettingerf606f872003-03-16 03:11:04 +00001161/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 Some opcodes tend to come in pairs thus making it possible to
1163 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001164 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 Verifying the prediction costs a single high-speed test of a register
1167 variable against a constant. If the pairing was good, then the
1168 processor's own internal branch predication has a high likelihood of
1169 success, resulting in a nearly zero-overhead transition to the
1170 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001171 including its unpredictable switch-case branch. Combined with the
1172 processor's internal branch prediction, a successful PREDICT has the
1173 effect of making the two opcodes run as if they were a single new opcode
1174 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001175
Georg Brandl86b2fb92008-07-16 03:43:04 +00001176 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 predictions turned-on and interpret the results as if some opcodes
1178 had been combined or turn-off predictions so that the opcode frequency
1179 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001180
1181 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 the CPU to record separate branch prediction information for each
1183 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001184
Raymond Hettingerf606f872003-03-16 03:11:04 +00001185*/
1186
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001187#define PREDICT_ID(op) PRED_##op
1188
Antoine Pitrou042b1282010-08-13 21:15:58 +00001189#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001190#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001191#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001192#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001193 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001194 _Py_CODEUNIT word = *next_instr; \
1195 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001196 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001197 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001198 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001199 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001200 } \
1201 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001202#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001203#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001204
Raymond Hettingerf606f872003-03-16 03:11:04 +00001205
Guido van Rossum374a9221991-04-04 10:40:29 +00001206/* Stack manipulation macros */
1207
Martin v. Löwis18e16552006-02-15 17:27:45 +00001208/* The stack can grow at most MAXINT deep, as co_nlocals and
1209 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001210#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1211#define EMPTY() (STACK_LEVEL() == 0)
1212#define TOP() (stack_pointer[-1])
1213#define SECOND() (stack_pointer[-2])
1214#define THIRD() (stack_pointer[-3])
1215#define FOURTH() (stack_pointer[-4])
1216#define PEEK(n) (stack_pointer[-(n)])
1217#define SET_TOP(v) (stack_pointer[-1] = (v))
1218#define SET_SECOND(v) (stack_pointer[-2] = (v))
1219#define SET_THIRD(v) (stack_pointer[-3] = (v))
1220#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001221#define BASIC_STACKADJ(n) (stack_pointer += n)
1222#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1223#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001224
Guido van Rossum96a42c81992-01-12 02:29:51 +00001225#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001227 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001228 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001229#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001230 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001231#define STACK_GROW(n) do { \
1232 assert(n >= 0); \
1233 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001234 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001235 assert(STACK_LEVEL() <= co->co_stacksize); \
1236 } while (0)
1237#define STACK_SHRINK(n) do { \
1238 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001239 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001240 (void)(BASIC_STACKADJ(-n)); \
1241 assert(STACK_LEVEL() <= co->co_stacksize); \
1242 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001243#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001244 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001245 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001246#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001247#define PUSH(v) BASIC_PUSH(v)
1248#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001249#define STACK_GROW(n) BASIC_STACKADJ(n)
1250#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001251#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001252#endif
1253
Guido van Rossum681d79a1995-07-18 14:51:37 +00001254/* Local variable macros */
1255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001257
1258/* The SETLOCAL() macro must not DECREF the local variable in-place and
1259 then store the new value; it must copy the old value to a temporary
1260 value, then store the new value, and then DECREF the temporary value.
1261 This is because it is possible that during the DECREF the frame is
1262 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1263 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001265 GETLOCAL(i) = value; \
1266 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001267
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001268
1269#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 while (STACK_LEVEL() > (b)->b_level) { \
1271 PyObject *v = POP(); \
1272 Py_XDECREF(v); \
1273 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001274
1275#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001276 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001278 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1280 while (STACK_LEVEL() > (b)->b_level + 3) { \
1281 value = POP(); \
1282 Py_XDECREF(value); \
1283 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001284 exc_info = tstate->exc_info; \
1285 type = exc_info->exc_type; \
1286 value = exc_info->exc_value; \
1287 traceback = exc_info->exc_traceback; \
1288 exc_info->exc_type = POP(); \
1289 exc_info->exc_value = POP(); \
1290 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 Py_XDECREF(type); \
1292 Py_XDECREF(value); \
1293 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001294 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001295
Inada Naoki91234a12019-06-03 21:30:58 +09001296 /* macros for opcode cache */
1297#define OPCACHE_CHECK() \
1298 do { \
1299 co_opcache = NULL; \
1300 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001301 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001302 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001303 if (co_opcache_offset > 0) { \
1304 assert(co_opcache_offset <= co->co_opcache_size); \
1305 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001306 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001307 } \
1308 } \
1309 } while (0)
1310
Pablo Galindo109826c2020-10-20 06:22:44 +01001311#define OPCACHE_DEOPT() \
1312 do { \
1313 if (co_opcache != NULL) { \
1314 co_opcache->optimized = -1; \
1315 unsigned char co_opcache_offset = \
1316 co->co_opcache_map[next_instr - first_instr]; \
1317 assert(co_opcache_offset <= co->co_opcache_size); \
1318 co->co_opcache_map[co_opcache_offset] = 0; \
1319 co_opcache = NULL; \
1320 } \
1321 } while (0)
1322
1323#define OPCACHE_DEOPT_LOAD_ATTR() \
1324 do { \
1325 if (co_opcache != NULL) { \
1326 OPCACHE_STAT_ATTR_DEOPT(); \
1327 OPCACHE_DEOPT(); \
1328 } \
1329 } while (0)
1330
1331#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1332 do { \
1333 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1334 OPCACHE_DEOPT_LOAD_ATTR(); \
1335 } \
1336 } while (0)
1337
Inada Naoki91234a12019-06-03 21:30:58 +09001338#if OPCACHE_STATS
1339
1340#define OPCACHE_STAT_GLOBAL_HIT() \
1341 do { \
1342 if (co->co_opcache != NULL) opcache_global_hits++; \
1343 } while (0)
1344
1345#define OPCACHE_STAT_GLOBAL_MISS() \
1346 do { \
1347 if (co->co_opcache != NULL) opcache_global_misses++; \
1348 } while (0)
1349
1350#define OPCACHE_STAT_GLOBAL_OPT() \
1351 do { \
1352 if (co->co_opcache != NULL) opcache_global_opts++; \
1353 } while (0)
1354
Pablo Galindo109826c2020-10-20 06:22:44 +01001355#define OPCACHE_STAT_ATTR_HIT() \
1356 do { \
1357 if (co->co_opcache != NULL) opcache_attr_hits++; \
1358 } while (0)
1359
1360#define OPCACHE_STAT_ATTR_MISS() \
1361 do { \
1362 if (co->co_opcache != NULL) opcache_attr_misses++; \
1363 } while (0)
1364
1365#define OPCACHE_STAT_ATTR_OPT() \
1366 do { \
1367 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1368 } while (0)
1369
1370#define OPCACHE_STAT_ATTR_DEOPT() \
1371 do { \
1372 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1373 } while (0)
1374
1375#define OPCACHE_STAT_ATTR_TOTAL() \
1376 do { \
1377 if (co->co_opcache != NULL) opcache_attr_total++; \
1378 } while (0)
1379
Inada Naoki91234a12019-06-03 21:30:58 +09001380#else /* OPCACHE_STATS */
1381
1382#define OPCACHE_STAT_GLOBAL_HIT()
1383#define OPCACHE_STAT_GLOBAL_MISS()
1384#define OPCACHE_STAT_GLOBAL_OPT()
1385
Pablo Galindo109826c2020-10-20 06:22:44 +01001386#define OPCACHE_STAT_ATTR_HIT()
1387#define OPCACHE_STAT_ATTR_MISS()
1388#define OPCACHE_STAT_ATTR_OPT()
1389#define OPCACHE_STAT_ATTR_DEOPT()
1390#define OPCACHE_STAT_ATTR_TOTAL()
1391
Inada Naoki91234a12019-06-03 21:30:58 +09001392#endif
1393
Guido van Rossuma027efa1997-05-05 20:56:21 +00001394/* Start of code */
1395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001397 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001399 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001402 co = f->f_code;
1403 PyCodeAddressRange bounds;
1404 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (tstate->use_tracing) {
1407 if (tstate->c_tracefunc != NULL) {
1408 /* tstate->c_tracefunc, if defined, is a
1409 function that will be called on *every* entry
1410 to a code block. Its return value, if not
1411 None, is a function that will be called at
1412 the start of each executed line of code.
1413 (Actually, the function must return itself
1414 in order to continue tracing.) The trace
1415 functions are called with three arguments:
1416 a pointer to the current frame, a string
1417 indicating why the function is called, and
1418 an argument which depends on the situation.
1419 The global trace function is also called
1420 whenever an exception is detected. */
1421 if (call_trace_protected(tstate->c_tracefunc,
1422 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001423 tstate, f, &bounds,
1424 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 /* Trace function raised an error */
1426 goto exit_eval_frame;
1427 }
1428 }
1429 if (tstate->c_profilefunc != NULL) {
1430 /* Similar for c_profilefunc, except it needn't
1431 return itself and isn't called for "line" events */
1432 if (call_trace_protected(tstate->c_profilefunc,
1433 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001434 tstate, f, &bounds,
1435 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Profile function raised an error */
1437 goto exit_eval_frame;
1438 }
1439 }
1440 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001441
Łukasz Langaa785c872016-09-09 17:37:37 -07001442 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1443 dtrace_function_entry(f);
1444
Mark Shannon877df852020-11-12 09:43:29 +00001445 int instr_prev = -1;
1446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 names = co->co_names;
1448 consts = co->co_consts;
1449 fastlocals = f->f_localsplus;
1450 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001451 assert(PyBytes_Check(co->co_code));
1452 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001453 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1454 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1455 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001456 /*
1457 f->f_lasti refers to the index of the last instruction,
1458 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001459
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001460 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001461 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 When the PREDICT() macros are enabled, some opcode pairs follow in
1464 direct succession without updating f->f_lasti. A successful
1465 prediction effectively links the two codes together as if they
1466 were a single new opcode; accordingly,f->f_lasti will point to
1467 the first code in the pair (for instance, GET_ITER followed by
1468 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001469 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001471 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001472 next_instr = first_instr;
1473 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001474 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1475 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001476 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001477 stack_pointer = f->f_valuestack + f->f_stackdepth;
1478 /* Set f->f_stackdepth to -1.
1479 * Update when returning or calling trace function.
1480 Having f_stackdepth <= 0 ensures that invalid
1481 values are not visible to the cycle GC.
1482 We choose -1 rather than 0 to assist debugging.
1483 */
1484 f->f_stackdepth = -1;
1485 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001486
Inada Naoki91234a12019-06-03 21:30:58 +09001487 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1488 co->co_opcache_flag++;
1489 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1490 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001491 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001492 }
1493#if OPCACHE_STATS
1494 opcache_code_objects_extra_mem +=
1495 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1496 sizeof(_PyOpcache) * co->co_opcache_size;
1497 opcache_code_objects++;
1498#endif
1499 }
1500 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001501
Tim Peters5ca576e2001-06-18 22:08:13 +00001502#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001503 {
1504 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1505 if (r < 0) {
1506 goto exit_eval_frame;
1507 }
1508 lltrace = r;
1509 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001510#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001512 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001514 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515
Victor Stinnerace47d72013-07-18 01:41:08 +02001516#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001517 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001518 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001519 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001520 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001521#endif
1522
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001523main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1526 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001527 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 /* Do periodic things. Doing this every time through
1530 the loop would add too much overhead, so we do it
1531 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001532 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 event needs attention (e.g. a signal handler or
1534 async I/O handler); see Py_AddPendingCall() and
1535 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Eric Snow7bda9de2019-03-08 17:25:54 -07001537 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001538 opcode = _Py_OPCODE(*next_instr);
1539 if (opcode == SETUP_FINALLY ||
1540 opcode == SETUP_WITH ||
1541 opcode == BEFORE_ASYNC_WITH ||
1542 opcode == YIELD_FROM) {
1543 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001544 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001545 - If we're about to enter the 'with:'. It will prevent
1546 emitting a resource warning in the common idiom
1547 'with open(path) as file:'.
1548 - If we're about to enter the 'async with:'.
1549 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001550 *very* useful, but might help in some cases and it's
1551 traditional)
1552 - If we're resuming a chain of nested 'yield from' or
1553 'await' calls, then each frame is parked with YIELD_FROM
1554 as its next opcode. If the user hit control-C we want to
1555 wait until we've reached the innermost frame before
1556 running the signal handler and raising KeyboardInterrupt
1557 (see bpo-30039).
1558 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 goto fast_next_opcode;
1560 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001561
Victor Stinnerda2914d2020-03-20 09:29:08 +01001562 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 }
1565 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 fast_next_opcode:
1568 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Łukasz Langaa785c872016-09-09 17:37:37 -07001570 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001571 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001574
Victor Stinnerdab84232020-03-17 18:56:44 +01001575 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001576 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001578 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001580 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 err = maybe_call_line_trace(tstate->c_tracefunc,
1583 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001584 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001585 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 /* Reload possibly changed frame fields */
1587 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001588 stack_pointer = f->f_valuestack+f->f_stackdepth;
1589 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001596
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001597 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001598 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001599#ifdef DYNAMIC_EXECUTION_PROFILE
1600#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 dxpairs[lastopcode][opcode]++;
1602 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001603#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001605#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001606
Guido van Rossum96a42c81992-01-12 02:29:51 +00001607#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (lltrace) {
1611 if (HAS_ARG(opcode)) {
1612 printf("%d: %d, %d\n",
1613 f->f_lasti, opcode, oparg);
1614 }
1615 else {
1616 printf("%d: %d\n",
1617 f->f_lasti, opcode);
1618 }
1619 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001620#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001625 It is essential that any operation that fails must goto error
1626 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001630 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *value = GETLOCAL(oparg);
1634 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001635 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001636 UNBOUNDLOCAL_ERROR_MSG,
1637 PyTuple_GetItem(co->co_varnames, oparg));
1638 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 Py_INCREF(value);
1641 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 }
1644
Benjamin Petersonddd19492018-09-16 22:38:02 -07001645 case TARGET(LOAD_CONST): {
1646 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 PyObject *value = GETITEM(consts, oparg);
1648 Py_INCREF(value);
1649 PUSH(value);
1650 FAST_DISPATCH();
1651 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(STORE_FAST): {
1654 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 PyObject *value = POP();
1656 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001659
Benjamin Petersonddd19492018-09-16 22:38:02 -07001660 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 PyObject *value = POP();
1662 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *top = TOP();
1668 PyObject *second = SECOND();
1669 SET_TOP(second);
1670 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Petersonddd19492018-09-16 22:38:02 -07001674 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 PyObject *top = TOP();
1676 PyObject *second = SECOND();
1677 PyObject *third = THIRD();
1678 SET_TOP(second);
1679 SET_SECOND(third);
1680 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001685 PyObject *top = TOP();
1686 PyObject *second = SECOND();
1687 PyObject *third = THIRD();
1688 PyObject *fourth = FOURTH();
1689 SET_TOP(second);
1690 SET_SECOND(third);
1691 SET_THIRD(fourth);
1692 SET_FOURTH(top);
1693 FAST_DISPATCH();
1694 }
1695
Benjamin Petersonddd19492018-09-16 22:38:02 -07001696 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 PyObject *top = TOP();
1698 Py_INCREF(top);
1699 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001706 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001708 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 SET_TOP(top);
1710 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001711 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 PyObject *value = TOP();
1716 PyObject *res = PyNumber_Positive(value);
1717 Py_DECREF(value);
1718 SET_TOP(res);
1719 if (res == NULL)
1720 goto error;
1721 DISPATCH();
1722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 PyObject *value = TOP();
1726 PyObject *res = PyNumber_Negative(value);
1727 Py_DECREF(value);
1728 SET_TOP(res);
1729 if (res == NULL)
1730 goto error;
1731 DISPATCH();
1732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Benjamin Petersonddd19492018-09-16 22:38:02 -07001734 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 PyObject *value = TOP();
1736 int err = PyObject_IsTrue(value);
1737 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (err == 0) {
1739 Py_INCREF(Py_True);
1740 SET_TOP(Py_True);
1741 DISPATCH();
1742 }
1743 else if (err > 0) {
1744 Py_INCREF(Py_False);
1745 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 DISPATCH();
1747 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001748 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 goto error;
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 PyObject *value = TOP();
1754 PyObject *res = PyNumber_Invert(value);
1755 Py_DECREF(value);
1756 SET_TOP(res);
1757 if (res == NULL)
1758 goto error;
1759 DISPATCH();
1760 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Benjamin Petersonddd19492018-09-16 22:38:02 -07001762 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 PyObject *exp = POP();
1764 PyObject *base = TOP();
1765 PyObject *res = PyNumber_Power(base, exp, Py_None);
1766 Py_DECREF(base);
1767 Py_DECREF(exp);
1768 SET_TOP(res);
1769 if (res == NULL)
1770 goto error;
1771 DISPATCH();
1772 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Benjamin Petersonddd19492018-09-16 22:38:02 -07001774 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 PyObject *right = POP();
1776 PyObject *left = TOP();
1777 PyObject *res = PyNumber_Multiply(left, right);
1778 Py_DECREF(left);
1779 Py_DECREF(right);
1780 SET_TOP(res);
1781 if (res == NULL)
1782 goto error;
1783 DISPATCH();
1784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Benjamin Petersonddd19492018-09-16 22:38:02 -07001786 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001787 PyObject *right = POP();
1788 PyObject *left = TOP();
1789 PyObject *res = PyNumber_MatrixMultiply(left, right);
1790 Py_DECREF(left);
1791 Py_DECREF(right);
1792 SET_TOP(res);
1793 if (res == NULL)
1794 goto error;
1795 DISPATCH();
1796 }
1797
Benjamin Petersonddd19492018-09-16 22:38:02 -07001798 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001799 PyObject *divisor = POP();
1800 PyObject *dividend = TOP();
1801 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1802 Py_DECREF(dividend);
1803 Py_DECREF(divisor);
1804 SET_TOP(quotient);
1805 if (quotient == NULL)
1806 goto error;
1807 DISPATCH();
1808 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Benjamin Petersonddd19492018-09-16 22:38:02 -07001810 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 PyObject *divisor = POP();
1812 PyObject *dividend = TOP();
1813 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1814 Py_DECREF(dividend);
1815 Py_DECREF(divisor);
1816 SET_TOP(quotient);
1817 if (quotient == NULL)
1818 goto error;
1819 DISPATCH();
1820 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001821
Benjamin Petersonddd19492018-09-16 22:38:02 -07001822 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 PyObject *divisor = POP();
1824 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001825 PyObject *res;
1826 if (PyUnicode_CheckExact(dividend) && (
1827 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1828 // fast path; string formatting, but not if the RHS is a str subclass
1829 // (see issue28598)
1830 res = PyUnicode_Format(dividend, divisor);
1831 } else {
1832 res = PyNumber_Remainder(dividend, divisor);
1833 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 Py_DECREF(divisor);
1835 Py_DECREF(dividend);
1836 SET_TOP(res);
1837 if (res == NULL)
1838 goto error;
1839 DISPATCH();
1840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Benjamin Petersonddd19492018-09-16 22:38:02 -07001842 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 PyObject *right = POP();
1844 PyObject *left = TOP();
1845 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001846 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001847 CPython using bytecode, it is simply worthless.
1848 See http://bugs.python.org/issue21955 and
1849 http://bugs.python.org/issue10044 for the discussion. In short,
1850 no patch shown any impact on a realistic benchmark, only a minor
1851 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 if (PyUnicode_CheckExact(left) &&
1853 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001854 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001855 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001856 }
1857 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 sum = PyNumber_Add(left, right);
1859 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001860 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_DECREF(right);
1862 SET_TOP(sum);
1863 if (sum == NULL)
1864 goto error;
1865 DISPATCH();
1866 }
1867
Benjamin Petersonddd19492018-09-16 22:38:02 -07001868 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 PyObject *right = POP();
1870 PyObject *left = TOP();
1871 PyObject *diff = PyNumber_Subtract(left, right);
1872 Py_DECREF(right);
1873 Py_DECREF(left);
1874 SET_TOP(diff);
1875 if (diff == NULL)
1876 goto error;
1877 DISPATCH();
1878 }
1879
Benjamin Petersonddd19492018-09-16 22:38:02 -07001880 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 PyObject *sub = POP();
1882 PyObject *container = TOP();
1883 PyObject *res = PyObject_GetItem(container, sub);
1884 Py_DECREF(container);
1885 Py_DECREF(sub);
1886 SET_TOP(res);
1887 if (res == NULL)
1888 goto error;
1889 DISPATCH();
1890 }
1891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *right = POP();
1894 PyObject *left = TOP();
1895 PyObject *res = PyNumber_Lshift(left, right);
1896 Py_DECREF(left);
1897 Py_DECREF(right);
1898 SET_TOP(res);
1899 if (res == NULL)
1900 goto error;
1901 DISPATCH();
1902 }
1903
Benjamin Petersonddd19492018-09-16 22:38:02 -07001904 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 PyObject *right = POP();
1906 PyObject *left = TOP();
1907 PyObject *res = PyNumber_Rshift(left, right);
1908 Py_DECREF(left);
1909 Py_DECREF(right);
1910 SET_TOP(res);
1911 if (res == NULL)
1912 goto error;
1913 DISPATCH();
1914 }
1915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 PyObject *right = POP();
1918 PyObject *left = TOP();
1919 PyObject *res = PyNumber_And(left, right);
1920 Py_DECREF(left);
1921 Py_DECREF(right);
1922 SET_TOP(res);
1923 if (res == NULL)
1924 goto error;
1925 DISPATCH();
1926 }
1927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *right = POP();
1930 PyObject *left = TOP();
1931 PyObject *res = PyNumber_Xor(left, right);
1932 Py_DECREF(left);
1933 Py_DECREF(right);
1934 SET_TOP(res);
1935 if (res == NULL)
1936 goto error;
1937 DISPATCH();
1938 }
1939
Benjamin Petersonddd19492018-09-16 22:38:02 -07001940 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *right = POP();
1942 PyObject *left = TOP();
1943 PyObject *res = PyNumber_Or(left, right);
1944 Py_DECREF(left);
1945 Py_DECREF(right);
1946 SET_TOP(res);
1947 if (res == NULL)
1948 goto error;
1949 DISPATCH();
1950 }
1951
Benjamin Petersonddd19492018-09-16 22:38:02 -07001952 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *v = POP();
1954 PyObject *list = PEEK(oparg);
1955 int err;
1956 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 if (err != 0)
1959 goto error;
1960 PREDICT(JUMP_ABSOLUTE);
1961 DISPATCH();
1962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Benjamin Petersonddd19492018-09-16 22:38:02 -07001964 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001966 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 int err;
1968 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 if (err != 0)
1971 goto error;
1972 PREDICT(JUMP_ABSOLUTE);
1973 DISPATCH();
1974 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001975
Benjamin Petersonddd19492018-09-16 22:38:02 -07001976 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 PyObject *exp = POP();
1978 PyObject *base = TOP();
1979 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1980 Py_DECREF(base);
1981 Py_DECREF(exp);
1982 SET_TOP(res);
1983 if (res == NULL)
1984 goto error;
1985 DISPATCH();
1986 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001987
Benjamin Petersonddd19492018-09-16 22:38:02 -07001988 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 PyObject *right = POP();
1990 PyObject *left = TOP();
1991 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1992 Py_DECREF(left);
1993 Py_DECREF(right);
1994 SET_TOP(res);
1995 if (res == NULL)
1996 goto error;
1997 DISPATCH();
1998 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Benjamin Petersonddd19492018-09-16 22:38:02 -07002000 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002001 PyObject *right = POP();
2002 PyObject *left = TOP();
2003 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2004 Py_DECREF(left);
2005 Py_DECREF(right);
2006 SET_TOP(res);
2007 if (res == NULL)
2008 goto error;
2009 DISPATCH();
2010 }
2011
Benjamin Petersonddd19492018-09-16 22:38:02 -07002012 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 PyObject *divisor = POP();
2014 PyObject *dividend = TOP();
2015 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2016 Py_DECREF(dividend);
2017 Py_DECREF(divisor);
2018 SET_TOP(quotient);
2019 if (quotient == NULL)
2020 goto error;
2021 DISPATCH();
2022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 PyObject *divisor = POP();
2026 PyObject *dividend = TOP();
2027 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2028 Py_DECREF(dividend);
2029 Py_DECREF(divisor);
2030 SET_TOP(quotient);
2031 if (quotient == NULL)
2032 goto error;
2033 DISPATCH();
2034 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Benjamin Petersonddd19492018-09-16 22:38:02 -07002036 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 PyObject *right = POP();
2038 PyObject *left = TOP();
2039 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2040 Py_DECREF(left);
2041 Py_DECREF(right);
2042 SET_TOP(mod);
2043 if (mod == NULL)
2044 goto error;
2045 DISPATCH();
2046 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Benjamin Petersonddd19492018-09-16 22:38:02 -07002048 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 PyObject *right = POP();
2050 PyObject *left = TOP();
2051 PyObject *sum;
2052 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002053 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002054 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002055 }
2056 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 sum = PyNumber_InPlaceAdd(left, right);
2058 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(right);
2061 SET_TOP(sum);
2062 if (sum == NULL)
2063 goto error;
2064 DISPATCH();
2065 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002066
Benjamin Petersonddd19492018-09-16 22:38:02 -07002067 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 PyObject *right = POP();
2069 PyObject *left = TOP();
2070 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2071 Py_DECREF(left);
2072 Py_DECREF(right);
2073 SET_TOP(diff);
2074 if (diff == NULL)
2075 goto error;
2076 DISPATCH();
2077 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Benjamin Petersonddd19492018-09-16 22:38:02 -07002079 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 PyObject *right = POP();
2081 PyObject *left = TOP();
2082 PyObject *res = PyNumber_InPlaceLshift(left, right);
2083 Py_DECREF(left);
2084 Py_DECREF(right);
2085 SET_TOP(res);
2086 if (res == NULL)
2087 goto error;
2088 DISPATCH();
2089 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002090
Benjamin Petersonddd19492018-09-16 22:38:02 -07002091 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 PyObject *right = POP();
2093 PyObject *left = TOP();
2094 PyObject *res = PyNumber_InPlaceRshift(left, right);
2095 Py_DECREF(left);
2096 Py_DECREF(right);
2097 SET_TOP(res);
2098 if (res == NULL)
2099 goto error;
2100 DISPATCH();
2101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002102
Benjamin Petersonddd19492018-09-16 22:38:02 -07002103 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 PyObject *right = POP();
2105 PyObject *left = TOP();
2106 PyObject *res = PyNumber_InPlaceAnd(left, right);
2107 Py_DECREF(left);
2108 Py_DECREF(right);
2109 SET_TOP(res);
2110 if (res == NULL)
2111 goto error;
2112 DISPATCH();
2113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002114
Benjamin Petersonddd19492018-09-16 22:38:02 -07002115 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PyObject *right = POP();
2117 PyObject *left = TOP();
2118 PyObject *res = PyNumber_InPlaceXor(left, right);
2119 Py_DECREF(left);
2120 Py_DECREF(right);
2121 SET_TOP(res);
2122 if (res == NULL)
2123 goto error;
2124 DISPATCH();
2125 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002126
Benjamin Petersonddd19492018-09-16 22:38:02 -07002127 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 PyObject *right = POP();
2129 PyObject *left = TOP();
2130 PyObject *res = PyNumber_InPlaceOr(left, right);
2131 Py_DECREF(left);
2132 Py_DECREF(right);
2133 SET_TOP(res);
2134 if (res == NULL)
2135 goto error;
2136 DISPATCH();
2137 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002138
Benjamin Petersonddd19492018-09-16 22:38:02 -07002139 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 PyObject *sub = TOP();
2141 PyObject *container = SECOND();
2142 PyObject *v = THIRD();
2143 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002144 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002145 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 Py_DECREF(container);
2149 Py_DECREF(sub);
2150 if (err != 0)
2151 goto error;
2152 DISPATCH();
2153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Benjamin Petersonddd19492018-09-16 22:38:02 -07002155 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 PyObject *sub = TOP();
2157 PyObject *container = SECOND();
2158 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002159 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002160 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 err = PyObject_DelItem(container, sub);
2162 Py_DECREF(container);
2163 Py_DECREF(sub);
2164 if (err != 0)
2165 goto error;
2166 DISPATCH();
2167 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002168
Benjamin Petersonddd19492018-09-16 22:38:02 -07002169 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002170 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002172 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002173 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002175 _PyErr_SetString(tstate, PyExc_RuntimeError,
2176 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 Py_DECREF(value);
2178 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002180 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_DECREF(value);
2182 if (res == NULL)
2183 goto error;
2184 Py_DECREF(res);
2185 DISPATCH();
2186 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002187
Benjamin Petersonddd19492018-09-16 22:38:02 -07002188 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 switch (oparg) {
2191 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002193 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002196 /* fall through */
2197 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002198 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002199 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 break;
2202 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002203 _PyErr_SetString(tstate, PyExc_SystemError,
2204 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 break;
2206 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 goto error;
2208 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002209
Benjamin Petersonddd19492018-09-16 22:38:02 -07002210 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002212 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002213 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002214 f->f_state = FRAME_RETURNED;
2215 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002216 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002218
Benjamin Petersonddd19492018-09-16 22:38:02 -07002219 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002220 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002221 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002222 PyObject *obj = TOP();
2223 PyTypeObject *type = Py_TYPE(obj);
2224
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002225 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002226 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002227 }
Yury Selivanov75445082015-05-11 22:57:16 -04002228
2229 if (getter != NULL) {
2230 iter = (*getter)(obj);
2231 Py_DECREF(obj);
2232 if (iter == NULL) {
2233 SET_TOP(NULL);
2234 goto error;
2235 }
2236 }
2237 else {
2238 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002239 _PyErr_Format(tstate, PyExc_TypeError,
2240 "'async for' requires an object with "
2241 "__aiter__ method, got %.100s",
2242 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002243 Py_DECREF(obj);
2244 goto error;
2245 }
2246
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002247 if (Py_TYPE(iter)->tp_as_async == NULL ||
2248 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002249
Yury Selivanov398ff912017-03-02 22:20:00 -05002250 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002251 _PyErr_Format(tstate, PyExc_TypeError,
2252 "'async for' received an object from __aiter__ "
2253 "that does not implement __anext__: %.100s",
2254 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002255 Py_DECREF(iter);
2256 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002257 }
2258
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002259 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002260 DISPATCH();
2261 }
2262
Benjamin Petersonddd19492018-09-16 22:38:02 -07002263 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002264 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002265 PyObject *next_iter = NULL;
2266 PyObject *awaitable = NULL;
2267 PyObject *aiter = TOP();
2268 PyTypeObject *type = Py_TYPE(aiter);
2269
Yury Selivanoveb636452016-09-08 22:01:51 -07002270 if (PyAsyncGen_CheckExact(aiter)) {
2271 awaitable = type->tp_as_async->am_anext(aiter);
2272 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002273 goto error;
2274 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002275 } else {
2276 if (type->tp_as_async != NULL){
2277 getter = type->tp_as_async->am_anext;
2278 }
Yury Selivanov75445082015-05-11 22:57:16 -04002279
Yury Selivanoveb636452016-09-08 22:01:51 -07002280 if (getter != NULL) {
2281 next_iter = (*getter)(aiter);
2282 if (next_iter == NULL) {
2283 goto error;
2284 }
2285 }
2286 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002287 _PyErr_Format(tstate, PyExc_TypeError,
2288 "'async for' requires an iterator with "
2289 "__anext__ method, got %.100s",
2290 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002291 goto error;
2292 }
Yury Selivanov75445082015-05-11 22:57:16 -04002293
Yury Selivanoveb636452016-09-08 22:01:51 -07002294 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2295 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002296 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002297 PyExc_TypeError,
2298 "'async for' received an invalid object "
2299 "from __anext__: %.100s",
2300 Py_TYPE(next_iter)->tp_name);
2301
2302 Py_DECREF(next_iter);
2303 goto error;
2304 } else {
2305 Py_DECREF(next_iter);
2306 }
2307 }
Yury Selivanov75445082015-05-11 22:57:16 -04002308
2309 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002310 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002311 DISPATCH();
2312 }
2313
Benjamin Petersonddd19492018-09-16 22:38:02 -07002314 case TARGET(GET_AWAITABLE): {
2315 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002316 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002317 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002318
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002319 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002320 int opcode_at_minus_3 = 0;
2321 if ((next_instr - first_instr) > 2) {
2322 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2323 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002324 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002325 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002326 _Py_OPCODE(next_instr[-2]));
2327 }
2328
Yury Selivanov75445082015-05-11 22:57:16 -04002329 Py_DECREF(iterable);
2330
Yury Selivanovc724bae2016-03-02 11:30:46 -05002331 if (iter != NULL && PyCoro_CheckExact(iter)) {
2332 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2333 if (yf != NULL) {
2334 /* `iter` is a coroutine object that is being
2335 awaited, `yf` is a pointer to the current awaitable
2336 being awaited on. */
2337 Py_DECREF(yf);
2338 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002339 _PyErr_SetString(tstate, PyExc_RuntimeError,
2340 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002341 /* The code below jumps to `error` if `iter` is NULL. */
2342 }
2343 }
2344
Yury Selivanov75445082015-05-11 22:57:16 -04002345 SET_TOP(iter); /* Even if it's NULL */
2346
2347 if (iter == NULL) {
2348 goto error;
2349 }
2350
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002351 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002352 DISPATCH();
2353 }
2354
Benjamin Petersonddd19492018-09-16 22:38:02 -07002355 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002357 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002358 PySendResult gen_status;
2359 if (tstate->c_tracefunc == NULL) {
2360 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002361 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002362 _Py_IDENTIFIER(send);
2363 if (v == Py_None && PyIter_Check(receiver)) {
2364 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002365 }
2366 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002367 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002368 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002369 if (retval == NULL) {
2370 if (tstate->c_tracefunc != NULL
2371 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002372 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002373 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2374 gen_status = PYGEN_RETURN;
2375 }
2376 else {
2377 gen_status = PYGEN_ERROR;
2378 }
2379 }
2380 else {
2381 gen_status = PYGEN_NEXT;
2382 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002385 if (gen_status == PYGEN_ERROR) {
2386 assert (retval == NULL);
2387 goto error;
2388 }
2389 if (gen_status == PYGEN_RETURN) {
2390 assert (retval != NULL);
2391
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002392 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002393 SET_TOP(retval);
2394 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002396 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002397 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002398 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002399 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002400 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002401 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002402 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002403 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002404 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002406
Benjamin Petersonddd19492018-09-16 22:38:02 -07002407 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002409
2410 if (co->co_flags & CO_ASYNC_GENERATOR) {
2411 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2412 Py_DECREF(retval);
2413 if (w == NULL) {
2414 retval = NULL;
2415 goto error;
2416 }
2417 retval = w;
2418 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002419 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002420 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002421 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002423
Benjamin Petersonddd19492018-09-16 22:38:02 -07002424 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002425 PyObject *type, *value, *traceback;
2426 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyTryBlock *b = PyFrame_BlockPop(f);
2428 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 _PyErr_SetString(tstate, PyExc_SystemError,
2430 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002433 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2434 STACK_LEVEL() <= (b)->b_level + 4);
2435 exc_info = tstate->exc_info;
2436 type = exc_info->exc_type;
2437 value = exc_info->exc_value;
2438 traceback = exc_info->exc_traceback;
2439 exc_info->exc_type = POP();
2440 exc_info->exc_value = POP();
2441 exc_info->exc_traceback = POP();
2442 Py_XDECREF(type);
2443 Py_XDECREF(value);
2444 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002447
Benjamin Petersonddd19492018-09-16 22:38:02 -07002448 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002449 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Mark Shannonfee55262019-11-21 09:11:43 +00002453 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002454 assert(f->f_iblock > 0);
2455 if (oparg) {
2456 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2457 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002458 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002459 PyObject *val = POP();
2460 PyObject *tb = POP();
2461 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002462 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002463 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Benjamin Petersonddd19492018-09-16 22:38:02 -07002466 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002467 PyObject *exc = POP();
2468 assert(PyExceptionClass_Check(exc));
2469 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2470 PyTryBlock *b = PyFrame_BlockPop(f);
2471 assert(b->b_type == EXCEPT_HANDLER);
2472 Py_DECREF(exc);
2473 UNWIND_EXCEPT_HANDLER(b);
2474 Py_DECREF(POP());
2475 JUMPBY(oparg);
2476 FAST_DISPATCH();
2477 }
2478 else {
2479 PyObject *val = POP();
2480 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002481 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002482 goto exception_unwind;
2483 }
2484 }
2485
Zackery Spytzce6a0702019-08-25 03:44:09 -06002486 case TARGET(LOAD_ASSERTION_ERROR): {
2487 PyObject *value = PyExc_AssertionError;
2488 Py_INCREF(value);
2489 PUSH(value);
2490 FAST_DISPATCH();
2491 }
2492
Benjamin Petersonddd19492018-09-16 22:38:02 -07002493 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002494 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002495
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002497 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002498 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002500 if (!_PyErr_Occurred(tstate)) {
2501 _PyErr_SetString(tstate, PyExc_NameError,
2502 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002503 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002507 }
2508 else {
2509 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2510 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002511 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2513 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002514 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2515 _PyErr_SetString(tstate, PyExc_NameError,
2516 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002521 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002523
Benjamin Petersonddd19492018-09-16 22:38:02 -07002524 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 PyObject *name = GETITEM(names, oparg);
2526 PyObject *v = POP();
2527 PyObject *ns = f->f_locals;
2528 int err;
2529 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002530 _PyErr_Format(tstate, PyExc_SystemError,
2531 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002535 if (PyDict_CheckExact(ns))
2536 err = PyDict_SetItem(ns, name, v);
2537 else
2538 err = PyObject_SetItem(ns, name, v);
2539 Py_DECREF(v);
2540 if (err != 0)
2541 goto error;
2542 DISPATCH();
2543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002544
Benjamin Petersonddd19492018-09-16 22:38:02 -07002545 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 PyObject *name = GETITEM(names, oparg);
2547 PyObject *ns = f->f_locals;
2548 int err;
2549 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002550 _PyErr_Format(tstate, PyExc_SystemError,
2551 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 err = PyObject_DelItem(ns, name);
2555 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 NAME_ERROR_MSG,
2558 name);
2559 goto error;
2560 }
2561 DISPATCH();
2562 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002563
Benjamin Petersonddd19492018-09-16 22:38:02 -07002564 case TARGET(UNPACK_SEQUENCE): {
2565 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 PyObject *seq = POP(), *item, **items;
2567 if (PyTuple_CheckExact(seq) &&
2568 PyTuple_GET_SIZE(seq) == oparg) {
2569 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 item = items[oparg];
2572 Py_INCREF(item);
2573 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 } else if (PyList_CheckExact(seq) &&
2576 PyList_GET_SIZE(seq) == oparg) {
2577 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 item = items[oparg];
2580 Py_INCREF(item);
2581 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002583 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002585 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 } else {
2587 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 Py_DECREF(seq);
2589 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002592 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002594
Benjamin Petersonddd19492018-09-16 22:38:02 -07002595 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2597 PyObject *seq = POP();
2598
Victor Stinner438a12d2019-05-24 17:01:38 +02002599 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 stack_pointer + totalargs)) {
2601 stack_pointer += totalargs;
2602 } else {
2603 Py_DECREF(seq);
2604 goto error;
2605 }
2606 Py_DECREF(seq);
2607 DISPATCH();
2608 }
2609
Benjamin Petersonddd19492018-09-16 22:38:02 -07002610 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 PyObject *name = GETITEM(names, oparg);
2612 PyObject *owner = TOP();
2613 PyObject *v = SECOND();
2614 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002615 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 Py_DECREF(owner);
2619 if (err != 0)
2620 goto error;
2621 DISPATCH();
2622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002623
Benjamin Petersonddd19492018-09-16 22:38:02 -07002624 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 PyObject *name = GETITEM(names, oparg);
2626 PyObject *owner = POP();
2627 int err;
2628 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2629 Py_DECREF(owner);
2630 if (err != 0)
2631 goto error;
2632 DISPATCH();
2633 }
2634
Benjamin Petersonddd19492018-09-16 22:38:02 -07002635 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 PyObject *name = GETITEM(names, oparg);
2637 PyObject *v = POP();
2638 int err;
2639 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 if (err != 0)
2642 goto error;
2643 DISPATCH();
2644 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002645
Benjamin Petersonddd19492018-09-16 22:38:02 -07002646 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 PyObject *name = GETITEM(names, oparg);
2648 int err;
2649 err = PyDict_DelItem(f->f_globals, name);
2650 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002651 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2652 format_exc_check_arg(tstate, PyExc_NameError,
2653 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002654 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002656 }
2657 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002659
Benjamin Petersonddd19492018-09-16 22:38:02 -07002660 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 PyObject *name = GETITEM(names, oparg);
2662 PyObject *locals = f->f_locals;
2663 PyObject *v;
2664 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002665 _PyErr_Format(tstate, PyExc_SystemError,
2666 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002670 v = PyDict_GetItemWithError(locals, name);
2671 if (v != NULL) {
2672 Py_INCREF(v);
2673 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002674 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002675 goto error;
2676 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 }
2678 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002680 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002681 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002682 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002683 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 }
2685 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002687 v = PyDict_GetItemWithError(f->f_globals, name);
2688 if (v != NULL) {
2689 Py_INCREF(v);
2690 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002691 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002692 goto error;
2693 }
2694 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002695 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002696 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002698 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002699 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002700 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002704 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002706 }
2707 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 v = PyObject_GetItem(f->f_builtins, name);
2709 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002710 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002711 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002712 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002714 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002716 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002717 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002725 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002727 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002728 && PyDict_CheckExact(f->f_builtins))
2729 {
Inada Naoki91234a12019-06-03 21:30:58 +09002730 OPCACHE_CHECK();
2731 if (co_opcache != NULL && co_opcache->optimized > 0) {
2732 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2733
2734 if (lg->globals_ver ==
2735 ((PyDictObject *)f->f_globals)->ma_version_tag
2736 && lg->builtins_ver ==
2737 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2738 {
2739 PyObject *ptr = lg->ptr;
2740 OPCACHE_STAT_GLOBAL_HIT();
2741 assert(ptr != NULL);
2742 Py_INCREF(ptr);
2743 PUSH(ptr);
2744 DISPATCH();
2745 }
2746 }
2747
2748 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002750 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 name);
2752 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002753 if (!_PyErr_OCCURRED()) {
2754 /* _PyDict_LoadGlobal() returns NULL without raising
2755 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002756 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002757 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002758 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 }
Inada Naoki91234a12019-06-03 21:30:58 +09002761
2762 if (co_opcache != NULL) {
2763 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2764
2765 if (co_opcache->optimized == 0) {
2766 /* Wasn't optimized before. */
2767 OPCACHE_STAT_GLOBAL_OPT();
2768 } else {
2769 OPCACHE_STAT_GLOBAL_MISS();
2770 }
2771
2772 co_opcache->optimized = 1;
2773 lg->globals_ver =
2774 ((PyDictObject *)f->f_globals)->ma_version_tag;
2775 lg->builtins_ver =
2776 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2777 lg->ptr = v; /* borrowed */
2778 }
2779
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002782 else {
2783 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002784
2785 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002786 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 v = PyObject_GetItem(f->f_globals, name);
2788 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002789 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002790 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002791 }
2792 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002793
Victor Stinnerb4efc962015-11-20 09:24:02 +01002794 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 v = PyObject_GetItem(f->f_builtins, name);
2796 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002797 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002798 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002799 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002800 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002801 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002803 }
2804 }
2805 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002809
Benjamin Petersonddd19492018-09-16 22:38:02 -07002810 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 PyObject *v = GETLOCAL(oparg);
2812 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 SETLOCAL(oparg, NULL);
2814 DISPATCH();
2815 }
2816 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002817 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 UNBOUNDLOCAL_ERROR_MSG,
2819 PyTuple_GetItem(co->co_varnames, oparg)
2820 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 goto error;
2822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002823
Benjamin Petersonddd19492018-09-16 22:38:02 -07002824 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002826 PyObject *oldobj = PyCell_GET(cell);
2827 if (oldobj != NULL) {
2828 PyCell_SET(cell, NULL);
2829 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002830 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002831 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002832 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 goto error;
2834 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002835
Benjamin Petersonddd19492018-09-16 22:38:02 -07002836 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 PyObject *cell = freevars[oparg];
2838 Py_INCREF(cell);
2839 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002842
Benjamin Petersonddd19492018-09-16 22:38:02 -07002843 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002844 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002845 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002846 assert(locals);
2847 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2848 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2849 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2850 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2851 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002852 value = PyDict_GetItemWithError(locals, name);
2853 if (value != NULL) {
2854 Py_INCREF(value);
2855 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002856 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002857 goto error;
2858 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002859 }
2860 else {
2861 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002862 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002863 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002864 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002865 }
2866 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002867 }
2868 }
2869 if (!value) {
2870 PyObject *cell = freevars[oparg];
2871 value = PyCell_GET(cell);
2872 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002873 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002874 goto error;
2875 }
2876 Py_INCREF(value);
2877 }
2878 PUSH(value);
2879 DISPATCH();
2880 }
2881
Benjamin Petersonddd19492018-09-16 22:38:02 -07002882 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 PyObject *cell = freevars[oparg];
2884 PyObject *value = PyCell_GET(cell);
2885 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002886 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 Py_INCREF(value);
2890 PUSH(value);
2891 DISPATCH();
2892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002893
Benjamin Petersonddd19492018-09-16 22:38:02 -07002894 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 PyObject *v = POP();
2896 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002897 PyObject *oldobj = PyCell_GET(cell);
2898 PyCell_SET(cell, v);
2899 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 DISPATCH();
2901 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002902
Benjamin Petersonddd19492018-09-16 22:38:02 -07002903 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002904 PyObject *str;
2905 PyObject *empty = PyUnicode_New(0, 0);
2906 if (empty == NULL) {
2907 goto error;
2908 }
2909 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2910 Py_DECREF(empty);
2911 if (str == NULL)
2912 goto error;
2913 while (--oparg >= 0) {
2914 PyObject *item = POP();
2915 Py_DECREF(item);
2916 }
2917 PUSH(str);
2918 DISPATCH();
2919 }
2920
Benjamin Petersonddd19492018-09-16 22:38:02 -07002921 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 PyObject *tup = PyTuple_New(oparg);
2923 if (tup == NULL)
2924 goto error;
2925 while (--oparg >= 0) {
2926 PyObject *item = POP();
2927 PyTuple_SET_ITEM(tup, oparg, item);
2928 }
2929 PUSH(tup);
2930 DISPATCH();
2931 }
2932
Benjamin Petersonddd19492018-09-16 22:38:02 -07002933 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 PyObject *list = PyList_New(oparg);
2935 if (list == NULL)
2936 goto error;
2937 while (--oparg >= 0) {
2938 PyObject *item = POP();
2939 PyList_SET_ITEM(list, oparg, item);
2940 }
2941 PUSH(list);
2942 DISPATCH();
2943 }
2944
Mark Shannon13bc1392020-01-23 09:25:17 +00002945 case TARGET(LIST_TO_TUPLE): {
2946 PyObject *list = POP();
2947 PyObject *tuple = PyList_AsTuple(list);
2948 Py_DECREF(list);
2949 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002950 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002951 }
2952 PUSH(tuple);
2953 DISPATCH();
2954 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002955
Mark Shannon13bc1392020-01-23 09:25:17 +00002956 case TARGET(LIST_EXTEND): {
2957 PyObject *iterable = POP();
2958 PyObject *list = PEEK(oparg);
2959 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2960 if (none_val == NULL) {
2961 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002962 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002963 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002964 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002965 _PyErr_Format(tstate, PyExc_TypeError,
2966 "Value after * must be an iterable, not %.200s",
2967 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002968 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002969 Py_DECREF(iterable);
2970 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002971 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002972 Py_DECREF(none_val);
2973 Py_DECREF(iterable);
2974 DISPATCH();
2975 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002976
Mark Shannon13bc1392020-01-23 09:25:17 +00002977 case TARGET(SET_UPDATE): {
2978 PyObject *iterable = POP();
2979 PyObject *set = PEEK(oparg);
2980 int err = _PySet_Update(set, iterable);
2981 Py_DECREF(iterable);
2982 if (err < 0) {
2983 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002984 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002985 DISPATCH();
2986 }
2987
Benjamin Petersonddd19492018-09-16 22:38:02 -07002988 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 PyObject *set = PySet_New(NULL);
2990 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002991 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 if (set == NULL)
2993 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002994 for (i = oparg; i > 0; i--) {
2995 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 if (err == 0)
2997 err = PySet_Add(set, item);
2998 Py_DECREF(item);
2999 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003000 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 if (err != 0) {
3002 Py_DECREF(set);
3003 goto error;
3004 }
3005 PUSH(set);
3006 DISPATCH();
3007 }
3008
Benjamin Petersonddd19492018-09-16 22:38:02 -07003009 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003010 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3012 if (map == NULL)
3013 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003014 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003015 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003016 PyObject *key = PEEK(2*i);
3017 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003018 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003019 if (err != 0) {
3020 Py_DECREF(map);
3021 goto error;
3022 }
3023 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003024
3025 while (oparg--) {
3026 Py_DECREF(POP());
3027 Py_DECREF(POP());
3028 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PUSH(map);
3030 DISPATCH();
3031 }
3032
Benjamin Petersonddd19492018-09-16 22:38:02 -07003033 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003034 _Py_IDENTIFIER(__annotations__);
3035 int err;
3036 PyObject *ann_dict;
3037 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003038 _PyErr_Format(tstate, PyExc_SystemError,
3039 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003040 goto error;
3041 }
3042 /* check if __annotations__ in locals()... */
3043 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003044 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003045 &PyId___annotations__);
3046 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003047 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003048 goto error;
3049 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003050 /* ...if not, create a new one */
3051 ann_dict = PyDict_New();
3052 if (ann_dict == NULL) {
3053 goto error;
3054 }
3055 err = _PyDict_SetItemId(f->f_locals,
3056 &PyId___annotations__, ann_dict);
3057 Py_DECREF(ann_dict);
3058 if (err != 0) {
3059 goto error;
3060 }
3061 }
3062 }
3063 else {
3064 /* do the same if locals() is not a dict */
3065 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3066 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003067 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003068 }
3069 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3070 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003071 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003072 goto error;
3073 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003074 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003075 ann_dict = PyDict_New();
3076 if (ann_dict == NULL) {
3077 goto error;
3078 }
3079 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3080 Py_DECREF(ann_dict);
3081 if (err != 0) {
3082 goto error;
3083 }
3084 }
3085 else {
3086 Py_DECREF(ann_dict);
3087 }
3088 }
3089 DISPATCH();
3090 }
3091
Benjamin Petersonddd19492018-09-16 22:38:02 -07003092 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003093 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003094 PyObject *map;
3095 PyObject *keys = TOP();
3096 if (!PyTuple_CheckExact(keys) ||
3097 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003098 _PyErr_SetString(tstate, PyExc_SystemError,
3099 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003100 goto error;
3101 }
3102 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3103 if (map == NULL) {
3104 goto error;
3105 }
3106 for (i = oparg; i > 0; i--) {
3107 int err;
3108 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3109 PyObject *value = PEEK(i + 1);
3110 err = PyDict_SetItem(map, key, value);
3111 if (err != 0) {
3112 Py_DECREF(map);
3113 goto error;
3114 }
3115 }
3116
3117 Py_DECREF(POP());
3118 while (oparg--) {
3119 Py_DECREF(POP());
3120 }
3121 PUSH(map);
3122 DISPATCH();
3123 }
3124
Mark Shannon8a4cd702020-01-27 09:57:45 +00003125 case TARGET(DICT_UPDATE): {
3126 PyObject *update = POP();
3127 PyObject *dict = PEEK(oparg);
3128 if (PyDict_Update(dict, update) < 0) {
3129 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3130 _PyErr_Format(tstate, PyExc_TypeError,
3131 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003132 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003133 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003134 Py_DECREF(update);
3135 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003136 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003137 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003138 DISPATCH();
3139 }
3140
Mark Shannon8a4cd702020-01-27 09:57:45 +00003141 case TARGET(DICT_MERGE): {
3142 PyObject *update = POP();
3143 PyObject *dict = PEEK(oparg);
3144
3145 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3146 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3147 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003148 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003149 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003150 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003151 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003152 DISPATCH();
3153 }
3154
Benjamin Petersonddd19492018-09-16 22:38:02 -07003155 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003156 PyObject *value = TOP();
3157 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 PyObject *map;
3159 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003160 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003161 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003163 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 Py_DECREF(value);
3165 Py_DECREF(key);
3166 if (err != 0)
3167 goto error;
3168 PREDICT(JUMP_ABSOLUTE);
3169 DISPATCH();
3170 }
3171
Benjamin Petersonddd19492018-09-16 22:38:02 -07003172 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 PyObject *name = GETITEM(names, oparg);
3174 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003175
3176 PyTypeObject *type = Py_TYPE(owner);
3177 PyObject *res;
3178 PyObject **dictptr;
3179 PyObject *dict;
3180 _PyOpCodeOpt_LoadAttr *la;
3181
3182 OPCACHE_STAT_ATTR_TOTAL();
3183
3184 OPCACHE_CHECK();
3185 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3186 {
3187 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003188 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003189 la = &co_opcache->u.la;
3190 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3191 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003192 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3193 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3194 // so ~offset is strictly < -1 (assuming 2's complement).
3195 if (la->hint < -1) {
3196 // Even faster path -- slot hint.
3197 Py_ssize_t offset = ~la->hint;
3198 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3199 char *addr = (char *)owner + offset;
3200 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003201 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003202 Py_INCREF(res);
3203 SET_TOP(res);
3204 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003205 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003206 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003207 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3208 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003209 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003210 // Fast path for dict.
3211 assert(type->tp_dict != NULL);
3212 assert(type->tp_dictoffset > 0);
3213
3214 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3215 dict = *dictptr;
3216 if (dict != NULL && PyDict_CheckExact(dict)) {
3217 Py_ssize_t hint = la->hint;
3218 Py_INCREF(dict);
3219 res = NULL;
3220 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3221
3222 if (res != NULL) {
3223 if (la->hint == hint && hint >= 0) {
3224 // Our hint has helped -- cache hit.
3225 OPCACHE_STAT_ATTR_HIT();
3226 } else {
3227 // The hint we provided didn't work.
3228 // Maybe next time?
3229 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3230 }
3231
3232 Py_INCREF(res);
3233 SET_TOP(res);
3234 Py_DECREF(owner);
3235 Py_DECREF(dict);
3236 DISPATCH();
3237 } else {
3238 // This attribute can be missing sometimes;
3239 // we don't want to optimize this lookup.
3240 OPCACHE_DEOPT_LOAD_ATTR();
3241 Py_DECREF(dict);
3242 }
3243 } else {
3244 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3245 OPCACHE_DEOPT_LOAD_ATTR();
3246 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003247 }
3248 } else {
3249 // The type of the object has either been updated,
3250 // or is different. Maybe it will stabilize?
3251 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3252 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003253 OPCACHE_STAT_ATTR_MISS();
3254 }
3255
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003256 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003257 type->tp_getattro == PyObject_GenericGetAttr)
3258 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003259 if (type->tp_dict == NULL) {
3260 if (PyType_Ready(type) < 0) {
3261 Py_DECREF(owner);
3262 SET_TOP(NULL);
3263 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003264 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003265 }
3266 PyObject *descr = _PyType_Lookup(type, name);
3267 if (descr != NULL) {
3268 // We found an attribute with a data-like descriptor.
3269 PyTypeObject *dtype = Py_TYPE(descr);
3270 if (dtype == &PyMemberDescr_Type) { // It's a slot
3271 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3272 struct PyMemberDef *dmem = member->d_member;
3273 if (dmem->type == T_OBJECT_EX) {
3274 Py_ssize_t offset = dmem->offset;
3275 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003276
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003277 if (co_opcache->optimized == 0) {
3278 // First time we optimize this opcode.
3279 OPCACHE_STAT_ATTR_OPT();
3280 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3281 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3282 }
3283
3284 la = &co_opcache->u.la;
3285 la->type = type;
3286 la->tp_version_tag = type->tp_version_tag;
3287 la->hint = ~offset;
3288
3289 char *addr = (char *)owner + offset;
3290 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003291 if (res != NULL) {
3292 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003293 Py_DECREF(owner);
3294 SET_TOP(res);
3295
Pablo Galindo109826c2020-10-20 06:22:44 +01003296 DISPATCH();
3297 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003298 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003299 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003300 // Else it's a slot of a different type. We don't handle those.
3301 }
3302 // Else it's some other kind of descriptor that we don't handle.
3303 OPCACHE_DEOPT_LOAD_ATTR();
3304 } else if (type->tp_dictoffset > 0) {
3305 // We found an instance with a __dict__.
3306 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3307 dict = *dictptr;
3308
3309 if (dict != NULL && PyDict_CheckExact(dict)) {
3310 Py_INCREF(dict);
3311 res = NULL;
3312 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3313 if (res != NULL) {
3314 Py_INCREF(res);
3315 Py_DECREF(dict);
3316 Py_DECREF(owner);
3317 SET_TOP(res);
3318
3319 if (co_opcache->optimized == 0) {
3320 // First time we optimize this opcode.
3321 OPCACHE_STAT_ATTR_OPT();
3322 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3323 }
3324
3325 la = &co_opcache->u.la;
3326 la->type = type;
3327 la->tp_version_tag = type->tp_version_tag;
3328 la->hint = hint;
3329
3330 DISPATCH();
3331 }
3332 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003333 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003334 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003335 OPCACHE_DEOPT_LOAD_ATTR();
3336 }
3337 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003338 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003339 OPCACHE_DEOPT_LOAD_ATTR();
3340 }
3341 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3342 OPCACHE_DEOPT_LOAD_ATTR();
3343 }
3344 }
3345
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003346 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003347 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 Py_DECREF(owner);
3349 SET_TOP(res);
3350 if (res == NULL)
3351 goto error;
3352 DISPATCH();
3353 }
3354
Benjamin Petersonddd19492018-09-16 22:38:02 -07003355 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003356 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 PyObject *right = POP();
3358 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003359 PyObject *res = PyObject_RichCompare(left, right, oparg);
3360 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 Py_DECREF(left);
3362 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 if (res == NULL)
3364 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 PREDICT(POP_JUMP_IF_FALSE);
3366 PREDICT(POP_JUMP_IF_TRUE);
3367 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003369
Mark Shannon9af0e472020-01-14 10:12:45 +00003370 case TARGET(IS_OP): {
3371 PyObject *right = POP();
3372 PyObject *left = TOP();
3373 int res = (left == right)^oparg;
3374 PyObject *b = res ? Py_True : Py_False;
3375 Py_INCREF(b);
3376 SET_TOP(b);
3377 Py_DECREF(left);
3378 Py_DECREF(right);
3379 PREDICT(POP_JUMP_IF_FALSE);
3380 PREDICT(POP_JUMP_IF_TRUE);
3381 FAST_DISPATCH();
3382 }
3383
3384 case TARGET(CONTAINS_OP): {
3385 PyObject *right = POP();
3386 PyObject *left = POP();
3387 int res = PySequence_Contains(right, left);
3388 Py_DECREF(left);
3389 Py_DECREF(right);
3390 if (res < 0) {
3391 goto error;
3392 }
3393 PyObject *b = (res^oparg) ? Py_True : Py_False;
3394 Py_INCREF(b);
3395 PUSH(b);
3396 PREDICT(POP_JUMP_IF_FALSE);
3397 PREDICT(POP_JUMP_IF_TRUE);
3398 FAST_DISPATCH();
3399 }
3400
3401#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3402 "BaseException is not allowed"
3403
3404 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3405 PyObject *right = POP();
3406 PyObject *left = POP();
3407 if (PyTuple_Check(right)) {
3408 Py_ssize_t i, length;
3409 length = PyTuple_GET_SIZE(right);
3410 for (i = 0; i < length; i++) {
3411 PyObject *exc = PyTuple_GET_ITEM(right, i);
3412 if (!PyExceptionClass_Check(exc)) {
3413 _PyErr_SetString(tstate, PyExc_TypeError,
3414 CANNOT_CATCH_MSG);
3415 Py_DECREF(left);
3416 Py_DECREF(right);
3417 goto error;
3418 }
3419 }
3420 }
3421 else {
3422 if (!PyExceptionClass_Check(right)) {
3423 _PyErr_SetString(tstate, PyExc_TypeError,
3424 CANNOT_CATCH_MSG);
3425 Py_DECREF(left);
3426 Py_DECREF(right);
3427 goto error;
3428 }
3429 }
3430 int res = PyErr_GivenExceptionMatches(left, right);
3431 Py_DECREF(left);
3432 Py_DECREF(right);
3433 if (res > 0) {
3434 /* Exception matches -- Do nothing */;
3435 }
3436 else if (res == 0) {
3437 JUMPTO(oparg);
3438 }
3439 else {
3440 goto error;
3441 }
3442 DISPATCH();
3443 }
3444
Benjamin Petersonddd19492018-09-16 22:38:02 -07003445 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003447 PyObject *fromlist = POP();
3448 PyObject *level = TOP();
3449 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003450 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003451 Py_DECREF(level);
3452 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 SET_TOP(res);
3454 if (res == NULL)
3455 goto error;
3456 DISPATCH();
3457 }
3458
Benjamin Petersonddd19492018-09-16 22:38:02 -07003459 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003460 PyObject *from = POP(), *locals;
3461 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003462 if (PyFrame_FastToLocalsWithError(f) < 0) {
3463 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003464 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003465 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003466
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 locals = f->f_locals;
3468 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003469 _PyErr_SetString(tstate, PyExc_SystemError,
3470 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003471 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003472 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003474 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003476 Py_DECREF(from);
3477 if (err != 0)
3478 goto error;
3479 DISPATCH();
3480 }
Guido van Rossum25831651993-05-19 14:50:45 +00003481
Benjamin Petersonddd19492018-09-16 22:38:02 -07003482 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 PyObject *name = GETITEM(names, oparg);
3484 PyObject *from = TOP();
3485 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003486 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 PUSH(res);
3488 if (res == NULL)
3489 goto error;
3490 DISPATCH();
3491 }
Thomas Wouters52152252000-08-17 22:55:00 +00003492
Benjamin Petersonddd19492018-09-16 22:38:02 -07003493 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 JUMPBY(oparg);
3495 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003496 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003497
Benjamin Petersonddd19492018-09-16 22:38:02 -07003498 case TARGET(POP_JUMP_IF_FALSE): {
3499 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 PyObject *cond = POP();
3501 int err;
3502 if (cond == Py_True) {
3503 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 FAST_DISPATCH();
3505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003506 if (cond == Py_False) {
3507 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 JUMPTO(oparg);
3509 FAST_DISPATCH();
3510 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 err = PyObject_IsTrue(cond);
3512 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003514 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 else if (err == 0)
3516 JUMPTO(oparg);
3517 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003518 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003520 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003521
Benjamin Petersonddd19492018-09-16 22:38:02 -07003522 case TARGET(POP_JUMP_IF_TRUE): {
3523 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 PyObject *cond = POP();
3525 int err;
3526 if (cond == Py_False) {
3527 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 FAST_DISPATCH();
3529 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003530 if (cond == Py_True) {
3531 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 JUMPTO(oparg);
3533 FAST_DISPATCH();
3534 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 err = PyObject_IsTrue(cond);
3536 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 JUMPTO(oparg);
3539 }
3540 else if (err == 0)
3541 ;
3542 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003545 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003546
Benjamin Petersonddd19492018-09-16 22:38:02 -07003547 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003548 PyObject *cond = TOP();
3549 int err;
3550 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003551 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003552 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 FAST_DISPATCH();
3554 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 JUMPTO(oparg);
3557 FAST_DISPATCH();
3558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003561 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 }
3564 else if (err == 0)
3565 JUMPTO(oparg);
3566 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003569 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003570
Benjamin Petersonddd19492018-09-16 22:38:02 -07003571 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003572 PyObject *cond = TOP();
3573 int err;
3574 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003575 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 FAST_DISPATCH();
3578 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003579 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 JUMPTO(oparg);
3581 FAST_DISPATCH();
3582 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 JUMPTO(oparg);
3586 }
3587 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003588 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 }
3591 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003594 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003595
Benjamin Petersonddd19492018-09-16 22:38:02 -07003596 case TARGET(JUMP_ABSOLUTE): {
3597 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003599#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 /* Enabling this path speeds-up all while and for-loops by bypassing
3601 the per-loop checks for signals. By default, this should be turned-off
3602 because it prevents detection of a control-break in tight loops like
3603 "while 1: pass". Compile with this option turned-on when you need
3604 the speed-up and do not need break checking inside tight loops (ones
3605 that contain only instructions ending with FAST_DISPATCH).
3606 */
3607 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003608#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003610#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003611 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003612
Benjamin Petersonddd19492018-09-16 22:38:02 -07003613 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003615 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003616 PyObject *iter = PyObject_GetIter(iterable);
3617 Py_DECREF(iterable);
3618 SET_TOP(iter);
3619 if (iter == NULL)
3620 goto error;
3621 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003622 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003623 DISPATCH();
3624 }
3625
Benjamin Petersonddd19492018-09-16 22:38:02 -07003626 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003627 /* before: [obj]; after [getiter(obj)] */
3628 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003629 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003630 if (PyCoro_CheckExact(iterable)) {
3631 /* `iterable` is a coroutine */
3632 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3633 /* and it is used in a 'yield from' expression of a
3634 regular generator. */
3635 Py_DECREF(iterable);
3636 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003637 _PyErr_SetString(tstate, PyExc_TypeError,
3638 "cannot 'yield from' a coroutine object "
3639 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003640 goto error;
3641 }
3642 }
3643 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003644 /* `iterable` is not a generator. */
3645 iter = PyObject_GetIter(iterable);
3646 Py_DECREF(iterable);
3647 SET_TOP(iter);
3648 if (iter == NULL)
3649 goto error;
3650 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003651 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003652 DISPATCH();
3653 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003654
Benjamin Petersonddd19492018-09-16 22:38:02 -07003655 case TARGET(FOR_ITER): {
3656 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003658 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003659 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003660 if (next != NULL) {
3661 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 PREDICT(STORE_FAST);
3663 PREDICT(UNPACK_SEQUENCE);
3664 DISPATCH();
3665 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003666 if (_PyErr_Occurred(tstate)) {
3667 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003668 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003669 }
3670 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00003671 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02003672 }
3673 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
3675 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003676 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003677 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 JUMPBY(oparg);
3679 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003680 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003681
Benjamin Petersonddd19492018-09-16 22:38:02 -07003682 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003683 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 STACK_LEVEL());
3685 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003687
Benjamin Petersonddd19492018-09-16 22:38:02 -07003688 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003689 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003690 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003691 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003692 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003693 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003694 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003695 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003696 }
3697 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3698 if (exit == NULL) {
3699 Py_DECREF(enter);
3700 goto error;
3701 }
Yury Selivanov75445082015-05-11 22:57:16 -04003702 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003703 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003704 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003705 Py_DECREF(enter);
3706 if (res == NULL)
3707 goto error;
3708 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003709 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003710 DISPATCH();
3711 }
3712
Benjamin Petersonddd19492018-09-16 22:38:02 -07003713 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003714 PyObject *res = POP();
3715 /* Setup the finally block before pushing the result
3716 of __aenter__ on the stack. */
3717 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3718 STACK_LEVEL());
3719 PUSH(res);
3720 DISPATCH();
3721 }
3722
Benjamin Petersonddd19492018-09-16 22:38:02 -07003723 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003724 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003725 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003726 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003727 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003728 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003729 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003730 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003731 }
3732 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003733 if (exit == NULL) {
3734 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003735 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003736 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003737 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003738 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003739 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003740 Py_DECREF(enter);
3741 if (res == NULL)
3742 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* Setup the finally block before pushing the result
3744 of __enter__ on the stack. */
3745 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3746 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003747
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003748 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 DISPATCH();
3750 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003751
Mark Shannonfee55262019-11-21 09:11:43 +00003752 case TARGET(WITH_EXCEPT_START): {
3753 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003755 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3756 - SEVENTH: the context.__exit__ bound method
3757 We call SEVENTH(TOP, SECOND, THIRD).
3758 Then we push again the TOP exception and the __exit__
3759 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003762 PyObject *exc, *val, *tb, *res;
3763
Victor Stinner842cfff2016-12-01 14:45:31 +01003764 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003765 val = SECOND();
3766 tb = THIRD();
3767 assert(exc != Py_None);
3768 assert(!PyLong_Check(exc));
3769 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003770 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003771 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003772 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003773 if (res == NULL)
3774 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003775
Yury Selivanov75445082015-05-11 22:57:16 -04003776 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003777 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003779
Benjamin Petersonddd19492018-09-16 22:38:02 -07003780 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003781 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003782 PyObject *name = GETITEM(names, oparg);
3783 PyObject *obj = TOP();
3784 PyObject *meth = NULL;
3785
3786 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3787
Yury Selivanovf2392132016-12-13 19:03:51 -05003788 if (meth == NULL) {
3789 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003790 goto error;
3791 }
3792
3793 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003794 /* We can bypass temporary bound method object.
3795 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003796
INADA Naoki015bce62017-01-16 17:23:30 +09003797 meth | self | arg1 | ... | argN
3798 */
3799 SET_TOP(meth);
3800 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003801 }
3802 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003803 /* meth is not an unbound method (but a regular attr, or
3804 something was returned by a descriptor protocol). Set
3805 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003806 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003807
3808 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003809 */
INADA Naoki015bce62017-01-16 17:23:30 +09003810 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003811 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003812 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003813 }
3814 DISPATCH();
3815 }
3816
Benjamin Petersonddd19492018-09-16 22:38:02 -07003817 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003818 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003819 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003820
3821 sp = stack_pointer;
3822
INADA Naoki015bce62017-01-16 17:23:30 +09003823 meth = PEEK(oparg + 2);
3824 if (meth == NULL) {
3825 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3826 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003827
3828 Stack layout:
3829
INADA Naoki015bce62017-01-16 17:23:30 +09003830 ... | NULL | callable | arg1 | ... | argN
3831 ^- TOP()
3832 ^- (-oparg)
3833 ^- (-oparg-1)
3834 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003835
Ville Skyttä49b27342017-08-03 09:00:59 +03003836 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003837 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003838 */
Mark Shannon86433452021-01-07 16:49:02 +00003839 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003840 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003841 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003842 }
3843 else {
3844 /* This is a method call. Stack layout:
3845
INADA Naoki015bce62017-01-16 17:23:30 +09003846 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003847 ^- TOP()
3848 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003849 ^- (-oparg-1)
3850 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003851
INADA Naoki015bce62017-01-16 17:23:30 +09003852 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003853 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003854 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003855 */
Mark Shannon86433452021-01-07 16:49:02 +00003856 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003857 stack_pointer = sp;
3858 }
3859
3860 PUSH(res);
3861 if (res == NULL)
3862 goto error;
3863 DISPATCH();
3864 }
3865
Benjamin Petersonddd19492018-09-16 22:38:02 -07003866 case TARGET(CALL_FUNCTION): {
3867 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003868 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003870 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003872 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003873 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003874 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003875 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003876 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003878
Benjamin Petersonddd19492018-09-16 22:38:02 -07003879 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003880 PyObject **sp, *res, *names;
3881
3882 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003883 assert(PyTuple_Check(names));
3884 assert(PyTuple_GET_SIZE(names) <= oparg);
3885 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003887 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003889 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003890 Py_DECREF(names);
3891
3892 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003893 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003894 }
3895 DISPATCH();
3896 }
3897
Benjamin Petersonddd19492018-09-16 22:38:02 -07003898 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003899 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003900 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003901 if (oparg & 0x01) {
3902 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003903 if (!PyDict_CheckExact(kwargs)) {
3904 PyObject *d = PyDict_New();
3905 if (d == NULL)
3906 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003907 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003908 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003909 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003910 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003911 goto error;
3912 }
3913 Py_DECREF(kwargs);
3914 kwargs = d;
3915 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003916 assert(PyDict_CheckExact(kwargs));
3917 }
3918 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003919 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003920 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003921 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003922 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003923 goto error;
3924 }
3925 Py_SETREF(callargs, PySequence_Tuple(callargs));
3926 if (callargs == NULL) {
3927 goto error;
3928 }
3929 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003930 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003931
Mark Shannon86433452021-01-07 16:49:02 +00003932 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003933 Py_DECREF(func);
3934 Py_DECREF(callargs);
3935 Py_XDECREF(kwargs);
3936
3937 SET_TOP(result);
3938 if (result == NULL) {
3939 goto error;
3940 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003941 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003943
Benjamin Petersonddd19492018-09-16 22:38:02 -07003944 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003945 PyObject *qualname = POP();
3946 PyObject *codeobj = POP();
3947 PyFunctionObject *func = (PyFunctionObject *)
3948 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003949
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003950 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003951 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003952 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003953 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003955
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003956 if (oparg & 0x08) {
3957 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00003958 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003959 }
3960 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02003961 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003962 func->func_annotations = POP();
3963 }
3964 if (oparg & 0x02) {
3965 assert(PyDict_CheckExact(TOP()));
3966 func->func_kwdefaults = POP();
3967 }
3968 if (oparg & 0x01) {
3969 assert(PyTuple_CheckExact(TOP()));
3970 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003972
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003973 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003974 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003976
Benjamin Petersonddd19492018-09-16 22:38:02 -07003977 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003978 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003980 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003982 step = NULL;
3983 stop = POP();
3984 start = TOP();
3985 slice = PySlice_New(start, stop, step);
3986 Py_DECREF(start);
3987 Py_DECREF(stop);
3988 Py_XDECREF(step);
3989 SET_TOP(slice);
3990 if (slice == NULL)
3991 goto error;
3992 DISPATCH();
3993 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003994
Benjamin Petersonddd19492018-09-16 22:38:02 -07003995 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003996 /* Handles f-string value formatting. */
3997 PyObject *result;
3998 PyObject *fmt_spec;
3999 PyObject *value;
4000 PyObject *(*conv_fn)(PyObject *);
4001 int which_conversion = oparg & FVC_MASK;
4002 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4003
4004 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004005 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004006
4007 /* See if any conversion is specified. */
4008 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004009 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004010 case FVC_STR: conv_fn = PyObject_Str; break;
4011 case FVC_REPR: conv_fn = PyObject_Repr; break;
4012 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004013 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004014 _PyErr_Format(tstate, PyExc_SystemError,
4015 "unexpected conversion flag %d",
4016 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004017 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004018 }
4019
4020 /* If there's a conversion function, call it and replace
4021 value with that result. Otherwise, just use value,
4022 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004023 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004024 result = conv_fn(value);
4025 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004026 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004027 Py_XDECREF(fmt_spec);
4028 goto error;
4029 }
4030 value = result;
4031 }
4032
4033 /* If value is a unicode object, and there's no fmt_spec,
4034 then we know the result of format(value) is value
4035 itself. In that case, skip calling format(). I plan to
4036 move this optimization in to PyObject_Format()
4037 itself. */
4038 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4039 /* Do nothing, just transfer ownership to result. */
4040 result = value;
4041 } else {
4042 /* Actually call format(). */
4043 result = PyObject_Format(value, fmt_spec);
4044 Py_DECREF(value);
4045 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004046 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004047 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004048 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004049 }
4050
Eric V. Smith135d5f42016-02-05 18:23:08 -05004051 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004052 DISPATCH();
4053 }
4054
Benjamin Petersonddd19492018-09-16 22:38:02 -07004055 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004056 int oldoparg = oparg;
4057 NEXTOPARG();
4058 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004060 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004061
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004062
Antoine Pitrou042b1282010-08-13 21:15:58 +00004063#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004065#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 default:
4067 fprintf(stderr,
4068 "XXX lineno: %d, opcode: %d\n",
4069 PyFrame_GetLineNumber(f),
4070 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004071 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004072 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004075
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004076 /* This should never be reached. Every opcode should end with DISPATCH()
4077 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004078 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004080error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004081 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004082#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004083 if (!_PyErr_Occurred(tstate)) {
4084 _PyErr_SetString(tstate, PyExc_SystemError,
4085 "error return without exception set");
4086 }
Victor Stinner365b6932013-07-12 00:11:58 +02004087#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004088 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004089#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004091 /* Log traceback info. */
4092 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004093
Mark Shannoncb9879b2020-07-17 11:44:23 +01004094 if (tstate->c_tracefunc != NULL) {
4095 /* Make sure state is set to FRAME_EXECUTING for tracing */
4096 assert(f->f_state == FRAME_EXECUTING);
4097 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004098 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004099 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004100 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004101exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004102 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004103 /* Unwind stacks if an exception occurred */
4104 while (f->f_iblock > 0) {
4105 /* Pop the current block. */
4106 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (b->b_type == EXCEPT_HANDLER) {
4109 UNWIND_EXCEPT_HANDLER(b);
4110 continue;
4111 }
4112 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004113 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 PyObject *exc, *val, *tb;
4115 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004116 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004118 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004119 PUSH(exc_info->exc_traceback);
4120 PUSH(exc_info->exc_value);
4121 if (exc_info->exc_type != NULL) {
4122 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 }
4124 else {
4125 Py_INCREF(Py_None);
4126 PUSH(Py_None);
4127 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004128 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 /* Make the raw exception data
4130 available to the handler,
4131 so a program can emulate the
4132 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004133 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004134 if (tb != NULL)
4135 PyException_SetTraceback(val, tb);
4136 else
4137 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004139 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004141 exc_info->exc_value = val;
4142 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 if (tb == NULL)
4144 tb = Py_None;
4145 Py_INCREF(tb);
4146 PUSH(tb);
4147 PUSH(val);
4148 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004150 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004151 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004152 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004153 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004154 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004155 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 }
4157 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004158
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004159 /* End the loop as we still have an error */
4160 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004162
Pablo Galindof00828a2019-05-09 16:52:02 +01004163 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004164 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 /* Pop remaining stack entries. */
4167 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004168 PyObject *o = POP();
4169 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004171 f->f_stackdepth = 0;
4172 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004173exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004175 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004176 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004177 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004178 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
4180 }
4181 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004182 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004183 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004184 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
4186 }
4187 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004190exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004191 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4192 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004193 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004195
Victor Stinner0b72b232020-03-12 23:18:39 +01004196 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004197}
4198
Benjamin Petersonb204a422011-06-05 22:04:07 -05004199static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004200format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004201 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004202{
4203 int err;
4204 Py_ssize_t len = PyList_GET_SIZE(names);
4205 PyObject *name_str, *comma, *tail, *tmp;
4206
4207 assert(PyList_CheckExact(names));
4208 assert(len >= 1);
4209 /* Deal with the joys of natural language. */
4210 switch (len) {
4211 case 1:
4212 name_str = PyList_GET_ITEM(names, 0);
4213 Py_INCREF(name_str);
4214 break;
4215 case 2:
4216 name_str = PyUnicode_FromFormat("%U and %U",
4217 PyList_GET_ITEM(names, len - 2),
4218 PyList_GET_ITEM(names, len - 1));
4219 break;
4220 default:
4221 tail = PyUnicode_FromFormat(", %U, and %U",
4222 PyList_GET_ITEM(names, len - 2),
4223 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004224 if (tail == NULL)
4225 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004226 /* Chop off the last two objects in the list. This shouldn't actually
4227 fail, but we can't be too careful. */
4228 err = PyList_SetSlice(names, len - 2, len, NULL);
4229 if (err == -1) {
4230 Py_DECREF(tail);
4231 return;
4232 }
4233 /* Stitch everything up into a nice comma-separated list. */
4234 comma = PyUnicode_FromString(", ");
4235 if (comma == NULL) {
4236 Py_DECREF(tail);
4237 return;
4238 }
4239 tmp = PyUnicode_Join(comma, names);
4240 Py_DECREF(comma);
4241 if (tmp == NULL) {
4242 Py_DECREF(tail);
4243 return;
4244 }
4245 name_str = PyUnicode_Concat(tmp, tail);
4246 Py_DECREF(tmp);
4247 Py_DECREF(tail);
4248 break;
4249 }
4250 if (name_str == NULL)
4251 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004252 _PyErr_Format(tstate, PyExc_TypeError,
4253 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004254 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004255 len,
4256 kind,
4257 len == 1 ? "" : "s",
4258 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004259 Py_DECREF(name_str);
4260}
4261
4262static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004263missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4264 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004265 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004266{
Victor Stinner74319ae2016-08-25 00:04:09 +02004267 Py_ssize_t i, j = 0;
4268 Py_ssize_t start, end;
4269 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004270 const char *kind = positional ? "positional" : "keyword-only";
4271 PyObject *missing_names;
4272
4273 /* Compute the names of the arguments that are missing. */
4274 missing_names = PyList_New(missing);
4275 if (missing_names == NULL)
4276 return;
4277 if (positional) {
4278 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004279 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004280 }
4281 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004282 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004283 end = start + co->co_kwonlyargcount;
4284 }
4285 for (i = start; i < end; i++) {
4286 if (GETLOCAL(i) == NULL) {
4287 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4288 PyObject *name = PyObject_Repr(raw);
4289 if (name == NULL) {
4290 Py_DECREF(missing_names);
4291 return;
4292 }
4293 PyList_SET_ITEM(missing_names, j++, name);
4294 }
4295 }
4296 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004297 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004298 Py_DECREF(missing_names);
4299}
4300
4301static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004302too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004303 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004304 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004305{
4306 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004307 Py_ssize_t kwonly_given = 0;
4308 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004309 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004310 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004311
Benjamin Petersone109c702011-06-24 09:37:26 -05004312 assert((co->co_flags & CO_VARARGS) == 0);
4313 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004314 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004315 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004316 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004317 }
4318 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004319 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004320 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004321 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004322 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004323 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004324 }
4325 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004326 plural = (co_argcount != 1);
4327 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004328 }
4329 if (sig == NULL)
4330 return;
4331 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004332 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4333 kwonly_sig = PyUnicode_FromFormat(format,
4334 given != 1 ? "s" : "",
4335 kwonly_given,
4336 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004337 if (kwonly_sig == NULL) {
4338 Py_DECREF(sig);
4339 return;
4340 }
4341 }
4342 else {
4343 /* This will not fail. */
4344 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004345 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004346 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004347 _PyErr_Format(tstate, PyExc_TypeError,
4348 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004349 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004350 sig,
4351 plural ? "s" : "",
4352 given,
4353 kwonly_sig,
4354 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004355 Py_DECREF(sig);
4356 Py_DECREF(kwonly_sig);
4357}
4358
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004359static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004360positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004361 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004362 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004363{
4364 int posonly_conflicts = 0;
4365 PyObject* posonly_names = PyList_New(0);
4366
4367 for(int k=0; k < co->co_posonlyargcount; k++){
4368 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4369
4370 for (int k2=0; k2<kwcount; k2++){
4371 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004372 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004373 if (kwname == posonly_name){
4374 if(PyList_Append(posonly_names, kwname) != 0) {
4375 goto fail;
4376 }
4377 posonly_conflicts++;
4378 continue;
4379 }
4380
4381 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4382
4383 if ( cmp > 0) {
4384 if(PyList_Append(posonly_names, kwname) != 0) {
4385 goto fail;
4386 }
4387 posonly_conflicts++;
4388 } else if (cmp < 0) {
4389 goto fail;
4390 }
4391
4392 }
4393 }
4394 if (posonly_conflicts) {
4395 PyObject* comma = PyUnicode_FromString(", ");
4396 if (comma == NULL) {
4397 goto fail;
4398 }
4399 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4400 Py_DECREF(comma);
4401 if (error_names == NULL) {
4402 goto fail;
4403 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004404 _PyErr_Format(tstate, PyExc_TypeError,
4405 "%U() got some positional-only arguments passed"
4406 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004407 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004408 Py_DECREF(error_names);
4409 goto fail;
4410 }
4411
4412 Py_DECREF(posonly_names);
4413 return 0;
4414
4415fail:
4416 Py_XDECREF(posonly_names);
4417 return 1;
4418
4419}
4420
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004421
Mark Shannon0332e562021-02-01 10:42:03 +00004422PyFrameObject *
4423_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004424 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004425 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004426 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004427{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004428 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004429
Mark Shannond6c33fb2021-01-29 13:24:55 +00004430 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4431 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004432 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004433
Victor Stinnerc7020012016-08-16 23:40:29 +02004434 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004435 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004436 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004438 }
Victor Stinner232dda62020-06-04 15:19:02 +02004439 PyObject **fastlocals = f->f_localsplus;
4440 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004441
Victor Stinnerc7020012016-08-16 23:40:29 +02004442 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004443 PyObject *kwdict;
4444 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004445 if (co->co_flags & CO_VARKEYWORDS) {
4446 kwdict = PyDict_New();
4447 if (kwdict == NULL)
4448 goto fail;
4449 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004450 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004451 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004452 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004453 SETLOCAL(i, kwdict);
4454 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004455 else {
4456 kwdict = NULL;
4457 }
4458
Pablo Galindocd74e662019-06-01 18:08:04 +01004459 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004460 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004461 if (argcount > co->co_argcount) {
4462 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004463 }
4464 else {
4465 n = argcount;
4466 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004467 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004468 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004469 Py_INCREF(x);
4470 SETLOCAL(j, x);
4471 }
4472
Victor Stinnerc7020012016-08-16 23:40:29 +02004473 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004474 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004475 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004476 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004477 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004478 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004479 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004480 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004481
Mark Shannon0332e562021-02-01 10:42:03 +00004482 /* Handle keyword arguments */
4483 if (kwnames != NULL) {
4484 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4485 for (i = 0; i < kwcount; i++) {
4486 PyObject **co_varnames;
4487 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4488 PyObject *value = args[i+argcount];
4489 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004490
Mark Shannon0332e562021-02-01 10:42:03 +00004491 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4492 _PyErr_Format(tstate, PyExc_TypeError,
4493 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004494 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004496 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004497
Mark Shannon0332e562021-02-01 10:42:03 +00004498 /* Speed hack: do raw pointer compares. As names are
4499 normally interned this should almost always hit. */
4500 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4501 for (j = co->co_posonlyargcount; j < total_args; j++) {
4502 PyObject *varname = co_varnames[j];
4503 if (varname == keyword) {
4504 goto kw_found;
4505 }
4506 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004507
Mark Shannon0332e562021-02-01 10:42:03 +00004508 /* Slow fallback, just in case */
4509 for (j = co->co_posonlyargcount; j < total_args; j++) {
4510 PyObject *varname = co_varnames[j];
4511 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4512 if (cmp > 0) {
4513 goto kw_found;
4514 }
4515 else if (cmp < 0) {
4516 goto fail;
4517 }
4518 }
4519
4520 assert(j >= total_args);
4521 if (kwdict == NULL) {
4522
4523 if (co->co_posonlyargcount
4524 && positional_only_passed_as_keyword(tstate, co,
4525 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004526 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004527 {
4528 goto fail;
4529 }
4530
4531 _PyErr_Format(tstate, PyExc_TypeError,
4532 "%U() got an unexpected keyword argument '%S'",
4533 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004534 goto fail;
4535 }
4536
Mark Shannon0332e562021-02-01 10:42:03 +00004537 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4538 goto fail;
4539 }
4540 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004541
Mark Shannon0332e562021-02-01 10:42:03 +00004542 kw_found:
4543 if (GETLOCAL(j) != NULL) {
4544 _PyErr_Format(tstate, PyExc_TypeError,
4545 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004546 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004547 goto fail;
4548 }
4549 Py_INCREF(value);
4550 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004553
4554 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004555 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004556 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4557 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 goto fail;
4559 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004560
4561 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004562 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004563 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004564 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004565 Py_ssize_t missing = 0;
4566 for (i = argcount; i < m; i++) {
4567 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004568 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004569 }
4570 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004571 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004572 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004573 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004574 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004575 }
4576 if (n > m)
4577 i = n - m;
4578 else
4579 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004580 if (defcount) {
4581 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4582 for (; i < defcount; i++) {
4583 if (GETLOCAL(m+i) == NULL) {
4584 PyObject *def = defs[i];
4585 Py_INCREF(def);
4586 SETLOCAL(m+i, def);
4587 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004588 }
4589 }
4590 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004591
4592 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004593 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004594 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004595 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004596 if (GETLOCAL(i) != NULL)
4597 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004598 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004599 if (con->fc_kwdefaults != NULL) {
4600 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004601 if (def) {
4602 Py_INCREF(def);
4603 SETLOCAL(i, def);
4604 continue;
4605 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004606 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004607 goto fail;
4608 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004609 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004610 missing++;
4611 }
4612 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004613 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004614 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004615 goto fail;
4616 }
4617 }
4618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004620 vars into frame. */
4621 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004623 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004624 /* Possibly account for the cell variable being an argument. */
4625 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004626 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004627 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004628 /* Clear the local copy. */
4629 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004630 }
4631 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004632 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004633 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004634 if (c == NULL)
4635 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004636 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004638
4639 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004640 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004641 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05004642 Py_INCREF(o);
4643 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004645
Mark Shannon0332e562021-02-01 10:42:03 +00004646 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00004647
Thomas Woutersce272b62007-09-19 21:19:28 +00004648fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 /* decref'ing the frame can cause __del__ methods to get invoked,
4651 which can call back into Python. While we're done with the
4652 current Python frame (f), the associated C stack is still in use,
4653 so recursion_depth must be boosted for the duration.
4654 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004655 if (Py_REFCNT(f) > 1) {
4656 Py_DECREF(f);
4657 _PyObject_GC_TRACK(f);
4658 }
4659 else {
4660 ++tstate->recursion_depth;
4661 Py_DECREF(f);
4662 --tstate->recursion_depth;
4663 }
Mark Shannon0332e562021-02-01 10:42:03 +00004664 return NULL;
4665}
4666
4667static PyObject *
4668make_coro(PyFrameConstructor *con, PyFrameObject *f)
4669{
4670 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
4671 PyObject *gen;
4672 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
4673
4674 /* Don't need to keep the reference to f_back, it will be set
4675 * when the generator is resumed. */
4676 Py_CLEAR(f->f_back);
4677
4678 /* Create a new generator that owns the ready to run frame
4679 * and return that as the value. */
4680 if (is_coro) {
4681 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
4682 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
4683 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
4684 } else {
4685 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
4686 }
4687 if (gen == NULL) {
4688 return NULL;
4689 }
4690
4691 _PyObject_GC_TRACK(f);
4692
4693 return gen;
4694}
4695
4696PyObject *
4697_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
4698 PyObject *locals,
4699 PyObject* const* args, size_t argcount,
4700 PyObject *kwnames)
4701{
4702 PyFrameObject *f = _PyEval_MakeFrameVector(
4703 tstate, con, locals, args, argcount, kwnames);
4704 if (f == NULL) {
4705 return NULL;
4706 }
4707 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
4708 return make_coro(con, f);
4709 }
4710 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
4711
4712 /* decref'ing the frame can cause __del__ methods to get invoked,
4713 which can call back into Python. While we're done with the
4714 current Python frame (f), the associated C stack is still in use,
4715 so recursion_depth must be boosted for the duration.
4716 */
4717 if (Py_REFCNT(f) > 1) {
4718 Py_DECREF(f);
4719 _PyObject_GC_TRACK(f);
4720 }
4721 else {
4722 ++tstate->recursion_depth;
4723 Py_DECREF(f);
4724 --tstate->recursion_depth;
4725 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004727}
4728
Mark Shannond6c33fb2021-01-29 13:24:55 +00004729/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004730PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00004731PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4732 PyObject *const *args, int argcount,
4733 PyObject *const *kws, int kwcount,
4734 PyObject *const *defs, int defcount,
4735 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004736{
Mark Shannon0332e562021-02-01 10:42:03 +00004737 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004738 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
4739 if (defaults == NULL) {
4740 return NULL;
4741 }
4742 PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals);
4743 if (builtins == NULL) {
4744 Py_DECREF(defaults);
4745 return NULL;
4746 }
Mark Shannon0332e562021-02-01 10:42:03 +00004747 PyCodeObject *code = (PyCodeObject *)_co;
4748 assert ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == 0);
4749 if (locals == NULL) {
4750 locals = globals;
4751 }
4752 PyObject *kwnames;
4753 PyObject *const *allargs;
4754 PyObject **newargs;
4755 if (kwcount == 0) {
4756 allargs = args;
4757 kwnames = NULL;
4758 }
4759 else {
4760 kwnames = PyTuple_New(kwcount);
4761 if (kwnames == NULL) {
4762 res = NULL;
4763 goto fail;
4764 }
4765 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
4766 if (newargs == NULL) {
4767 res = NULL;
4768 Py_DECREF(kwnames);
4769 goto fail;
4770 }
4771 for (int i = 0; i < argcount; i++) {
4772 newargs[i] = args[i];
4773 }
4774 for (int i = 0; i < kwcount; i++) {
4775 Py_INCREF(kws[2*i]);
4776 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
4777 newargs[argcount+i] = kws[2*i+1];
4778 }
4779 allargs = newargs;
4780 }
4781 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
4782 if (kwargs == NULL) {
4783 res = NULL;
4784 Py_DECREF(kwnames);
4785 goto fail;
4786 }
4787 for (int i = 0; i < kwcount; i++) {
4788 Py_INCREF(kws[2*i]);
4789 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
4790 kwargs[i] = kws[2*i+1];
4791 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004792 PyFrameConstructor constr = {
4793 .fc_globals = globals,
4794 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00004795 .fc_name = ((PyCodeObject *)_co)->co_name,
4796 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004797 .fc_code = _co,
4798 .fc_defaults = defaults,
4799 .fc_kwdefaults = kwdefs,
4800 .fc_closure = closure
4801 };
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004802 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00004803 res = _PyEval_Vector(tstate, &constr, locals,
4804 allargs, argcount,
4805 kwnames);
4806 if (kwcount) {
4807 Py_DECREF(kwnames);
4808 PyMem_Free(newargs);
4809 }
4810fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00004811 Py_DECREF(defaults);
4812 Py_DECREF(builtins);
4813 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004814}
4815
Tim Peters5ca576e2001-06-18 22:08:13 +00004816
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004817static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004818special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004821 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004822 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004823 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 return NULL;
4825 }
4826 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004827}
4828
4829
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004830/* Logic for the raise statement (too complicated for inlining).
4831 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004832static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004833do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 if (exc == NULL) {
4838 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004839 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004841 type = exc_info->exc_type;
4842 value = exc_info->exc_value;
4843 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004844 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004845 _PyErr_SetString(tstate, PyExc_RuntimeError,
4846 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004847 return 0;
4848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 Py_XINCREF(type);
4850 Py_XINCREF(value);
4851 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004852 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004853 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 /* We support the following forms of raise:
4857 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004858 raise <instance>
4859 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 if (PyExceptionClass_Check(exc)) {
4862 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004863 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 if (value == NULL)
4865 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004866 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004867 _PyErr_Format(tstate, PyExc_TypeError,
4868 "calling %R should have returned an instance of "
4869 "BaseException, not %R",
4870 type, Py_TYPE(value));
4871 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004872 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 }
4874 else if (PyExceptionInstance_Check(exc)) {
4875 value = exc;
4876 type = PyExceptionInstance_Class(exc);
4877 Py_INCREF(type);
4878 }
4879 else {
4880 /* Not something you can raise. You get an exception
4881 anyway, just not what you specified :-) */
4882 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004883 _PyErr_SetString(tstate, PyExc_TypeError,
4884 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 goto raise_error;
4886 }
Collin Winter828f04a2007-08-31 00:04:24 +00004887
Serhiy Storchakac0191582016-09-27 11:37:10 +03004888 assert(type != NULL);
4889 assert(value != NULL);
4890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 if (cause) {
4892 PyObject *fixed_cause;
4893 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004894 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 if (fixed_cause == NULL)
4896 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004897 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004899 else if (PyExceptionInstance_Check(cause)) {
4900 fixed_cause = cause;
4901 }
4902 else if (cause == Py_None) {
4903 Py_DECREF(cause);
4904 fixed_cause = NULL;
4905 }
4906 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004907 _PyErr_SetString(tstate, PyExc_TypeError,
4908 "exception causes must derive from "
4909 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 goto raise_error;
4911 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004912 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913 }
Collin Winter828f04a2007-08-31 00:04:24 +00004914
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004916 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004917 Py_DECREF(value);
4918 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004919 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004920
4921raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 Py_XDECREF(value);
4923 Py_XDECREF(type);
4924 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004925 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004926}
4927
Tim Petersd6d010b2001-06-21 02:49:55 +00004928/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004929 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004930
Guido van Rossum0368b722007-05-11 16:50:42 +00004931 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4932 with a variable target.
4933*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004934
Barry Warsawe42b18f1997-08-25 22:13:04 +00004935static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004936unpack_iterable(PyThreadState *tstate, PyObject *v,
4937 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 int i = 0, j = 0;
4940 Py_ssize_t ll = 0;
4941 PyObject *it; /* iter(v) */
4942 PyObject *w;
4943 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004948 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004949 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004950 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004951 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004952 _PyErr_Format(tstate, PyExc_TypeError,
4953 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004954 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004955 }
4956 return 0;
4957 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 for (; i < argcnt; i++) {
4960 w = PyIter_Next(it);
4961 if (w == NULL) {
4962 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004963 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004964 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004965 _PyErr_Format(tstate, PyExc_ValueError,
4966 "not enough values to unpack "
4967 "(expected %d, got %d)",
4968 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004969 }
4970 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004971 _PyErr_Format(tstate, PyExc_ValueError,
4972 "not enough values to unpack "
4973 "(expected at least %d, got %d)",
4974 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 }
4977 goto Error;
4978 }
4979 *--sp = w;
4980 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 if (argcntafter == -1) {
4983 /* We better have exhausted the iterator now. */
4984 w = PyIter_Next(it);
4985 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004986 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 goto Error;
4988 Py_DECREF(it);
4989 return 1;
4990 }
4991 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004992 _PyErr_Format(tstate, PyExc_ValueError,
4993 "too many values to unpack (expected %d)",
4994 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 goto Error;
4996 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 l = PySequence_List(it);
4999 if (l == NULL)
5000 goto Error;
5001 *--sp = l;
5002 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 ll = PyList_GET_SIZE(l);
5005 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005006 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005007 "not enough values to unpack (expected at least %d, got %zd)",
5008 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 goto Error;
5010 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 /* Pop the "after-variable" args off the list. */
5013 for (j = argcntafter; j > 0; j--, i++) {
5014 *--sp = PyList_GET_ITEM(l, ll - j);
5015 }
5016 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005017 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 Py_DECREF(it);
5019 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005020
Tim Petersd6d010b2001-06-21 02:49:55 +00005021Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 for (; i > 0; i--, sp++)
5023 Py_DECREF(*sp);
5024 Py_XDECREF(it);
5025 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005026}
5027
5028
Guido van Rossum96a42c81992-01-12 02:29:51 +00005029#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005030static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005031prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005034 if (PyObject_Print(v, stdout, 0) != 0) {
5035 /* Don't know what else to do */
5036 _PyErr_Clear(tstate);
5037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 printf("\n");
5039 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005040}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005041#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005042
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005043static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005044call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005045 PyThreadState *tstate,
5046 PyFrameObject *f,
5047 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005048{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005049 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005051 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005052 if (value == NULL) {
5053 value = Py_None;
5054 Py_INCREF(value);
5055 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005056 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005057 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005058 arg = PyTuple_Pack(3, type, value, traceback);
5059 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005060 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 return;
5062 }
Mark Shannon86433452021-01-07 16:49:02 +00005063 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005065 if (err == 0) {
5066 _PyErr_Restore(tstate, type, value, orig_traceback);
5067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 else {
5069 Py_XDECREF(type);
5070 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005071 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005072 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005073}
5074
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005075static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005076call_trace_protected(Py_tracefunc func, PyObject *obj,
5077 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005078 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 PyObject *type, *value, *traceback;
5082 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005083 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00005084 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 if (err == 0)
5086 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005087 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 return 0;
5089 }
5090 else {
5091 Py_XDECREF(type);
5092 Py_XDECREF(value);
5093 Py_XDECREF(traceback);
5094 return -1;
5095 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005096}
5097
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005098static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005099call_trace(Py_tracefunc func, PyObject *obj,
5100 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00005101 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 int result;
5105 if (tstate->tracing)
5106 return 0;
5107 tstate->tracing++;
5108 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005109 if (frame->f_lasti < 0) {
5110 frame->f_lineno = frame->f_code->co_firstlineno;
5111 }
5112 else {
5113 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005116 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5118 || (tstate->c_profilefunc != NULL));
5119 tstate->tracing--;
5120 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005121}
5122
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005123PyObject *
5124_PyEval_CallTracing(PyObject *func, PyObject *args)
5125{
Victor Stinner50b48572018-11-01 01:51:40 +01005126 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 int save_tracing = tstate->tracing;
5128 int save_use_tracing = tstate->use_tracing;
5129 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 tstate->tracing = 0;
5132 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5133 || (tstate->c_profilefunc != NULL));
5134 result = PyObject_Call(func, args, NULL);
5135 tstate->tracing = save_tracing;
5136 tstate->use_tracing = save_use_tracing;
5137 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005138}
5139
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005140/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005141static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005142maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005143 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005144 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005147
Nick Coghlan5a851672017-09-08 10:14:16 +10005148 /* If the last instruction falls at the start of a line or if it
5149 represents a jump backwards, update the frame's line number and
5150 then call the trace function if we're tracing source lines.
5151 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005152 int lastline = bounds->ar_line;
5153 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5154 if (line != -1 && frame->f_trace_lines) {
5155 /* Trace backward edges or first instruction of a new line */
5156 if (frame->f_lasti < *instr_prev ||
5157 (line != lastline && frame->f_lasti == bounds->ar_start))
5158 {
Mark Shannon86433452021-01-07 16:49:02 +00005159 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 }
George King20faa682017-10-18 17:44:22 -07005162 /* Always emit an opcode event if we're tracing all opcodes. */
5163 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005164 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005165 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005166 *instr_prev = frame->f_lasti;
5167 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005168}
5169
Victor Stinner309d7cc2020-03-13 16:39:12 +01005170int
5171_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5172{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005173 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005174 /* The caller must hold the GIL */
5175 assert(PyGILState_Check());
5176
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005177 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005178 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005179 PyThreadState *current_tstate = _PyThreadState_GET();
5180 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005181 return -1;
5182 }
5183
5184 PyObject *profileobj = tstate->c_profileobj;
5185
5186 tstate->c_profilefunc = NULL;
5187 tstate->c_profileobj = NULL;
5188 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5189 tstate->use_tracing = tstate->c_tracefunc != NULL;
5190 Py_XDECREF(profileobj);
5191
5192 Py_XINCREF(arg);
5193 tstate->c_profileobj = arg;
5194 tstate->c_profilefunc = func;
5195
5196 /* Flag that tracing or profiling is turned on */
5197 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5198 return 0;
5199}
5200
Fred Drake5755ce62001-06-27 19:19:46 +00005201void
5202PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005203{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005204 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005205 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005206 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005207 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5208 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005209}
5210
5211int
5212_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5213{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005214 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005215 /* The caller must hold the GIL */
5216 assert(PyGILState_Check());
5217
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005218 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005219 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005220 PyThreadState *current_tstate = _PyThreadState_GET();
5221 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005222 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005223 }
5224
Victor Stinnerda2914d2020-03-20 09:29:08 +01005225 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005226 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005227 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005228
5229 tstate->c_tracefunc = NULL;
5230 tstate->c_traceobj = NULL;
5231 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5232 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5233 Py_XDECREF(traceobj);
5234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005235 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005236 tstate->c_traceobj = arg;
5237 tstate->c_tracefunc = func;
5238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005240 tstate->use_tracing = ((func != NULL)
5241 || (tstate->c_profilefunc != NULL));
5242
5243 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005244}
5245
5246void
5247PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5248{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005249 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005250 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005251 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005252 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5253 }
Fred Draked0838392001-06-16 21:02:31 +00005254}
5255
Victor Stinner309d7cc2020-03-13 16:39:12 +01005256
Yury Selivanov75445082015-05-11 22:57:16 -04005257void
Victor Stinner838f2642019-06-13 22:41:23 +02005258_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005259{
5260 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005261 tstate->coroutine_origin_tracking_depth = new_depth;
5262}
5263
5264int
5265_PyEval_GetCoroutineOriginTrackingDepth(void)
5266{
Victor Stinner50b48572018-11-01 01:51:40 +01005267 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005268 return tstate->coroutine_origin_tracking_depth;
5269}
5270
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005271int
Yury Selivanoveb636452016-09-08 22:01:51 -07005272_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5273{
Victor Stinner50b48572018-11-01 01:51:40 +01005274 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005275
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005276 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005277 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005278 }
5279
Yury Selivanoveb636452016-09-08 22:01:51 -07005280 Py_XINCREF(firstiter);
5281 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005282 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005283}
5284
5285PyObject *
5286_PyEval_GetAsyncGenFirstiter(void)
5287{
Victor Stinner50b48572018-11-01 01:51:40 +01005288 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005289 return tstate->async_gen_firstiter;
5290}
5291
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005292int
Yury Selivanoveb636452016-09-08 22:01:51 -07005293_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5294{
Victor Stinner50b48572018-11-01 01:51:40 +01005295 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005296
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005297 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005298 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005299 }
5300
Yury Selivanoveb636452016-09-08 22:01:51 -07005301 Py_XINCREF(finalizer);
5302 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005303 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005304}
5305
5306PyObject *
5307_PyEval_GetAsyncGenFinalizer(void)
5308{
Victor Stinner50b48572018-11-01 01:51:40 +01005309 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005310 return tstate->async_gen_finalizer;
5311}
5312
Victor Stinner438a12d2019-05-24 17:01:38 +02005313PyFrameObject *
5314PyEval_GetFrame(void)
5315{
5316 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005317 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005318}
5319
Guido van Rossumb209a111997-04-29 18:18:01 +00005320PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005321PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005322{
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005324 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005326 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 else
5328 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005329}
5330
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005331/* Convenience function to get a builtin from its name */
5332PyObject *
5333_PyEval_GetBuiltinId(_Py_Identifier *name)
5334{
Victor Stinner438a12d2019-05-24 17:01:38 +02005335 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005336 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5337 if (attr) {
5338 Py_INCREF(attr);
5339 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005340 else if (!_PyErr_Occurred(tstate)) {
5341 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005342 }
5343 return attr;
5344}
5345
Guido van Rossumb209a111997-04-29 18:18:01 +00005346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005347PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005348{
Victor Stinner438a12d2019-05-24 17:01:38 +02005349 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005350 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005351 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005352 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005354 }
5355
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005357 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005358 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005359
5360 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005362}
5363
Guido van Rossumb209a111997-04-29 18:18:01 +00005364PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005365PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005366{
Victor Stinner438a12d2019-05-24 17:01:38 +02005367 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005368 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005369 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005371 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005372
5373 assert(current_frame->f_globals != NULL);
5374 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005375}
5376
Guido van Rossum6135a871995-01-09 17:53:26 +00005377int
Tim Peters5ba58662001-07-16 02:29:45 +00005378PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005379{
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005381 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 if (current_frame != NULL) {
5385 const int codeflags = current_frame->f_code->co_flags;
5386 const int compilerflags = codeflags & PyCF_MASK;
5387 if (compilerflags) {
5388 result = 1;
5389 cf->cf_flags |= compilerflags;
5390 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005391#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 if (codeflags & CO_GENERATOR_ALLOWED) {
5393 result = 1;
5394 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5395 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005396#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 }
5398 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005399}
5400
Guido van Rossum3f5da241990-12-20 15:06:42 +00005401
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005402const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005403PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 if (PyMethod_Check(func))
5406 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5407 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005408 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 else if (PyCFunction_Check(func))
5410 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5411 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005412 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005413}
5414
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005415const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005416PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 if (PyMethod_Check(func))
5419 return "()";
5420 else if (PyFunction_Check(func))
5421 return "()";
5422 else if (PyCFunction_Check(func))
5423 return "()";
5424 else
5425 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005426}
5427
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005428#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005429if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005430 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005431 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005432 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 x = NULL; \
5434 } \
5435 else { \
5436 x = call; \
5437 if (tstate->c_profilefunc != NULL) { \
5438 if (x == NULL) { \
5439 call_trace_protected(tstate->c_profilefunc, \
5440 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005441 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005442 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 /* XXX should pass (type, value, tb) */ \
5444 } else { \
5445 if (call_trace(tstate->c_profilefunc, \
5446 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005447 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005448 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 Py_DECREF(x); \
5450 x = NULL; \
5451 } \
5452 } \
5453 } \
5454 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005455} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 x = call; \
5457 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005458
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005459
5460static PyObject *
5461trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005462 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005463 PyObject *func,
5464 PyObject **args, Py_ssize_t nargs,
5465 PyObject *kwnames)
5466{
5467 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005468 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005469 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005470 return x;
5471 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005472 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005473 /* We need to create a temporary bound method as argument
5474 for profiling.
5475
5476 If nargs == 0, then this cannot work because we have no
5477 "self". In any case, the call itself would raise
5478 TypeError (foo needs an argument), so we just skip
5479 profiling. */
5480 PyObject *self = args[0];
5481 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5482 if (func == NULL) {
5483 return NULL;
5484 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005485 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005486 args+1, nargs-1,
5487 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005488 Py_DECREF(func);
5489 return x;
5490 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005491 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005492}
5493
Victor Stinner415c5102017-01-11 00:54:57 +01005494/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5495 to reduce the stack consumption. */
5496Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005497call_function(PyThreadState *tstate,
5498 PyCodeAddressRange *bounds,
5499 PyObject ***pp_stack,
5500 Py_ssize_t oparg,
5501 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005502{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005503 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 PyObject *func = *pfunc;
5505 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005506 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5507 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005508 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005509
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005510 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005511 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005512 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005513 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005514 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005515 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005516
Victor Stinner438a12d2019-05-24 17:01:38 +02005517 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005518
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005519 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 while ((*pp_stack) > pfunc) {
5521 w = EXT_POP(*pp_stack);
5522 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005525 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005526}
5527
Jeremy Hylton52820442001-01-03 23:52:36 +00005528static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005529do_call_core(PyThreadState *tstate,
5530 PyCodeAddressRange *bounds,
5531 PyObject *func,
5532 PyObject *callargs,
5533 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005534{
jdemeyere89de732018-09-19 12:06:20 +02005535 PyObject *result;
5536
scoder4c9ea092020-05-12 16:12:41 +02005537 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005538 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005539 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005541 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005542 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5543 if (nargs > 0 && tstate->use_tracing) {
5544 /* We need to create a temporary bound method as argument
5545 for profiling.
5546
5547 If nargs == 0, then this cannot work because we have no
5548 "self". In any case, the call itself would raise
5549 TypeError (foo needs an argument), so we just skip
5550 profiling. */
5551 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5552 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5553 if (func == NULL) {
5554 return NULL;
5555 }
5556
Victor Stinner4d231bc2019-11-14 13:36:21 +01005557 C_TRACE(result, _PyObject_FastCallDictTstate(
5558 tstate, func,
5559 &_PyTuple_ITEMS(callargs)[1],
5560 nargs - 1,
5561 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005562 Py_DECREF(func);
5563 return result;
5564 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005565 }
jdemeyere89de732018-09-19 12:06:20 +02005566 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005567}
5568
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005569/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005570 nb_index slot defined, and store in *pi.
5571 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005572 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005573 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005574*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005575int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005576_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005577{
Victor Stinner438a12d2019-05-24 17:01:38 +02005578 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005579 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005581 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005582 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005583 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005584 return 0;
5585 }
5586 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005587 _PyErr_SetString(tstate, PyExc_TypeError,
5588 "slice indices must be integers or "
5589 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 return 0;
5591 }
5592 *pi = x;
5593 }
5594 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005595}
5596
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005597int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005598_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005599{
Victor Stinner438a12d2019-05-24 17:01:38 +02005600 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005601 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005602 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005603 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005604 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005605 return 0;
5606 }
5607 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005608 _PyErr_SetString(tstate, PyExc_TypeError,
5609 "slice indices must be integers or "
5610 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005611 return 0;
5612 }
5613 *pi = x;
5614 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005615}
5616
Thomas Wouters52152252000-08-17 22:55:00 +00005617static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005618import_name(PyThreadState *tstate, PyFrameObject *f,
5619 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005620{
5621 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005622 PyObject *import_func, *res;
5623 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005624
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005625 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005626 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005627 if (!_PyErr_Occurred(tstate)) {
5628 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005629 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005630 return NULL;
5631 }
5632
5633 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005634 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005635 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005636 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005637 return NULL;
5638 }
5639 res = PyImport_ImportModuleLevelObject(
5640 name,
5641 f->f_globals,
5642 f->f_locals == NULL ? Py_None : f->f_locals,
5643 fromlist,
5644 ilevel);
5645 return res;
5646 }
5647
5648 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005649
5650 stack[0] = name;
5651 stack[1] = f->f_globals;
5652 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5653 stack[3] = fromlist;
5654 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005655 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005656 Py_DECREF(import_func);
5657 return res;
5658}
5659
5660static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005661import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005663 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005664 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005665
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005666 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005667 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005668 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005669 /* Issue #17636: in case this failed because of a circular relative
5670 import, try to fallback on reading the module directly from
5671 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005672 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005673 if (pkgname == NULL) {
5674 goto error;
5675 }
Oren Milman6db70332017-09-19 14:23:01 +03005676 if (!PyUnicode_Check(pkgname)) {
5677 Py_CLEAR(pkgname);
5678 goto error;
5679 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005680 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005681 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005682 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005683 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005684 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005685 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005686 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005687 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005688 goto error;
5689 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005690 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005691 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005692 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005693 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005694 if (pkgname == NULL) {
5695 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5696 if (pkgname_or_unknown == NULL) {
5697 Py_XDECREF(pkgpath);
5698 return NULL;
5699 }
5700 } else {
5701 pkgname_or_unknown = pkgname;
5702 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005703
5704 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005705 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005706 errmsg = PyUnicode_FromFormat(
5707 "cannot import name %R from %R (unknown location)",
5708 name, pkgname_or_unknown
5709 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005710 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005711 PyErr_SetImportError(errmsg, pkgname, NULL);
5712 }
5713 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005714 _Py_IDENTIFIER(__spec__);
5715 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005716 const char *fmt =
5717 _PyModuleSpec_IsInitializing(spec) ?
5718 "cannot import name %R from partially initialized module %R "
5719 "(most likely due to a circular import) (%S)" :
5720 "cannot import name %R from %R (%S)";
5721 Py_XDECREF(spec);
5722
5723 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005724 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005725 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005726 }
5727
Xiang Zhang4830f582017-03-21 11:13:42 +08005728 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005729 Py_XDECREF(pkgname_or_unknown);
5730 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005731 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005732}
Guido van Rossumac7be682001-01-17 15:42:30 +00005733
Thomas Wouters52152252000-08-17 22:55:00 +00005734static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005735import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005736{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005737 _Py_IDENTIFIER(__all__);
5738 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005739 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005740 int skip_leading_underscores = 0;
5741 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005742
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005743 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5744 return -1; /* Unexpected error */
5745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005746 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005747 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5748 return -1;
5749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005751 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005752 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005753 return -1;
5754 }
5755 all = PyMapping_Keys(dict);
5756 Py_DECREF(dict);
5757 if (all == NULL)
5758 return -1;
5759 skip_leading_underscores = 1;
5760 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005762 for (pos = 0, err = 0; ; pos++) {
5763 name = PySequence_GetItem(all, pos);
5764 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005765 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005766 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005767 }
5768 else {
5769 _PyErr_Clear(tstate);
5770 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005771 break;
5772 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005773 if (!PyUnicode_Check(name)) {
5774 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5775 if (modname == NULL) {
5776 Py_DECREF(name);
5777 err = -1;
5778 break;
5779 }
5780 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005781 _PyErr_Format(tstate, PyExc_TypeError,
5782 "module __name__ must be a string, not %.100s",
5783 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005784 }
5785 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005786 _PyErr_Format(tstate, PyExc_TypeError,
5787 "%s in %U.%s must be str, not %.100s",
5788 skip_leading_underscores ? "Key" : "Item",
5789 modname,
5790 skip_leading_underscores ? "__dict__" : "__all__",
5791 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005792 }
5793 Py_DECREF(modname);
5794 Py_DECREF(name);
5795 err = -1;
5796 break;
5797 }
5798 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005799 if (PyUnicode_READY(name) == -1) {
5800 Py_DECREF(name);
5801 err = -1;
5802 break;
5803 }
5804 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5805 Py_DECREF(name);
5806 continue;
5807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005808 }
5809 value = PyObject_GetAttr(v, name);
5810 if (value == NULL)
5811 err = -1;
5812 else if (PyDict_CheckExact(locals))
5813 err = PyDict_SetItem(locals, name, value);
5814 else
5815 err = PyObject_SetItem(locals, name, value);
5816 Py_DECREF(name);
5817 Py_XDECREF(value);
5818 if (err != 0)
5819 break;
5820 }
5821 Py_DECREF(all);
5822 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005823}
5824
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005825static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005826check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005827{
Victor Stinnera102ed72020-02-07 02:24:48 +01005828 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005829 /* check_args_iterable() may be called with a live exception:
5830 * clear it to prevent calling _PyObject_FunctionStr() with an
5831 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005832 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005833 PyObject *funcstr = _PyObject_FunctionStr(func);
5834 if (funcstr != NULL) {
5835 _PyErr_Format(tstate, PyExc_TypeError,
5836 "%U argument after * must be an iterable, not %.200s",
5837 funcstr, Py_TYPE(args)->tp_name);
5838 Py_DECREF(funcstr);
5839 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005840 return -1;
5841 }
5842 return 0;
5843}
5844
5845static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005846format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005847{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005848 /* _PyDict_MergeEx raises attribute
5849 * error (percolated from an attempt
5850 * to get 'keys' attribute) instead of
5851 * a type error if its second argument
5852 * is not a mapping.
5853 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005854 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005855 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005856 PyObject *funcstr = _PyObject_FunctionStr(func);
5857 if (funcstr != NULL) {
5858 _PyErr_Format(
5859 tstate, PyExc_TypeError,
5860 "%U argument after ** must be a mapping, not %.200s",
5861 funcstr, Py_TYPE(kwargs)->tp_name);
5862 Py_DECREF(funcstr);
5863 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005864 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005865 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005866 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005867 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005868 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005869 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005870 PyObject *funcstr = _PyObject_FunctionStr(func);
5871 if (funcstr != NULL) {
5872 PyObject *key = PyTuple_GET_ITEM(val, 0);
5873 _PyErr_Format(
5874 tstate, PyExc_TypeError,
5875 "%U got multiple values for keyword argument '%S'",
5876 funcstr, key);
5877 Py_DECREF(funcstr);
5878 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005879 Py_XDECREF(exc);
5880 Py_XDECREF(val);
5881 Py_XDECREF(tb);
5882 }
5883 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005884 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005885 }
5886 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005887}
5888
Guido van Rossumac7be682001-01-17 15:42:30 +00005889static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005890format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5891 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005893 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005895 if (!obj)
5896 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005897
Serhiy Storchaka06515832016-11-20 09:13:07 +02005898 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005899 if (!obj_str)
5900 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005901
Victor Stinner438a12d2019-05-24 17:01:38 +02005902 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005903}
Guido van Rossum950361c1997-01-24 13:49:28 +00005904
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005905static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005906format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005907{
5908 PyObject *name;
5909 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005910 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005911 return;
5912 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5913 name = PyTuple_GET_ITEM(co->co_cellvars,
5914 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005915 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005916 PyExc_UnboundLocalError,
5917 UNBOUNDLOCAL_ERROR_MSG,
5918 name);
5919 } else {
5920 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5921 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005922 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005923 UNBOUNDFREE_ERROR_MSG, name);
5924 }
5925}
5926
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005927static void
Mark Shannonfee55262019-11-21 09:11:43 +00005928format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005929{
5930 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5931 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005932 _PyErr_Format(tstate, PyExc_TypeError,
5933 "'async with' received an object from __aenter__ "
5934 "that does not implement __await__: %.100s",
5935 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005936 }
Mark Shannonfee55262019-11-21 09:11:43 +00005937 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005938 _PyErr_Format(tstate, PyExc_TypeError,
5939 "'async with' received an object from __aexit__ "
5940 "that does not implement __await__: %.100s",
5941 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005942 }
5943 }
5944}
5945
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005946static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005947unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005948 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005949{
5950 PyObject *res;
5951 if (Py_REFCNT(v) == 2) {
5952 /* In the common case, there are 2 references to the value
5953 * stored in 'variable' when the += is performed: one on the
5954 * value stack (in 'v') and one still stored in the
5955 * 'variable'. We try to delete the variable now to reduce
5956 * the refcnt to 1.
5957 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005958 int opcode, oparg;
5959 NEXTOPARG();
5960 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005961 case STORE_FAST:
5962 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005963 PyObject **fastlocals = f->f_localsplus;
5964 if (GETLOCAL(oparg) == v)
5965 SETLOCAL(oparg, NULL);
5966 break;
5967 }
5968 case STORE_DEREF:
5969 {
5970 PyObject **freevars = (f->f_localsplus +
5971 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005972 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005973 if (PyCell_GET(c) == v) {
5974 PyCell_SET(c, NULL);
5975 Py_DECREF(v);
5976 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005977 break;
5978 }
5979 case STORE_NAME:
5980 {
5981 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005982 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005983 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005984 if (locals && PyDict_CheckExact(locals)) {
5985 PyObject *w = PyDict_GetItemWithError(locals, name);
5986 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005987 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005988 {
5989 Py_DECREF(v);
5990 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005991 }
5992 }
5993 break;
5994 }
5995 }
5996 }
5997 res = v;
5998 PyUnicode_Append(&res, w);
5999 return res;
6000}
6001
Guido van Rossum950361c1997-01-24 13:49:28 +00006002#ifdef DYNAMIC_EXECUTION_PROFILE
6003
Skip Montanarof118cb12001-10-15 20:51:38 +00006004static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006005getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006007 int i;
6008 PyObject *l = PyList_New(256);
6009 if (l == NULL) return NULL;
6010 for (i = 0; i < 256; i++) {
6011 PyObject *x = PyLong_FromLong(a[i]);
6012 if (x == NULL) {
6013 Py_DECREF(l);
6014 return NULL;
6015 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006016 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006017 }
6018 for (i = 0; i < 256; i++)
6019 a[i] = 0;
6020 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006021}
6022
6023PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006024_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006025{
6026#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006027 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006028#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006029 int i;
6030 PyObject *l = PyList_New(257);
6031 if (l == NULL) return NULL;
6032 for (i = 0; i < 257; i++) {
6033 PyObject *x = getarray(dxpairs[i]);
6034 if (x == NULL) {
6035 Py_DECREF(l);
6036 return NULL;
6037 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006038 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006039 }
6040 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006041#endif
6042}
6043
6044#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006045
6046Py_ssize_t
6047_PyEval_RequestCodeExtraIndex(freefunc free)
6048{
Victor Stinner81a7be32020-04-14 15:14:01 +02006049 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006050 Py_ssize_t new_index;
6051
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006052 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006053 return -1;
6054 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006055 new_index = interp->co_extra_user_count++;
6056 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006057 return new_index;
6058}
Łukasz Langaa785c872016-09-09 17:37:37 -07006059
6060static void
6061dtrace_function_entry(PyFrameObject *f)
6062{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006063 const char *filename;
6064 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006065 int lineno;
6066
Victor Stinner6d86a232020-04-29 00:56:58 +02006067 PyCodeObject *code = f->f_code;
6068 filename = PyUnicode_AsUTF8(code->co_filename);
6069 funcname = PyUnicode_AsUTF8(code->co_name);
6070 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006071
Andy Lestere6be9b52020-02-11 20:28:35 -06006072 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006073}
6074
6075static void
6076dtrace_function_return(PyFrameObject *f)
6077{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006078 const char *filename;
6079 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006080 int lineno;
6081
Victor Stinner6d86a232020-04-29 00:56:58 +02006082 PyCodeObject *code = f->f_code;
6083 filename = PyUnicode_AsUTF8(code->co_filename);
6084 funcname = PyUnicode_AsUTF8(code->co_name);
6085 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006086
Andy Lestere6be9b52020-02-11 20:28:35 -06006087 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006088}
6089
6090/* DTrace equivalent of maybe_call_line_trace. */
6091static void
6092maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00006093 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07006094{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006095 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006096
6097 /* If the last instruction executed isn't in the current
6098 instruction window, reset the window.
6099 */
Mark Shannon877df852020-11-12 09:43:29 +00006100 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006101 /* If the last instruction falls at the start of a line or if
6102 it represents a jump backwards, update the frame's line
6103 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00006104 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
6105 if (line != -1) {
6106 frame->f_lineno = line;
6107 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6108 if (!co_filename)
6109 co_filename = "?";
6110 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6111 if (!co_name)
6112 co_name = "?";
6113 PyDTrace_LINE(co_filename, co_name, line);
6114 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006115 }
6116 *instr_prev = frame->f_lasti;
6117}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006118
6119
6120/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6121 for the limited API. */
6122
6123#undef Py_EnterRecursiveCall
6124
6125int Py_EnterRecursiveCall(const char *where)
6126{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006127 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006128}
6129
6130#undef Py_LeaveRecursiveCall
6131
6132void Py_LeaveRecursiveCall(void)
6133{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006134 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006135}