blob: 5e2a160f0ed35c5e98ca0ca938ed76c3482f08c3 [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 Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossumc6004111993-11-05 10:22:19 +000033#include <ctype.h>
34
Guido van Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000036/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define LLTRACE 1 /* Low-level trace feature */
38#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#endif
40
Victor Stinner5c75f372019-04-17 23:02:26 +020041#if !defined(Py_BUILD_CORE)
42# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43#endif
44
Hai Shi46874c22020-01-30 17:20:25 -060045_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000046
Guido van Rossum374a9221991-04-04 10:40:29 +000047/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020048Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon86433452021-01-07 16:49:02 +000049 PyThreadState *tstate, PyCodeAddressRange *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020050 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
Mark Shannon86433452021-01-07 16:49:02 +000052 PyThreadState *tstate, PyCodeAddressRange *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020053 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000054
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000056static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020057static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059static int call_trace(Py_tracefunc, PyObject *,
60 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000061 PyCodeAddressRange *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000063static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010064 PyThreadState *, PyFrameObject *,
Mark Shannon86433452021-01-07 16:49:02 +000065 PyCodeAddressRange *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010066 int, PyObject *);
67static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000068 PyThreadState *, PyFrameObject *,
69 PyCodeAddressRange *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000070static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060071 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000072 PyCodeAddressRange *, int *);
73static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070074static void dtrace_function_entry(PyFrameObject *);
75static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000076
Victor Stinner438a12d2019-05-24 17:01:38 +020077static PyObject * import_name(PyThreadState *, PyFrameObject *,
78 PyObject *, PyObject *, PyObject *);
79static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
80static int import_all_from(PyThreadState *, PyObject *, PyObject *);
81static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
82static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
83static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030084 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020085static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
86static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
87static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000088static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000089
Paul Prescode68140d2000-08-30 20:25:01 +000090#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000092#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000094#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 "free variable '%.200s' referenced before assignment" \
96 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000097
Guido van Rossum950361c1997-01-24 13:49:28 +000098/* Dynamic execution profile */
99#ifdef DYNAMIC_EXECUTION_PROFILE
100#ifdef DXPAIRS
101static long dxpairs[257][256];
102#define dxp dxpairs[256]
103#else
104static long dxp[256];
105#endif
106#endif
107
Inada Naoki91234a12019-06-03 21:30:58 +0900108/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900109#ifdef Py_DEBUG
110// --with-pydebug is used to find memory leak. opcache makes it harder.
111// So we disable opcache when Py_DEBUG is defined.
112// See bpo-37146
113#define OPCACHE_MIN_RUNS 0 /* disable opcache */
114#else
Inada Naoki91234a12019-06-03 21:30:58 +0900115#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900116#endif
Pablo Galindo109826c2020-10-20 06:22:44 +0100117#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900118#define OPCACHE_STATS 0 /* Enable stats */
119
120#if OPCACHE_STATS
121static size_t opcache_code_objects = 0;
122static size_t opcache_code_objects_extra_mem = 0;
123
124static size_t opcache_global_opts = 0;
125static size_t opcache_global_hits = 0;
126static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100127
128static size_t opcache_attr_opts = 0;
129static size_t opcache_attr_hits = 0;
130static size_t opcache_attr_misses = 0;
131static size_t opcache_attr_deopts = 0;
132static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900133#endif
134
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100135
Victor Stinnerda2914d2020-03-20 09:29:08 +0100136#ifndef NDEBUG
137/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
138 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
139 when a thread continues to run after Python finalization, especially
140 daemon threads. */
141static int
142is_tstate_valid(PyThreadState *tstate)
143{
144 assert(!_PyMem_IsPtrFreed(tstate));
145 assert(!_PyMem_IsPtrFreed(tstate->interp));
146 return 1;
147}
148#endif
149
150
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000151/* This can set eval_breaker to 0 even though gil_drop_request became
152 1. We believe this is all right because the eval loop will release
153 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100154static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200155COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200156 struct _ceval_runtime_state *ceval,
157 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100158{
Victor Stinner299b8c62020-05-05 17:40:18 +0200159 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
160 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200161 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200162 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200163 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100164 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200165 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100166}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Victor Stinnerda2914d2020-03-20 09:29:08 +0100169static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200170SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100171{
Victor Stinner299b8c62020-05-05 17:40:18 +0200172 struct _ceval_state *ceval2 = &interp->ceval;
173 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
174 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200179RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180{
Victor Stinner299b8c62020-05-05 17:40:18 +0200181 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
184 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000186
Eric Snowfdf282d2019-01-11 14:26:55 -0700187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
Eric Snowfdf282d2019-01-11 14:26:55 -0700196
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000197
Victor Stinnerda2914d2020-03-20 09:29:08 +0100198static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200199UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
203 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
204 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100205}
206
207
208static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100209SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100210{
Victor Stinner299b8c62020-05-05 17:40:18 +0200211 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
212 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200213 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100214 if (force) {
215 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
216 }
217 else {
218 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
219 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
220 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221}
222
223
224static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200225UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100226{
Victor Stinner299b8c62020-05-05 17:40:18 +0200227 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
228 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200229 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200230 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100231}
232
233
234static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200235SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100236{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200237 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100238 ceval2->pending.async_exc = 1;
239 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
240}
241
242
243static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200244UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100245{
Victor Stinner299b8c62020-05-05 17:40:18 +0200246 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
247 struct _ceval_state *ceval2 = &interp->ceval;
248 ceval2->pending.async_exc = 0;
249 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100250}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000251
252
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000255#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257
Victor Stinner3026cad2020-06-01 16:02:40 +0200258void _Py_NO_RETURN
259_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100260{
Victor Stinner3026cad2020-06-01 16:02:40 +0200261 _Py_FatalErrorFunc(func,
262 "the function must be called with the GIL held, "
263 "but the GIL is released "
264 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100265}
266
Victor Stinner7be4e352020-05-05 20:27:47 +0200267#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
268int
269_PyEval_ThreadsInitialized(PyInterpreterState *interp)
270{
271 return gil_created(&interp->ceval.gil);
272}
273
274int
275PyEval_ThreadsInitialized(void)
276{
277 // Fatal error if there is no current interpreter
278 PyInterpreterState *interp = PyInterpreterState_Get();
279 return _PyEval_ThreadsInitialized(interp);
280}
281#else
Tim Peters7f468f22004-10-11 02:40:51 +0000282int
Victor Stinner175a7042020-03-10 00:37:48 +0100283_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
284{
285 return gil_created(&runtime->ceval.gil);
286}
287
288int
Tim Peters7f468f22004-10-11 02:40:51 +0000289PyEval_ThreadsInitialized(void)
290{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100291 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100292 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000293}
Victor Stinner7be4e352020-05-05 20:27:47 +0200294#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000295
Victor Stinner111e4ee2020-03-09 21:24:14 +0100296PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200297_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298{
Victor Stinner7be4e352020-05-05 20:27:47 +0200299#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200300 if (!_Py_IsMainInterpreter(tstate)) {
301 /* Currently, the GIL is shared by all interpreters,
302 and only the main interpreter is responsible to create
303 and destroy it. */
304 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100305 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200306#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100307
Victor Stinner7be4e352020-05-05 20:27:47 +0200308#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
309 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
310#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200311 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200312#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200313 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100314
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200315 PyThread_init_thread();
316 create_gil(gil);
317
318 take_gil(tstate);
319
320 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100321 return _PyStatus_OK();
322}
323
324void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200325_PyEval_FiniGIL(PyThreadState *tstate)
326{
Victor Stinner7be4e352020-05-05 20:27:47 +0200327#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200328 if (!_Py_IsMainInterpreter(tstate)) {
329 /* Currently, the GIL is shared by all interpreters,
330 and only the main interpreter is responsible to create
331 and destroy it. */
332 return;
333 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200334#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200335
Victor Stinner7be4e352020-05-05 20:27:47 +0200336#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
337 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
338#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200339 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200340#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200341 if (!gil_created(gil)) {
342 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
343 yet: do nothing. */
344 return;
345 }
346
347 destroy_gil(gil);
348 assert(!gil_created(gil));
349}
350
351void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100352PyEval_InitThreads(void)
353{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100354 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000355}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000356
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000357void
Inada Naoki91234a12019-06-03 21:30:58 +0900358_PyEval_Fini(void)
359{
360#if OPCACHE_STATS
361 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
362 opcache_code_objects);
363
364 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
365 opcache_code_objects_extra_mem);
366
367 fprintf(stderr, "\n");
368
369 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
370 opcache_global_hits,
371 (int) (100.0 * opcache_global_hits /
372 (opcache_global_hits + opcache_global_misses)));
373
374 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
375 opcache_global_misses,
376 (int) (100.0 * opcache_global_misses /
377 (opcache_global_hits + opcache_global_misses)));
378
379 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
380 opcache_global_opts);
381
382 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100383
384 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
385 opcache_attr_hits,
386 (int) (100.0 * opcache_attr_hits /
387 opcache_attr_total));
388
389 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
390 opcache_attr_misses,
391 (int) (100.0 * opcache_attr_misses /
392 opcache_attr_total));
393
394 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
395 opcache_attr_opts);
396
397 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
398 opcache_attr_deopts);
399
400 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
401 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900402#endif
403}
404
405void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000407{
Victor Stinner09532fe2019-05-10 23:39:09 +0200408 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200409 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200410 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100411
Victor Stinner85f5a692020-03-09 22:12:04 +0100412 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000413}
414
415void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000417{
Victor Stinner09532fe2019-05-10 23:39:09 +0200418 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200419 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100421 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100422 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200423 struct _ceval_runtime_state *ceval = &runtime->ceval;
424 struct _ceval_state *ceval2 = &tstate->interp->ceval;
425 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000426}
427
428void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100429_PyEval_ReleaseLock(PyThreadState *tstate)
430{
431 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200432 struct _ceval_state *ceval2 = &tstate->interp->ceval;
433 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100434}
435
436void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000437PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000438{
Victor Stinner3026cad2020-06-01 16:02:40 +0200439 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100440
Victor Stinner85f5a692020-03-09 22:12:04 +0100441 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200442
Victor Stinner85f5a692020-03-09 22:12:04 +0100443 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200444#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
445 (void)_PyThreadState_Swap(gilstate, tstate);
446#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100447 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100448 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200449 }
Victor Stinnere838a932020-05-05 19:56:48 +0200450#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000451}
452
453void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000454PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000455{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100456 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200457
Victor Stinner01b1cc12019-11-20 02:27:56 +0100458 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200459 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
460 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100461 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200462 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200463 struct _ceval_runtime_state *ceval = &runtime->ceval;
464 struct _ceval_state *ceval2 = &tstate->interp->ceval;
465 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000466}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000467
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900468#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200469/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200470 which are not running in the child process, and clear internal locks
471 which might be held by those threads. */
472PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200473_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000474{
Victor Stinner317bab02020-06-02 18:44:54 +0200475 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200476
477#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
478 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
479#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100480 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200481#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100482 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200483 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200484 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100485 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100486
487 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700488
Victor Stinner50e6e992020-03-19 02:41:21 +0100489 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900490 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200491 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700492 }
Jesse Nollera8513972008-07-17 16:49:17 +0000493
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200494 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100495 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200496 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000497}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900498#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000499
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000500/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600501 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000502
503void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100504_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000505{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200506 assert(is_tstate_valid(tstate));
507 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000508}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000510PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512{
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200514#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
515 PyThreadState *old_tstate = _PyThreadState_GET();
516 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
517#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200518 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200519#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200520 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100521
Victor Stinner0b1e3302020-05-05 16:14:31 +0200522 struct _ceval_runtime_state *ceval = &runtime->ceval;
523 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200524#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
525 assert(gil_created(&ceval2->gil));
526#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200528#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200529 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000531}
532
533void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535{
Victor Stinner3026cad2020-06-01 16:02:40 +0200536 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100537
Victor Stinner85f5a692020-03-09 22:12:04 +0100538 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100539
Victor Stinner85f5a692020-03-09 22:12:04 +0100540 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
541 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000542}
543
544
Guido van Rossuma9672091994-09-14 13:31:22 +0000545/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
546 signal handlers or Mac I/O completion routines) can schedule calls
547 to a function to be called synchronously.
548 The synchronous function is called with one void* argument.
549 It should return 0 for success or -1 for failure -- failure should
550 be accompanied by an exception.
551
552 If registry succeeds, the registry function returns 0; if it fails
553 (e.g. due to too many pending calls) it returns -1 (without setting
554 an exception condition).
555
556 Note that because registry may occur from within signal handlers,
557 or other asynchronous events, calling malloc() is unsafe!
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559 Any thread can schedule pending calls, but only the main thread
560 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000561 There is no facility to schedule calls to a particular thread, but
562 that should be easy to change, should that ever be required. In
563 that case, the static variables here should go into the python
564 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000566
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200567void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200568_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200569{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100570#ifdef MS_WINDOWS
571 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
572 // handler which can run in a thread different than the Python thread, in
573 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
574 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
575 //
576 // The next eval_frame_handle_pending() call will call
577 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
578 int force = 1;
579#else
580 int force = 0;
581#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200582 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100583 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200584 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100585 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200586}
587
Eric Snow5be45a62019-03-08 22:47:07 -0700588/* Push one item onto the queue while holding the lock. */
589static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200590_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600591 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700592{
Eric Snow842a2f02019-03-15 15:47:51 -0600593 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700594 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600595 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700596 return -1; /* Queue full */
597 }
Eric Snow842a2f02019-03-15 15:47:51 -0600598 pending->calls[i].func = func;
599 pending->calls[i].arg = arg;
600 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700601 return 0;
602}
603
604/* Pop one item off the queue while holding the lock. */
605static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200606_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600607 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700608{
Eric Snow842a2f02019-03-15 15:47:51 -0600609 int i = pending->first;
610 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700611 return; /* Queue empty */
612 }
613
Eric Snow842a2f02019-03-15 15:47:51 -0600614 *func = pending->calls[i].func;
615 *arg = pending->calls[i].arg;
616 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700617}
618
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200619/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000620 scheduling to be made from any thread, and even from an executing
621 callback.
622 */
623
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000624int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200625_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200626 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000627{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200628 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600629
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200630 /* Ensure that _PyEval_InitPendingCalls() was called
631 and that _PyEval_FiniPendingCalls() is not called yet. */
632 assert(pending->lock != NULL);
633
Eric Snow842a2f02019-03-15 15:47:51 -0600634 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600636 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700637
Victor Stinnere225beb2019-06-03 18:14:24 +0200638 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000641}
642
Victor Stinner09532fe2019-05-10 23:39:09 +0200643int
644Py_AddPendingCall(int (*func)(void *), void *arg)
645{
Victor Stinner50e6e992020-03-19 02:41:21 +0100646 /* Best-effort to support subinterpreters and calls with the GIL released.
647
648 First attempt _PyThreadState_GET() since it supports subinterpreters.
649
650 If the GIL is released, _PyThreadState_GET() returns NULL . In this
651 case, use PyGILState_GetThisThreadState() which works even if the GIL
652 is released.
653
654 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
655 see bpo-10915 and bpo-15751.
656
Victor Stinner8849e592020-03-18 19:28:53 +0100657 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100658 PyThreadState *tstate = _PyThreadState_GET();
659 if (tstate == NULL) {
660 tstate = PyGILState_GetThisThreadState();
661 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200662
663 PyInterpreterState *interp;
664 if (tstate != NULL) {
665 interp = tstate->interp;
666 }
667 else {
668 /* Last resort: use the main interpreter */
669 interp = _PyRuntime.interpreters.main;
670 }
671 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200672}
673
Eric Snowfdf282d2019-01-11 14:26:55 -0700674static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100675handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700676{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200677 assert(is_tstate_valid(tstate));
678 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700679 return 0;
680 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700681
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200682 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100683 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
684 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100685 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700686 return -1;
687 }
688 return 0;
689}
690
691static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100692make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000693{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200694 assert(is_tstate_valid(tstate));
695
Victor Stinnerd8316882020-03-20 14:50:35 +0100696 /* only execute pending calls on main thread */
697 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200698 return 0;
699 }
700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100702 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700703 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700705 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200706 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100707
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200708 /* unsignal before starting to call callbacks, so that any callback
709 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200710 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700711 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100714 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700715 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700716 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 void *arg = NULL;
718
719 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600720 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200721 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600722 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700723
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100724 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700725 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100726 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700727 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700728 res = func(arg);
729 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200730 goto error;
731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200733
Charles-François Natalif23339a2011-07-23 18:15:43 +0200734 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700735 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200736
737error:
738 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200739 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700740 return res;
741}
742
Eric Snow842a2f02019-03-15 15:47:51 -0600743void
Victor Stinner2b1df452020-01-13 18:46:59 +0100744_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600745{
Eric Snow842a2f02019-03-15 15:47:51 -0600746 assert(PyGILState_Check());
747
Victor Stinner50e6e992020-03-19 02:41:21 +0100748 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200749
Eric Snow842a2f02019-03-15 15:47:51 -0600750 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
751 return;
752 }
753
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100754 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200755 PyObject *exc, *val, *tb;
756 _PyErr_Fetch(tstate, &exc, &val, &tb);
757 PyErr_BadInternalCall();
758 _PyErr_ChainExceptions(exc, val, tb);
759 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600760 }
761}
762
Eric Snowfdf282d2019-01-11 14:26:55 -0700763/* Py_MakePendingCalls() is a simple wrapper for the sake
764 of backward-compatibility. */
765int
766Py_MakePendingCalls(void)
767{
768 assert(PyGILState_Check());
769
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100770 PyThreadState *tstate = _PyThreadState_GET();
771
Eric Snowfdf282d2019-01-11 14:26:55 -0700772 /* Python signal handler doesn't really queue a callback: it only signals
773 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100774 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700775 if (res != 0) {
776 return res;
777 }
778
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100779 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600780 if (res != 0) {
781 return res;
782 }
783
784 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000785}
786
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000787/* The interpreter's recursion limit */
788
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000789#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200790# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000791#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600792
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600793void
Victor Stinnerdab84232020-03-17 18:56:44 +0100794_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600795{
Victor Stinner7be4e352020-05-05 20:27:47 +0200796#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100797 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200798#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100799}
800
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200801int
Victor Stinnerdab84232020-03-17 18:56:44 +0100802_PyEval_InitState(struct _ceval_state *ceval)
803{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200804 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
805
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200806 struct _pending_calls *pending = &ceval->pending;
807 assert(pending->lock == NULL);
808
809 pending->lock = PyThread_allocate_lock();
810 if (pending->lock == NULL) {
811 return -1;
812 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200813
814#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
815 _gil_initialize(&ceval->gil);
816#endif
817
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200818 return 0;
819}
820
821void
822_PyEval_FiniState(struct _ceval_state *ceval)
823{
824 struct _pending_calls *pending = &ceval->pending;
825 if (pending->lock != NULL) {
826 PyThread_free_lock(pending->lock);
827 pending->lock = NULL;
828 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600829}
830
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000831int
832Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000833{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200834 PyInterpreterState *interp = _PyInterpreterState_GET();
835 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000836}
837
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000838void
839Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000840{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200841 PyThreadState *tstate = _PyThreadState_GET();
842 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000843}
844
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100845/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200846 if the recursion_depth reaches recursion_limit.
847 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000848 to guarantee that _Py_CheckRecursiveCall() is regularly called.
849 Without USE_STACKCHECK, there is no need for this. */
850int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100851_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000852{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200853 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000854
855#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700856 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (PyOS_CheckStack()) {
858 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200859 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return -1;
861 }
pdox18967932017-10-25 23:03:01 -0700862#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000863 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (tstate->recursion_depth > recursion_limit + 50) {
865 /* Overflowing while handling an overflow. Give up. */
866 Py_FatalError("Cannot recover from stack overflow.");
867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000869 else {
870 if (tstate->recursion_depth > recursion_limit) {
871 tstate->recursion_headroom++;
872 _PyErr_Format(tstate, PyExc_RecursionError,
873 "maximum recursion depth exceeded%s",
874 where);
875 tstate->recursion_headroom--;
876 --tstate->recursion_depth;
877 return -1;
878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 }
880 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000881}
882
Victor Stinner09532fe2019-05-10 23:39:09 +0200883static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200884static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000885
Victor Stinnere225beb2019-06-03 18:14:24 +0200886#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000887
Guido van Rossum374a9221991-04-04 10:40:29 +0000888
Guido van Rossumb209a111997-04-29 18:18:01 +0000889PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000890PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return PyEval_EvalCodeEx(co,
893 globals, locals,
894 (PyObject **)NULL, 0,
895 (PyObject **)NULL, 0,
896 (PyObject **)NULL, 0,
897 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000898}
899
900
901/* Interpreter main loop */
902
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000903PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100904PyEval_EvalFrame(PyFrameObject *f)
905{
Victor Stinner0b72b232020-03-12 23:18:39 +0100906 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100907 PyThreadState *tstate = _PyThreadState_GET();
908 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000909}
910
911PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000913{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100914 PyThreadState *tstate = _PyThreadState_GET();
915 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700916}
917
Victor Stinnerda2914d2020-03-20 09:29:08 +0100918
919/* Handle signals, pending calls, GIL drop request
920 and asynchronous exception */
921static int
922eval_frame_handle_pending(PyThreadState *tstate)
923{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100924 _PyRuntimeState * const runtime = &_PyRuntime;
925 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200926
927 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200928 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100929 if (handle_signals(tstate) != 0) {
930 return -1;
931 }
932 }
933
934 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200935 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100936 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
937 if (make_pending_calls(tstate) != 0) {
938 return -1;
939 }
940 }
941
942 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200943 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100944 /* Give another thread a chance */
945 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
946 Py_FatalError("tstate mix-up");
947 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200948 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100949
950 /* Other threads may run now */
951
952 take_gil(tstate);
953
Victor Stinnere838a932020-05-05 19:56:48 +0200954#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
955 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
956#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100957 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
958 Py_FatalError("orphan tstate");
959 }
Victor Stinnere838a932020-05-05 19:56:48 +0200960#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100961 }
962
963 /* Check for asynchronous exception. */
964 if (tstate->async_exc != NULL) {
965 PyObject *exc = tstate->async_exc;
966 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200967 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100968 _PyErr_SetNone(tstate, exc);
969 Py_DECREF(exc);
970 return -1;
971 }
972
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100973#ifdef MS_WINDOWS
974 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
975 // different thread than the Python thread, in which case
976 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
977 // current Python thread with the correct _Py_ThreadCanHandleSignals()
978 // value. It prevents to interrupt the eval loop at every instruction if
979 // the current Python thread cannot handle signals (if
980 // _Py_ThreadCanHandleSignals() is false).
981 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
982#endif
983
Victor Stinnerda2914d2020-03-20 09:29:08 +0100984 return 0;
985}
986
Victor Stinnerc6944e72016-11-11 02:13:35 +0100987PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100988_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700989{
Victor Stinner3026cad2020-06-01 16:02:40 +0200990 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100991
Guido van Rossum950361c1997-01-24 13:49:28 +0000992#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000994#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200995 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300996 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200997 int opcode; /* Current opcode */
998 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200999 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001001 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001002 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 is true when the line being executed has changed. The
1010 initial values are such as to make this false the first
1011 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001012
Serhiy Storchakaab874002016-09-11 13:48:15 +03001013 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyObject *names;
1015 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001016 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001017
Brett Cannon368b4b72012-04-02 12:17:59 -04001018#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001019 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001020#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001021
Antoine Pitroub52ec782009-01-25 16:34:23 +00001022/* Computed GOTOs, or
1023 the-optimization-commonly-but-improperly-known-as-"threaded code"
1024 using gcc's labels-as-values extension
1025 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1026
1027 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001029 combined with a lookup table of jump addresses. However, since the
1030 indirect jump instruction is shared by all opcodes, the CPU will have a
1031 hard time making the right prediction for where to jump next (actually,
1032 it will be always wrong except in the uncommon case of a sequence of
1033 several identical opcodes).
1034
1035 "Threaded code" in contrast, uses an explicit jump table and an explicit
1036 indirect jump instruction at the end of each opcode. Since the jump
1037 instruction is at a different address for each opcode, the CPU will make a
1038 separate prediction for each of these instructions, which is equivalent to
1039 predicting the second opcode of each opcode pair. These predictions have
1040 a much better chance to turn out valid, especially in small bytecode loops.
1041
1042 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001044 and potentially many more instructions (depending on the pipeline width).
1045 A correctly predicted branch, however, is nearly free.
1046
1047 At the time of this writing, the "threaded code" version is up to 15-20%
1048 faster than the normal "switch" version, depending on the compiler and the
1049 CPU architecture.
1050
1051 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1052 because it would render the measurements invalid.
1053
1054
1055 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1056 indirect jumps by sharing them between all opcodes. Such optimizations
1057 can be disabled on gcc by using the -fno-gcse flag (or possibly
1058 -fno-crossjumping).
1059*/
1060
Antoine Pitrou042b1282010-08-13 21:15:58 +00001061#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001062#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001063#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001064#endif
1065
Antoine Pitrou042b1282010-08-13 21:15:58 +00001066#ifdef HAVE_COMPUTED_GOTOS
1067 #ifndef USE_COMPUTED_GOTOS
1068 #define USE_COMPUTED_GOTOS 1
1069 #endif
1070#else
1071 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1072 #error "Computed gotos are not supported on this compiler."
1073 #endif
1074 #undef USE_COMPUTED_GOTOS
1075 #define USE_COMPUTED_GOTOS 0
1076#endif
1077
1078#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001079/* Import the static jump table */
1080#include "opcode_targets.h"
1081
Antoine Pitroub52ec782009-01-25 16:34:23 +00001082#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001083 op: \
1084 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001085
Antoine Pitroub52ec782009-01-25 16:34:23 +00001086#ifdef LLTRACE
1087#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001089 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001091 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001092 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 } \
1094 goto fast_next_opcode; \
1095 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001096#else
1097#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001099 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001101 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001102 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 } \
1104 goto fast_next_opcode; \
1105 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001106#endif
1107
Victor Stinner09532fe2019-05-10 23:39:09 +02001108#define DISPATCH() \
1109 { \
1110 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1111 FAST_DISPATCH(); \
1112 } \
1113 continue; \
1114 }
1115
Antoine Pitroub52ec782009-01-25 16:34:23 +00001116#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001117#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001118#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001119#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001120#endif
1121
1122
Neal Norwitza81d2202002-07-14 00:27:26 +00001123/* Tuple access macros */
1124
1125#ifndef Py_DEBUG
1126#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1127#else
1128#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1129#endif
1130
Guido van Rossum374a9221991-04-04 10:40:29 +00001131/* Code access macros */
1132
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001133/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001134#define INSTR_OFFSET() \
1135 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001136#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001137 _Py_CODEUNIT word = *next_instr; \
1138 opcode = _Py_OPCODE(word); \
1139 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001140 next_instr++; \
1141 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001142#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1143#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001144
Raymond Hettingerf606f872003-03-16 03:11:04 +00001145/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 Some opcodes tend to come in pairs thus making it possible to
1147 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001148 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 Verifying the prediction costs a single high-speed test of a register
1151 variable against a constant. If the pairing was good, then the
1152 processor's own internal branch predication has a high likelihood of
1153 success, resulting in a nearly zero-overhead transition to the
1154 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001155 including its unpredictable switch-case branch. Combined with the
1156 processor's internal branch prediction, a successful PREDICT has the
1157 effect of making the two opcodes run as if they were a single new opcode
1158 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001159
Georg Brandl86b2fb92008-07-16 03:43:04 +00001160 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 predictions turned-on and interpret the results as if some opcodes
1162 had been combined or turn-off predictions so that the opcode frequency
1163 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001164
1165 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 the CPU to record separate branch prediction information for each
1167 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001168
Raymond Hettingerf606f872003-03-16 03:11:04 +00001169*/
1170
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001171#define PREDICT_ID(op) PRED_##op
1172
Antoine Pitrou042b1282010-08-13 21:15:58 +00001173#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001174#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001175#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001176#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001177 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001178 _Py_CODEUNIT word = *next_instr; \
1179 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001180 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001181 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001182 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001183 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001184 } \
1185 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001186#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001187#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001188
Raymond Hettingerf606f872003-03-16 03:11:04 +00001189
Guido van Rossum374a9221991-04-04 10:40:29 +00001190/* Stack manipulation macros */
1191
Martin v. Löwis18e16552006-02-15 17:27:45 +00001192/* The stack can grow at most MAXINT deep, as co_nlocals and
1193 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001194#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1195#define EMPTY() (STACK_LEVEL() == 0)
1196#define TOP() (stack_pointer[-1])
1197#define SECOND() (stack_pointer[-2])
1198#define THIRD() (stack_pointer[-3])
1199#define FOURTH() (stack_pointer[-4])
1200#define PEEK(n) (stack_pointer[-(n)])
1201#define SET_TOP(v) (stack_pointer[-1] = (v))
1202#define SET_SECOND(v) (stack_pointer[-2] = (v))
1203#define SET_THIRD(v) (stack_pointer[-3] = (v))
1204#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001205#define BASIC_STACKADJ(n) (stack_pointer += n)
1206#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1207#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001208
Guido van Rossum96a42c81992-01-12 02:29:51 +00001209#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001211 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001212 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001213#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001214 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001215#define STACK_GROW(n) do { \
1216 assert(n >= 0); \
1217 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001218 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001219 assert(STACK_LEVEL() <= co->co_stacksize); \
1220 } while (0)
1221#define STACK_SHRINK(n) do { \
1222 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001223 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001224 (void)(BASIC_STACKADJ(-n)); \
1225 assert(STACK_LEVEL() <= co->co_stacksize); \
1226 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001227#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001228 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001229 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001230#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001231#define PUSH(v) BASIC_PUSH(v)
1232#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001233#define STACK_GROW(n) BASIC_STACKADJ(n)
1234#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001235#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001236#endif
1237
Guido van Rossum681d79a1995-07-18 14:51:37 +00001238/* Local variable macros */
1239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001241
1242/* The SETLOCAL() macro must not DECREF the local variable in-place and
1243 then store the new value; it must copy the old value to a temporary
1244 value, then store the new value, and then DECREF the temporary value.
1245 This is because it is possible that during the DECREF the frame is
1246 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1247 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001249 GETLOCAL(i) = value; \
1250 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001251
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001252
1253#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 while (STACK_LEVEL() > (b)->b_level) { \
1255 PyObject *v = POP(); \
1256 Py_XDECREF(v); \
1257 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001258
1259#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001260 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001262 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1264 while (STACK_LEVEL() > (b)->b_level + 3) { \
1265 value = POP(); \
1266 Py_XDECREF(value); \
1267 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001268 exc_info = tstate->exc_info; \
1269 type = exc_info->exc_type; \
1270 value = exc_info->exc_value; \
1271 traceback = exc_info->exc_traceback; \
1272 exc_info->exc_type = POP(); \
1273 exc_info->exc_value = POP(); \
1274 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 Py_XDECREF(type); \
1276 Py_XDECREF(value); \
1277 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001278 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001279
Inada Naoki91234a12019-06-03 21:30:58 +09001280 /* macros for opcode cache */
1281#define OPCACHE_CHECK() \
1282 do { \
1283 co_opcache = NULL; \
1284 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001285 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001286 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001287 if (co_opcache_offset > 0) { \
1288 assert(co_opcache_offset <= co->co_opcache_size); \
1289 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001290 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001291 } \
1292 } \
1293 } while (0)
1294
Pablo Galindo109826c2020-10-20 06:22:44 +01001295#define OPCACHE_DEOPT() \
1296 do { \
1297 if (co_opcache != NULL) { \
1298 co_opcache->optimized = -1; \
1299 unsigned char co_opcache_offset = \
1300 co->co_opcache_map[next_instr - first_instr]; \
1301 assert(co_opcache_offset <= co->co_opcache_size); \
1302 co->co_opcache_map[co_opcache_offset] = 0; \
1303 co_opcache = NULL; \
1304 } \
1305 } while (0)
1306
1307#define OPCACHE_DEOPT_LOAD_ATTR() \
1308 do { \
1309 if (co_opcache != NULL) { \
1310 OPCACHE_STAT_ATTR_DEOPT(); \
1311 OPCACHE_DEOPT(); \
1312 } \
1313 } while (0)
1314
1315#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1316 do { \
1317 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1318 OPCACHE_DEOPT_LOAD_ATTR(); \
1319 } \
1320 } while (0)
1321
Inada Naoki91234a12019-06-03 21:30:58 +09001322#if OPCACHE_STATS
1323
1324#define OPCACHE_STAT_GLOBAL_HIT() \
1325 do { \
1326 if (co->co_opcache != NULL) opcache_global_hits++; \
1327 } while (0)
1328
1329#define OPCACHE_STAT_GLOBAL_MISS() \
1330 do { \
1331 if (co->co_opcache != NULL) opcache_global_misses++; \
1332 } while (0)
1333
1334#define OPCACHE_STAT_GLOBAL_OPT() \
1335 do { \
1336 if (co->co_opcache != NULL) opcache_global_opts++; \
1337 } while (0)
1338
Pablo Galindo109826c2020-10-20 06:22:44 +01001339#define OPCACHE_STAT_ATTR_HIT() \
1340 do { \
1341 if (co->co_opcache != NULL) opcache_attr_hits++; \
1342 } while (0)
1343
1344#define OPCACHE_STAT_ATTR_MISS() \
1345 do { \
1346 if (co->co_opcache != NULL) opcache_attr_misses++; \
1347 } while (0)
1348
1349#define OPCACHE_STAT_ATTR_OPT() \
1350 do { \
1351 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1352 } while (0)
1353
1354#define OPCACHE_STAT_ATTR_DEOPT() \
1355 do { \
1356 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1357 } while (0)
1358
1359#define OPCACHE_STAT_ATTR_TOTAL() \
1360 do { \
1361 if (co->co_opcache != NULL) opcache_attr_total++; \
1362 } while (0)
1363
Inada Naoki91234a12019-06-03 21:30:58 +09001364#else /* OPCACHE_STATS */
1365
1366#define OPCACHE_STAT_GLOBAL_HIT()
1367#define OPCACHE_STAT_GLOBAL_MISS()
1368#define OPCACHE_STAT_GLOBAL_OPT()
1369
Pablo Galindo109826c2020-10-20 06:22:44 +01001370#define OPCACHE_STAT_ATTR_HIT()
1371#define OPCACHE_STAT_ATTR_MISS()
1372#define OPCACHE_STAT_ATTR_OPT()
1373#define OPCACHE_STAT_ATTR_DEOPT()
1374#define OPCACHE_STAT_ATTR_TOTAL()
1375
Inada Naoki91234a12019-06-03 21:30:58 +09001376#endif
1377
Guido van Rossuma027efa1997-05-05 20:56:21 +00001378/* Start of code */
1379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001381 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001383 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001386 co = f->f_code;
1387 PyCodeAddressRange bounds;
1388 _PyCode_InitAddressRange(co, &bounds);
Tim Peters5ca576e2001-06-18 22:08:13 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (tstate->use_tracing) {
1391 if (tstate->c_tracefunc != NULL) {
1392 /* tstate->c_tracefunc, if defined, is a
1393 function that will be called on *every* entry
1394 to a code block. Its return value, if not
1395 None, is a function that will be called at
1396 the start of each executed line of code.
1397 (Actually, the function must return itself
1398 in order to continue tracing.) The trace
1399 functions are called with three arguments:
1400 a pointer to the current frame, a string
1401 indicating why the function is called, and
1402 an argument which depends on the situation.
1403 The global trace function is also called
1404 whenever an exception is detected. */
1405 if (call_trace_protected(tstate->c_tracefunc,
1406 tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00001407 tstate, f, &bounds,
1408 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* Trace function raised an error */
1410 goto exit_eval_frame;
1411 }
1412 }
1413 if (tstate->c_profilefunc != NULL) {
1414 /* Similar for c_profilefunc, except it needn't
1415 return itself and isn't called for "line" events */
1416 if (call_trace_protected(tstate->c_profilefunc,
1417 tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00001418 tstate, f, &bounds,
1419 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* Profile function raised an error */
1421 goto exit_eval_frame;
1422 }
1423 }
1424 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001425
Łukasz Langaa785c872016-09-09 17:37:37 -07001426 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1427 dtrace_function_entry(f);
1428
Mark Shannon877df852020-11-12 09:43:29 +00001429 int instr_prev = -1;
1430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 names = co->co_names;
1432 consts = co->co_consts;
1433 fastlocals = f->f_localsplus;
1434 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001435 assert(PyBytes_Check(co->co_code));
1436 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001437 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1438 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1439 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001440 /*
1441 f->f_lasti refers to the index of the last instruction,
1442 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001443
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001444 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001445 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 When the PREDICT() macros are enabled, some opcode pairs follow in
1448 direct succession without updating f->f_lasti. A successful
1449 prediction effectively links the two codes together as if they
1450 were a single new opcode; accordingly,f->f_lasti will point to
1451 the first code in the pair (for instance, GET_ITER followed by
1452 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001453 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001455 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001456 next_instr = first_instr;
1457 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001458 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1459 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001460 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001461 stack_pointer = f->f_valuestack + f->f_stackdepth;
1462 /* Set f->f_stackdepth to -1.
1463 * Update when returning or calling trace function.
1464 Having f_stackdepth <= 0 ensures that invalid
1465 values are not visible to the cycle GC.
1466 We choose -1 rather than 0 to assist debugging.
1467 */
1468 f->f_stackdepth = -1;
1469 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001470
Inada Naoki91234a12019-06-03 21:30:58 +09001471 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1472 co->co_opcache_flag++;
1473 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1474 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001475 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001476 }
1477#if OPCACHE_STATS
1478 opcache_code_objects_extra_mem +=
1479 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1480 sizeof(_PyOpcache) * co->co_opcache_size;
1481 opcache_code_objects++;
1482#endif
1483 }
1484 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001485
Tim Peters5ca576e2001-06-18 22:08:13 +00001486#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001487 {
1488 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1489 if (r < 0) {
1490 goto exit_eval_frame;
1491 }
1492 lltrace = r;
1493 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001494#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001496 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001498 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001499
Victor Stinnerace47d72013-07-18 01:41:08 +02001500#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001501 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001502 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001503 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001504 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001505#endif
1506
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001507main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1510 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001511 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* Do periodic things. Doing this every time through
1514 the loop would add too much overhead, so we do it
1515 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001516 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 event needs attention (e.g. a signal handler or
1518 async I/O handler); see Py_AddPendingCall() and
1519 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Eric Snow7bda9de2019-03-08 17:25:54 -07001521 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001522 opcode = _Py_OPCODE(*next_instr);
1523 if (opcode == SETUP_FINALLY ||
1524 opcode == SETUP_WITH ||
1525 opcode == BEFORE_ASYNC_WITH ||
1526 opcode == YIELD_FROM) {
1527 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001528 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001529 - If we're about to enter the 'with:'. It will prevent
1530 emitting a resource warning in the common idiom
1531 'with open(path) as file:'.
1532 - If we're about to enter the 'async with:'.
1533 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001534 *very* useful, but might help in some cases and it's
1535 traditional)
1536 - If we're resuming a chain of nested 'yield from' or
1537 'await' calls, then each frame is parked with YIELD_FROM
1538 as its next opcode. If the user hit control-C we want to
1539 wait until we've reached the innermost frame before
1540 running the signal handler and raising KeyboardInterrupt
1541 (see bpo-30039).
1542 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 goto fast_next_opcode;
1544 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001545
Victor Stinnerda2914d2020-03-20 09:29:08 +01001546 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 }
1549 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 fast_next_opcode:
1552 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001553
Łukasz Langaa785c872016-09-09 17:37:37 -07001554 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001555 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001558
Victor Stinnerdab84232020-03-17 18:56:44 +01001559 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001560 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001562 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001564 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 err = maybe_call_line_trace(tstate->c_tracefunc,
1567 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001568 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001569 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 /* Reload possibly changed frame fields */
1571 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001572 stack_pointer = f->f_valuestack+f->f_stackdepth;
1573 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001580
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001581 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001582 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001583#ifdef DYNAMIC_EXECUTION_PROFILE
1584#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 dxpairs[lastopcode][opcode]++;
1586 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001587#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001589#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001590
Guido van Rossum96a42c81992-01-12 02:29:51 +00001591#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (lltrace) {
1595 if (HAS_ARG(opcode)) {
1596 printf("%d: %d, %d\n",
1597 f->f_lasti, opcode, oparg);
1598 }
1599 else {
1600 printf("%d: %d\n",
1601 f->f_lasti, opcode);
1602 }
1603 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001604#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001609 It is essential that any operation that fails must goto error
1610 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *value = GETLOCAL(oparg);
1618 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001619 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 UNBOUNDLOCAL_ERROR_MSG,
1621 PyTuple_GetItem(co->co_varnames, oparg));
1622 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 Py_INCREF(value);
1625 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 }
1628
Benjamin Petersonddd19492018-09-16 22:38:02 -07001629 case TARGET(LOAD_CONST): {
1630 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 PyObject *value = GETITEM(consts, oparg);
1632 Py_INCREF(value);
1633 PUSH(value);
1634 FAST_DISPATCH();
1635 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001636
Benjamin Petersonddd19492018-09-16 22:38:02 -07001637 case TARGET(STORE_FAST): {
1638 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 PyObject *value = POP();
1640 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 PyObject *value = POP();
1646 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Benjamin Petersonddd19492018-09-16 22:38:02 -07001650 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 PyObject *top = TOP();
1652 PyObject *second = SECOND();
1653 SET_TOP(second);
1654 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Benjamin Petersonddd19492018-09-16 22:38:02 -07001658 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 PyObject *top = TOP();
1660 PyObject *second = SECOND();
1661 PyObject *third = THIRD();
1662 SET_TOP(second);
1663 SET_SECOND(third);
1664 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Benjamin Petersonddd19492018-09-16 22:38:02 -07001668 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001669 PyObject *top = TOP();
1670 PyObject *second = SECOND();
1671 PyObject *third = THIRD();
1672 PyObject *fourth = FOURTH();
1673 SET_TOP(second);
1674 SET_SECOND(third);
1675 SET_THIRD(fourth);
1676 SET_FOURTH(top);
1677 FAST_DISPATCH();
1678 }
1679
Benjamin Petersonddd19492018-09-16 22:38:02 -07001680 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 PyObject *top = TOP();
1682 Py_INCREF(top);
1683 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Petersonddd19492018-09-16 22:38:02 -07001687 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001690 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001692 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 SET_TOP(top);
1694 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001695 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001697
Benjamin Petersonddd19492018-09-16 22:38:02 -07001698 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 PyObject *value = TOP();
1700 PyObject *res = PyNumber_Positive(value);
1701 Py_DECREF(value);
1702 SET_TOP(res);
1703 if (res == NULL)
1704 goto error;
1705 DISPATCH();
1706 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Benjamin Petersonddd19492018-09-16 22:38:02 -07001708 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 PyObject *value = TOP();
1710 PyObject *res = PyNumber_Negative(value);
1711 Py_DECREF(value);
1712 SET_TOP(res);
1713 if (res == NULL)
1714 goto error;
1715 DISPATCH();
1716 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001717
Benjamin Petersonddd19492018-09-16 22:38:02 -07001718 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 PyObject *value = TOP();
1720 int err = PyObject_IsTrue(value);
1721 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (err == 0) {
1723 Py_INCREF(Py_True);
1724 SET_TOP(Py_True);
1725 DISPATCH();
1726 }
1727 else if (err > 0) {
1728 Py_INCREF(Py_False);
1729 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 DISPATCH();
1731 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001732 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 goto error;
1734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *value = TOP();
1738 PyObject *res = PyNumber_Invert(value);
1739 Py_DECREF(value);
1740 SET_TOP(res);
1741 if (res == NULL)
1742 goto error;
1743 DISPATCH();
1744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001747 PyObject *exp = POP();
1748 PyObject *base = TOP();
1749 PyObject *res = PyNumber_Power(base, exp, Py_None);
1750 Py_DECREF(base);
1751 Py_DECREF(exp);
1752 SET_TOP(res);
1753 if (res == NULL)
1754 goto error;
1755 DISPATCH();
1756 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Benjamin Petersonddd19492018-09-16 22:38:02 -07001758 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 PyObject *right = POP();
1760 PyObject *left = TOP();
1761 PyObject *res = PyNumber_Multiply(left, right);
1762 Py_DECREF(left);
1763 Py_DECREF(right);
1764 SET_TOP(res);
1765 if (res == NULL)
1766 goto error;
1767 DISPATCH();
1768 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Benjamin Petersonddd19492018-09-16 22:38:02 -07001770 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001771 PyObject *right = POP();
1772 PyObject *left = TOP();
1773 PyObject *res = PyNumber_MatrixMultiply(left, right);
1774 Py_DECREF(left);
1775 Py_DECREF(right);
1776 SET_TOP(res);
1777 if (res == NULL)
1778 goto error;
1779 DISPATCH();
1780 }
1781
Benjamin Petersonddd19492018-09-16 22:38:02 -07001782 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 PyObject *divisor = POP();
1784 PyObject *dividend = TOP();
1785 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1786 Py_DECREF(dividend);
1787 Py_DECREF(divisor);
1788 SET_TOP(quotient);
1789 if (quotient == NULL)
1790 goto error;
1791 DISPATCH();
1792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Benjamin Petersonddd19492018-09-16 22:38:02 -07001794 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 PyObject *divisor = POP();
1796 PyObject *dividend = TOP();
1797 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1798 Py_DECREF(dividend);
1799 Py_DECREF(divisor);
1800 SET_TOP(quotient);
1801 if (quotient == NULL)
1802 goto error;
1803 DISPATCH();
1804 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001805
Benjamin Petersonddd19492018-09-16 22:38:02 -07001806 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 PyObject *divisor = POP();
1808 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001809 PyObject *res;
1810 if (PyUnicode_CheckExact(dividend) && (
1811 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1812 // fast path; string formatting, but not if the RHS is a str subclass
1813 // (see issue28598)
1814 res = PyUnicode_Format(dividend, divisor);
1815 } else {
1816 res = PyNumber_Remainder(dividend, divisor);
1817 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 Py_DECREF(divisor);
1819 Py_DECREF(dividend);
1820 SET_TOP(res);
1821 if (res == NULL)
1822 goto error;
1823 DISPATCH();
1824 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Benjamin Petersonddd19492018-09-16 22:38:02 -07001826 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 PyObject *right = POP();
1828 PyObject *left = TOP();
1829 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001830 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001831 CPython using bytecode, it is simply worthless.
1832 See http://bugs.python.org/issue21955 and
1833 http://bugs.python.org/issue10044 for the discussion. In short,
1834 no patch shown any impact on a realistic benchmark, only a minor
1835 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 if (PyUnicode_CheckExact(left) &&
1837 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001838 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001839 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001840 }
1841 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 sum = PyNumber_Add(left, right);
1843 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001844 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 Py_DECREF(right);
1846 SET_TOP(sum);
1847 if (sum == NULL)
1848 goto error;
1849 DISPATCH();
1850 }
1851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *right = POP();
1854 PyObject *left = TOP();
1855 PyObject *diff = PyNumber_Subtract(left, right);
1856 Py_DECREF(right);
1857 Py_DECREF(left);
1858 SET_TOP(diff);
1859 if (diff == NULL)
1860 goto error;
1861 DISPATCH();
1862 }
1863
Benjamin Petersonddd19492018-09-16 22:38:02 -07001864 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 PyObject *sub = POP();
1866 PyObject *container = TOP();
1867 PyObject *res = PyObject_GetItem(container, sub);
1868 Py_DECREF(container);
1869 Py_DECREF(sub);
1870 SET_TOP(res);
1871 if (res == NULL)
1872 goto error;
1873 DISPATCH();
1874 }
1875
Benjamin Petersonddd19492018-09-16 22:38:02 -07001876 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 PyObject *right = POP();
1878 PyObject *left = TOP();
1879 PyObject *res = PyNumber_Lshift(left, right);
1880 Py_DECREF(left);
1881 Py_DECREF(right);
1882 SET_TOP(res);
1883 if (res == NULL)
1884 goto error;
1885 DISPATCH();
1886 }
1887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *right = POP();
1890 PyObject *left = TOP();
1891 PyObject *res = PyNumber_Rshift(left, right);
1892 Py_DECREF(left);
1893 Py_DECREF(right);
1894 SET_TOP(res);
1895 if (res == NULL)
1896 goto error;
1897 DISPATCH();
1898 }
1899
Benjamin Petersonddd19492018-09-16 22:38:02 -07001900 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 PyObject *right = POP();
1902 PyObject *left = TOP();
1903 PyObject *res = PyNumber_And(left, right);
1904 Py_DECREF(left);
1905 Py_DECREF(right);
1906 SET_TOP(res);
1907 if (res == NULL)
1908 goto error;
1909 DISPATCH();
1910 }
1911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 PyObject *right = POP();
1914 PyObject *left = TOP();
1915 PyObject *res = PyNumber_Xor(left, right);
1916 Py_DECREF(left);
1917 Py_DECREF(right);
1918 SET_TOP(res);
1919 if (res == NULL)
1920 goto error;
1921 DISPATCH();
1922 }
1923
Benjamin Petersonddd19492018-09-16 22:38:02 -07001924 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 PyObject *right = POP();
1926 PyObject *left = TOP();
1927 PyObject *res = PyNumber_Or(left, right);
1928 Py_DECREF(left);
1929 Py_DECREF(right);
1930 SET_TOP(res);
1931 if (res == NULL)
1932 goto error;
1933 DISPATCH();
1934 }
1935
Benjamin Petersonddd19492018-09-16 22:38:02 -07001936 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 PyObject *v = POP();
1938 PyObject *list = PEEK(oparg);
1939 int err;
1940 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 if (err != 0)
1943 goto error;
1944 PREDICT(JUMP_ABSOLUTE);
1945 DISPATCH();
1946 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Benjamin Petersonddd19492018-09-16 22:38:02 -07001948 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001949 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001950 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 int err;
1952 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 if (err != 0)
1955 goto error;
1956 PREDICT(JUMP_ABSOLUTE);
1957 DISPATCH();
1958 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PyObject *exp = POP();
1962 PyObject *base = TOP();
1963 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1964 Py_DECREF(base);
1965 Py_DECREF(exp);
1966 SET_TOP(res);
1967 if (res == NULL)
1968 goto error;
1969 DISPATCH();
1970 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Benjamin Petersonddd19492018-09-16 22:38:02 -07001972 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 PyObject *right = POP();
1974 PyObject *left = TOP();
1975 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1976 Py_DECREF(left);
1977 Py_DECREF(right);
1978 SET_TOP(res);
1979 if (res == NULL)
1980 goto error;
1981 DISPATCH();
1982 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001983
Benjamin Petersonddd19492018-09-16 22:38:02 -07001984 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001985 PyObject *right = POP();
1986 PyObject *left = TOP();
1987 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1988 Py_DECREF(left);
1989 Py_DECREF(right);
1990 SET_TOP(res);
1991 if (res == NULL)
1992 goto error;
1993 DISPATCH();
1994 }
1995
Benjamin Petersonddd19492018-09-16 22:38:02 -07001996 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 PyObject *divisor = POP();
1998 PyObject *dividend = TOP();
1999 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2000 Py_DECREF(dividend);
2001 Py_DECREF(divisor);
2002 SET_TOP(quotient);
2003 if (quotient == NULL)
2004 goto error;
2005 DISPATCH();
2006 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002007
Benjamin Petersonddd19492018-09-16 22:38:02 -07002008 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 PyObject *divisor = POP();
2010 PyObject *dividend = TOP();
2011 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2012 Py_DECREF(dividend);
2013 Py_DECREF(divisor);
2014 SET_TOP(quotient);
2015 if (quotient == NULL)
2016 goto error;
2017 DISPATCH();
2018 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002019
Benjamin Petersonddd19492018-09-16 22:38:02 -07002020 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 PyObject *right = POP();
2022 PyObject *left = TOP();
2023 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2024 Py_DECREF(left);
2025 Py_DECREF(right);
2026 SET_TOP(mod);
2027 if (mod == NULL)
2028 goto error;
2029 DISPATCH();
2030 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002031
Benjamin Petersonddd19492018-09-16 22:38:02 -07002032 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 PyObject *right = POP();
2034 PyObject *left = TOP();
2035 PyObject *sum;
2036 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002037 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002038 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002039 }
2040 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 sum = PyNumber_InPlaceAdd(left, right);
2042 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002043 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 Py_DECREF(right);
2045 SET_TOP(sum);
2046 if (sum == NULL)
2047 goto error;
2048 DISPATCH();
2049 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Benjamin Petersonddd19492018-09-16 22:38:02 -07002051 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 PyObject *right = POP();
2053 PyObject *left = TOP();
2054 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2055 Py_DECREF(left);
2056 Py_DECREF(right);
2057 SET_TOP(diff);
2058 if (diff == NULL)
2059 goto error;
2060 DISPATCH();
2061 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002062
Benjamin Petersonddd19492018-09-16 22:38:02 -07002063 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 PyObject *right = POP();
2065 PyObject *left = TOP();
2066 PyObject *res = PyNumber_InPlaceLshift(left, right);
2067 Py_DECREF(left);
2068 Py_DECREF(right);
2069 SET_TOP(res);
2070 if (res == NULL)
2071 goto error;
2072 DISPATCH();
2073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Benjamin Petersonddd19492018-09-16 22:38:02 -07002075 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 PyObject *right = POP();
2077 PyObject *left = TOP();
2078 PyObject *res = PyNumber_InPlaceRshift(left, right);
2079 Py_DECREF(left);
2080 Py_DECREF(right);
2081 SET_TOP(res);
2082 if (res == NULL)
2083 goto error;
2084 DISPATCH();
2085 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002086
Benjamin Petersonddd19492018-09-16 22:38:02 -07002087 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 PyObject *right = POP();
2089 PyObject *left = TOP();
2090 PyObject *res = PyNumber_InPlaceAnd(left, right);
2091 Py_DECREF(left);
2092 Py_DECREF(right);
2093 SET_TOP(res);
2094 if (res == NULL)
2095 goto error;
2096 DISPATCH();
2097 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Benjamin Petersonddd19492018-09-16 22:38:02 -07002099 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 PyObject *right = POP();
2101 PyObject *left = TOP();
2102 PyObject *res = PyNumber_InPlaceXor(left, right);
2103 Py_DECREF(left);
2104 Py_DECREF(right);
2105 SET_TOP(res);
2106 if (res == NULL)
2107 goto error;
2108 DISPATCH();
2109 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002110
Benjamin Petersonddd19492018-09-16 22:38:02 -07002111 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 PyObject *right = POP();
2113 PyObject *left = TOP();
2114 PyObject *res = PyNumber_InPlaceOr(left, right);
2115 Py_DECREF(left);
2116 Py_DECREF(right);
2117 SET_TOP(res);
2118 if (res == NULL)
2119 goto error;
2120 DISPATCH();
2121 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002122
Benjamin Petersonddd19492018-09-16 22:38:02 -07002123 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 PyObject *sub = TOP();
2125 PyObject *container = SECOND();
2126 PyObject *v = THIRD();
2127 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002128 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002129 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 Py_DECREF(container);
2133 Py_DECREF(sub);
2134 if (err != 0)
2135 goto error;
2136 DISPATCH();
2137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Benjamin Petersonddd19492018-09-16 22:38:02 -07002139 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 PyObject *sub = TOP();
2141 PyObject *container = SECOND();
2142 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002143 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002144 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 err = PyObject_DelItem(container, sub);
2146 Py_DECREF(container);
2147 Py_DECREF(sub);
2148 if (err != 0)
2149 goto error;
2150 DISPATCH();
2151 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002152
Benjamin Petersonddd19492018-09-16 22:38:02 -07002153 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002154 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002156 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002157 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002159 _PyErr_SetString(tstate, PyExc_RuntimeError,
2160 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 Py_DECREF(value);
2162 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002164 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 Py_DECREF(value);
2166 if (res == NULL)
2167 goto error;
2168 Py_DECREF(res);
2169 DISPATCH();
2170 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002171
Benjamin Petersonddd19492018-09-16 22:38:02 -07002172 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 switch (oparg) {
2175 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002177 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002180 /* fall through */
2181 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002182 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002183 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 break;
2186 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002187 _PyErr_SetString(tstate, PyExc_SystemError,
2188 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 break;
2190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 goto error;
2192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002196 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002197 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002198 f->f_state = FRAME_RETURNED;
2199 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002200 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002202
Benjamin Petersonddd19492018-09-16 22:38:02 -07002203 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002204 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002205 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002206 PyObject *obj = TOP();
2207 PyTypeObject *type = Py_TYPE(obj);
2208
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002209 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002210 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002211 }
Yury Selivanov75445082015-05-11 22:57:16 -04002212
2213 if (getter != NULL) {
2214 iter = (*getter)(obj);
2215 Py_DECREF(obj);
2216 if (iter == NULL) {
2217 SET_TOP(NULL);
2218 goto error;
2219 }
2220 }
2221 else {
2222 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002223 _PyErr_Format(tstate, PyExc_TypeError,
2224 "'async for' requires an object with "
2225 "__aiter__ method, got %.100s",
2226 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002227 Py_DECREF(obj);
2228 goto error;
2229 }
2230
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002231 if (Py_TYPE(iter)->tp_as_async == NULL ||
2232 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002233
Yury Selivanov398ff912017-03-02 22:20:00 -05002234 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002235 _PyErr_Format(tstate, PyExc_TypeError,
2236 "'async for' received an object from __aiter__ "
2237 "that does not implement __anext__: %.100s",
2238 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002239 Py_DECREF(iter);
2240 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002241 }
2242
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002243 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002244 DISPATCH();
2245 }
2246
Benjamin Petersonddd19492018-09-16 22:38:02 -07002247 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002248 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002249 PyObject *next_iter = NULL;
2250 PyObject *awaitable = NULL;
2251 PyObject *aiter = TOP();
2252 PyTypeObject *type = Py_TYPE(aiter);
2253
Yury Selivanoveb636452016-09-08 22:01:51 -07002254 if (PyAsyncGen_CheckExact(aiter)) {
2255 awaitable = type->tp_as_async->am_anext(aiter);
2256 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002257 goto error;
2258 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002259 } else {
2260 if (type->tp_as_async != NULL){
2261 getter = type->tp_as_async->am_anext;
2262 }
Yury Selivanov75445082015-05-11 22:57:16 -04002263
Yury Selivanoveb636452016-09-08 22:01:51 -07002264 if (getter != NULL) {
2265 next_iter = (*getter)(aiter);
2266 if (next_iter == NULL) {
2267 goto error;
2268 }
2269 }
2270 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002271 _PyErr_Format(tstate, PyExc_TypeError,
2272 "'async for' requires an iterator with "
2273 "__anext__ method, got %.100s",
2274 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002275 goto error;
2276 }
Yury Selivanov75445082015-05-11 22:57:16 -04002277
Yury Selivanoveb636452016-09-08 22:01:51 -07002278 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2279 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002280 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002281 PyExc_TypeError,
2282 "'async for' received an invalid object "
2283 "from __anext__: %.100s",
2284 Py_TYPE(next_iter)->tp_name);
2285
2286 Py_DECREF(next_iter);
2287 goto error;
2288 } else {
2289 Py_DECREF(next_iter);
2290 }
2291 }
Yury Selivanov75445082015-05-11 22:57:16 -04002292
2293 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002294 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002295 DISPATCH();
2296 }
2297
Benjamin Petersonddd19492018-09-16 22:38:02 -07002298 case TARGET(GET_AWAITABLE): {
2299 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002300 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002301 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002302
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002303 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002304 int opcode_at_minus_3 = 0;
2305 if ((next_instr - first_instr) > 2) {
2306 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2307 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002308 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002309 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002310 _Py_OPCODE(next_instr[-2]));
2311 }
2312
Yury Selivanov75445082015-05-11 22:57:16 -04002313 Py_DECREF(iterable);
2314
Yury Selivanovc724bae2016-03-02 11:30:46 -05002315 if (iter != NULL && PyCoro_CheckExact(iter)) {
2316 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2317 if (yf != NULL) {
2318 /* `iter` is a coroutine object that is being
2319 awaited, `yf` is a pointer to the current awaitable
2320 being awaited on. */
2321 Py_DECREF(yf);
2322 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002323 _PyErr_SetString(tstate, PyExc_RuntimeError,
2324 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002325 /* The code below jumps to `error` if `iter` is NULL. */
2326 }
2327 }
2328
Yury Selivanov75445082015-05-11 22:57:16 -04002329 SET_TOP(iter); /* Even if it's NULL */
2330
2331 if (iter == NULL) {
2332 goto error;
2333 }
2334
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002335 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002336 DISPATCH();
2337 }
2338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002341 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002342 PySendResult gen_status;
2343 if (tstate->c_tracefunc == NULL) {
2344 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002345 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002346 _Py_IDENTIFIER(send);
2347 if (v == Py_None && PyIter_Check(receiver)) {
2348 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002349 }
2350 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002351 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002352 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002353 if (retval == NULL) {
2354 if (tstate->c_tracefunc != NULL
2355 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon86433452021-01-07 16:49:02 +00002356 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002357 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2358 gen_status = PYGEN_RETURN;
2359 }
2360 else {
2361 gen_status = PYGEN_ERROR;
2362 }
2363 }
2364 else {
2365 gen_status = PYGEN_NEXT;
2366 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002369 if (gen_status == PYGEN_ERROR) {
2370 assert (retval == NULL);
2371 goto error;
2372 }
2373 if (gen_status == PYGEN_RETURN) {
2374 assert (retval != NULL);
2375
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002376 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002377 SET_TOP(retval);
2378 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002380 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002381 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002382 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002383 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002384 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002385 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002386 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002387 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002388 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002390
Benjamin Petersonddd19492018-09-16 22:38:02 -07002391 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002393
2394 if (co->co_flags & CO_ASYNC_GENERATOR) {
2395 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2396 Py_DECREF(retval);
2397 if (w == NULL) {
2398 retval = NULL;
2399 goto error;
2400 }
2401 retval = w;
2402 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002403 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002404 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002405 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002407
Benjamin Petersonddd19492018-09-16 22:38:02 -07002408 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002409 PyObject *type, *value, *traceback;
2410 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 PyTryBlock *b = PyFrame_BlockPop(f);
2412 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 _PyErr_SetString(tstate, PyExc_SystemError,
2414 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002417 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2418 STACK_LEVEL() <= (b)->b_level + 4);
2419 exc_info = tstate->exc_info;
2420 type = exc_info->exc_type;
2421 value = exc_info->exc_value;
2422 traceback = exc_info->exc_traceback;
2423 exc_info->exc_type = POP();
2424 exc_info->exc_value = POP();
2425 exc_info->exc_traceback = POP();
2426 Py_XDECREF(type);
2427 Py_XDECREF(value);
2428 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002431
Benjamin Petersonddd19492018-09-16 22:38:02 -07002432 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002433 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002436
Mark Shannonfee55262019-11-21 09:11:43 +00002437 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002438 assert(f->f_iblock > 0);
2439 if (oparg) {
2440 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2441 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002442 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002443 PyObject *val = POP();
2444 PyObject *tb = POP();
2445 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002446 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002447 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002449
Benjamin Petersonddd19492018-09-16 22:38:02 -07002450 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002451 PyObject *exc = POP();
2452 assert(PyExceptionClass_Check(exc));
2453 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2454 PyTryBlock *b = PyFrame_BlockPop(f);
2455 assert(b->b_type == EXCEPT_HANDLER);
2456 Py_DECREF(exc);
2457 UNWIND_EXCEPT_HANDLER(b);
2458 Py_DECREF(POP());
2459 JUMPBY(oparg);
2460 FAST_DISPATCH();
2461 }
2462 else {
2463 PyObject *val = POP();
2464 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002466 goto exception_unwind;
2467 }
2468 }
2469
Zackery Spytzce6a0702019-08-25 03:44:09 -06002470 case TARGET(LOAD_ASSERTION_ERROR): {
2471 PyObject *value = PyExc_AssertionError;
2472 Py_INCREF(value);
2473 PUSH(value);
2474 FAST_DISPATCH();
2475 }
2476
Benjamin Petersonddd19492018-09-16 22:38:02 -07002477 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002478 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002481 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002482 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002484 if (!_PyErr_Occurred(tstate)) {
2485 _PyErr_SetString(tstate, PyExc_NameError,
2486 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002489 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002491 }
2492 else {
2493 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2494 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002495 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2497 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002498 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2499 _PyErr_SetString(tstate, PyExc_NameError,
2500 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002505 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Benjamin Petersonddd19492018-09-16 22:38:02 -07002508 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 PyObject *name = GETITEM(names, oparg);
2510 PyObject *v = POP();
2511 PyObject *ns = f->f_locals;
2512 int err;
2513 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002514 _PyErr_Format(tstate, PyExc_SystemError,
2515 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 if (PyDict_CheckExact(ns))
2520 err = PyDict_SetItem(ns, name, v);
2521 else
2522 err = PyObject_SetItem(ns, name, v);
2523 Py_DECREF(v);
2524 if (err != 0)
2525 goto error;
2526 DISPATCH();
2527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002528
Benjamin Petersonddd19492018-09-16 22:38:02 -07002529 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 PyObject *name = GETITEM(names, oparg);
2531 PyObject *ns = f->f_locals;
2532 int err;
2533 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002534 _PyErr_Format(tstate, PyExc_SystemError,
2535 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 err = PyObject_DelItem(ns, name);
2539 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 NAME_ERROR_MSG,
2542 name);
2543 goto error;
2544 }
2545 DISPATCH();
2546 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002547
Benjamin Petersonddd19492018-09-16 22:38:02 -07002548 case TARGET(UNPACK_SEQUENCE): {
2549 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 PyObject *seq = POP(), *item, **items;
2551 if (PyTuple_CheckExact(seq) &&
2552 PyTuple_GET_SIZE(seq) == oparg) {
2553 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 item = items[oparg];
2556 Py_INCREF(item);
2557 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 } else if (PyList_CheckExact(seq) &&
2560 PyList_GET_SIZE(seq) == oparg) {
2561 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 item = items[oparg];
2564 Py_INCREF(item);
2565 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002567 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002569 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 } else {
2571 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 Py_DECREF(seq);
2573 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002576 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002578
Benjamin Petersonddd19492018-09-16 22:38:02 -07002579 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002580 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2581 PyObject *seq = POP();
2582
Victor Stinner438a12d2019-05-24 17:01:38 +02002583 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 stack_pointer + totalargs)) {
2585 stack_pointer += totalargs;
2586 } else {
2587 Py_DECREF(seq);
2588 goto error;
2589 }
2590 Py_DECREF(seq);
2591 DISPATCH();
2592 }
2593
Benjamin Petersonddd19492018-09-16 22:38:02 -07002594 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PyObject *name = GETITEM(names, oparg);
2596 PyObject *owner = TOP();
2597 PyObject *v = SECOND();
2598 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002599 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 Py_DECREF(owner);
2603 if (err != 0)
2604 goto error;
2605 DISPATCH();
2606 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002607
Benjamin Petersonddd19492018-09-16 22:38:02 -07002608 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PyObject *name = GETITEM(names, oparg);
2610 PyObject *owner = POP();
2611 int err;
2612 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2613 Py_DECREF(owner);
2614 if (err != 0)
2615 goto error;
2616 DISPATCH();
2617 }
2618
Benjamin Petersonddd19492018-09-16 22:38:02 -07002619 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 PyObject *name = GETITEM(names, oparg);
2621 PyObject *v = POP();
2622 int err;
2623 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 if (err != 0)
2626 goto error;
2627 DISPATCH();
2628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002629
Benjamin Petersonddd19492018-09-16 22:38:02 -07002630 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 PyObject *name = GETITEM(names, oparg);
2632 int err;
2633 err = PyDict_DelItem(f->f_globals, name);
2634 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002635 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2636 format_exc_check_arg(tstate, PyExc_NameError,
2637 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002638 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002640 }
2641 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002643
Benjamin Petersonddd19492018-09-16 22:38:02 -07002644 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 PyObject *name = GETITEM(names, oparg);
2646 PyObject *locals = f->f_locals;
2647 PyObject *v;
2648 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002649 _PyErr_Format(tstate, PyExc_SystemError,
2650 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002654 v = PyDict_GetItemWithError(locals, name);
2655 if (v != NULL) {
2656 Py_INCREF(v);
2657 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002658 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002659 goto error;
2660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
2662 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002664 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002665 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002666 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002667 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
2669 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002671 v = PyDict_GetItemWithError(f->f_globals, name);
2672 if (v != NULL) {
2673 Py_INCREF(v);
2674 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002675 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002676 goto error;
2677 }
2678 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002679 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002680 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002682 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002683 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002684 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002686 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002688 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002690 }
2691 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 v = PyObject_GetItem(f->f_builtins, name);
2693 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002694 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002695 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002696 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002698 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002700 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002707
Benjamin Petersonddd19492018-09-16 22:38:02 -07002708 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002709 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002711 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002712 && PyDict_CheckExact(f->f_builtins))
2713 {
Inada Naoki91234a12019-06-03 21:30:58 +09002714 OPCACHE_CHECK();
2715 if (co_opcache != NULL && co_opcache->optimized > 0) {
2716 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2717
2718 if (lg->globals_ver ==
2719 ((PyDictObject *)f->f_globals)->ma_version_tag
2720 && lg->builtins_ver ==
2721 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2722 {
2723 PyObject *ptr = lg->ptr;
2724 OPCACHE_STAT_GLOBAL_HIT();
2725 assert(ptr != NULL);
2726 Py_INCREF(ptr);
2727 PUSH(ptr);
2728 DISPATCH();
2729 }
2730 }
2731
2732 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002734 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 name);
2736 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002737 if (!_PyErr_OCCURRED()) {
2738 /* _PyDict_LoadGlobal() returns NULL without raising
2739 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002740 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002741 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002742 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 }
Inada Naoki91234a12019-06-03 21:30:58 +09002745
2746 if (co_opcache != NULL) {
2747 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2748
2749 if (co_opcache->optimized == 0) {
2750 /* Wasn't optimized before. */
2751 OPCACHE_STAT_GLOBAL_OPT();
2752 } else {
2753 OPCACHE_STAT_GLOBAL_MISS();
2754 }
2755
2756 co_opcache->optimized = 1;
2757 lg->globals_ver =
2758 ((PyDictObject *)f->f_globals)->ma_version_tag;
2759 lg->builtins_ver =
2760 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2761 lg->ptr = v; /* borrowed */
2762 }
2763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002766 else {
2767 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002768
2769 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002770 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 v = PyObject_GetItem(f->f_globals, name);
2772 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002773 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002774 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002775 }
2776 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002777
Victor Stinnerb4efc962015-11-20 09:24:02 +01002778 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 v = PyObject_GetItem(f->f_builtins, name);
2780 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002781 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002782 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002783 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002784 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002785 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002787 }
2788 }
2789 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002793
Benjamin Petersonddd19492018-09-16 22:38:02 -07002794 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 PyObject *v = GETLOCAL(oparg);
2796 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 SETLOCAL(oparg, NULL);
2798 DISPATCH();
2799 }
2800 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002801 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 UNBOUNDLOCAL_ERROR_MSG,
2803 PyTuple_GetItem(co->co_varnames, oparg)
2804 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 goto error;
2806 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002807
Benjamin Petersonddd19492018-09-16 22:38:02 -07002808 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002810 PyObject *oldobj = PyCell_GET(cell);
2811 if (oldobj != NULL) {
2812 PyCell_SET(cell, NULL);
2813 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002814 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002815 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002816 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 goto error;
2818 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002819
Benjamin Petersonddd19492018-09-16 22:38:02 -07002820 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 PyObject *cell = freevars[oparg];
2822 Py_INCREF(cell);
2823 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002826
Benjamin Petersonddd19492018-09-16 22:38:02 -07002827 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002828 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002829 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002830 assert(locals);
2831 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2832 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2833 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2834 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2835 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002836 value = PyDict_GetItemWithError(locals, name);
2837 if (value != NULL) {
2838 Py_INCREF(value);
2839 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002840 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002841 goto error;
2842 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002843 }
2844 else {
2845 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002846 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002847 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002848 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002849 }
2850 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002851 }
2852 }
2853 if (!value) {
2854 PyObject *cell = freevars[oparg];
2855 value = PyCell_GET(cell);
2856 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002857 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002858 goto error;
2859 }
2860 Py_INCREF(value);
2861 }
2862 PUSH(value);
2863 DISPATCH();
2864 }
2865
Benjamin Petersonddd19492018-09-16 22:38:02 -07002866 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 PyObject *cell = freevars[oparg];
2868 PyObject *value = PyCell_GET(cell);
2869 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002870 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 Py_INCREF(value);
2874 PUSH(value);
2875 DISPATCH();
2876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002877
Benjamin Petersonddd19492018-09-16 22:38:02 -07002878 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 PyObject *v = POP();
2880 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002881 PyObject *oldobj = PyCell_GET(cell);
2882 PyCell_SET(cell, v);
2883 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 DISPATCH();
2885 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002886
Benjamin Petersonddd19492018-09-16 22:38:02 -07002887 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002888 PyObject *str;
2889 PyObject *empty = PyUnicode_New(0, 0);
2890 if (empty == NULL) {
2891 goto error;
2892 }
2893 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2894 Py_DECREF(empty);
2895 if (str == NULL)
2896 goto error;
2897 while (--oparg >= 0) {
2898 PyObject *item = POP();
2899 Py_DECREF(item);
2900 }
2901 PUSH(str);
2902 DISPATCH();
2903 }
2904
Benjamin Petersonddd19492018-09-16 22:38:02 -07002905 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 PyObject *tup = PyTuple_New(oparg);
2907 if (tup == NULL)
2908 goto error;
2909 while (--oparg >= 0) {
2910 PyObject *item = POP();
2911 PyTuple_SET_ITEM(tup, oparg, item);
2912 }
2913 PUSH(tup);
2914 DISPATCH();
2915 }
2916
Benjamin Petersonddd19492018-09-16 22:38:02 -07002917 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 PyObject *list = PyList_New(oparg);
2919 if (list == NULL)
2920 goto error;
2921 while (--oparg >= 0) {
2922 PyObject *item = POP();
2923 PyList_SET_ITEM(list, oparg, item);
2924 }
2925 PUSH(list);
2926 DISPATCH();
2927 }
2928
Mark Shannon13bc1392020-01-23 09:25:17 +00002929 case TARGET(LIST_TO_TUPLE): {
2930 PyObject *list = POP();
2931 PyObject *tuple = PyList_AsTuple(list);
2932 Py_DECREF(list);
2933 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002934 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002935 }
2936 PUSH(tuple);
2937 DISPATCH();
2938 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002939
Mark Shannon13bc1392020-01-23 09:25:17 +00002940 case TARGET(LIST_EXTEND): {
2941 PyObject *iterable = POP();
2942 PyObject *list = PEEK(oparg);
2943 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2944 if (none_val == NULL) {
2945 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002946 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002947 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002948 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002949 _PyErr_Format(tstate, PyExc_TypeError,
2950 "Value after * must be an iterable, not %.200s",
2951 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002952 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002953 Py_DECREF(iterable);
2954 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002955 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002956 Py_DECREF(none_val);
2957 Py_DECREF(iterable);
2958 DISPATCH();
2959 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002960
Mark Shannon13bc1392020-01-23 09:25:17 +00002961 case TARGET(SET_UPDATE): {
2962 PyObject *iterable = POP();
2963 PyObject *set = PEEK(oparg);
2964 int err = _PySet_Update(set, iterable);
2965 Py_DECREF(iterable);
2966 if (err < 0) {
2967 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002968 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002969 DISPATCH();
2970 }
2971
Benjamin Petersonddd19492018-09-16 22:38:02 -07002972 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 PyObject *set = PySet_New(NULL);
2974 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002975 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 if (set == NULL)
2977 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002978 for (i = oparg; i > 0; i--) {
2979 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 if (err == 0)
2981 err = PySet_Add(set, item);
2982 Py_DECREF(item);
2983 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002984 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 if (err != 0) {
2986 Py_DECREF(set);
2987 goto error;
2988 }
2989 PUSH(set);
2990 DISPATCH();
2991 }
2992
Benjamin Petersonddd19492018-09-16 22:38:02 -07002993 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002994 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2996 if (map == NULL)
2997 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002998 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002999 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003000 PyObject *key = PEEK(2*i);
3001 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003002 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003003 if (err != 0) {
3004 Py_DECREF(map);
3005 goto error;
3006 }
3007 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003008
3009 while (oparg--) {
3010 Py_DECREF(POP());
3011 Py_DECREF(POP());
3012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 PUSH(map);
3014 DISPATCH();
3015 }
3016
Benjamin Petersonddd19492018-09-16 22:38:02 -07003017 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003018 _Py_IDENTIFIER(__annotations__);
3019 int err;
3020 PyObject *ann_dict;
3021 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003022 _PyErr_Format(tstate, PyExc_SystemError,
3023 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003024 goto error;
3025 }
3026 /* check if __annotations__ in locals()... */
3027 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003028 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003029 &PyId___annotations__);
3030 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003031 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003032 goto error;
3033 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003034 /* ...if not, create a new one */
3035 ann_dict = PyDict_New();
3036 if (ann_dict == NULL) {
3037 goto error;
3038 }
3039 err = _PyDict_SetItemId(f->f_locals,
3040 &PyId___annotations__, ann_dict);
3041 Py_DECREF(ann_dict);
3042 if (err != 0) {
3043 goto error;
3044 }
3045 }
3046 }
3047 else {
3048 /* do the same if locals() is not a dict */
3049 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3050 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003051 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003052 }
3053 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3054 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003055 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003056 goto error;
3057 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003058 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003059 ann_dict = PyDict_New();
3060 if (ann_dict == NULL) {
3061 goto error;
3062 }
3063 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3064 Py_DECREF(ann_dict);
3065 if (err != 0) {
3066 goto error;
3067 }
3068 }
3069 else {
3070 Py_DECREF(ann_dict);
3071 }
3072 }
3073 DISPATCH();
3074 }
3075
Benjamin Petersonddd19492018-09-16 22:38:02 -07003076 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003077 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003078 PyObject *map;
3079 PyObject *keys = TOP();
3080 if (!PyTuple_CheckExact(keys) ||
3081 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003082 _PyErr_SetString(tstate, PyExc_SystemError,
3083 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003084 goto error;
3085 }
3086 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3087 if (map == NULL) {
3088 goto error;
3089 }
3090 for (i = oparg; i > 0; i--) {
3091 int err;
3092 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3093 PyObject *value = PEEK(i + 1);
3094 err = PyDict_SetItem(map, key, value);
3095 if (err != 0) {
3096 Py_DECREF(map);
3097 goto error;
3098 }
3099 }
3100
3101 Py_DECREF(POP());
3102 while (oparg--) {
3103 Py_DECREF(POP());
3104 }
3105 PUSH(map);
3106 DISPATCH();
3107 }
3108
Mark Shannon8a4cd702020-01-27 09:57:45 +00003109 case TARGET(DICT_UPDATE): {
3110 PyObject *update = POP();
3111 PyObject *dict = PEEK(oparg);
3112 if (PyDict_Update(dict, update) < 0) {
3113 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3114 _PyErr_Format(tstate, PyExc_TypeError,
3115 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003116 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003117 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003118 Py_DECREF(update);
3119 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003120 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003121 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003122 DISPATCH();
3123 }
3124
Mark Shannon8a4cd702020-01-27 09:57:45 +00003125 case TARGET(DICT_MERGE): {
3126 PyObject *update = POP();
3127 PyObject *dict = PEEK(oparg);
3128
3129 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3130 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3131 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003132 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003133 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003134 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003135 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003136 DISPATCH();
3137 }
3138
Benjamin Petersonddd19492018-09-16 22:38:02 -07003139 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003140 PyObject *value = TOP();
3141 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 PyObject *map;
3143 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003144 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003145 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003147 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 Py_DECREF(value);
3149 Py_DECREF(key);
3150 if (err != 0)
3151 goto error;
3152 PREDICT(JUMP_ABSOLUTE);
3153 DISPATCH();
3154 }
3155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PyObject *name = GETITEM(names, oparg);
3158 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003159
3160 PyTypeObject *type = Py_TYPE(owner);
3161 PyObject *res;
3162 PyObject **dictptr;
3163 PyObject *dict;
3164 _PyOpCodeOpt_LoadAttr *la;
3165
3166 OPCACHE_STAT_ATTR_TOTAL();
3167
3168 OPCACHE_CHECK();
3169 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3170 {
3171 if (co_opcache->optimized > 0) {
3172 /* Fast path -- cache hit makes LOAD_ATTR ~30% faster */
3173 la = &co_opcache->u.la;
3174 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3175 {
3176 assert(type->tp_dict != NULL);
3177 assert(type->tp_dictoffset > 0);
3178
3179 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3180 dict = *dictptr;
3181 if (dict != NULL && PyDict_CheckExact(dict)) {
3182 Py_ssize_t hint = la->hint;
3183 Py_INCREF(dict);
3184 res = NULL;
3185 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3186
3187 if (res != NULL) {
3188 if (la->hint == hint && hint >= 0) {
3189 /* Our hint has helped -- cache hit. */
3190 OPCACHE_STAT_ATTR_HIT();
3191 } else {
3192 /* The hint we provided didn't work.
3193 Maybe next time? */
3194 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3195 }
3196
3197 Py_INCREF(res);
3198 SET_TOP(res);
3199 Py_DECREF(owner);
3200 Py_DECREF(dict);
3201 DISPATCH();
3202 } else {
3203 // This attribute can be missing sometimes -- we
3204 // don't want to optimize this lookup.
3205 OPCACHE_DEOPT_LOAD_ATTR();
3206 Py_DECREF(dict);
3207 }
3208 } else {
3209 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3210 OPCACHE_DEOPT_LOAD_ATTR();
3211 }
3212 } else {
3213 // The type of the object has either been updated,
3214 // or is different. Maybe it will stabilize?
3215 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3216 }
3217
3218 OPCACHE_STAT_ATTR_MISS();
3219 }
3220
3221 if (co_opcache != NULL && /* co_opcache can be NULL after a DEOPT() call. */
3222 type->tp_getattro == PyObject_GenericGetAttr)
3223 {
Pablo Galindo109826c2020-10-20 06:22:44 +01003224 Py_ssize_t ret;
3225
3226 if (type->tp_dictoffset > 0) {
3227 if (type->tp_dict == NULL) {
3228 if (PyType_Ready(type) < 0) {
3229 Py_DECREF(owner);
3230 SET_TOP(NULL);
3231 goto error;
3232 }
3233 }
Pablo Galindo80449f22020-11-05 09:23:15 +00003234 if (_PyType_Lookup(type, name) == NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003235 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3236 dict = *dictptr;
3237
3238 if (dict != NULL && PyDict_CheckExact(dict)) {
3239 Py_INCREF(dict);
3240 res = NULL;
3241 ret = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3242 if (res != NULL) {
3243 Py_INCREF(res);
3244 Py_DECREF(dict);
3245 Py_DECREF(owner);
3246 SET_TOP(res);
3247
3248 if (co_opcache->optimized == 0) {
3249 // First time we optimize this opcode. */
3250 OPCACHE_STAT_ATTR_OPT();
3251 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3252 }
3253
3254 la = &co_opcache->u.la;
3255 la->type = type;
3256 la->tp_version_tag = type->tp_version_tag;
3257 la->hint = ret;
3258
3259 DISPATCH();
3260 }
3261 Py_DECREF(dict);
3262 } else {
3263 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3264 OPCACHE_DEOPT_LOAD_ATTR();
3265 }
3266 } else {
3267 // We failed to find an attribute without a data-like descriptor
3268 OPCACHE_DEOPT_LOAD_ATTR();
3269 }
3270 } else {
3271 // The object's class does not have a tp_dictoffset we can use
3272 OPCACHE_DEOPT_LOAD_ATTR();
3273 }
3274 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3275 OPCACHE_DEOPT_LOAD_ATTR();
3276 }
3277 }
3278
3279 /* slow path */
3280 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 Py_DECREF(owner);
3282 SET_TOP(res);
3283 if (res == NULL)
3284 goto error;
3285 DISPATCH();
3286 }
3287
Benjamin Petersonddd19492018-09-16 22:38:02 -07003288 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003289 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 PyObject *right = POP();
3291 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003292 PyObject *res = PyObject_RichCompare(left, right, oparg);
3293 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 Py_DECREF(left);
3295 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 if (res == NULL)
3297 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 PREDICT(POP_JUMP_IF_FALSE);
3299 PREDICT(POP_JUMP_IF_TRUE);
3300 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003302
Mark Shannon9af0e472020-01-14 10:12:45 +00003303 case TARGET(IS_OP): {
3304 PyObject *right = POP();
3305 PyObject *left = TOP();
3306 int res = (left == right)^oparg;
3307 PyObject *b = res ? Py_True : Py_False;
3308 Py_INCREF(b);
3309 SET_TOP(b);
3310 Py_DECREF(left);
3311 Py_DECREF(right);
3312 PREDICT(POP_JUMP_IF_FALSE);
3313 PREDICT(POP_JUMP_IF_TRUE);
3314 FAST_DISPATCH();
3315 }
3316
3317 case TARGET(CONTAINS_OP): {
3318 PyObject *right = POP();
3319 PyObject *left = POP();
3320 int res = PySequence_Contains(right, left);
3321 Py_DECREF(left);
3322 Py_DECREF(right);
3323 if (res < 0) {
3324 goto error;
3325 }
3326 PyObject *b = (res^oparg) ? Py_True : Py_False;
3327 Py_INCREF(b);
3328 PUSH(b);
3329 PREDICT(POP_JUMP_IF_FALSE);
3330 PREDICT(POP_JUMP_IF_TRUE);
3331 FAST_DISPATCH();
3332 }
3333
3334#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3335 "BaseException is not allowed"
3336
3337 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3338 PyObject *right = POP();
3339 PyObject *left = POP();
3340 if (PyTuple_Check(right)) {
3341 Py_ssize_t i, length;
3342 length = PyTuple_GET_SIZE(right);
3343 for (i = 0; i < length; i++) {
3344 PyObject *exc = PyTuple_GET_ITEM(right, i);
3345 if (!PyExceptionClass_Check(exc)) {
3346 _PyErr_SetString(tstate, PyExc_TypeError,
3347 CANNOT_CATCH_MSG);
3348 Py_DECREF(left);
3349 Py_DECREF(right);
3350 goto error;
3351 }
3352 }
3353 }
3354 else {
3355 if (!PyExceptionClass_Check(right)) {
3356 _PyErr_SetString(tstate, PyExc_TypeError,
3357 CANNOT_CATCH_MSG);
3358 Py_DECREF(left);
3359 Py_DECREF(right);
3360 goto error;
3361 }
3362 }
3363 int res = PyErr_GivenExceptionMatches(left, right);
3364 Py_DECREF(left);
3365 Py_DECREF(right);
3366 if (res > 0) {
3367 /* Exception matches -- Do nothing */;
3368 }
3369 else if (res == 0) {
3370 JUMPTO(oparg);
3371 }
3372 else {
3373 goto error;
3374 }
3375 DISPATCH();
3376 }
3377
Benjamin Petersonddd19492018-09-16 22:38:02 -07003378 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003380 PyObject *fromlist = POP();
3381 PyObject *level = TOP();
3382 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003383 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003384 Py_DECREF(level);
3385 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 SET_TOP(res);
3387 if (res == NULL)
3388 goto error;
3389 DISPATCH();
3390 }
3391
Benjamin Petersonddd19492018-09-16 22:38:02 -07003392 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 PyObject *from = POP(), *locals;
3394 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003395 if (PyFrame_FastToLocalsWithError(f) < 0) {
3396 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003397 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003398 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003399
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 locals = f->f_locals;
3401 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003402 _PyErr_SetString(tstate, PyExc_SystemError,
3403 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003404 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003407 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 Py_DECREF(from);
3410 if (err != 0)
3411 goto error;
3412 DISPATCH();
3413 }
Guido van Rossum25831651993-05-19 14:50:45 +00003414
Benjamin Petersonddd19492018-09-16 22:38:02 -07003415 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 PyObject *name = GETITEM(names, oparg);
3417 PyObject *from = TOP();
3418 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003419 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 PUSH(res);
3421 if (res == NULL)
3422 goto error;
3423 DISPATCH();
3424 }
Thomas Wouters52152252000-08-17 22:55:00 +00003425
Benjamin Petersonddd19492018-09-16 22:38:02 -07003426 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 JUMPBY(oparg);
3428 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003430
Benjamin Petersonddd19492018-09-16 22:38:02 -07003431 case TARGET(POP_JUMP_IF_FALSE): {
3432 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 PyObject *cond = POP();
3434 int err;
3435 if (cond == Py_True) {
3436 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 FAST_DISPATCH();
3438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 if (cond == Py_False) {
3440 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 JUMPTO(oparg);
3442 FAST_DISPATCH();
3443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003444 err = PyObject_IsTrue(cond);
3445 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003447 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 else if (err == 0)
3449 JUMPTO(oparg);
3450 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003454
Benjamin Petersonddd19492018-09-16 22:38:02 -07003455 case TARGET(POP_JUMP_IF_TRUE): {
3456 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 PyObject *cond = POP();
3458 int err;
3459 if (cond == Py_False) {
3460 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 FAST_DISPATCH();
3462 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 if (cond == Py_True) {
3464 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 JUMPTO(oparg);
3466 FAST_DISPATCH();
3467 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003468 err = PyObject_IsTrue(cond);
3469 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 JUMPTO(oparg);
3472 }
3473 else if (err == 0)
3474 ;
3475 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003476 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003479
Benjamin Petersonddd19492018-09-16 22:38:02 -07003480 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 PyObject *cond = TOP();
3482 int err;
3483 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003484 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 FAST_DISPATCH();
3487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 JUMPTO(oparg);
3490 FAST_DISPATCH();
3491 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003494 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 }
3497 else if (err == 0)
3498 JUMPTO(oparg);
3499 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003503
Benjamin Petersonddd19492018-09-16 22:38:02 -07003504 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 PyObject *cond = TOP();
3506 int err;
3507 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003508 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 FAST_DISPATCH();
3511 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 JUMPTO(oparg);
3514 FAST_DISPATCH();
3515 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003516 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 JUMPTO(oparg);
3519 }
3520 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003521 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 }
3524 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003525 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003528
Benjamin Petersonddd19492018-09-16 22:38:02 -07003529 case TARGET(JUMP_ABSOLUTE): {
3530 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003532#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 /* Enabling this path speeds-up all while and for-loops by bypassing
3534 the per-loop checks for signals. By default, this should be turned-off
3535 because it prevents detection of a control-break in tight loops like
3536 "while 1: pass". Compile with this option turned-on when you need
3537 the speed-up and do not need break checking inside tight loops (ones
3538 that contain only instructions ending with FAST_DISPATCH).
3539 */
3540 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003541#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003543#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003544 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003545
Benjamin Petersonddd19492018-09-16 22:38:02 -07003546 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003548 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003549 PyObject *iter = PyObject_GetIter(iterable);
3550 Py_DECREF(iterable);
3551 SET_TOP(iter);
3552 if (iter == NULL)
3553 goto error;
3554 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003555 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003556 DISPATCH();
3557 }
3558
Benjamin Petersonddd19492018-09-16 22:38:02 -07003559 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003560 /* before: [obj]; after [getiter(obj)] */
3561 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003562 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003563 if (PyCoro_CheckExact(iterable)) {
3564 /* `iterable` is a coroutine */
3565 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3566 /* and it is used in a 'yield from' expression of a
3567 regular generator. */
3568 Py_DECREF(iterable);
3569 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003570 _PyErr_SetString(tstate, PyExc_TypeError,
3571 "cannot 'yield from' a coroutine object "
3572 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003573 goto error;
3574 }
3575 }
3576 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003577 /* `iterable` is not a generator. */
3578 iter = PyObject_GetIter(iterable);
3579 Py_DECREF(iterable);
3580 SET_TOP(iter);
3581 if (iter == NULL)
3582 goto error;
3583 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003584 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 DISPATCH();
3586 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003587
Benjamin Petersonddd19492018-09-16 22:38:02 -07003588 case TARGET(FOR_ITER): {
3589 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003592 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 if (next != NULL) {
3594 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 PREDICT(STORE_FAST);
3596 PREDICT(UNPACK_SEQUENCE);
3597 DISPATCH();
3598 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003599 if (_PyErr_Occurred(tstate)) {
3600 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003601 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003602 }
3603 else if (tstate->c_tracefunc != NULL) {
Mark Shannon86433452021-01-07 16:49:02 +00003604 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &bounds);
Victor Stinner438a12d2019-05-24 17:01:38 +02003605 }
3606 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 }
3608 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003609 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 JUMPBY(oparg);
3612 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003613 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003614
Benjamin Petersonddd19492018-09-16 22:38:02 -07003615 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003616 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 STACK_LEVEL());
3618 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003620
Benjamin Petersonddd19492018-09-16 22:38:02 -07003621 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003622 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003623 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003624 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003625 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003626 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003627 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003628 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003629 }
3630 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3631 if (exit == NULL) {
3632 Py_DECREF(enter);
3633 goto error;
3634 }
Yury Selivanov75445082015-05-11 22:57:16 -04003635 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003636 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003637 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003638 Py_DECREF(enter);
3639 if (res == NULL)
3640 goto error;
3641 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003642 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003643 DISPATCH();
3644 }
3645
Benjamin Petersonddd19492018-09-16 22:38:02 -07003646 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003647 PyObject *res = POP();
3648 /* Setup the finally block before pushing the result
3649 of __aenter__ on the stack. */
3650 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3651 STACK_LEVEL());
3652 PUSH(res);
3653 DISPATCH();
3654 }
3655
Benjamin Petersonddd19492018-09-16 22:38:02 -07003656 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003657 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003658 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003659 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003660 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003661 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003662 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003663 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003664 }
3665 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003666 if (exit == NULL) {
3667 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003668 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003669 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003670 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003672 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003673 Py_DECREF(enter);
3674 if (res == NULL)
3675 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 /* Setup the finally block before pushing the result
3677 of __enter__ on the stack. */
3678 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3679 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003680
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003681 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 DISPATCH();
3683 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003684
Mark Shannonfee55262019-11-21 09:11:43 +00003685 case TARGET(WITH_EXCEPT_START): {
3686 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003688 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3689 - SEVENTH: the context.__exit__ bound method
3690 We call SEVENTH(TOP, SECOND, THIRD).
3691 Then we push again the TOP exception and the __exit__
3692 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003695 PyObject *exc, *val, *tb, *res;
3696
Victor Stinner842cfff2016-12-01 14:45:31 +01003697 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003698 val = SECOND();
3699 tb = THIRD();
3700 assert(exc != Py_None);
3701 assert(!PyLong_Check(exc));
3702 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003703 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003704 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003705 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 if (res == NULL)
3707 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003708
Yury Selivanov75445082015-05-11 22:57:16 -04003709 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003710 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003712
Benjamin Petersonddd19492018-09-16 22:38:02 -07003713 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003714 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003715 PyObject *name = GETITEM(names, oparg);
3716 PyObject *obj = TOP();
3717 PyObject *meth = NULL;
3718
3719 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3720
Yury Selivanovf2392132016-12-13 19:03:51 -05003721 if (meth == NULL) {
3722 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003723 goto error;
3724 }
3725
3726 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003727 /* We can bypass temporary bound method object.
3728 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003729
INADA Naoki015bce62017-01-16 17:23:30 +09003730 meth | self | arg1 | ... | argN
3731 */
3732 SET_TOP(meth);
3733 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003734 }
3735 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003736 /* meth is not an unbound method (but a regular attr, or
3737 something was returned by a descriptor protocol). Set
3738 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003739 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003740
3741 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003742 */
INADA Naoki015bce62017-01-16 17:23:30 +09003743 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003744 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003745 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003746 }
3747 DISPATCH();
3748 }
3749
Benjamin Petersonddd19492018-09-16 22:38:02 -07003750 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003751 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003752 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003753
3754 sp = stack_pointer;
3755
INADA Naoki015bce62017-01-16 17:23:30 +09003756 meth = PEEK(oparg + 2);
3757 if (meth == NULL) {
3758 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3759 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003760
3761 Stack layout:
3762
INADA Naoki015bce62017-01-16 17:23:30 +09003763 ... | NULL | callable | arg1 | ... | argN
3764 ^- TOP()
3765 ^- (-oparg)
3766 ^- (-oparg-1)
3767 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003768
Ville Skyttä49b27342017-08-03 09:00:59 +03003769 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003770 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003771 */
Mark Shannon86433452021-01-07 16:49:02 +00003772 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003773 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003774 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003775 }
3776 else {
3777 /* This is a method call. Stack layout:
3778
INADA Naoki015bce62017-01-16 17:23:30 +09003779 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003780 ^- TOP()
3781 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003782 ^- (-oparg-1)
3783 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003784
INADA Naoki015bce62017-01-16 17:23:30 +09003785 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003786 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003787 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003788 */
Mark Shannon86433452021-01-07 16:49:02 +00003789 res = call_function(tstate, &bounds, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003790 stack_pointer = sp;
3791 }
3792
3793 PUSH(res);
3794 if (res == NULL)
3795 goto error;
3796 DISPATCH();
3797 }
3798
Benjamin Petersonddd19492018-09-16 22:38:02 -07003799 case TARGET(CALL_FUNCTION): {
3800 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003803 res = call_function(tstate, &bounds, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003805 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003806 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003807 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003808 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003809 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003811
Benjamin Petersonddd19492018-09-16 22:38:02 -07003812 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003813 PyObject **sp, *res, *names;
3814
3815 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003816 assert(PyTuple_Check(names));
3817 assert(PyTuple_GET_SIZE(names) <= oparg);
3818 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 sp = stack_pointer;
Mark Shannon86433452021-01-07 16:49:02 +00003820 res = call_function(tstate, &bounds, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003822 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003823 Py_DECREF(names);
3824
3825 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003826 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003827 }
3828 DISPATCH();
3829 }
3830
Benjamin Petersonddd19492018-09-16 22:38:02 -07003831 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003832 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003833 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003834 if (oparg & 0x01) {
3835 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003836 if (!PyDict_CheckExact(kwargs)) {
3837 PyObject *d = PyDict_New();
3838 if (d == NULL)
3839 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003840 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003841 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003842 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003843 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003844 goto error;
3845 }
3846 Py_DECREF(kwargs);
3847 kwargs = d;
3848 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003849 assert(PyDict_CheckExact(kwargs));
3850 }
3851 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003852 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003853 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003854 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003855 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003856 goto error;
3857 }
3858 Py_SETREF(callargs, PySequence_Tuple(callargs));
3859 if (callargs == NULL) {
3860 goto error;
3861 }
3862 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003863 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003864
Mark Shannon86433452021-01-07 16:49:02 +00003865 result = do_call_core(tstate, &bounds, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003866 Py_DECREF(func);
3867 Py_DECREF(callargs);
3868 Py_XDECREF(kwargs);
3869
3870 SET_TOP(result);
3871 if (result == NULL) {
3872 goto error;
3873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003874 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003876
Benjamin Petersonddd19492018-09-16 22:38:02 -07003877 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003878 PyObject *qualname = POP();
3879 PyObject *codeobj = POP();
3880 PyFunctionObject *func = (PyFunctionObject *)
3881 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003882
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003883 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003884 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003885 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003886 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003888
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003889 if (oparg & 0x08) {
3890 assert(PyTuple_CheckExact(TOP()));
3891 func ->func_closure = POP();
3892 }
3893 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02003894 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003895 func->func_annotations = POP();
3896 }
3897 if (oparg & 0x02) {
3898 assert(PyDict_CheckExact(TOP()));
3899 func->func_kwdefaults = POP();
3900 }
3901 if (oparg & 0x01) {
3902 assert(PyTuple_CheckExact(TOP()));
3903 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003905
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003906 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003907 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003909
Benjamin Petersonddd19492018-09-16 22:38:02 -07003910 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003911 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003913 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003915 step = NULL;
3916 stop = POP();
3917 start = TOP();
3918 slice = PySlice_New(start, stop, step);
3919 Py_DECREF(start);
3920 Py_DECREF(stop);
3921 Py_XDECREF(step);
3922 SET_TOP(slice);
3923 if (slice == NULL)
3924 goto error;
3925 DISPATCH();
3926 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003927
Benjamin Petersonddd19492018-09-16 22:38:02 -07003928 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003929 /* Handles f-string value formatting. */
3930 PyObject *result;
3931 PyObject *fmt_spec;
3932 PyObject *value;
3933 PyObject *(*conv_fn)(PyObject *);
3934 int which_conversion = oparg & FVC_MASK;
3935 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3936
3937 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003938 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003939
3940 /* See if any conversion is specified. */
3941 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003942 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003943 case FVC_STR: conv_fn = PyObject_Str; break;
3944 case FVC_REPR: conv_fn = PyObject_Repr; break;
3945 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003946 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003947 _PyErr_Format(tstate, PyExc_SystemError,
3948 "unexpected conversion flag %d",
3949 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003950 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003951 }
3952
3953 /* If there's a conversion function, call it and replace
3954 value with that result. Otherwise, just use value,
3955 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003956 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003957 result = conv_fn(value);
3958 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003959 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003960 Py_XDECREF(fmt_spec);
3961 goto error;
3962 }
3963 value = result;
3964 }
3965
3966 /* If value is a unicode object, and there's no fmt_spec,
3967 then we know the result of format(value) is value
3968 itself. In that case, skip calling format(). I plan to
3969 move this optimization in to PyObject_Format()
3970 itself. */
3971 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3972 /* Do nothing, just transfer ownership to result. */
3973 result = value;
3974 } else {
3975 /* Actually call format(). */
3976 result = PyObject_Format(value, fmt_spec);
3977 Py_DECREF(value);
3978 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003979 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003980 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003981 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003982 }
3983
Eric V. Smith135d5f42016-02-05 18:23:08 -05003984 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003985 DISPATCH();
3986 }
3987
Benjamin Petersonddd19492018-09-16 22:38:02 -07003988 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003989 int oldoparg = oparg;
3990 NEXTOPARG();
3991 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003993 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003994
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003995
Antoine Pitrou042b1282010-08-13 21:15:58 +00003996#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003998#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 default:
4000 fprintf(stderr,
4001 "XXX lineno: %d, opcode: %d\n",
4002 PyFrame_GetLineNumber(f),
4003 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004004 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004005 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004009 /* This should never be reached. Every opcode should end with DISPATCH()
4010 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004011 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004012
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004013error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004014 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004015#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004016 if (!_PyErr_Occurred(tstate)) {
4017 _PyErr_SetString(tstate, PyExc_SystemError,
4018 "error return without exception set");
4019 }
Victor Stinner365b6932013-07-12 00:11:58 +02004020#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004021 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004022#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004023
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004024 /* Log traceback info. */
4025 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004026
Mark Shannoncb9879b2020-07-17 11:44:23 +01004027 if (tstate->c_tracefunc != NULL) {
4028 /* Make sure state is set to FRAME_EXECUTING for tracing */
4029 assert(f->f_state == FRAME_EXECUTING);
4030 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004031 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004032 tstate, f, &bounds);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004033 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004034exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004035 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004036 /* Unwind stacks if an exception occurred */
4037 while (f->f_iblock > 0) {
4038 /* Pop the current block. */
4039 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 if (b->b_type == EXCEPT_HANDLER) {
4042 UNWIND_EXCEPT_HANDLER(b);
4043 continue;
4044 }
4045 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004046 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 PyObject *exc, *val, *tb;
4048 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004049 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004051 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004052 PUSH(exc_info->exc_traceback);
4053 PUSH(exc_info->exc_value);
4054 if (exc_info->exc_type != NULL) {
4055 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 }
4057 else {
4058 Py_INCREF(Py_None);
4059 PUSH(Py_None);
4060 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004061 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 /* Make the raw exception data
4063 available to the handler,
4064 so a program can emulate the
4065 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004066 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004067 if (tb != NULL)
4068 PyException_SetTraceback(val, tb);
4069 else
4070 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004072 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004074 exc_info->exc_value = val;
4075 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 if (tb == NULL)
4077 tb = Py_None;
4078 Py_INCREF(tb);
4079 PUSH(tb);
4080 PUSH(val);
4081 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004083 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004084 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004085 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004086 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004087 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004088 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 }
4090 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004091
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004092 /* End the loop as we still have an error */
4093 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004095
Pablo Galindof00828a2019-05-09 16:52:02 +01004096 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004097 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 /* Pop remaining stack entries. */
4100 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004101 PyObject *o = POP();
4102 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004104 f->f_stackdepth = 0;
4105 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004106exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004108 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004109 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon86433452021-01-07 16:49:02 +00004110 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004111 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 }
4113 }
4114 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004115 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon86433452021-01-07 16:49:02 +00004116 tstate, f, &bounds, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004117 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
4119 }
4120 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004123exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004124 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4125 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004126 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004128
Victor Stinner0b72b232020-03-12 23:18:39 +01004129 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004130}
4131
Benjamin Petersonb204a422011-06-05 22:04:07 -05004132static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004133format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004134 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004135{
4136 int err;
4137 Py_ssize_t len = PyList_GET_SIZE(names);
4138 PyObject *name_str, *comma, *tail, *tmp;
4139
4140 assert(PyList_CheckExact(names));
4141 assert(len >= 1);
4142 /* Deal with the joys of natural language. */
4143 switch (len) {
4144 case 1:
4145 name_str = PyList_GET_ITEM(names, 0);
4146 Py_INCREF(name_str);
4147 break;
4148 case 2:
4149 name_str = PyUnicode_FromFormat("%U and %U",
4150 PyList_GET_ITEM(names, len - 2),
4151 PyList_GET_ITEM(names, len - 1));
4152 break;
4153 default:
4154 tail = PyUnicode_FromFormat(", %U, and %U",
4155 PyList_GET_ITEM(names, len - 2),
4156 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004157 if (tail == NULL)
4158 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004159 /* Chop off the last two objects in the list. This shouldn't actually
4160 fail, but we can't be too careful. */
4161 err = PyList_SetSlice(names, len - 2, len, NULL);
4162 if (err == -1) {
4163 Py_DECREF(tail);
4164 return;
4165 }
4166 /* Stitch everything up into a nice comma-separated list. */
4167 comma = PyUnicode_FromString(", ");
4168 if (comma == NULL) {
4169 Py_DECREF(tail);
4170 return;
4171 }
4172 tmp = PyUnicode_Join(comma, names);
4173 Py_DECREF(comma);
4174 if (tmp == NULL) {
4175 Py_DECREF(tail);
4176 return;
4177 }
4178 name_str = PyUnicode_Concat(tmp, tail);
4179 Py_DECREF(tmp);
4180 Py_DECREF(tail);
4181 break;
4182 }
4183 if (name_str == NULL)
4184 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004185 _PyErr_Format(tstate, PyExc_TypeError,
4186 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004187 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004188 len,
4189 kind,
4190 len == 1 ? "" : "s",
4191 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004192 Py_DECREF(name_str);
4193}
4194
4195static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004196missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4197 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004198 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004199{
Victor Stinner74319ae2016-08-25 00:04:09 +02004200 Py_ssize_t i, j = 0;
4201 Py_ssize_t start, end;
4202 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004203 const char *kind = positional ? "positional" : "keyword-only";
4204 PyObject *missing_names;
4205
4206 /* Compute the names of the arguments that are missing. */
4207 missing_names = PyList_New(missing);
4208 if (missing_names == NULL)
4209 return;
4210 if (positional) {
4211 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004212 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004213 }
4214 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004215 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004216 end = start + co->co_kwonlyargcount;
4217 }
4218 for (i = start; i < end; i++) {
4219 if (GETLOCAL(i) == NULL) {
4220 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4221 PyObject *name = PyObject_Repr(raw);
4222 if (name == NULL) {
4223 Py_DECREF(missing_names);
4224 return;
4225 }
4226 PyList_SET_ITEM(missing_names, j++, name);
4227 }
4228 }
4229 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004230 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004231 Py_DECREF(missing_names);
4232}
4233
4234static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004235too_many_positional(PyThreadState *tstate, PyCodeObject *co,
4236 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004237 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238{
4239 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004240 Py_ssize_t kwonly_given = 0;
4241 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004242 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004243 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004244
Benjamin Petersone109c702011-06-24 09:37:26 -05004245 assert((co->co_flags & CO_VARARGS) == 0);
4246 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004247 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004248 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004249 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004250 }
4251 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004252 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004253 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004254 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004255 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004256 }
4257 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004258 plural = (co_argcount != 1);
4259 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004260 }
4261 if (sig == NULL)
4262 return;
4263 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004264 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4265 kwonly_sig = PyUnicode_FromFormat(format,
4266 given != 1 ? "s" : "",
4267 kwonly_given,
4268 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004269 if (kwonly_sig == NULL) {
4270 Py_DECREF(sig);
4271 return;
4272 }
4273 }
4274 else {
4275 /* This will not fail. */
4276 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004277 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004278 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004279 _PyErr_Format(tstate, PyExc_TypeError,
4280 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004281 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004282 sig,
4283 plural ? "s" : "",
4284 given,
4285 kwonly_sig,
4286 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004287 Py_DECREF(sig);
4288 Py_DECREF(kwonly_sig);
4289}
4290
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004291static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004292positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004293 Py_ssize_t kwcount, PyObject* const* kwnames,
4294 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004295{
4296 int posonly_conflicts = 0;
4297 PyObject* posonly_names = PyList_New(0);
4298
4299 for(int k=0; k < co->co_posonlyargcount; k++){
4300 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4301
4302 for (int k2=0; k2<kwcount; k2++){
4303 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4304 PyObject* kwname = kwnames[k2];
4305 if (kwname == posonly_name){
4306 if(PyList_Append(posonly_names, kwname) != 0) {
4307 goto fail;
4308 }
4309 posonly_conflicts++;
4310 continue;
4311 }
4312
4313 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4314
4315 if ( cmp > 0) {
4316 if(PyList_Append(posonly_names, kwname) != 0) {
4317 goto fail;
4318 }
4319 posonly_conflicts++;
4320 } else if (cmp < 0) {
4321 goto fail;
4322 }
4323
4324 }
4325 }
4326 if (posonly_conflicts) {
4327 PyObject* comma = PyUnicode_FromString(", ");
4328 if (comma == NULL) {
4329 goto fail;
4330 }
4331 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4332 Py_DECREF(comma);
4333 if (error_names == NULL) {
4334 goto fail;
4335 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004336 _PyErr_Format(tstate, PyExc_TypeError,
4337 "%U() got some positional-only arguments passed"
4338 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004339 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004340 Py_DECREF(error_names);
4341 goto fail;
4342 }
4343
4344 Py_DECREF(posonly_names);
4345 return 0;
4346
4347fail:
4348 Py_XDECREF(posonly_names);
4349 return 1;
4350
4351}
4352
Guido van Rossumc2e20742006-02-27 22:32:47 +00004353/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004354 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004355 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004356
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004357PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004358_PyEval_EvalCode(PyThreadState *tstate,
4359 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004360 PyObject *const *args, Py_ssize_t argcount,
4361 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004362 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004363 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004364 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004365 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004366{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004367 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004368
Victor Stinner232dda62020-06-04 15:19:02 +02004369 PyCodeObject *co = (PyCodeObject*)_co;
4370
4371 if (!name) {
4372 name = co->co_name;
4373 }
4374 assert(name != NULL);
4375 assert(PyUnicode_Check(name));
4376
4377 if (!qualname) {
4378 qualname = name;
4379 }
4380 assert(qualname != NULL);
4381 assert(PyUnicode_Check(qualname));
4382
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004383 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004384 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004387 _PyErr_SetString(tstate, PyExc_SystemError,
4388 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 return NULL;
4390 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004391
Victor Stinnerc7020012016-08-16 23:40:29 +02004392 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004393 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004394 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004396 }
Victor Stinner232dda62020-06-04 15:19:02 +02004397 PyObject **fastlocals = f->f_localsplus;
4398 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004399
Victor Stinnerc7020012016-08-16 23:40:29 +02004400 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004401 PyObject *kwdict;
4402 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004403 if (co->co_flags & CO_VARKEYWORDS) {
4404 kwdict = PyDict_New();
4405 if (kwdict == NULL)
4406 goto fail;
4407 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004408 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004409 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004410 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004411 SETLOCAL(i, kwdict);
4412 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004413 else {
4414 kwdict = NULL;
4415 }
4416
Pablo Galindocd74e662019-06-01 18:08:04 +01004417 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004418 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004419 if (argcount > co->co_argcount) {
4420 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004421 }
4422 else {
4423 n = argcount;
4424 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004425 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004426 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004427 Py_INCREF(x);
4428 SETLOCAL(j, x);
4429 }
4430
Victor Stinnerc7020012016-08-16 23:40:29 +02004431 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004432 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004433 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004434 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004435 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004436 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004437 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004438 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004439
Serhiy Storchakab7281052016-09-12 00:52:40 +03004440 /* Handle keyword arguments passed as two strided arrays */
4441 kwcount *= kwstep;
4442 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004443 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004444 PyObject *keyword = kwnames[i];
4445 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004446 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004447
Benjamin Petersonb204a422011-06-05 22:04:07 -05004448 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004449 _PyErr_Format(tstate, PyExc_TypeError,
4450 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004451 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004452 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004454
Benjamin Petersonb204a422011-06-05 22:04:07 -05004455 /* Speed hack: do raw pointer compares. As names are
4456 normally interned this should almost always hit. */
4457 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004458 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004459 PyObject *varname = co_varnames[j];
4460 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004461 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004462 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004463 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004464
Benjamin Petersonb204a422011-06-05 22:04:07 -05004465 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004466 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004467 PyObject *varname = co_varnames[j];
4468 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004469 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004470 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004471 }
4472 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004474 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004475 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004476
Victor Stinner231d1f32017-01-11 02:12:06 +01004477 assert(j >= total_args);
4478 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004479
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 if (co->co_posonlyargcount
4481 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004482 kwcount, kwnames,
4483 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004484 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004485 goto fail;
4486 }
4487
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 _PyErr_Format(tstate, PyExc_TypeError,
4489 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004490 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004491 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004493
Christian Heimes0bd447f2013-07-20 14:48:10 +02004494 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4495 goto fail;
4496 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004497 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004498
Benjamin Petersonb204a422011-06-05 22:04:07 -05004499 kw_found:
4500 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_Format(tstate, PyExc_TypeError,
4502 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004503 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004504 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004506 Py_INCREF(value);
4507 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004509
4510 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004511 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004512 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4513 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 goto fail;
4515 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004516
4517 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004518 if (argcount < co->co_argcount) {
4519 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004520 Py_ssize_t missing = 0;
4521 for (i = argcount; i < m; i++) {
4522 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004523 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004524 }
4525 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004526 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004527 missing_arguments(tstate, co, missing, defcount, fastlocals,
4528 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004529 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004530 }
4531 if (n > m)
4532 i = n - m;
4533 else
4534 i = 0;
4535 for (; i < defcount; i++) {
4536 if (GETLOCAL(m+i) == NULL) {
4537 PyObject *def = defs[i];
4538 Py_INCREF(def);
4539 SETLOCAL(m+i, def);
4540 }
4541 }
4542 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004543
4544 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004545 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004546 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004547 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004548 if (GETLOCAL(i) != NULL)
4549 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004550 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004551 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004552 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004553 if (def) {
4554 Py_INCREF(def);
4555 SETLOCAL(i, def);
4556 continue;
4557 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004558 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004559 goto fail;
4560 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004561 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004562 missing++;
4563 }
4564 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004565 missing_arguments(tstate, co, missing, -1, fastlocals,
4566 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004567 goto fail;
4568 }
4569 }
4570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004572 vars into frame. */
4573 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004575 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004576 /* Possibly account for the cell variable being an argument. */
4577 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004578 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004579 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004580 /* Clear the local copy. */
4581 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004582 }
4583 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004584 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004585 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004586 if (c == NULL)
4587 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004588 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004590
4591 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004592 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4593 PyObject *o = PyTuple_GET_ITEM(closure, i);
4594 Py_INCREF(o);
4595 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004597
Yury Selivanoveb636452016-09-08 22:01:51 -07004598 /* Handle generator/coroutine/asynchronous generator */
4599 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004600 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004601 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* Don't need to keep the reference to f_back, it will be set
4604 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004605 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 /* Create a new generator that owns the ready to run frame
4608 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004609 if (is_coro) {
4610 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004611 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4612 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004613 } else {
4614 gen = PyGen_NewWithQualName(f, name, qualname);
4615 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004616 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004617 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004618 }
INADA Naoki9c157762016-12-26 18:52:46 +09004619
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004620 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004621
Yury Selivanov75445082015-05-11 22:57:16 -04004622 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004624
Victor Stinnerb9e68122019-11-14 12:20:46 +01004625 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004626
Thomas Woutersce272b62007-09-19 21:19:28 +00004627fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 /* decref'ing the frame can cause __del__ methods to get invoked,
4630 which can call back into Python. While we're done with the
4631 current Python frame (f), the associated C stack is still in use,
4632 so recursion_depth must be boosted for the duration.
4633 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004634 if (Py_REFCNT(f) > 1) {
4635 Py_DECREF(f);
4636 _PyObject_GC_TRACK(f);
4637 }
4638 else {
4639 ++tstate->recursion_depth;
4640 Py_DECREF(f);
4641 --tstate->recursion_depth;
4642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004644}
4645
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004646
4647PyObject *
4648_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4649 PyObject *const *args, Py_ssize_t argcount,
4650 PyObject *const *kwnames, PyObject *const *kwargs,
4651 Py_ssize_t kwcount, int kwstep,
4652 PyObject *const *defs, Py_ssize_t defcount,
4653 PyObject *kwdefs, PyObject *closure,
4654 PyObject *name, PyObject *qualname)
4655{
4656 PyThreadState *tstate = _PyThreadState_GET();
4657 return _PyEval_EvalCode(tstate, _co, globals, locals,
4658 args, argcount,
4659 kwnames, kwargs,
4660 kwcount, kwstep,
4661 defs, defcount,
4662 kwdefs, closure,
4663 name, qualname);
4664}
4665
Victor Stinner40ee3012014-06-16 15:59:28 +02004666PyObject *
4667PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004668 PyObject *const *args, int argcount,
4669 PyObject *const *kws, int kwcount,
4670 PyObject *const *defs, int defcount,
4671 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004672{
4673 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004674 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004675 kws, kws != NULL ? kws + 1 : NULL,
4676 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004677 defs, defcount,
4678 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004679 NULL, NULL);
4680}
Tim Peters5ca576e2001-06-18 22:08:13 +00004681
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004682static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004683special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004686 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004687 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004688 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 return NULL;
4690 }
4691 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004692}
4693
4694
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004695/* Logic for the raise statement (too complicated for inlining).
4696 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004697static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004698do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 if (exc == NULL) {
4703 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004704 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004706 type = exc_info->exc_type;
4707 value = exc_info->exc_value;
4708 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004709 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004710 _PyErr_SetString(tstate, PyExc_RuntimeError,
4711 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004712 return 0;
4713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 Py_XINCREF(type);
4715 Py_XINCREF(value);
4716 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004717 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004718 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 /* We support the following forms of raise:
4722 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004723 raise <instance>
4724 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 if (PyExceptionClass_Check(exc)) {
4727 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004728 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 if (value == NULL)
4730 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004731 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004732 _PyErr_Format(tstate, PyExc_TypeError,
4733 "calling %R should have returned an instance of "
4734 "BaseException, not %R",
4735 type, Py_TYPE(value));
4736 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 }
4739 else if (PyExceptionInstance_Check(exc)) {
4740 value = exc;
4741 type = PyExceptionInstance_Class(exc);
4742 Py_INCREF(type);
4743 }
4744 else {
4745 /* Not something you can raise. You get an exception
4746 anyway, just not what you specified :-) */
4747 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004748 _PyErr_SetString(tstate, PyExc_TypeError,
4749 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 goto raise_error;
4751 }
Collin Winter828f04a2007-08-31 00:04:24 +00004752
Serhiy Storchakac0191582016-09-27 11:37:10 +03004753 assert(type != NULL);
4754 assert(value != NULL);
4755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 if (cause) {
4757 PyObject *fixed_cause;
4758 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004759 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 if (fixed_cause == NULL)
4761 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004762 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004764 else if (PyExceptionInstance_Check(cause)) {
4765 fixed_cause = cause;
4766 }
4767 else if (cause == Py_None) {
4768 Py_DECREF(cause);
4769 fixed_cause = NULL;
4770 }
4771 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004772 _PyErr_SetString(tstate, PyExc_TypeError,
4773 "exception causes must derive from "
4774 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 goto raise_error;
4776 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004777 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 }
Collin Winter828f04a2007-08-31 00:04:24 +00004779
Victor Stinner438a12d2019-05-24 17:01:38 +02004780 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004781 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004782 Py_DECREF(value);
4783 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004784 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004785
4786raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 Py_XDECREF(value);
4788 Py_XDECREF(type);
4789 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004790 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004791}
4792
Tim Petersd6d010b2001-06-21 02:49:55 +00004793/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004794 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004795
Guido van Rossum0368b722007-05-11 16:50:42 +00004796 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4797 with a variable target.
4798*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004799
Barry Warsawe42b18f1997-08-25 22:13:04 +00004800static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004801unpack_iterable(PyThreadState *tstate, PyObject *v,
4802 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 int i = 0, j = 0;
4805 Py_ssize_t ll = 0;
4806 PyObject *it; /* iter(v) */
4807 PyObject *w;
4808 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004813 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004814 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004815 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004816 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004817 _PyErr_Format(tstate, PyExc_TypeError,
4818 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004819 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004820 }
4821 return 0;
4822 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 for (; i < argcnt; i++) {
4825 w = PyIter_Next(it);
4826 if (w == NULL) {
4827 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004828 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004829 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004830 _PyErr_Format(tstate, PyExc_ValueError,
4831 "not enough values to unpack "
4832 "(expected %d, got %d)",
4833 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004834 }
4835 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004836 _PyErr_Format(tstate, PyExc_ValueError,
4837 "not enough values to unpack "
4838 "(expected at least %d, got %d)",
4839 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 }
4842 goto Error;
4843 }
4844 *--sp = w;
4845 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 if (argcntafter == -1) {
4848 /* We better have exhausted the iterator now. */
4849 w = PyIter_Next(it);
4850 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004851 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 goto Error;
4853 Py_DECREF(it);
4854 return 1;
4855 }
4856 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004857 _PyErr_Format(tstate, PyExc_ValueError,
4858 "too many values to unpack (expected %d)",
4859 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 goto Error;
4861 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 l = PySequence_List(it);
4864 if (l == NULL)
4865 goto Error;
4866 *--sp = l;
4867 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 ll = PyList_GET_SIZE(l);
4870 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004871 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004872 "not enough values to unpack (expected at least %d, got %zd)",
4873 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 goto Error;
4875 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 /* Pop the "after-variable" args off the list. */
4878 for (j = argcntafter; j > 0; j--, i++) {
4879 *--sp = PyList_GET_ITEM(l, ll - j);
4880 }
4881 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004882 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 Py_DECREF(it);
4884 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004885
Tim Petersd6d010b2001-06-21 02:49:55 +00004886Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 for (; i > 0; i--, sp++)
4888 Py_DECREF(*sp);
4889 Py_XDECREF(it);
4890 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004891}
4892
4893
Guido van Rossum96a42c81992-01-12 02:29:51 +00004894#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004895static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004896prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004899 if (PyObject_Print(v, stdout, 0) != 0) {
4900 /* Don't know what else to do */
4901 _PyErr_Clear(tstate);
4902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 printf("\n");
4904 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004905}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004906#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004907
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004908static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004909call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00004910 PyThreadState *tstate,
4911 PyFrameObject *f,
4912 PyCodeAddressRange *bounds)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004913{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004914 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004916 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 if (value == NULL) {
4918 value = Py_None;
4919 Py_INCREF(value);
4920 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004921 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004922 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 arg = PyTuple_Pack(3, type, value, traceback);
4924 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004925 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 return;
4927 }
Mark Shannon86433452021-01-07 16:49:02 +00004928 err = call_trace(func, self, tstate, f, bounds, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004930 if (err == 0) {
4931 _PyErr_Restore(tstate, type, value, orig_traceback);
4932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 else {
4934 Py_XDECREF(type);
4935 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004936 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004938}
4939
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004940static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004941call_trace_protected(Py_tracefunc func, PyObject *obj,
4942 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00004943 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 PyObject *type, *value, *traceback;
4947 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004948 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon86433452021-01-07 16:49:02 +00004949 err = call_trace(func, obj, tstate, frame, bounds, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 if (err == 0)
4951 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004952 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 return 0;
4954 }
4955 else {
4956 Py_XDECREF(type);
4957 Py_XDECREF(value);
4958 Py_XDECREF(traceback);
4959 return -1;
4960 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004961}
4962
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004963static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004964call_trace(Py_tracefunc func, PyObject *obj,
4965 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon86433452021-01-07 16:49:02 +00004966 PyCodeAddressRange *bounds,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004967 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 int result;
4970 if (tstate->tracing)
4971 return 0;
4972 tstate->tracing++;
4973 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00004974 if (frame->f_lasti < 0) {
4975 frame->f_lineno = frame->f_code->co_firstlineno;
4976 }
4977 else {
4978 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
4979 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00004981 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4983 || (tstate->c_profilefunc != NULL));
4984 tstate->tracing--;
4985 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004986}
4987
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004988PyObject *
4989_PyEval_CallTracing(PyObject *func, PyObject *args)
4990{
Victor Stinner50b48572018-11-01 01:51:40 +01004991 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 int save_tracing = tstate->tracing;
4993 int save_use_tracing = tstate->use_tracing;
4994 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 tstate->tracing = 0;
4997 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4998 || (tstate->c_profilefunc != NULL));
4999 result = PyObject_Call(func, args, NULL);
5000 tstate->tracing = save_tracing;
5001 tstate->use_tracing = save_use_tracing;
5002 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005003}
5004
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005005/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005006static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005007maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005008 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005009 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005012
Nick Coghlan5a851672017-09-08 10:14:16 +10005013 /* If the last instruction falls at the start of a line or if it
5014 represents a jump backwards, update the frame's line number and
5015 then call the trace function if we're tracing source lines.
5016 */
Mark Shannonee9f98d2021-01-05 12:04:10 +00005017 int lastline = bounds->ar_line;
5018 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
5019 if (line != -1 && frame->f_trace_lines) {
5020 /* Trace backward edges or first instruction of a new line */
5021 if (frame->f_lasti < *instr_prev ||
5022 (line != lastline && frame->f_lasti == bounds->ar_start))
5023 {
Mark Shannon86433452021-01-07 16:49:02 +00005024 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005025 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 }
George King20faa682017-10-18 17:44:22 -07005027 /* Always emit an opcode event if we're tracing all opcodes. */
5028 if (frame->f_trace_opcodes) {
Mark Shannon86433452021-01-07 16:49:02 +00005029 result = call_trace(func, obj, tstate, frame, bounds, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005030 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 *instr_prev = frame->f_lasti;
5032 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005033}
5034
Victor Stinner309d7cc2020-03-13 16:39:12 +01005035int
5036_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5037{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005038 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005039 /* The caller must hold the GIL */
5040 assert(PyGILState_Check());
5041
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005042 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005043 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005044 PyThreadState *current_tstate = _PyThreadState_GET();
5045 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005046 return -1;
5047 }
5048
5049 PyObject *profileobj = tstate->c_profileobj;
5050
5051 tstate->c_profilefunc = NULL;
5052 tstate->c_profileobj = NULL;
5053 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5054 tstate->use_tracing = tstate->c_tracefunc != NULL;
5055 Py_XDECREF(profileobj);
5056
5057 Py_XINCREF(arg);
5058 tstate->c_profileobj = arg;
5059 tstate->c_profilefunc = func;
5060
5061 /* Flag that tracing or profiling is turned on */
5062 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5063 return 0;
5064}
5065
Fred Drake5755ce62001-06-27 19:19:46 +00005066void
5067PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005068{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005069 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005070 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005071 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005072 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5073 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005074}
5075
5076int
5077_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5078{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005079 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005080 /* The caller must hold the GIL */
5081 assert(PyGILState_Check());
5082
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005083 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005084 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005085 PyThreadState *current_tstate = _PyThreadState_GET();
5086 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005087 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005088 }
5089
Victor Stinnerda2914d2020-03-20 09:29:08 +01005090 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005091 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005092 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005093
5094 tstate->c_tracefunc = NULL;
5095 tstate->c_traceobj = NULL;
5096 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5097 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5098 Py_XDECREF(traceobj);
5099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005101 tstate->c_traceobj = arg;
5102 tstate->c_tracefunc = func;
5103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005105 tstate->use_tracing = ((func != NULL)
5106 || (tstate->c_profilefunc != NULL));
5107
5108 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005109}
5110
5111void
5112PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5113{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005114 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005115 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005116 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005117 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5118 }
Fred Draked0838392001-06-16 21:02:31 +00005119}
5120
Victor Stinner309d7cc2020-03-13 16:39:12 +01005121
Yury Selivanov75445082015-05-11 22:57:16 -04005122void
Victor Stinner838f2642019-06-13 22:41:23 +02005123_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005124{
5125 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005126 tstate->coroutine_origin_tracking_depth = new_depth;
5127}
5128
5129int
5130_PyEval_GetCoroutineOriginTrackingDepth(void)
5131{
Victor Stinner50b48572018-11-01 01:51:40 +01005132 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005133 return tstate->coroutine_origin_tracking_depth;
5134}
5135
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005136int
Yury Selivanoveb636452016-09-08 22:01:51 -07005137_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5138{
Victor Stinner50b48572018-11-01 01:51:40 +01005139 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005140
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005141 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005142 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005143 }
5144
Yury Selivanoveb636452016-09-08 22:01:51 -07005145 Py_XINCREF(firstiter);
5146 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005147 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005148}
5149
5150PyObject *
5151_PyEval_GetAsyncGenFirstiter(void)
5152{
Victor Stinner50b48572018-11-01 01:51:40 +01005153 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005154 return tstate->async_gen_firstiter;
5155}
5156
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005157int
Yury Selivanoveb636452016-09-08 22:01:51 -07005158_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5159{
Victor Stinner50b48572018-11-01 01:51:40 +01005160 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005161
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005162 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005163 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005164 }
5165
Yury Selivanoveb636452016-09-08 22:01:51 -07005166 Py_XINCREF(finalizer);
5167 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005168 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005169}
5170
5171PyObject *
5172_PyEval_GetAsyncGenFinalizer(void)
5173{
Victor Stinner50b48572018-11-01 01:51:40 +01005174 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005175 return tstate->async_gen_finalizer;
5176}
5177
Victor Stinner438a12d2019-05-24 17:01:38 +02005178PyFrameObject *
5179PyEval_GetFrame(void)
5180{
5181 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005182 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005183}
5184
Guido van Rossumb209a111997-04-29 18:18:01 +00005185PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005186PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005187{
Victor Stinner438a12d2019-05-24 17:01:38 +02005188 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005189 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005191 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 else
5193 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005194}
5195
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005196/* Convenience function to get a builtin from its name */
5197PyObject *
5198_PyEval_GetBuiltinId(_Py_Identifier *name)
5199{
Victor Stinner438a12d2019-05-24 17:01:38 +02005200 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005201 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5202 if (attr) {
5203 Py_INCREF(attr);
5204 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005205 else if (!_PyErr_Occurred(tstate)) {
5206 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005207 }
5208 return attr;
5209}
5210
Guido van Rossumb209a111997-04-29 18:18:01 +00005211PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005212PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005213{
Victor Stinner438a12d2019-05-24 17:01:38 +02005214 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005215 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005216 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005217 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005219 }
5220
Victor Stinner438a12d2019-05-24 17:01:38 +02005221 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005222 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005223 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005224
5225 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005226 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005227}
5228
Guido van Rossumb209a111997-04-29 18:18:01 +00005229PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005230PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005231{
Victor Stinner438a12d2019-05-24 17:01:38 +02005232 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005233 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005234 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005235 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005236 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005237
5238 assert(current_frame->f_globals != NULL);
5239 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005240}
5241
Guido van Rossum6135a871995-01-09 17:53:26 +00005242int
Tim Peters5ba58662001-07-16 02:29:45 +00005243PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005244{
Victor Stinner438a12d2019-05-24 17:01:38 +02005245 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005246 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 if (current_frame != NULL) {
5250 const int codeflags = current_frame->f_code->co_flags;
5251 const int compilerflags = codeflags & PyCF_MASK;
5252 if (compilerflags) {
5253 result = 1;
5254 cf->cf_flags |= compilerflags;
5255 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005256#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 if (codeflags & CO_GENERATOR_ALLOWED) {
5258 result = 1;
5259 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5260 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 }
5263 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005264}
5265
Guido van Rossum3f5da241990-12-20 15:06:42 +00005266
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005267const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005268PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 if (PyMethod_Check(func))
5271 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5272 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005273 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 else if (PyCFunction_Check(func))
5275 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5276 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005277 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005278}
5279
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005280const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005281PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 if (PyMethod_Check(func))
5284 return "()";
5285 else if (PyFunction_Check(func))
5286 return "()";
5287 else if (PyCFunction_Check(func))
5288 return "()";
5289 else
5290 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005291}
5292
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005293#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005294if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005295 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005296 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005297 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 x = NULL; \
5299 } \
5300 else { \
5301 x = call; \
5302 if (tstate->c_profilefunc != NULL) { \
5303 if (x == NULL) { \
5304 call_trace_protected(tstate->c_profilefunc, \
5305 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005306 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005307 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 /* XXX should pass (type, value, tb) */ \
5309 } else { \
5310 if (call_trace(tstate->c_profilefunc, \
5311 tstate->c_profileobj, \
Mark Shannon86433452021-01-07 16:49:02 +00005312 tstate, tstate->frame, bounds, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005313 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 Py_DECREF(x); \
5315 x = NULL; \
5316 } \
5317 } \
5318 } \
5319 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005320} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 x = call; \
5322 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005323
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005324
5325static PyObject *
5326trace_call_function(PyThreadState *tstate,
Mark Shannon86433452021-01-07 16:49:02 +00005327 PyCodeAddressRange *bounds,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005328 PyObject *func,
5329 PyObject **args, Py_ssize_t nargs,
5330 PyObject *kwnames)
5331{
5332 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005333 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005334 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005335 return x;
5336 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005337 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005338 /* We need to create a temporary bound method as argument
5339 for profiling.
5340
5341 If nargs == 0, then this cannot work because we have no
5342 "self". In any case, the call itself would raise
5343 TypeError (foo needs an argument), so we just skip
5344 profiling. */
5345 PyObject *self = args[0];
5346 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5347 if (func == NULL) {
5348 return NULL;
5349 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005350 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005351 args+1, nargs-1,
5352 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005353 Py_DECREF(func);
5354 return x;
5355 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005356 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005357}
5358
Victor Stinner415c5102017-01-11 00:54:57 +01005359/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5360 to reduce the stack consumption. */
5361Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005362call_function(PyThreadState *tstate,
5363 PyCodeAddressRange *bounds,
5364 PyObject ***pp_stack,
5365 Py_ssize_t oparg,
5366 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005367{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005368 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 PyObject *func = *pfunc;
5370 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005371 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5372 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005373 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005374
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005375 if (tstate->use_tracing) {
Mark Shannon86433452021-01-07 16:49:02 +00005376 x = trace_call_function(tstate, bounds, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005377 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005378 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005379 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005381
Victor Stinner438a12d2019-05-24 17:01:38 +02005382 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005383
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005384 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 while ((*pp_stack) > pfunc) {
5386 w = EXT_POP(*pp_stack);
5387 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005391}
5392
Jeremy Hylton52820442001-01-03 23:52:36 +00005393static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005394do_call_core(PyThreadState *tstate,
5395 PyCodeAddressRange *bounds,
5396 PyObject *func,
5397 PyObject *callargs,
5398 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005399{
jdemeyere89de732018-09-19 12:06:20 +02005400 PyObject *result;
5401
scoder4c9ea092020-05-12 16:12:41 +02005402 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005403 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005404 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005406 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005407 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5408 if (nargs > 0 && tstate->use_tracing) {
5409 /* We need to create a temporary bound method as argument
5410 for profiling.
5411
5412 If nargs == 0, then this cannot work because we have no
5413 "self". In any case, the call itself would raise
5414 TypeError (foo needs an argument), so we just skip
5415 profiling. */
5416 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5417 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5418 if (func == NULL) {
5419 return NULL;
5420 }
5421
Victor Stinner4d231bc2019-11-14 13:36:21 +01005422 C_TRACE(result, _PyObject_FastCallDictTstate(
5423 tstate, func,
5424 &_PyTuple_ITEMS(callargs)[1],
5425 nargs - 1,
5426 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005427 Py_DECREF(func);
5428 return result;
5429 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005430 }
jdemeyere89de732018-09-19 12:06:20 +02005431 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005432}
5433
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005434/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005435 nb_index slot defined, and store in *pi.
5436 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005437 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005438 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005439*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005440int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005441_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005442{
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005444 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005446 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 return 0;
5450 }
5451 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005452 _PyErr_SetString(tstate, PyExc_TypeError,
5453 "slice indices must be integers or "
5454 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 return 0;
5456 }
5457 *pi = x;
5458 }
5459 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005460}
5461
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005462int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005463_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005464{
Victor Stinner438a12d2019-05-24 17:01:38 +02005465 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005466 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005467 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005468 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005469 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005470 return 0;
5471 }
5472 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005473 _PyErr_SetString(tstate, PyExc_TypeError,
5474 "slice indices must be integers or "
5475 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005476 return 0;
5477 }
5478 *pi = x;
5479 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005480}
5481
Thomas Wouters52152252000-08-17 22:55:00 +00005482static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005483import_name(PyThreadState *tstate, PyFrameObject *f,
5484 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005485{
5486 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005487 PyObject *import_func, *res;
5488 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005489
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005490 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005491 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005492 if (!_PyErr_Occurred(tstate)) {
5493 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005494 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005495 return NULL;
5496 }
5497
5498 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005499 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005500 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005501 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005502 return NULL;
5503 }
5504 res = PyImport_ImportModuleLevelObject(
5505 name,
5506 f->f_globals,
5507 f->f_locals == NULL ? Py_None : f->f_locals,
5508 fromlist,
5509 ilevel);
5510 return res;
5511 }
5512
5513 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005514
5515 stack[0] = name;
5516 stack[1] = f->f_globals;
5517 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5518 stack[3] = fromlist;
5519 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005520 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005521 Py_DECREF(import_func);
5522 return res;
5523}
5524
5525static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005526import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005529 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005530
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005531 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005532 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005533 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005534 /* Issue #17636: in case this failed because of a circular relative
5535 import, try to fallback on reading the module directly from
5536 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005537 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005538 if (pkgname == NULL) {
5539 goto error;
5540 }
Oren Milman6db70332017-09-19 14:23:01 +03005541 if (!PyUnicode_Check(pkgname)) {
5542 Py_CLEAR(pkgname);
5543 goto error;
5544 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005545 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005546 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005547 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005548 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005549 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005550 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005551 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005552 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005553 goto error;
5554 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005555 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005557 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005558 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005559 if (pkgname == NULL) {
5560 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5561 if (pkgname_or_unknown == NULL) {
5562 Py_XDECREF(pkgpath);
5563 return NULL;
5564 }
5565 } else {
5566 pkgname_or_unknown = pkgname;
5567 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005568
5569 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005570 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005571 errmsg = PyUnicode_FromFormat(
5572 "cannot import name %R from %R (unknown location)",
5573 name, pkgname_or_unknown
5574 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005575 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005576 PyErr_SetImportError(errmsg, pkgname, NULL);
5577 }
5578 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005579 _Py_IDENTIFIER(__spec__);
5580 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005581 const char *fmt =
5582 _PyModuleSpec_IsInitializing(spec) ?
5583 "cannot import name %R from partially initialized module %R "
5584 "(most likely due to a circular import) (%S)" :
5585 "cannot import name %R from %R (%S)";
5586 Py_XDECREF(spec);
5587
5588 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005589 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005590 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005591 }
5592
Xiang Zhang4830f582017-03-21 11:13:42 +08005593 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005594 Py_XDECREF(pkgname_or_unknown);
5595 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005596 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005597}
Guido van Rossumac7be682001-01-17 15:42:30 +00005598
Thomas Wouters52152252000-08-17 22:55:00 +00005599static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005600import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005601{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005602 _Py_IDENTIFIER(__all__);
5603 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005604 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 int skip_leading_underscores = 0;
5606 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005607
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005608 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5609 return -1; /* Unexpected error */
5610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005611 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005612 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5613 return -1;
5614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005615 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005616 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005617 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005618 return -1;
5619 }
5620 all = PyMapping_Keys(dict);
5621 Py_DECREF(dict);
5622 if (all == NULL)
5623 return -1;
5624 skip_leading_underscores = 1;
5625 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 for (pos = 0, err = 0; ; pos++) {
5628 name = PySequence_GetItem(all, pos);
5629 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005630 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005631 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005632 }
5633 else {
5634 _PyErr_Clear(tstate);
5635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005636 break;
5637 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005638 if (!PyUnicode_Check(name)) {
5639 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5640 if (modname == NULL) {
5641 Py_DECREF(name);
5642 err = -1;
5643 break;
5644 }
5645 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005646 _PyErr_Format(tstate, PyExc_TypeError,
5647 "module __name__ must be a string, not %.100s",
5648 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005649 }
5650 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005651 _PyErr_Format(tstate, PyExc_TypeError,
5652 "%s in %U.%s must be str, not %.100s",
5653 skip_leading_underscores ? "Key" : "Item",
5654 modname,
5655 skip_leading_underscores ? "__dict__" : "__all__",
5656 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005657 }
5658 Py_DECREF(modname);
5659 Py_DECREF(name);
5660 err = -1;
5661 break;
5662 }
5663 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005664 if (PyUnicode_READY(name) == -1) {
5665 Py_DECREF(name);
5666 err = -1;
5667 break;
5668 }
5669 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5670 Py_DECREF(name);
5671 continue;
5672 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005673 }
5674 value = PyObject_GetAttr(v, name);
5675 if (value == NULL)
5676 err = -1;
5677 else if (PyDict_CheckExact(locals))
5678 err = PyDict_SetItem(locals, name, value);
5679 else
5680 err = PyObject_SetItem(locals, name, value);
5681 Py_DECREF(name);
5682 Py_XDECREF(value);
5683 if (err != 0)
5684 break;
5685 }
5686 Py_DECREF(all);
5687 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005688}
5689
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005690static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005691check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005692{
Victor Stinnera102ed72020-02-07 02:24:48 +01005693 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005694 /* check_args_iterable() may be called with a live exception:
5695 * clear it to prevent calling _PyObject_FunctionStr() with an
5696 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005697 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005698 PyObject *funcstr = _PyObject_FunctionStr(func);
5699 if (funcstr != NULL) {
5700 _PyErr_Format(tstate, PyExc_TypeError,
5701 "%U argument after * must be an iterable, not %.200s",
5702 funcstr, Py_TYPE(args)->tp_name);
5703 Py_DECREF(funcstr);
5704 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005705 return -1;
5706 }
5707 return 0;
5708}
5709
5710static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005711format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005712{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005713 /* _PyDict_MergeEx raises attribute
5714 * error (percolated from an attempt
5715 * to get 'keys' attribute) instead of
5716 * a type error if its second argument
5717 * is not a mapping.
5718 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005719 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005720 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005721 PyObject *funcstr = _PyObject_FunctionStr(func);
5722 if (funcstr != NULL) {
5723 _PyErr_Format(
5724 tstate, PyExc_TypeError,
5725 "%U argument after ** must be a mapping, not %.200s",
5726 funcstr, Py_TYPE(kwargs)->tp_name);
5727 Py_DECREF(funcstr);
5728 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005729 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005730 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005731 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005732 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005733 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005734 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005735 PyObject *funcstr = _PyObject_FunctionStr(func);
5736 if (funcstr != NULL) {
5737 PyObject *key = PyTuple_GET_ITEM(val, 0);
5738 _PyErr_Format(
5739 tstate, PyExc_TypeError,
5740 "%U got multiple values for keyword argument '%S'",
5741 funcstr, key);
5742 Py_DECREF(funcstr);
5743 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005744 Py_XDECREF(exc);
5745 Py_XDECREF(val);
5746 Py_XDECREF(tb);
5747 }
5748 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005749 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005750 }
5751 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005752}
5753
Guido van Rossumac7be682001-01-17 15:42:30 +00005754static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005755format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5756 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 if (!obj)
5761 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005762
Serhiy Storchaka06515832016-11-20 09:13:07 +02005763 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005764 if (!obj_str)
5765 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005766
Victor Stinner438a12d2019-05-24 17:01:38 +02005767 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005768}
Guido van Rossum950361c1997-01-24 13:49:28 +00005769
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005770static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005771format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005772{
5773 PyObject *name;
5774 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005776 return;
5777 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5778 name = PyTuple_GET_ITEM(co->co_cellvars,
5779 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005780 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005781 PyExc_UnboundLocalError,
5782 UNBOUNDLOCAL_ERROR_MSG,
5783 name);
5784 } else {
5785 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5786 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005787 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005788 UNBOUNDFREE_ERROR_MSG, name);
5789 }
5790}
5791
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005792static void
Mark Shannonfee55262019-11-21 09:11:43 +00005793format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005794{
5795 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5796 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005797 _PyErr_Format(tstate, PyExc_TypeError,
5798 "'async with' received an object from __aenter__ "
5799 "that does not implement __await__: %.100s",
5800 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005801 }
Mark Shannonfee55262019-11-21 09:11:43 +00005802 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005803 _PyErr_Format(tstate, PyExc_TypeError,
5804 "'async with' received an object from __aexit__ "
5805 "that does not implement __await__: %.100s",
5806 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005807 }
5808 }
5809}
5810
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005811static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005812unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005813 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005814{
5815 PyObject *res;
5816 if (Py_REFCNT(v) == 2) {
5817 /* In the common case, there are 2 references to the value
5818 * stored in 'variable' when the += is performed: one on the
5819 * value stack (in 'v') and one still stored in the
5820 * 'variable'. We try to delete the variable now to reduce
5821 * the refcnt to 1.
5822 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005823 int opcode, oparg;
5824 NEXTOPARG();
5825 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005826 case STORE_FAST:
5827 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005828 PyObject **fastlocals = f->f_localsplus;
5829 if (GETLOCAL(oparg) == v)
5830 SETLOCAL(oparg, NULL);
5831 break;
5832 }
5833 case STORE_DEREF:
5834 {
5835 PyObject **freevars = (f->f_localsplus +
5836 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005837 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005838 if (PyCell_GET(c) == v) {
5839 PyCell_SET(c, NULL);
5840 Py_DECREF(v);
5841 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005842 break;
5843 }
5844 case STORE_NAME:
5845 {
5846 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005847 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005848 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005849 if (locals && PyDict_CheckExact(locals)) {
5850 PyObject *w = PyDict_GetItemWithError(locals, name);
5851 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005852 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005853 {
5854 Py_DECREF(v);
5855 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005856 }
5857 }
5858 break;
5859 }
5860 }
5861 }
5862 res = v;
5863 PyUnicode_Append(&res, w);
5864 return res;
5865}
5866
Guido van Rossum950361c1997-01-24 13:49:28 +00005867#ifdef DYNAMIC_EXECUTION_PROFILE
5868
Skip Montanarof118cb12001-10-15 20:51:38 +00005869static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005870getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005872 int i;
5873 PyObject *l = PyList_New(256);
5874 if (l == NULL) return NULL;
5875 for (i = 0; i < 256; i++) {
5876 PyObject *x = PyLong_FromLong(a[i]);
5877 if (x == NULL) {
5878 Py_DECREF(l);
5879 return NULL;
5880 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005881 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005882 }
5883 for (i = 0; i < 256; i++)
5884 a[i] = 0;
5885 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005886}
5887
5888PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005889_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005890{
5891#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005892 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005893#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005894 int i;
5895 PyObject *l = PyList_New(257);
5896 if (l == NULL) return NULL;
5897 for (i = 0; i < 257; i++) {
5898 PyObject *x = getarray(dxpairs[i]);
5899 if (x == NULL) {
5900 Py_DECREF(l);
5901 return NULL;
5902 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005903 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005904 }
5905 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005906#endif
5907}
5908
5909#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005910
5911Py_ssize_t
5912_PyEval_RequestCodeExtraIndex(freefunc free)
5913{
Victor Stinner81a7be32020-04-14 15:14:01 +02005914 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005915 Py_ssize_t new_index;
5916
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005917 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005918 return -1;
5919 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005920 new_index = interp->co_extra_user_count++;
5921 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005922 return new_index;
5923}
Łukasz Langaa785c872016-09-09 17:37:37 -07005924
5925static void
5926dtrace_function_entry(PyFrameObject *f)
5927{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005928 const char *filename;
5929 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005930 int lineno;
5931
Victor Stinner6d86a232020-04-29 00:56:58 +02005932 PyCodeObject *code = f->f_code;
5933 filename = PyUnicode_AsUTF8(code->co_filename);
5934 funcname = PyUnicode_AsUTF8(code->co_name);
5935 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005936
Andy Lestere6be9b52020-02-11 20:28:35 -06005937 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005938}
5939
5940static void
5941dtrace_function_return(PyFrameObject *f)
5942{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005943 const char *filename;
5944 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005945 int lineno;
5946
Victor Stinner6d86a232020-04-29 00:56:58 +02005947 PyCodeObject *code = f->f_code;
5948 filename = PyUnicode_AsUTF8(code->co_filename);
5949 funcname = PyUnicode_AsUTF8(code->co_name);
5950 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005951
Andy Lestere6be9b52020-02-11 20:28:35 -06005952 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005953}
5954
5955/* DTrace equivalent of maybe_call_line_trace. */
5956static void
5957maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005958 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07005959{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005960 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005961
5962 /* If the last instruction executed isn't in the current
5963 instruction window, reset the window.
5964 */
Mark Shannon877df852020-11-12 09:43:29 +00005965 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07005966 /* If the last instruction falls at the start of a line or if
5967 it represents a jump backwards, update the frame's line
5968 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00005969 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
5970 if (line != -1) {
5971 frame->f_lineno = line;
5972 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5973 if (!co_filename)
5974 co_filename = "?";
5975 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5976 if (!co_name)
5977 co_name = "?";
5978 PyDTrace_LINE(co_filename, co_name, line);
5979 }
Łukasz Langaa785c872016-09-09 17:37:37 -07005980 }
5981 *instr_prev = frame->f_lasti;
5982}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005983
5984
5985/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5986 for the limited API. */
5987
5988#undef Py_EnterRecursiveCall
5989
5990int Py_EnterRecursiveCall(const char *where)
5991{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005992 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005993}
5994
5995#undef Py_LeaveRecursiveCall
5996
5997void Py_LeaveRecursiveCall(void)
5998{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005999 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006000}