blob: 3d65e161302a950cac62586f89eca1d7fbd2e216 [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(
49 PyThreadState *tstate, PyObject ***pp_stack,
50 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
52 PyThreadState *tstate, PyObject *func,
53 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 *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000062static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010063 PyThreadState *, PyFrameObject *,
64 int, PyObject *);
65static void call_exc_trace(Py_tracefunc, PyObject *,
66 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000067static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000069 PyCodeAddressRange *, int *);
70static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070071static void dtrace_function_entry(PyFrameObject *);
72static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000073
Victor Stinner438a12d2019-05-24 17:01:38 +020074static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000085static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900106#ifdef Py_DEBUG
107// --with-pydebug is used to find memory leak. opcache makes it harder.
108// So we disable opcache when Py_DEBUG is defined.
109// See bpo-37146
110#define OPCACHE_MIN_RUNS 0 /* disable opcache */
111#else
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900113#endif
Pablo Galindo109826c2020-10-20 06:22:44 +0100114#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900115#define OPCACHE_STATS 0 /* Enable stats */
116
117#if OPCACHE_STATS
118static size_t opcache_code_objects = 0;
119static size_t opcache_code_objects_extra_mem = 0;
120
121static size_t opcache_global_opts = 0;
122static size_t opcache_global_hits = 0;
123static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100124
125static size_t opcache_attr_opts = 0;
126static size_t opcache_attr_hits = 0;
127static size_t opcache_attr_misses = 0;
128static size_t opcache_attr_deopts = 0;
129static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900130#endif
131
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100132
Victor Stinnerda2914d2020-03-20 09:29:08 +0100133#ifndef NDEBUG
134/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
135 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
136 when a thread continues to run after Python finalization, especially
137 daemon threads. */
138static int
139is_tstate_valid(PyThreadState *tstate)
140{
141 assert(!_PyMem_IsPtrFreed(tstate));
142 assert(!_PyMem_IsPtrFreed(tstate->interp));
143 return 1;
144}
145#endif
146
147
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000148/* This can set eval_breaker to 0 even though gil_drop_request became
149 1. We believe this is all right because the eval loop will release
150 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100151static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 struct _ceval_runtime_state *ceval,
154 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155{
Victor Stinner299b8c62020-05-05 17:40:18 +0200156 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
157 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200158 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200159 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100161 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100163}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000164
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165
Victor Stinnerda2914d2020-03-20 09:29:08 +0100166static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200167SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168{
Victor Stinner299b8c62020-05-05 17:40:18 +0200169 struct _ceval_state *ceval2 = &interp->ceval;
170 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
171 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000173
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000174
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200176RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100177{
Victor Stinner299b8c62020-05-05 17:40:18 +0200178 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
179 struct _ceval_state *ceval2 = &interp->ceval;
180 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
181 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
Eric Snowfdf282d2019-01-11 14:26:55 -0700184
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200186SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100187{
Victor Stinner299b8c62020-05-05 17:40:18 +0200188 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
189 struct _ceval_state *ceval2 = &interp->ceval;
190 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
191 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192}
Eric Snowfdf282d2019-01-11 14:26:55 -0700193
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000194
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200196UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100197{
Victor Stinner299b8c62020-05-05 17:40:18 +0200198 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
199 struct _ceval_state *ceval2 = &interp->ceval;
200 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
201 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100202}
203
204
205static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100206SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100207{
Victor Stinner299b8c62020-05-05 17:40:18 +0200208 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
209 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200210 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100211 if (force) {
212 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
213 }
214 else {
215 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
216 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
217 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100218}
219
220
221static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223{
Victor Stinner299b8c62020-05-05 17:40:18 +0200224 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
225 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200226 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200227 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100228}
229
230
231static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200232SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100233{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200234 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235 ceval2->pending.async_exc = 1;
236 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
237}
238
239
240static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200241UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100242{
Victor Stinner299b8c62020-05-05 17:40:18 +0200243 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
244 struct _ceval_state *ceval2 = &interp->ceval;
245 ceval2->pending.async_exc = 0;
246 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100247}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
249
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000252#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254
Victor Stinner3026cad2020-06-01 16:02:40 +0200255void _Py_NO_RETURN
256_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100257{
Victor Stinner3026cad2020-06-01 16:02:40 +0200258 _Py_FatalErrorFunc(func,
259 "the function must be called with the GIL held, "
260 "but the GIL is released "
261 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100262}
263
Victor Stinner7be4e352020-05-05 20:27:47 +0200264#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
265int
266_PyEval_ThreadsInitialized(PyInterpreterState *interp)
267{
268 return gil_created(&interp->ceval.gil);
269}
270
271int
272PyEval_ThreadsInitialized(void)
273{
274 // Fatal error if there is no current interpreter
275 PyInterpreterState *interp = PyInterpreterState_Get();
276 return _PyEval_ThreadsInitialized(interp);
277}
278#else
Tim Peters7f468f22004-10-11 02:40:51 +0000279int
Victor Stinner175a7042020-03-10 00:37:48 +0100280_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
281{
282 return gil_created(&runtime->ceval.gil);
283}
284
285int
Tim Peters7f468f22004-10-11 02:40:51 +0000286PyEval_ThreadsInitialized(void)
287{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100288 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100289 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000290}
Victor Stinner7be4e352020-05-05 20:27:47 +0200291#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000292
Victor Stinner111e4ee2020-03-09 21:24:14 +0100293PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200294_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295{
Victor Stinner7be4e352020-05-05 20:27:47 +0200296#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200297 if (!_Py_IsMainInterpreter(tstate)) {
298 /* Currently, the GIL is shared by all interpreters,
299 and only the main interpreter is responsible to create
300 and destroy it. */
301 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100302 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200303#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100304
Victor Stinner7be4e352020-05-05 20:27:47 +0200305#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
306 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
307#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200308 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200310 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100311
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200312 PyThread_init_thread();
313 create_gil(gil);
314
315 take_gil(tstate);
316
317 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100318 return _PyStatus_OK();
319}
320
321void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200322_PyEval_FiniGIL(PyThreadState *tstate)
323{
Victor Stinner7be4e352020-05-05 20:27:47 +0200324#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200325 if (!_Py_IsMainInterpreter(tstate)) {
326 /* Currently, the GIL is shared by all interpreters,
327 and only the main interpreter is responsible to create
328 and destroy it. */
329 return;
330 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200331#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200332
Victor Stinner7be4e352020-05-05 20:27:47 +0200333#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
334 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
335#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200336 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200338 if (!gil_created(gil)) {
339 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
340 yet: do nothing. */
341 return;
342 }
343
344 destroy_gil(gil);
345 assert(!gil_created(gil));
346}
347
348void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100349PyEval_InitThreads(void)
350{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100351 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000353
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000354void
Inada Naoki91234a12019-06-03 21:30:58 +0900355_PyEval_Fini(void)
356{
357#if OPCACHE_STATS
358 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
359 opcache_code_objects);
360
361 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
362 opcache_code_objects_extra_mem);
363
364 fprintf(stderr, "\n");
365
366 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
367 opcache_global_hits,
368 (int) (100.0 * opcache_global_hits /
369 (opcache_global_hits + opcache_global_misses)));
370
371 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
372 opcache_global_misses,
373 (int) (100.0 * opcache_global_misses /
374 (opcache_global_hits + opcache_global_misses)));
375
376 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
377 opcache_global_opts);
378
379 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100380
381 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
382 opcache_attr_hits,
383 (int) (100.0 * opcache_attr_hits /
384 opcache_attr_total));
385
386 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
387 opcache_attr_misses,
388 (int) (100.0 * opcache_attr_misses /
389 opcache_attr_total));
390
391 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
392 opcache_attr_opts);
393
394 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
395 opcache_attr_deopts);
396
397 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
398 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900399#endif
400}
401
402void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404{
Victor Stinner09532fe2019-05-10 23:39:09 +0200405 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200406 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200407 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100408
Victor Stinner85f5a692020-03-09 22:12:04 +0100409 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000410}
411
412void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000413PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414{
Victor Stinner09532fe2019-05-10 23:39:09 +0200415 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200416 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100418 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100419 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200420 struct _ceval_runtime_state *ceval = &runtime->ceval;
421 struct _ceval_state *ceval2 = &tstate->interp->ceval;
422 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423}
424
425void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100426_PyEval_ReleaseLock(PyThreadState *tstate)
427{
428 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200429 struct _ceval_state *ceval2 = &tstate->interp->ceval;
430 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100431}
432
433void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000435{
Victor Stinner3026cad2020-06-01 16:02:40 +0200436 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100437
Victor Stinner85f5a692020-03-09 22:12:04 +0100438 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200439
Victor Stinner85f5a692020-03-09 22:12:04 +0100440 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200441#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
442 (void)_PyThreadState_Swap(gilstate, tstate);
443#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100444 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100445 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200446 }
Victor Stinnere838a932020-05-05 19:56:48 +0200447#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000448}
449
450void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000451PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000452{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100453 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200454
Victor Stinner01b1cc12019-11-20 02:27:56 +0100455 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200456 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
457 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100458 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200459 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200460 struct _ceval_runtime_state *ceval = &runtime->ceval;
461 struct _ceval_state *ceval2 = &tstate->interp->ceval;
462 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000463}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000464
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900465#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200466/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200467 which are not running in the child process, and clear internal locks
468 which might be held by those threads. */
469PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200470_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000471{
Victor Stinner317bab02020-06-02 18:44:54 +0200472 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200473
474#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
475 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
476#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100477 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200478#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100479 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200480 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200481 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100482 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100483
484 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700485
Victor Stinner50e6e992020-03-19 02:41:21 +0100486 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900487 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200488 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700489 }
Jesse Nollera8513972008-07-17 16:49:17 +0000490
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200491 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100492 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200493 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000494}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900495#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000496
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000497/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600498 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000499
500void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100501_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000502{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200503 assert(is_tstate_valid(tstate));
504 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000505}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000506
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000507PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000508PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509{
Victor Stinner09532fe2019-05-10 23:39:09 +0200510 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200511#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
512 PyThreadState *old_tstate = _PyThreadState_GET();
513 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
514#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200515 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200516#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200517 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100518
Victor Stinner0b1e3302020-05-05 16:14:31 +0200519 struct _ceval_runtime_state *ceval = &runtime->ceval;
520 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200521#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
522 assert(gil_created(&ceval2->gil));
523#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200524 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200525#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200526 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528}
529
530void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532{
Victor Stinner3026cad2020-06-01 16:02:40 +0200533 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100534
Victor Stinner85f5a692020-03-09 22:12:04 +0100535 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100536
Victor Stinner85f5a692020-03-09 22:12:04 +0100537 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
538 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000539}
540
541
Guido van Rossuma9672091994-09-14 13:31:22 +0000542/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
543 signal handlers or Mac I/O completion routines) can schedule calls
544 to a function to be called synchronously.
545 The synchronous function is called with one void* argument.
546 It should return 0 for success or -1 for failure -- failure should
547 be accompanied by an exception.
548
549 If registry succeeds, the registry function returns 0; if it fails
550 (e.g. due to too many pending calls) it returns -1 (without setting
551 an exception condition).
552
553 Note that because registry may occur from within signal handlers,
554 or other asynchronous events, calling malloc() is unsafe!
555
Guido van Rossuma9672091994-09-14 13:31:22 +0000556 Any thread can schedule pending calls, but only the main thread
557 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558 There is no facility to schedule calls to a particular thread, but
559 that should be easy to change, should that ever be required. In
560 that case, the static variables here should go into the python
561 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000562*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000563
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200564void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200565_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200566{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100567#ifdef MS_WINDOWS
568 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
569 // handler which can run in a thread different than the Python thread, in
570 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
571 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
572 //
573 // The next eval_frame_handle_pending() call will call
574 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
575 int force = 1;
576#else
577 int force = 0;
578#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200579 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100580 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200581 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100582 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200583}
584
Eric Snow5be45a62019-03-08 22:47:07 -0700585/* Push one item onto the queue while holding the lock. */
586static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200587_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600588 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700589{
Eric Snow842a2f02019-03-15 15:47:51 -0600590 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700591 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600592 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700593 return -1; /* Queue full */
594 }
Eric Snow842a2f02019-03-15 15:47:51 -0600595 pending->calls[i].func = func;
596 pending->calls[i].arg = arg;
597 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700598 return 0;
599}
600
601/* Pop one item off the queue while holding the lock. */
602static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200603_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600604 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700605{
Eric Snow842a2f02019-03-15 15:47:51 -0600606 int i = pending->first;
607 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700608 return; /* Queue empty */
609 }
610
Eric Snow842a2f02019-03-15 15:47:51 -0600611 *func = pending->calls[i].func;
612 *arg = pending->calls[i].arg;
613 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700614}
615
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200616/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000617 scheduling to be made from any thread, and even from an executing
618 callback.
619 */
620
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000621int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200622_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200623 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000624{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200625 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600626
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200627 /* Ensure that _PyEval_InitPendingCalls() was called
628 and that _PyEval_FiniPendingCalls() is not called yet. */
629 assert(pending->lock != NULL);
630
Eric Snow842a2f02019-03-15 15:47:51 -0600631 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200632 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600633 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700634
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200636 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000638}
639
Victor Stinner09532fe2019-05-10 23:39:09 +0200640int
641Py_AddPendingCall(int (*func)(void *), void *arg)
642{
Victor Stinner50e6e992020-03-19 02:41:21 +0100643 /* Best-effort to support subinterpreters and calls with the GIL released.
644
645 First attempt _PyThreadState_GET() since it supports subinterpreters.
646
647 If the GIL is released, _PyThreadState_GET() returns NULL . In this
648 case, use PyGILState_GetThisThreadState() which works even if the GIL
649 is released.
650
651 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
652 see bpo-10915 and bpo-15751.
653
Victor Stinner8849e592020-03-18 19:28:53 +0100654 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100655 PyThreadState *tstate = _PyThreadState_GET();
656 if (tstate == NULL) {
657 tstate = PyGILState_GetThisThreadState();
658 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200659
660 PyInterpreterState *interp;
661 if (tstate != NULL) {
662 interp = tstate->interp;
663 }
664 else {
665 /* Last resort: use the main interpreter */
666 interp = _PyRuntime.interpreters.main;
667 }
668 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200669}
670
Eric Snowfdf282d2019-01-11 14:26:55 -0700671static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100672handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700673{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200674 assert(is_tstate_valid(tstate));
675 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700676 return 0;
677 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700678
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200679 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100680 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
681 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100682 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 return -1;
684 }
685 return 0;
686}
687
688static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100689make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000690{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200691 assert(is_tstate_valid(tstate));
692
Victor Stinnerd8316882020-03-20 14:50:35 +0100693 /* only execute pending calls on main thread */
694 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200695 return 0;
696 }
697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100699 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700700 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700702 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200703 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100704
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200705 /* unsignal before starting to call callbacks, so that any callback
706 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200707 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700708 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100711 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700712 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700713 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 void *arg = NULL;
715
716 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600717 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200718 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600719 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700720
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100721 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700722 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100723 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700724 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700725 res = func(arg);
726 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200727 goto error;
728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200730
Charles-François Natalif23339a2011-07-23 18:15:43 +0200731 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700732 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200733
734error:
735 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200736 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700737 return res;
738}
739
Eric Snow842a2f02019-03-15 15:47:51 -0600740void
Victor Stinner2b1df452020-01-13 18:46:59 +0100741_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600742{
Eric Snow842a2f02019-03-15 15:47:51 -0600743 assert(PyGILState_Check());
744
Victor Stinner50e6e992020-03-19 02:41:21 +0100745 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200746
Eric Snow842a2f02019-03-15 15:47:51 -0600747 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
748 return;
749 }
750
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100751 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200752 PyObject *exc, *val, *tb;
753 _PyErr_Fetch(tstate, &exc, &val, &tb);
754 PyErr_BadInternalCall();
755 _PyErr_ChainExceptions(exc, val, tb);
756 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600757 }
758}
759
Eric Snowfdf282d2019-01-11 14:26:55 -0700760/* Py_MakePendingCalls() is a simple wrapper for the sake
761 of backward-compatibility. */
762int
763Py_MakePendingCalls(void)
764{
765 assert(PyGILState_Check());
766
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100767 PyThreadState *tstate = _PyThreadState_GET();
768
Eric Snowfdf282d2019-01-11 14:26:55 -0700769 /* Python signal handler doesn't really queue a callback: it only signals
770 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700772 if (res != 0) {
773 return res;
774 }
775
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100776 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600777 if (res != 0) {
778 return res;
779 }
780
781 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000782}
783
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000784/* The interpreter's recursion limit */
785
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000786#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200787# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000788#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600789
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600790void
Victor Stinnerdab84232020-03-17 18:56:44 +0100791_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600792{
Victor Stinner7be4e352020-05-05 20:27:47 +0200793#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100794 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200795#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100796}
797
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200798int
Victor Stinnerdab84232020-03-17 18:56:44 +0100799_PyEval_InitState(struct _ceval_state *ceval)
800{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200801 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
802
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200803 struct _pending_calls *pending = &ceval->pending;
804 assert(pending->lock == NULL);
805
806 pending->lock = PyThread_allocate_lock();
807 if (pending->lock == NULL) {
808 return -1;
809 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200810
811#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
812 _gil_initialize(&ceval->gil);
813#endif
814
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200815 return 0;
816}
817
818void
819_PyEval_FiniState(struct _ceval_state *ceval)
820{
821 struct _pending_calls *pending = &ceval->pending;
822 if (pending->lock != NULL) {
823 PyThread_free_lock(pending->lock);
824 pending->lock = NULL;
825 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600826}
827
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000828int
829Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000830{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200831 PyInterpreterState *interp = _PyInterpreterState_GET();
832 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000833}
834
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000835void
836Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000837{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200838 PyThreadState *tstate = _PyThreadState_GET();
839 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000840}
841
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100842/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200843 if the recursion_depth reaches recursion_limit.
844 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000845 to guarantee that _Py_CheckRecursiveCall() is regularly called.
846 Without USE_STACKCHECK, there is no need for this. */
847int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100848_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000849{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200850 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000851
852#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700853 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 if (PyOS_CheckStack()) {
855 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200856 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 return -1;
858 }
pdox18967932017-10-25 23:03:01 -0700859#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (tstate->overflowed) {
861 if (tstate->recursion_depth > recursion_limit + 50) {
862 /* Overflowing while handling an overflow. Give up. */
863 Py_FatalError("Cannot recover from stack overflow.");
864 }
865 return 0;
866 }
867 if (tstate->recursion_depth > recursion_limit) {
868 --tstate->recursion_depth;
869 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200870 _PyErr_Format(tstate, PyExc_RecursionError,
871 "maximum recursion depth exceeded%s",
872 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 return -1;
874 }
875 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000876}
877
Victor Stinner09532fe2019-05-10 23:39:09 +0200878static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200879static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000880
Victor Stinnere225beb2019-06-03 18:14:24 +0200881#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883
Guido van Rossumb209a111997-04-29 18:18:01 +0000884PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000885PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 return PyEval_EvalCodeEx(co,
888 globals, locals,
889 (PyObject **)NULL, 0,
890 (PyObject **)NULL, 0,
891 (PyObject **)NULL, 0,
892 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000893}
894
895
896/* Interpreter main loop */
897
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000898PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100899PyEval_EvalFrame(PyFrameObject *f)
900{
Victor Stinner0b72b232020-03-12 23:18:39 +0100901 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100902 PyThreadState *tstate = _PyThreadState_GET();
903 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000904}
905
906PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000908{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100909 PyThreadState *tstate = _PyThreadState_GET();
910 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700911}
912
Victor Stinnerda2914d2020-03-20 09:29:08 +0100913
914/* Handle signals, pending calls, GIL drop request
915 and asynchronous exception */
916static int
917eval_frame_handle_pending(PyThreadState *tstate)
918{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100919 _PyRuntimeState * const runtime = &_PyRuntime;
920 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200921
922 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200923 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100924 if (handle_signals(tstate) != 0) {
925 return -1;
926 }
927 }
928
929 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200930 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100931 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
932 if (make_pending_calls(tstate) != 0) {
933 return -1;
934 }
935 }
936
937 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200938 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100939 /* Give another thread a chance */
940 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
941 Py_FatalError("tstate mix-up");
942 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200943 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100944
945 /* Other threads may run now */
946
947 take_gil(tstate);
948
Victor Stinnere838a932020-05-05 19:56:48 +0200949#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
950 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
951#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100952 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
953 Py_FatalError("orphan tstate");
954 }
Victor Stinnere838a932020-05-05 19:56:48 +0200955#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100956 }
957
958 /* Check for asynchronous exception. */
959 if (tstate->async_exc != NULL) {
960 PyObject *exc = tstate->async_exc;
961 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200962 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100963 _PyErr_SetNone(tstate, exc);
964 Py_DECREF(exc);
965 return -1;
966 }
967
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100968#ifdef MS_WINDOWS
969 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
970 // different thread than the Python thread, in which case
971 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
972 // current Python thread with the correct _Py_ThreadCanHandleSignals()
973 // value. It prevents to interrupt the eval loop at every instruction if
974 // the current Python thread cannot handle signals (if
975 // _Py_ThreadCanHandleSignals() is false).
976 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
977#endif
978
Victor Stinnerda2914d2020-03-20 09:29:08 +0100979 return 0;
980}
981
Victor Stinnerc6944e72016-11-11 02:13:35 +0100982PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100983_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700984{
Victor Stinner3026cad2020-06-01 16:02:40 +0200985 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100986
Guido van Rossum950361c1997-01-24 13:49:28 +0000987#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000989#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200990 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300991 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200992 int opcode; /* Current opcode */
993 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200994 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100996 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100997 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 is true when the line being executed has changed. The
1005 initial values are such as to make this false the first
1006 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001007
Serhiy Storchakaab874002016-09-11 13:48:15 +03001008 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 PyObject *names;
1010 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001011 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001012
Brett Cannon368b4b72012-04-02 12:17:59 -04001013#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001014 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001015#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001016
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017/* Computed GOTOs, or
1018 the-optimization-commonly-but-improperly-known-as-"threaded code"
1019 using gcc's labels-as-values extension
1020 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1021
1022 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024 combined with a lookup table of jump addresses. However, since the
1025 indirect jump instruction is shared by all opcodes, the CPU will have a
1026 hard time making the right prediction for where to jump next (actually,
1027 it will be always wrong except in the uncommon case of a sequence of
1028 several identical opcodes).
1029
1030 "Threaded code" in contrast, uses an explicit jump table and an explicit
1031 indirect jump instruction at the end of each opcode. Since the jump
1032 instruction is at a different address for each opcode, the CPU will make a
1033 separate prediction for each of these instructions, which is equivalent to
1034 predicting the second opcode of each opcode pair. These predictions have
1035 a much better chance to turn out valid, especially in small bytecode loops.
1036
1037 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001039 and potentially many more instructions (depending on the pipeline width).
1040 A correctly predicted branch, however, is nearly free.
1041
1042 At the time of this writing, the "threaded code" version is up to 15-20%
1043 faster than the normal "switch" version, depending on the compiler and the
1044 CPU architecture.
1045
1046 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1047 because it would render the measurements invalid.
1048
1049
1050 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1051 indirect jumps by sharing them between all opcodes. Such optimizations
1052 can be disabled on gcc by using the -fno-gcse flag (or possibly
1053 -fno-crossjumping).
1054*/
1055
Antoine Pitrou042b1282010-08-13 21:15:58 +00001056#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001057#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001058#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001059#endif
1060
Antoine Pitrou042b1282010-08-13 21:15:58 +00001061#ifdef HAVE_COMPUTED_GOTOS
1062 #ifndef USE_COMPUTED_GOTOS
1063 #define USE_COMPUTED_GOTOS 1
1064 #endif
1065#else
1066 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1067 #error "Computed gotos are not supported on this compiler."
1068 #endif
1069 #undef USE_COMPUTED_GOTOS
1070 #define USE_COMPUTED_GOTOS 0
1071#endif
1072
1073#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001074/* Import the static jump table */
1075#include "opcode_targets.h"
1076
Antoine Pitroub52ec782009-01-25 16:34:23 +00001077#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001078 op: \
1079 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001080
Antoine Pitroub52ec782009-01-25 16:34:23 +00001081#ifdef LLTRACE
1082#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001084 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001086 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001087 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 } \
1089 goto fast_next_opcode; \
1090 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001091#else
1092#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001094 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001096 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001097 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 } \
1099 goto fast_next_opcode; \
1100 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001101#endif
1102
Victor Stinner09532fe2019-05-10 23:39:09 +02001103#define DISPATCH() \
1104 { \
1105 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1106 FAST_DISPATCH(); \
1107 } \
1108 continue; \
1109 }
1110
Antoine Pitroub52ec782009-01-25 16:34:23 +00001111#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001112#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001113#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001114#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001115#endif
1116
1117
Neal Norwitza81d2202002-07-14 00:27:26 +00001118/* Tuple access macros */
1119
1120#ifndef Py_DEBUG
1121#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1122#else
1123#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1124#endif
1125
Guido van Rossum374a9221991-04-04 10:40:29 +00001126/* Code access macros */
1127
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001128/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001129#define INSTR_OFFSET() \
1130 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001131#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001132 _Py_CODEUNIT word = *next_instr; \
1133 opcode = _Py_OPCODE(word); \
1134 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001135 next_instr++; \
1136 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001137#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1138#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001139
Raymond Hettingerf606f872003-03-16 03:11:04 +00001140/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 Some opcodes tend to come in pairs thus making it possible to
1142 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001143 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 Verifying the prediction costs a single high-speed test of a register
1146 variable against a constant. If the pairing was good, then the
1147 processor's own internal branch predication has a high likelihood of
1148 success, resulting in a nearly zero-overhead transition to the
1149 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001150 including its unpredictable switch-case branch. Combined with the
1151 processor's internal branch prediction, a successful PREDICT has the
1152 effect of making the two opcodes run as if they were a single new opcode
1153 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001154
Georg Brandl86b2fb92008-07-16 03:43:04 +00001155 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 predictions turned-on and interpret the results as if some opcodes
1157 had been combined or turn-off predictions so that the opcode frequency
1158 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001159
1160 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 the CPU to record separate branch prediction information for each
1162 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001163
Raymond Hettingerf606f872003-03-16 03:11:04 +00001164*/
1165
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001166#define PREDICT_ID(op) PRED_##op
1167
Antoine Pitrou042b1282010-08-13 21:15:58 +00001168#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001169#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001170#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001171#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001172 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001173 _Py_CODEUNIT word = *next_instr; \
1174 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001175 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001176 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001177 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001178 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001179 } \
1180 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001181#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001182#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001183
Raymond Hettingerf606f872003-03-16 03:11:04 +00001184
Guido van Rossum374a9221991-04-04 10:40:29 +00001185/* Stack manipulation macros */
1186
Martin v. Löwis18e16552006-02-15 17:27:45 +00001187/* The stack can grow at most MAXINT deep, as co_nlocals and
1188 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001189#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1190#define EMPTY() (STACK_LEVEL() == 0)
1191#define TOP() (stack_pointer[-1])
1192#define SECOND() (stack_pointer[-2])
1193#define THIRD() (stack_pointer[-3])
1194#define FOURTH() (stack_pointer[-4])
1195#define PEEK(n) (stack_pointer[-(n)])
1196#define SET_TOP(v) (stack_pointer[-1] = (v))
1197#define SET_SECOND(v) (stack_pointer[-2] = (v))
1198#define SET_THIRD(v) (stack_pointer[-3] = (v))
1199#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001200#define BASIC_STACKADJ(n) (stack_pointer += n)
1201#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1202#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001203
Guido van Rossum96a42c81992-01-12 02:29:51 +00001204#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001206 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001207 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001208#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001209 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001210#define STACK_GROW(n) do { \
1211 assert(n >= 0); \
1212 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001213 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001214 assert(STACK_LEVEL() <= co->co_stacksize); \
1215 } while (0)
1216#define STACK_SHRINK(n) do { \
1217 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001218 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001219 (void)(BASIC_STACKADJ(-n)); \
1220 assert(STACK_LEVEL() <= co->co_stacksize); \
1221 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001222#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001223 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001224 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001225#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001226#define PUSH(v) BASIC_PUSH(v)
1227#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001228#define STACK_GROW(n) BASIC_STACKADJ(n)
1229#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001230#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001231#endif
1232
Guido van Rossum681d79a1995-07-18 14:51:37 +00001233/* Local variable macros */
1234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001236
1237/* The SETLOCAL() macro must not DECREF the local variable in-place and
1238 then store the new value; it must copy the old value to a temporary
1239 value, then store the new value, and then DECREF the temporary value.
1240 This is because it is possible that during the DECREF the frame is
1241 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1242 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001244 GETLOCAL(i) = value; \
1245 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001246
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001247
1248#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 while (STACK_LEVEL() > (b)->b_level) { \
1250 PyObject *v = POP(); \
1251 Py_XDECREF(v); \
1252 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001253
1254#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001255 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001257 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1259 while (STACK_LEVEL() > (b)->b_level + 3) { \
1260 value = POP(); \
1261 Py_XDECREF(value); \
1262 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001263 exc_info = tstate->exc_info; \
1264 type = exc_info->exc_type; \
1265 value = exc_info->exc_value; \
1266 traceback = exc_info->exc_traceback; \
1267 exc_info->exc_type = POP(); \
1268 exc_info->exc_value = POP(); \
1269 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 Py_XDECREF(type); \
1271 Py_XDECREF(value); \
1272 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001273 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001274
Inada Naoki91234a12019-06-03 21:30:58 +09001275 /* macros for opcode cache */
1276#define OPCACHE_CHECK() \
1277 do { \
1278 co_opcache = NULL; \
1279 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001280 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001281 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001282 if (co_opcache_offset > 0) { \
1283 assert(co_opcache_offset <= co->co_opcache_size); \
1284 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001285 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001286 } \
1287 } \
1288 } while (0)
1289
Pablo Galindo109826c2020-10-20 06:22:44 +01001290#define OPCACHE_DEOPT() \
1291 do { \
1292 if (co_opcache != NULL) { \
1293 co_opcache->optimized = -1; \
1294 unsigned char co_opcache_offset = \
1295 co->co_opcache_map[next_instr - first_instr]; \
1296 assert(co_opcache_offset <= co->co_opcache_size); \
1297 co->co_opcache_map[co_opcache_offset] = 0; \
1298 co_opcache = NULL; \
1299 } \
1300 } while (0)
1301
1302#define OPCACHE_DEOPT_LOAD_ATTR() \
1303 do { \
1304 if (co_opcache != NULL) { \
1305 OPCACHE_STAT_ATTR_DEOPT(); \
1306 OPCACHE_DEOPT(); \
1307 } \
1308 } while (0)
1309
1310#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1311 do { \
1312 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1313 OPCACHE_DEOPT_LOAD_ATTR(); \
1314 } \
1315 } while (0)
1316
Inada Naoki91234a12019-06-03 21:30:58 +09001317#if OPCACHE_STATS
1318
1319#define OPCACHE_STAT_GLOBAL_HIT() \
1320 do { \
1321 if (co->co_opcache != NULL) opcache_global_hits++; \
1322 } while (0)
1323
1324#define OPCACHE_STAT_GLOBAL_MISS() \
1325 do { \
1326 if (co->co_opcache != NULL) opcache_global_misses++; \
1327 } while (0)
1328
1329#define OPCACHE_STAT_GLOBAL_OPT() \
1330 do { \
1331 if (co->co_opcache != NULL) opcache_global_opts++; \
1332 } while (0)
1333
Pablo Galindo109826c2020-10-20 06:22:44 +01001334#define OPCACHE_STAT_ATTR_HIT() \
1335 do { \
1336 if (co->co_opcache != NULL) opcache_attr_hits++; \
1337 } while (0)
1338
1339#define OPCACHE_STAT_ATTR_MISS() \
1340 do { \
1341 if (co->co_opcache != NULL) opcache_attr_misses++; \
1342 } while (0)
1343
1344#define OPCACHE_STAT_ATTR_OPT() \
1345 do { \
1346 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1347 } while (0)
1348
1349#define OPCACHE_STAT_ATTR_DEOPT() \
1350 do { \
1351 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1352 } while (0)
1353
1354#define OPCACHE_STAT_ATTR_TOTAL() \
1355 do { \
1356 if (co->co_opcache != NULL) opcache_attr_total++; \
1357 } while (0)
1358
Inada Naoki91234a12019-06-03 21:30:58 +09001359#else /* OPCACHE_STATS */
1360
1361#define OPCACHE_STAT_GLOBAL_HIT()
1362#define OPCACHE_STAT_GLOBAL_MISS()
1363#define OPCACHE_STAT_GLOBAL_OPT()
1364
Pablo Galindo109826c2020-10-20 06:22:44 +01001365#define OPCACHE_STAT_ATTR_HIT()
1366#define OPCACHE_STAT_ATTR_MISS()
1367#define OPCACHE_STAT_ATTR_OPT()
1368#define OPCACHE_STAT_ATTR_DEOPT()
1369#define OPCACHE_STAT_ATTR_TOTAL()
1370
Inada Naoki91234a12019-06-03 21:30:58 +09001371#endif
1372
Guido van Rossuma027efa1997-05-05 20:56:21 +00001373/* Start of code */
1374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001376 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001378 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (tstate->use_tracing) {
1383 if (tstate->c_tracefunc != NULL) {
1384 /* tstate->c_tracefunc, if defined, is a
1385 function that will be called on *every* entry
1386 to a code block. Its return value, if not
1387 None, is a function that will be called at
1388 the start of each executed line of code.
1389 (Actually, the function must return itself
1390 in order to continue tracing.) The trace
1391 functions are called with three arguments:
1392 a pointer to the current frame, a string
1393 indicating why the function is called, and
1394 an argument which depends on the situation.
1395 The global trace function is also called
1396 whenever an exception is detected. */
1397 if (call_trace_protected(tstate->c_tracefunc,
1398 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001399 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 /* Trace function raised an error */
1401 goto exit_eval_frame;
1402 }
1403 }
1404 if (tstate->c_profilefunc != NULL) {
1405 /* Similar for c_profilefunc, except it needn't
1406 return itself and isn't called for "line" events */
1407 if (call_trace_protected(tstate->c_profilefunc,
1408 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001409 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 /* Profile function raised an error */
1411 goto exit_eval_frame;
1412 }
1413 }
1414 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001415
Łukasz Langaa785c872016-09-09 17:37:37 -07001416 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1417 dtrace_function_entry(f);
1418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 co = f->f_code;
Mark Shannon877df852020-11-12 09:43:29 +00001420 PyCodeAddressRange bounds;
1421 _PyCode_InitAddressRange(co, &bounds);
1422 int instr_prev = -1;
1423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 names = co->co_names;
1425 consts = co->co_consts;
1426 fastlocals = f->f_localsplus;
1427 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001428 assert(PyBytes_Check(co->co_code));
1429 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001430 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1431 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1432 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001433 /*
1434 f->f_lasti refers to the index of the last instruction,
1435 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001436
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001437 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001438 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 When the PREDICT() macros are enabled, some opcode pairs follow in
1441 direct succession without updating f->f_lasti. A successful
1442 prediction effectively links the two codes together as if they
1443 were a single new opcode; accordingly,f->f_lasti will point to
1444 the first code in the pair (for instance, GET_ITER followed by
1445 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001446 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001448 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001449 next_instr = first_instr;
1450 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001451 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1452 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001453 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001454 stack_pointer = f->f_valuestack + f->f_stackdepth;
1455 /* Set f->f_stackdepth to -1.
1456 * Update when returning or calling trace function.
1457 Having f_stackdepth <= 0 ensures that invalid
1458 values are not visible to the cycle GC.
1459 We choose -1 rather than 0 to assist debugging.
1460 */
1461 f->f_stackdepth = -1;
1462 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001463
Inada Naoki91234a12019-06-03 21:30:58 +09001464 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1465 co->co_opcache_flag++;
1466 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1467 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001468 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001469 }
1470#if OPCACHE_STATS
1471 opcache_code_objects_extra_mem +=
1472 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1473 sizeof(_PyOpcache) * co->co_opcache_size;
1474 opcache_code_objects++;
1475#endif
1476 }
1477 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001478
Tim Peters5ca576e2001-06-18 22:08:13 +00001479#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001480 {
1481 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1482 if (r < 0) {
1483 goto exit_eval_frame;
1484 }
1485 lltrace = r;
1486 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001487#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001489 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001491 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001492
Victor Stinnerace47d72013-07-18 01:41:08 +02001493#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001494 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001495 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001496 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001497 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001498#endif
1499
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001500main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1503 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001504 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 /* Do periodic things. Doing this every time through
1507 the loop would add too much overhead, so we do it
1508 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001509 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 event needs attention (e.g. a signal handler or
1511 async I/O handler); see Py_AddPendingCall() and
1512 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Eric Snow7bda9de2019-03-08 17:25:54 -07001514 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001515 opcode = _Py_OPCODE(*next_instr);
1516 if (opcode == SETUP_FINALLY ||
1517 opcode == SETUP_WITH ||
1518 opcode == BEFORE_ASYNC_WITH ||
1519 opcode == YIELD_FROM) {
1520 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001521 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001522 - If we're about to enter the 'with:'. It will prevent
1523 emitting a resource warning in the common idiom
1524 'with open(path) as file:'.
1525 - If we're about to enter the 'async with:'.
1526 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001527 *very* useful, but might help in some cases and it's
1528 traditional)
1529 - If we're resuming a chain of nested 'yield from' or
1530 'await' calls, then each frame is parked with YIELD_FROM
1531 as its next opcode. If the user hit control-C we want to
1532 wait until we've reached the innermost frame before
1533 running the signal handler and raising KeyboardInterrupt
1534 (see bpo-30039).
1535 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 goto fast_next_opcode;
1537 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001538
Victor Stinnerda2914d2020-03-20 09:29:08 +01001539 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 }
1542 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 fast_next_opcode:
1545 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Łukasz Langaa785c872016-09-09 17:37:37 -07001547 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001548 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001551
Victor Stinnerdab84232020-03-17 18:56:44 +01001552 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001553 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001555 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001557 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 err = maybe_call_line_trace(tstate->c_tracefunc,
1560 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001561 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001562 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 /* Reload possibly changed frame fields */
1564 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001565 stack_pointer = f->f_valuestack+f->f_stackdepth;
1566 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001573
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001574 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001575 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001576#ifdef DYNAMIC_EXECUTION_PROFILE
1577#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 dxpairs[lastopcode][opcode]++;
1579 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001580#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001582#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001583
Guido van Rossum96a42c81992-01-12 02:29:51 +00001584#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (lltrace) {
1588 if (HAS_ARG(opcode)) {
1589 printf("%d: %d, %d\n",
1590 f->f_lasti, opcode, oparg);
1591 }
1592 else {
1593 printf("%d: %d\n",
1594 f->f_lasti, opcode);
1595 }
1596 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001597#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001602 It is essential that any operation that fails must goto error
1603 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Benjamin Petersonddd19492018-09-16 22:38:02 -07001605 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001607 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001608
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 PyObject *value = GETLOCAL(oparg);
1611 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001612 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 UNBOUNDLOCAL_ERROR_MSG,
1614 PyTuple_GetItem(co->co_varnames, oparg));
1615 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 Py_INCREF(value);
1618 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 }
1621
Benjamin Petersonddd19492018-09-16 22:38:02 -07001622 case TARGET(LOAD_CONST): {
1623 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 PyObject *value = GETITEM(consts, oparg);
1625 Py_INCREF(value);
1626 PUSH(value);
1627 FAST_DISPATCH();
1628 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001629
Benjamin Petersonddd19492018-09-16 22:38:02 -07001630 case TARGET(STORE_FAST): {
1631 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 PyObject *value = POP();
1633 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001636
Benjamin Petersonddd19492018-09-16 22:38:02 -07001637 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 PyObject *value = POP();
1639 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Benjamin Petersonddd19492018-09-16 22:38:02 -07001643 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001644 PyObject *top = TOP();
1645 PyObject *second = SECOND();
1646 SET_TOP(second);
1647 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 PyObject *top = TOP();
1653 PyObject *second = SECOND();
1654 PyObject *third = THIRD();
1655 SET_TOP(second);
1656 SET_SECOND(third);
1657 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Petersonddd19492018-09-16 22:38:02 -07001661 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001662 PyObject *top = TOP();
1663 PyObject *second = SECOND();
1664 PyObject *third = THIRD();
1665 PyObject *fourth = FOURTH();
1666 SET_TOP(second);
1667 SET_SECOND(third);
1668 SET_THIRD(fourth);
1669 SET_FOURTH(top);
1670 FAST_DISPATCH();
1671 }
1672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *top = TOP();
1675 Py_INCREF(top);
1676 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Benjamin Petersonddd19492018-09-16 22:38:02 -07001680 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001683 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001685 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 SET_TOP(top);
1687 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001688 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *value = TOP();
1693 PyObject *res = PyNumber_Positive(value);
1694 Py_DECREF(value);
1695 SET_TOP(res);
1696 if (res == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Petersonddd19492018-09-16 22:38:02 -07001701 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 PyObject *value = TOP();
1703 PyObject *res = PyNumber_Negative(value);
1704 Py_DECREF(value);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 PyObject *value = TOP();
1713 int err = PyObject_IsTrue(value);
1714 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (err == 0) {
1716 Py_INCREF(Py_True);
1717 SET_TOP(Py_True);
1718 DISPATCH();
1719 }
1720 else if (err > 0) {
1721 Py_INCREF(Py_False);
1722 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 DISPATCH();
1724 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001725 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 goto error;
1727 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Benjamin Petersonddd19492018-09-16 22:38:02 -07001729 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 PyObject *value = TOP();
1731 PyObject *res = PyNumber_Invert(value);
1732 Py_DECREF(value);
1733 SET_TOP(res);
1734 if (res == NULL)
1735 goto error;
1736 DISPATCH();
1737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 PyObject *exp = POP();
1741 PyObject *base = TOP();
1742 PyObject *res = PyNumber_Power(base, exp, Py_None);
1743 Py_DECREF(base);
1744 Py_DECREF(exp);
1745 SET_TOP(res);
1746 if (res == NULL)
1747 goto error;
1748 DISPATCH();
1749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 PyObject *right = POP();
1753 PyObject *left = TOP();
1754 PyObject *res = PyNumber_Multiply(left, right);
1755 Py_DECREF(left);
1756 Py_DECREF(right);
1757 SET_TOP(res);
1758 if (res == NULL)
1759 goto error;
1760 DISPATCH();
1761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001764 PyObject *right = POP();
1765 PyObject *left = TOP();
1766 PyObject *res = PyNumber_MatrixMultiply(left, right);
1767 Py_DECREF(left);
1768 Py_DECREF(right);
1769 SET_TOP(res);
1770 if (res == NULL)
1771 goto error;
1772 DISPATCH();
1773 }
1774
Benjamin Petersonddd19492018-09-16 22:38:02 -07001775 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 PyObject *divisor = POP();
1777 PyObject *dividend = TOP();
1778 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1779 Py_DECREF(dividend);
1780 Py_DECREF(divisor);
1781 SET_TOP(quotient);
1782 if (quotient == NULL)
1783 goto error;
1784 DISPATCH();
1785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Benjamin Petersonddd19492018-09-16 22:38:02 -07001787 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 PyObject *divisor = POP();
1789 PyObject *dividend = TOP();
1790 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1791 Py_DECREF(dividend);
1792 Py_DECREF(divisor);
1793 SET_TOP(quotient);
1794 if (quotient == NULL)
1795 goto error;
1796 DISPATCH();
1797 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001798
Benjamin Petersonddd19492018-09-16 22:38:02 -07001799 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 PyObject *divisor = POP();
1801 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001802 PyObject *res;
1803 if (PyUnicode_CheckExact(dividend) && (
1804 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1805 // fast path; string formatting, but not if the RHS is a str subclass
1806 // (see issue28598)
1807 res = PyUnicode_Format(dividend, divisor);
1808 } else {
1809 res = PyNumber_Remainder(dividend, divisor);
1810 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 Py_DECREF(divisor);
1812 Py_DECREF(dividend);
1813 SET_TOP(res);
1814 if (res == NULL)
1815 goto error;
1816 DISPATCH();
1817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Benjamin Petersonddd19492018-09-16 22:38:02 -07001819 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyObject *right = POP();
1821 PyObject *left = TOP();
1822 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001823 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001824 CPython using bytecode, it is simply worthless.
1825 See http://bugs.python.org/issue21955 and
1826 http://bugs.python.org/issue10044 for the discussion. In short,
1827 no patch shown any impact on a realistic benchmark, only a minor
1828 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001829 if (PyUnicode_CheckExact(left) &&
1830 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001831 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001832 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001833 }
1834 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 sum = PyNumber_Add(left, right);
1836 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001837 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 Py_DECREF(right);
1839 SET_TOP(sum);
1840 if (sum == NULL)
1841 goto error;
1842 DISPATCH();
1843 }
1844
Benjamin Petersonddd19492018-09-16 22:38:02 -07001845 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 PyObject *right = POP();
1847 PyObject *left = TOP();
1848 PyObject *diff = PyNumber_Subtract(left, right);
1849 Py_DECREF(right);
1850 Py_DECREF(left);
1851 SET_TOP(diff);
1852 if (diff == NULL)
1853 goto error;
1854 DISPATCH();
1855 }
1856
Benjamin Petersonddd19492018-09-16 22:38:02 -07001857 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 PyObject *sub = POP();
1859 PyObject *container = TOP();
1860 PyObject *res = PyObject_GetItem(container, sub);
1861 Py_DECREF(container);
1862 Py_DECREF(sub);
1863 SET_TOP(res);
1864 if (res == NULL)
1865 goto error;
1866 DISPATCH();
1867 }
1868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 PyObject *right = POP();
1871 PyObject *left = TOP();
1872 PyObject *res = PyNumber_Lshift(left, right);
1873 Py_DECREF(left);
1874 Py_DECREF(right);
1875 SET_TOP(res);
1876 if (res == NULL)
1877 goto error;
1878 DISPATCH();
1879 }
1880
Benjamin Petersonddd19492018-09-16 22:38:02 -07001881 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 PyObject *right = POP();
1883 PyObject *left = TOP();
1884 PyObject *res = PyNumber_Rshift(left, right);
1885 Py_DECREF(left);
1886 Py_DECREF(right);
1887 SET_TOP(res);
1888 if (res == NULL)
1889 goto error;
1890 DISPATCH();
1891 }
1892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyObject *right = POP();
1895 PyObject *left = TOP();
1896 PyObject *res = PyNumber_And(left, right);
1897 Py_DECREF(left);
1898 Py_DECREF(right);
1899 SET_TOP(res);
1900 if (res == NULL)
1901 goto error;
1902 DISPATCH();
1903 }
1904
Benjamin Petersonddd19492018-09-16 22:38:02 -07001905 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PyObject *right = POP();
1907 PyObject *left = TOP();
1908 PyObject *res = PyNumber_Xor(left, right);
1909 Py_DECREF(left);
1910 Py_DECREF(right);
1911 SET_TOP(res);
1912 if (res == NULL)
1913 goto error;
1914 DISPATCH();
1915 }
1916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 PyObject *right = POP();
1919 PyObject *left = TOP();
1920 PyObject *res = PyNumber_Or(left, right);
1921 Py_DECREF(left);
1922 Py_DECREF(right);
1923 SET_TOP(res);
1924 if (res == NULL)
1925 goto error;
1926 DISPATCH();
1927 }
1928
Benjamin Petersonddd19492018-09-16 22:38:02 -07001929 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 PyObject *v = POP();
1931 PyObject *list = PEEK(oparg);
1932 int err;
1933 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 if (err != 0)
1936 goto error;
1937 PREDICT(JUMP_ABSOLUTE);
1938 DISPATCH();
1939 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Benjamin Petersonddd19492018-09-16 22:38:02 -07001941 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001943 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 int err;
1945 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 if (err != 0)
1948 goto error;
1949 PREDICT(JUMP_ABSOLUTE);
1950 DISPATCH();
1951 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Benjamin Petersonddd19492018-09-16 22:38:02 -07001953 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 PyObject *exp = POP();
1955 PyObject *base = TOP();
1956 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1957 Py_DECREF(base);
1958 Py_DECREF(exp);
1959 SET_TOP(res);
1960 if (res == NULL)
1961 goto error;
1962 DISPATCH();
1963 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Benjamin Petersonddd19492018-09-16 22:38:02 -07001965 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 PyObject *right = POP();
1967 PyObject *left = TOP();
1968 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1969 Py_DECREF(left);
1970 Py_DECREF(right);
1971 SET_TOP(res);
1972 if (res == NULL)
1973 goto error;
1974 DISPATCH();
1975 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001978 PyObject *right = POP();
1979 PyObject *left = TOP();
1980 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1981 Py_DECREF(left);
1982 Py_DECREF(right);
1983 SET_TOP(res);
1984 if (res == NULL)
1985 goto error;
1986 DISPATCH();
1987 }
1988
Benjamin Petersonddd19492018-09-16 22:38:02 -07001989 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 PyObject *divisor = POP();
1991 PyObject *dividend = TOP();
1992 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1993 Py_DECREF(dividend);
1994 Py_DECREF(divisor);
1995 SET_TOP(quotient);
1996 if (quotient == NULL)
1997 goto error;
1998 DISPATCH();
1999 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002000
Benjamin Petersonddd19492018-09-16 22:38:02 -07002001 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 PyObject *divisor = POP();
2003 PyObject *dividend = TOP();
2004 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2005 Py_DECREF(dividend);
2006 Py_DECREF(divisor);
2007 SET_TOP(quotient);
2008 if (quotient == NULL)
2009 goto error;
2010 DISPATCH();
2011 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Benjamin Petersonddd19492018-09-16 22:38:02 -07002013 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 PyObject *right = POP();
2015 PyObject *left = TOP();
2016 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2017 Py_DECREF(left);
2018 Py_DECREF(right);
2019 SET_TOP(mod);
2020 if (mod == NULL)
2021 goto error;
2022 DISPATCH();
2023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Benjamin Petersonddd19492018-09-16 22:38:02 -07002025 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 PyObject *right = POP();
2027 PyObject *left = TOP();
2028 PyObject *sum;
2029 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002030 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002031 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002032 }
2033 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 sum = PyNumber_InPlaceAdd(left, right);
2035 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002036 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 Py_DECREF(right);
2038 SET_TOP(sum);
2039 if (sum == NULL)
2040 goto error;
2041 DISPATCH();
2042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Benjamin Petersonddd19492018-09-16 22:38:02 -07002044 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 PyObject *right = POP();
2046 PyObject *left = TOP();
2047 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2048 Py_DECREF(left);
2049 Py_DECREF(right);
2050 SET_TOP(diff);
2051 if (diff == NULL)
2052 goto error;
2053 DISPATCH();
2054 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002055
Benjamin Petersonddd19492018-09-16 22:38:02 -07002056 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 PyObject *right = POP();
2058 PyObject *left = TOP();
2059 PyObject *res = PyNumber_InPlaceLshift(left, right);
2060 Py_DECREF(left);
2061 Py_DECREF(right);
2062 SET_TOP(res);
2063 if (res == NULL)
2064 goto error;
2065 DISPATCH();
2066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Benjamin Petersonddd19492018-09-16 22:38:02 -07002068 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 PyObject *right = POP();
2070 PyObject *left = TOP();
2071 PyObject *res = PyNumber_InPlaceRshift(left, right);
2072 Py_DECREF(left);
2073 Py_DECREF(right);
2074 SET_TOP(res);
2075 if (res == NULL)
2076 goto error;
2077 DISPATCH();
2078 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Benjamin Petersonddd19492018-09-16 22:38:02 -07002080 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 PyObject *right = POP();
2082 PyObject *left = TOP();
2083 PyObject *res = PyNumber_InPlaceAnd(left, right);
2084 Py_DECREF(left);
2085 Py_DECREF(right);
2086 SET_TOP(res);
2087 if (res == NULL)
2088 goto error;
2089 DISPATCH();
2090 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Benjamin Petersonddd19492018-09-16 22:38:02 -07002092 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 PyObject *right = POP();
2094 PyObject *left = TOP();
2095 PyObject *res = PyNumber_InPlaceXor(left, right);
2096 Py_DECREF(left);
2097 Py_DECREF(right);
2098 SET_TOP(res);
2099 if (res == NULL)
2100 goto error;
2101 DISPATCH();
2102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Benjamin Petersonddd19492018-09-16 22:38:02 -07002104 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PyObject *right = POP();
2106 PyObject *left = TOP();
2107 PyObject *res = PyNumber_InPlaceOr(left, right);
2108 Py_DECREF(left);
2109 Py_DECREF(right);
2110 SET_TOP(res);
2111 if (res == NULL)
2112 goto error;
2113 DISPATCH();
2114 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002115
Benjamin Petersonddd19492018-09-16 22:38:02 -07002116 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PyObject *sub = TOP();
2118 PyObject *container = SECOND();
2119 PyObject *v = THIRD();
2120 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002121 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002122 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 Py_DECREF(container);
2126 Py_DECREF(sub);
2127 if (err != 0)
2128 goto error;
2129 DISPATCH();
2130 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Benjamin Petersonddd19492018-09-16 22:38:02 -07002132 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 PyObject *sub = TOP();
2134 PyObject *container = SECOND();
2135 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002136 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002137 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 err = PyObject_DelItem(container, sub);
2139 Py_DECREF(container);
2140 Py_DECREF(sub);
2141 if (err != 0)
2142 goto error;
2143 DISPATCH();
2144 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002145
Benjamin Petersonddd19492018-09-16 22:38:02 -07002146 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002147 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002149 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002150 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002152 _PyErr_SetString(tstate, PyExc_RuntimeError,
2153 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 Py_DECREF(value);
2155 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002157 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 Py_DECREF(value);
2159 if (res == NULL)
2160 goto error;
2161 Py_DECREF(res);
2162 DISPATCH();
2163 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002164
Benjamin Petersonddd19492018-09-16 22:38:02 -07002165 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 switch (oparg) {
2168 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002170 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002173 /* fall through */
2174 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002175 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002176 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 break;
2179 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002180 _PyErr_SetString(tstate, PyExc_SystemError,
2181 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 break;
2183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 goto error;
2185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002186
Benjamin Petersonddd19492018-09-16 22:38:02 -07002187 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002189 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002190 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002191 f->f_state = FRAME_RETURNED;
2192 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002193 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002195
Benjamin Petersonddd19492018-09-16 22:38:02 -07002196 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002197 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002198 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002199 PyObject *obj = TOP();
2200 PyTypeObject *type = Py_TYPE(obj);
2201
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002202 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002203 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002204 }
Yury Selivanov75445082015-05-11 22:57:16 -04002205
2206 if (getter != NULL) {
2207 iter = (*getter)(obj);
2208 Py_DECREF(obj);
2209 if (iter == NULL) {
2210 SET_TOP(NULL);
2211 goto error;
2212 }
2213 }
2214 else {
2215 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002216 _PyErr_Format(tstate, PyExc_TypeError,
2217 "'async for' requires an object with "
2218 "__aiter__ method, got %.100s",
2219 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002220 Py_DECREF(obj);
2221 goto error;
2222 }
2223
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002224 if (Py_TYPE(iter)->tp_as_async == NULL ||
2225 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002226
Yury Selivanov398ff912017-03-02 22:20:00 -05002227 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002228 _PyErr_Format(tstate, PyExc_TypeError,
2229 "'async for' received an object from __aiter__ "
2230 "that does not implement __anext__: %.100s",
2231 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002232 Py_DECREF(iter);
2233 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002234 }
2235
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002236 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002237 DISPATCH();
2238 }
2239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002241 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002242 PyObject *next_iter = NULL;
2243 PyObject *awaitable = NULL;
2244 PyObject *aiter = TOP();
2245 PyTypeObject *type = Py_TYPE(aiter);
2246
Yury Selivanoveb636452016-09-08 22:01:51 -07002247 if (PyAsyncGen_CheckExact(aiter)) {
2248 awaitable = type->tp_as_async->am_anext(aiter);
2249 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002250 goto error;
2251 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002252 } else {
2253 if (type->tp_as_async != NULL){
2254 getter = type->tp_as_async->am_anext;
2255 }
Yury Selivanov75445082015-05-11 22:57:16 -04002256
Yury Selivanoveb636452016-09-08 22:01:51 -07002257 if (getter != NULL) {
2258 next_iter = (*getter)(aiter);
2259 if (next_iter == NULL) {
2260 goto error;
2261 }
2262 }
2263 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002264 _PyErr_Format(tstate, PyExc_TypeError,
2265 "'async for' requires an iterator with "
2266 "__anext__ method, got %.100s",
2267 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002268 goto error;
2269 }
Yury Selivanov75445082015-05-11 22:57:16 -04002270
Yury Selivanoveb636452016-09-08 22:01:51 -07002271 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2272 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002273 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002274 PyExc_TypeError,
2275 "'async for' received an invalid object "
2276 "from __anext__: %.100s",
2277 Py_TYPE(next_iter)->tp_name);
2278
2279 Py_DECREF(next_iter);
2280 goto error;
2281 } else {
2282 Py_DECREF(next_iter);
2283 }
2284 }
Yury Selivanov75445082015-05-11 22:57:16 -04002285
2286 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002287 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002288 DISPATCH();
2289 }
2290
Benjamin Petersonddd19492018-09-16 22:38:02 -07002291 case TARGET(GET_AWAITABLE): {
2292 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002293 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002294 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002295
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002296 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002297 int opcode_at_minus_3 = 0;
2298 if ((next_instr - first_instr) > 2) {
2299 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2300 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002301 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002302 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002303 _Py_OPCODE(next_instr[-2]));
2304 }
2305
Yury Selivanov75445082015-05-11 22:57:16 -04002306 Py_DECREF(iterable);
2307
Yury Selivanovc724bae2016-03-02 11:30:46 -05002308 if (iter != NULL && PyCoro_CheckExact(iter)) {
2309 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2310 if (yf != NULL) {
2311 /* `iter` is a coroutine object that is being
2312 awaited, `yf` is a pointer to the current awaitable
2313 being awaited on. */
2314 Py_DECREF(yf);
2315 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002316 _PyErr_SetString(tstate, PyExc_RuntimeError,
2317 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002318 /* The code below jumps to `error` if `iter` is NULL. */
2319 }
2320 }
2321
Yury Selivanov75445082015-05-11 22:57:16 -04002322 SET_TOP(iter); /* Even if it's NULL */
2323
2324 if (iter == NULL) {
2325 goto error;
2326 }
2327
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002328 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002329 DISPATCH();
2330 }
2331
Benjamin Petersonddd19492018-09-16 22:38:02 -07002332 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002334 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002335 PySendResult gen_status;
2336 if (tstate->c_tracefunc == NULL) {
2337 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002338 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002339 _Py_IDENTIFIER(send);
2340 if (v == Py_None && PyIter_Check(receiver)) {
2341 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002342 }
2343 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002344 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002345 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002346 if (retval == NULL) {
2347 if (tstate->c_tracefunc != NULL
2348 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2349 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2350 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2351 gen_status = PYGEN_RETURN;
2352 }
2353 else {
2354 gen_status = PYGEN_ERROR;
2355 }
2356 }
2357 else {
2358 gen_status = PYGEN_NEXT;
2359 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002362 if (gen_status == PYGEN_ERROR) {
2363 assert (retval == NULL);
2364 goto error;
2365 }
2366 if (gen_status == PYGEN_RETURN) {
2367 assert (retval != NULL);
2368
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002369 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002370 SET_TOP(retval);
2371 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002373 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002374 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002375 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002376 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002377 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002378 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002379 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002380 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002381 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002383
Benjamin Petersonddd19492018-09-16 22:38:02 -07002384 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002386
2387 if (co->co_flags & CO_ASYNC_GENERATOR) {
2388 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2389 Py_DECREF(retval);
2390 if (w == NULL) {
2391 retval = NULL;
2392 goto error;
2393 }
2394 retval = w;
2395 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002396 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002397 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002398 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002400
Benjamin Petersonddd19492018-09-16 22:38:02 -07002401 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002402 PyObject *type, *value, *traceback;
2403 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 PyTryBlock *b = PyFrame_BlockPop(f);
2405 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002406 _PyErr_SetString(tstate, PyExc_SystemError,
2407 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002410 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2411 STACK_LEVEL() <= (b)->b_level + 4);
2412 exc_info = tstate->exc_info;
2413 type = exc_info->exc_type;
2414 value = exc_info->exc_value;
2415 traceback = exc_info->exc_traceback;
2416 exc_info->exc_type = POP();
2417 exc_info->exc_value = POP();
2418 exc_info->exc_traceback = POP();
2419 Py_XDECREF(type);
2420 Py_XDECREF(value);
2421 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002424
Benjamin Petersonddd19492018-09-16 22:38:02 -07002425 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002426 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002429
Mark Shannonfee55262019-11-21 09:11:43 +00002430 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002431 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002432 PyObject *val = POP();
2433 PyObject *tb = POP();
2434 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002435 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002436 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002438
Benjamin Petersonddd19492018-09-16 22:38:02 -07002439 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002440 PyObject *exc = POP();
2441 assert(PyExceptionClass_Check(exc));
2442 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2443 PyTryBlock *b = PyFrame_BlockPop(f);
2444 assert(b->b_type == EXCEPT_HANDLER);
2445 Py_DECREF(exc);
2446 UNWIND_EXCEPT_HANDLER(b);
2447 Py_DECREF(POP());
2448 JUMPBY(oparg);
2449 FAST_DISPATCH();
2450 }
2451 else {
2452 PyObject *val = POP();
2453 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002454 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002455 goto exception_unwind;
2456 }
2457 }
2458
Zackery Spytzce6a0702019-08-25 03:44:09 -06002459 case TARGET(LOAD_ASSERTION_ERROR): {
2460 PyObject *value = PyExc_AssertionError;
2461 Py_INCREF(value);
2462 PUSH(value);
2463 FAST_DISPATCH();
2464 }
2465
Benjamin Petersonddd19492018-09-16 22:38:02 -07002466 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002467 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002470 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002471 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 if (!_PyErr_Occurred(tstate)) {
2474 _PyErr_SetString(tstate, PyExc_NameError,
2475 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002480 }
2481 else {
2482 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2483 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002484 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2486 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002487 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2488 _PyErr_SetString(tstate, PyExc_NameError,
2489 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002491 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002493 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002494 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Benjamin Petersonddd19492018-09-16 22:38:02 -07002497 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 PyObject *name = GETITEM(names, oparg);
2499 PyObject *v = POP();
2500 PyObject *ns = f->f_locals;
2501 int err;
2502 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002503 _PyErr_Format(tstate, PyExc_SystemError,
2504 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 if (PyDict_CheckExact(ns))
2509 err = PyDict_SetItem(ns, name, v);
2510 else
2511 err = PyObject_SetItem(ns, name, v);
2512 Py_DECREF(v);
2513 if (err != 0)
2514 goto error;
2515 DISPATCH();
2516 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002517
Benjamin Petersonddd19492018-09-16 22:38:02 -07002518 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 PyObject *name = GETITEM(names, oparg);
2520 PyObject *ns = f->f_locals;
2521 int err;
2522 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002523 _PyErr_Format(tstate, PyExc_SystemError,
2524 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 err = PyObject_DelItem(ns, name);
2528 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 NAME_ERROR_MSG,
2531 name);
2532 goto error;
2533 }
2534 DISPATCH();
2535 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002536
Benjamin Petersonddd19492018-09-16 22:38:02 -07002537 case TARGET(UNPACK_SEQUENCE): {
2538 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 PyObject *seq = POP(), *item, **items;
2540 if (PyTuple_CheckExact(seq) &&
2541 PyTuple_GET_SIZE(seq) == oparg) {
2542 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002544 item = items[oparg];
2545 Py_INCREF(item);
2546 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 } else if (PyList_CheckExact(seq) &&
2549 PyList_GET_SIZE(seq) == oparg) {
2550 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 item = items[oparg];
2553 Py_INCREF(item);
2554 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002558 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 } else {
2560 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 Py_DECREF(seq);
2562 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002565 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002567
Benjamin Petersonddd19492018-09-16 22:38:02 -07002568 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2570 PyObject *seq = POP();
2571
Victor Stinner438a12d2019-05-24 17:01:38 +02002572 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 stack_pointer + totalargs)) {
2574 stack_pointer += totalargs;
2575 } else {
2576 Py_DECREF(seq);
2577 goto error;
2578 }
2579 Py_DECREF(seq);
2580 DISPATCH();
2581 }
2582
Benjamin Petersonddd19492018-09-16 22:38:02 -07002583 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 PyObject *name = GETITEM(names, oparg);
2585 PyObject *owner = TOP();
2586 PyObject *v = SECOND();
2587 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002588 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 Py_DECREF(owner);
2592 if (err != 0)
2593 goto error;
2594 DISPATCH();
2595 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002596
Benjamin Petersonddd19492018-09-16 22:38:02 -07002597 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 PyObject *name = GETITEM(names, oparg);
2599 PyObject *owner = POP();
2600 int err;
2601 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2602 Py_DECREF(owner);
2603 if (err != 0)
2604 goto error;
2605 DISPATCH();
2606 }
2607
Benjamin Petersonddd19492018-09-16 22:38:02 -07002608 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PyObject *name = GETITEM(names, oparg);
2610 PyObject *v = POP();
2611 int err;
2612 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 if (err != 0)
2615 goto error;
2616 DISPATCH();
2617 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002618
Benjamin Petersonddd19492018-09-16 22:38:02 -07002619 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 PyObject *name = GETITEM(names, oparg);
2621 int err;
2622 err = PyDict_DelItem(f->f_globals, name);
2623 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002624 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2625 format_exc_check_arg(tstate, PyExc_NameError,
2626 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002627 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002629 }
2630 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002632
Benjamin Petersonddd19492018-09-16 22:38:02 -07002633 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 PyObject *name = GETITEM(names, oparg);
2635 PyObject *locals = f->f_locals;
2636 PyObject *v;
2637 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002638 _PyErr_Format(tstate, PyExc_SystemError,
2639 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002643 v = PyDict_GetItemWithError(locals, name);
2644 if (v != NULL) {
2645 Py_INCREF(v);
2646 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002648 goto error;
2649 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
2651 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002653 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002655 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002656 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 }
2658 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002660 v = PyDict_GetItemWithError(f->f_globals, name);
2661 if (v != NULL) {
2662 Py_INCREF(v);
2663 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002664 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002665 goto error;
2666 }
2667 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002668 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002669 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002671 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002672 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002673 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002675 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002677 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002679 }
2680 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 v = PyObject_GetItem(f->f_builtins, name);
2682 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002683 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002684 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002685 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002687 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002689 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002696
Benjamin Petersonddd19492018-09-16 22:38:02 -07002697 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002698 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002700 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002701 && PyDict_CheckExact(f->f_builtins))
2702 {
Inada Naoki91234a12019-06-03 21:30:58 +09002703 OPCACHE_CHECK();
2704 if (co_opcache != NULL && co_opcache->optimized > 0) {
2705 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2706
2707 if (lg->globals_ver ==
2708 ((PyDictObject *)f->f_globals)->ma_version_tag
2709 && lg->builtins_ver ==
2710 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2711 {
2712 PyObject *ptr = lg->ptr;
2713 OPCACHE_STAT_GLOBAL_HIT();
2714 assert(ptr != NULL);
2715 Py_INCREF(ptr);
2716 PUSH(ptr);
2717 DISPATCH();
2718 }
2719 }
2720
2721 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002722 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002723 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 name);
2725 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002726 if (!_PyErr_OCCURRED()) {
2727 /* _PyDict_LoadGlobal() returns NULL without raising
2728 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002729 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002730 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002731 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 }
Inada Naoki91234a12019-06-03 21:30:58 +09002734
2735 if (co_opcache != NULL) {
2736 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2737
2738 if (co_opcache->optimized == 0) {
2739 /* Wasn't optimized before. */
2740 OPCACHE_STAT_GLOBAL_OPT();
2741 } else {
2742 OPCACHE_STAT_GLOBAL_MISS();
2743 }
2744
2745 co_opcache->optimized = 1;
2746 lg->globals_ver =
2747 ((PyDictObject *)f->f_globals)->ma_version_tag;
2748 lg->builtins_ver =
2749 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2750 lg->ptr = v; /* borrowed */
2751 }
2752
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002755 else {
2756 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002757
2758 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002759 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 v = PyObject_GetItem(f->f_globals, name);
2761 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002762 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002763 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002764 }
2765 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002766
Victor Stinnerb4efc962015-11-20 09:24:02 +01002767 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 v = PyObject_GetItem(f->f_builtins, name);
2769 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002770 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002771 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002772 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002773 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002774 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002776 }
2777 }
2778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002782
Benjamin Petersonddd19492018-09-16 22:38:02 -07002783 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 PyObject *v = GETLOCAL(oparg);
2785 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 SETLOCAL(oparg, NULL);
2787 DISPATCH();
2788 }
2789 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002790 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 UNBOUNDLOCAL_ERROR_MSG,
2792 PyTuple_GetItem(co->co_varnames, oparg)
2793 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 goto error;
2795 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002796
Benjamin Petersonddd19492018-09-16 22:38:02 -07002797 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002799 PyObject *oldobj = PyCell_GET(cell);
2800 if (oldobj != NULL) {
2801 PyCell_SET(cell, NULL);
2802 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002803 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002804 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002805 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 goto error;
2807 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002808
Benjamin Petersonddd19492018-09-16 22:38:02 -07002809 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 PyObject *cell = freevars[oparg];
2811 Py_INCREF(cell);
2812 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002815
Benjamin Petersonddd19492018-09-16 22:38:02 -07002816 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002817 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002818 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002819 assert(locals);
2820 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2821 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2822 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2823 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2824 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002825 value = PyDict_GetItemWithError(locals, name);
2826 if (value != NULL) {
2827 Py_INCREF(value);
2828 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002829 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002830 goto error;
2831 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002832 }
2833 else {
2834 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002835 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002836 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002837 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002838 }
2839 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002840 }
2841 }
2842 if (!value) {
2843 PyObject *cell = freevars[oparg];
2844 value = PyCell_GET(cell);
2845 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002846 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002847 goto error;
2848 }
2849 Py_INCREF(value);
2850 }
2851 PUSH(value);
2852 DISPATCH();
2853 }
2854
Benjamin Petersonddd19492018-09-16 22:38:02 -07002855 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 PyObject *cell = freevars[oparg];
2857 PyObject *value = PyCell_GET(cell);
2858 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002859 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 Py_INCREF(value);
2863 PUSH(value);
2864 DISPATCH();
2865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Benjamin Petersonddd19492018-09-16 22:38:02 -07002867 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 PyObject *v = POP();
2869 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002870 PyObject *oldobj = PyCell_GET(cell);
2871 PyCell_SET(cell, v);
2872 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 DISPATCH();
2874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Benjamin Petersonddd19492018-09-16 22:38:02 -07002876 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002877 PyObject *str;
2878 PyObject *empty = PyUnicode_New(0, 0);
2879 if (empty == NULL) {
2880 goto error;
2881 }
2882 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2883 Py_DECREF(empty);
2884 if (str == NULL)
2885 goto error;
2886 while (--oparg >= 0) {
2887 PyObject *item = POP();
2888 Py_DECREF(item);
2889 }
2890 PUSH(str);
2891 DISPATCH();
2892 }
2893
Benjamin Petersonddd19492018-09-16 22:38:02 -07002894 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 PyObject *tup = PyTuple_New(oparg);
2896 if (tup == NULL)
2897 goto error;
2898 while (--oparg >= 0) {
2899 PyObject *item = POP();
2900 PyTuple_SET_ITEM(tup, oparg, item);
2901 }
2902 PUSH(tup);
2903 DISPATCH();
2904 }
2905
Benjamin Petersonddd19492018-09-16 22:38:02 -07002906 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 PyObject *list = PyList_New(oparg);
2908 if (list == NULL)
2909 goto error;
2910 while (--oparg >= 0) {
2911 PyObject *item = POP();
2912 PyList_SET_ITEM(list, oparg, item);
2913 }
2914 PUSH(list);
2915 DISPATCH();
2916 }
2917
Mark Shannon13bc1392020-01-23 09:25:17 +00002918 case TARGET(LIST_TO_TUPLE): {
2919 PyObject *list = POP();
2920 PyObject *tuple = PyList_AsTuple(list);
2921 Py_DECREF(list);
2922 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002923 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002924 }
2925 PUSH(tuple);
2926 DISPATCH();
2927 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002928
Mark Shannon13bc1392020-01-23 09:25:17 +00002929 case TARGET(LIST_EXTEND): {
2930 PyObject *iterable = POP();
2931 PyObject *list = PEEK(oparg);
2932 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2933 if (none_val == NULL) {
2934 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002935 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002936 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002937 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002938 _PyErr_Format(tstate, PyExc_TypeError,
2939 "Value after * must be an iterable, not %.200s",
2940 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002941 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002942 Py_DECREF(iterable);
2943 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002944 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002945 Py_DECREF(none_val);
2946 Py_DECREF(iterable);
2947 DISPATCH();
2948 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002949
Mark Shannon13bc1392020-01-23 09:25:17 +00002950 case TARGET(SET_UPDATE): {
2951 PyObject *iterable = POP();
2952 PyObject *set = PEEK(oparg);
2953 int err = _PySet_Update(set, iterable);
2954 Py_DECREF(iterable);
2955 if (err < 0) {
2956 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002957 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002958 DISPATCH();
2959 }
2960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 PyObject *set = PySet_New(NULL);
2963 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002964 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 if (set == NULL)
2966 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002967 for (i = oparg; i > 0; i--) {
2968 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 if (err == 0)
2970 err = PySet_Add(set, item);
2971 Py_DECREF(item);
2972 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002973 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 if (err != 0) {
2975 Py_DECREF(set);
2976 goto error;
2977 }
2978 PUSH(set);
2979 DISPATCH();
2980 }
2981
Benjamin Petersonddd19492018-09-16 22:38:02 -07002982 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002983 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2985 if (map == NULL)
2986 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002987 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002988 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002989 PyObject *key = PEEK(2*i);
2990 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002991 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002992 if (err != 0) {
2993 Py_DECREF(map);
2994 goto error;
2995 }
2996 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002997
2998 while (oparg--) {
2999 Py_DECREF(POP());
3000 Py_DECREF(POP());
3001 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 PUSH(map);
3003 DISPATCH();
3004 }
3005
Benjamin Petersonddd19492018-09-16 22:38:02 -07003006 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003007 _Py_IDENTIFIER(__annotations__);
3008 int err;
3009 PyObject *ann_dict;
3010 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003011 _PyErr_Format(tstate, PyExc_SystemError,
3012 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003013 goto error;
3014 }
3015 /* check if __annotations__ in locals()... */
3016 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003017 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003018 &PyId___annotations__);
3019 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003020 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003021 goto error;
3022 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003023 /* ...if not, create a new one */
3024 ann_dict = PyDict_New();
3025 if (ann_dict == NULL) {
3026 goto error;
3027 }
3028 err = _PyDict_SetItemId(f->f_locals,
3029 &PyId___annotations__, ann_dict);
3030 Py_DECREF(ann_dict);
3031 if (err != 0) {
3032 goto error;
3033 }
3034 }
3035 }
3036 else {
3037 /* do the same if locals() is not a dict */
3038 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3039 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003040 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003041 }
3042 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3043 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003044 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003045 goto error;
3046 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003047 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003048 ann_dict = PyDict_New();
3049 if (ann_dict == NULL) {
3050 goto error;
3051 }
3052 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3053 Py_DECREF(ann_dict);
3054 if (err != 0) {
3055 goto error;
3056 }
3057 }
3058 else {
3059 Py_DECREF(ann_dict);
3060 }
3061 }
3062 DISPATCH();
3063 }
3064
Benjamin Petersonddd19492018-09-16 22:38:02 -07003065 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003066 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003067 PyObject *map;
3068 PyObject *keys = TOP();
3069 if (!PyTuple_CheckExact(keys) ||
3070 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003071 _PyErr_SetString(tstate, PyExc_SystemError,
3072 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003073 goto error;
3074 }
3075 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3076 if (map == NULL) {
3077 goto error;
3078 }
3079 for (i = oparg; i > 0; i--) {
3080 int err;
3081 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3082 PyObject *value = PEEK(i + 1);
3083 err = PyDict_SetItem(map, key, value);
3084 if (err != 0) {
3085 Py_DECREF(map);
3086 goto error;
3087 }
3088 }
3089
3090 Py_DECREF(POP());
3091 while (oparg--) {
3092 Py_DECREF(POP());
3093 }
3094 PUSH(map);
3095 DISPATCH();
3096 }
3097
Mark Shannon8a4cd702020-01-27 09:57:45 +00003098 case TARGET(DICT_UPDATE): {
3099 PyObject *update = POP();
3100 PyObject *dict = PEEK(oparg);
3101 if (PyDict_Update(dict, update) < 0) {
3102 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3103 _PyErr_Format(tstate, PyExc_TypeError,
3104 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003105 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003106 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003107 Py_DECREF(update);
3108 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003110 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003111 DISPATCH();
3112 }
3113
Mark Shannon8a4cd702020-01-27 09:57:45 +00003114 case TARGET(DICT_MERGE): {
3115 PyObject *update = POP();
3116 PyObject *dict = PEEK(oparg);
3117
3118 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3119 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3120 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003121 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003122 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003123 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003124 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003125 DISPATCH();
3126 }
3127
Benjamin Petersonddd19492018-09-16 22:38:02 -07003128 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003129 PyObject *value = TOP();
3130 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 PyObject *map;
3132 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003133 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003134 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003136 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 Py_DECREF(value);
3138 Py_DECREF(key);
3139 if (err != 0)
3140 goto error;
3141 PREDICT(JUMP_ABSOLUTE);
3142 DISPATCH();
3143 }
3144
Benjamin Petersonddd19492018-09-16 22:38:02 -07003145 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 PyObject *name = GETITEM(names, oparg);
3147 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003148
3149 PyTypeObject *type = Py_TYPE(owner);
3150 PyObject *res;
3151 PyObject **dictptr;
3152 PyObject *dict;
3153 _PyOpCodeOpt_LoadAttr *la;
3154
3155 OPCACHE_STAT_ATTR_TOTAL();
3156
3157 OPCACHE_CHECK();
3158 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3159 {
3160 if (co_opcache->optimized > 0) {
3161 /* Fast path -- cache hit makes LOAD_ATTR ~30% faster */
3162 la = &co_opcache->u.la;
3163 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3164 {
3165 assert(type->tp_dict != NULL);
3166 assert(type->tp_dictoffset > 0);
3167
3168 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3169 dict = *dictptr;
3170 if (dict != NULL && PyDict_CheckExact(dict)) {
3171 Py_ssize_t hint = la->hint;
3172 Py_INCREF(dict);
3173 res = NULL;
3174 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3175
3176 if (res != NULL) {
3177 if (la->hint == hint && hint >= 0) {
3178 /* Our hint has helped -- cache hit. */
3179 OPCACHE_STAT_ATTR_HIT();
3180 } else {
3181 /* The hint we provided didn't work.
3182 Maybe next time? */
3183 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3184 }
3185
3186 Py_INCREF(res);
3187 SET_TOP(res);
3188 Py_DECREF(owner);
3189 Py_DECREF(dict);
3190 DISPATCH();
3191 } else {
3192 // This attribute can be missing sometimes -- we
3193 // don't want to optimize this lookup.
3194 OPCACHE_DEOPT_LOAD_ATTR();
3195 Py_DECREF(dict);
3196 }
3197 } else {
3198 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3199 OPCACHE_DEOPT_LOAD_ATTR();
3200 }
3201 } else {
3202 // The type of the object has either been updated,
3203 // or is different. Maybe it will stabilize?
3204 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3205 }
3206
3207 OPCACHE_STAT_ATTR_MISS();
3208 }
3209
3210 if (co_opcache != NULL && /* co_opcache can be NULL after a DEOPT() call. */
3211 type->tp_getattro == PyObject_GenericGetAttr)
3212 {
Pablo Galindo109826c2020-10-20 06:22:44 +01003213 Py_ssize_t ret;
3214
3215 if (type->tp_dictoffset > 0) {
3216 if (type->tp_dict == NULL) {
3217 if (PyType_Ready(type) < 0) {
3218 Py_DECREF(owner);
3219 SET_TOP(NULL);
3220 goto error;
3221 }
3222 }
Pablo Galindo80449f22020-11-05 09:23:15 +00003223 if (_PyType_Lookup(type, name) == NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003224 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3225 dict = *dictptr;
3226
3227 if (dict != NULL && PyDict_CheckExact(dict)) {
3228 Py_INCREF(dict);
3229 res = NULL;
3230 ret = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3231 if (res != NULL) {
3232 Py_INCREF(res);
3233 Py_DECREF(dict);
3234 Py_DECREF(owner);
3235 SET_TOP(res);
3236
3237 if (co_opcache->optimized == 0) {
3238 // First time we optimize this opcode. */
3239 OPCACHE_STAT_ATTR_OPT();
3240 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3241 }
3242
3243 la = &co_opcache->u.la;
3244 la->type = type;
3245 la->tp_version_tag = type->tp_version_tag;
3246 la->hint = ret;
3247
3248 DISPATCH();
3249 }
3250 Py_DECREF(dict);
3251 } else {
3252 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3253 OPCACHE_DEOPT_LOAD_ATTR();
3254 }
3255 } else {
3256 // We failed to find an attribute without a data-like descriptor
3257 OPCACHE_DEOPT_LOAD_ATTR();
3258 }
3259 } else {
3260 // The object's class does not have a tp_dictoffset we can use
3261 OPCACHE_DEOPT_LOAD_ATTR();
3262 }
3263 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3264 OPCACHE_DEOPT_LOAD_ATTR();
3265 }
3266 }
3267
3268 /* slow path */
3269 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 Py_DECREF(owner);
3271 SET_TOP(res);
3272 if (res == NULL)
3273 goto error;
3274 DISPATCH();
3275 }
3276
Benjamin Petersonddd19492018-09-16 22:38:02 -07003277 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003278 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 PyObject *right = POP();
3280 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003281 PyObject *res = PyObject_RichCompare(left, right, oparg);
3282 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 Py_DECREF(left);
3284 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 if (res == NULL)
3286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 PREDICT(POP_JUMP_IF_FALSE);
3288 PREDICT(POP_JUMP_IF_TRUE);
3289 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003291
Mark Shannon9af0e472020-01-14 10:12:45 +00003292 case TARGET(IS_OP): {
3293 PyObject *right = POP();
3294 PyObject *left = TOP();
3295 int res = (left == right)^oparg;
3296 PyObject *b = res ? Py_True : Py_False;
3297 Py_INCREF(b);
3298 SET_TOP(b);
3299 Py_DECREF(left);
3300 Py_DECREF(right);
3301 PREDICT(POP_JUMP_IF_FALSE);
3302 PREDICT(POP_JUMP_IF_TRUE);
3303 FAST_DISPATCH();
3304 }
3305
3306 case TARGET(CONTAINS_OP): {
3307 PyObject *right = POP();
3308 PyObject *left = POP();
3309 int res = PySequence_Contains(right, left);
3310 Py_DECREF(left);
3311 Py_DECREF(right);
3312 if (res < 0) {
3313 goto error;
3314 }
3315 PyObject *b = (res^oparg) ? Py_True : Py_False;
3316 Py_INCREF(b);
3317 PUSH(b);
3318 PREDICT(POP_JUMP_IF_FALSE);
3319 PREDICT(POP_JUMP_IF_TRUE);
3320 FAST_DISPATCH();
3321 }
3322
3323#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3324 "BaseException is not allowed"
3325
3326 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3327 PyObject *right = POP();
3328 PyObject *left = POP();
3329 if (PyTuple_Check(right)) {
3330 Py_ssize_t i, length;
3331 length = PyTuple_GET_SIZE(right);
3332 for (i = 0; i < length; i++) {
3333 PyObject *exc = PyTuple_GET_ITEM(right, i);
3334 if (!PyExceptionClass_Check(exc)) {
3335 _PyErr_SetString(tstate, PyExc_TypeError,
3336 CANNOT_CATCH_MSG);
3337 Py_DECREF(left);
3338 Py_DECREF(right);
3339 goto error;
3340 }
3341 }
3342 }
3343 else {
3344 if (!PyExceptionClass_Check(right)) {
3345 _PyErr_SetString(tstate, PyExc_TypeError,
3346 CANNOT_CATCH_MSG);
3347 Py_DECREF(left);
3348 Py_DECREF(right);
3349 goto error;
3350 }
3351 }
3352 int res = PyErr_GivenExceptionMatches(left, right);
3353 Py_DECREF(left);
3354 Py_DECREF(right);
3355 if (res > 0) {
3356 /* Exception matches -- Do nothing */;
3357 }
3358 else if (res == 0) {
3359 JUMPTO(oparg);
3360 }
3361 else {
3362 goto error;
3363 }
3364 DISPATCH();
3365 }
3366
Benjamin Petersonddd19492018-09-16 22:38:02 -07003367 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003369 PyObject *fromlist = POP();
3370 PyObject *level = TOP();
3371 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003372 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003373 Py_DECREF(level);
3374 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 SET_TOP(res);
3376 if (res == NULL)
3377 goto error;
3378 DISPATCH();
3379 }
3380
Benjamin Petersonddd19492018-09-16 22:38:02 -07003381 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 PyObject *from = POP(), *locals;
3383 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003384 if (PyFrame_FastToLocalsWithError(f) < 0) {
3385 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003386 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003387 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003388
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003389 locals = f->f_locals;
3390 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003391 _PyErr_SetString(tstate, PyExc_SystemError,
3392 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003393 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003396 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 Py_DECREF(from);
3399 if (err != 0)
3400 goto error;
3401 DISPATCH();
3402 }
Guido van Rossum25831651993-05-19 14:50:45 +00003403
Benjamin Petersonddd19492018-09-16 22:38:02 -07003404 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 PyObject *name = GETITEM(names, oparg);
3406 PyObject *from = TOP();
3407 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003408 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 PUSH(res);
3410 if (res == NULL)
3411 goto error;
3412 DISPATCH();
3413 }
Thomas Wouters52152252000-08-17 22:55:00 +00003414
Benjamin Petersonddd19492018-09-16 22:38:02 -07003415 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 JUMPBY(oparg);
3417 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003419
Benjamin Petersonddd19492018-09-16 22:38:02 -07003420 case TARGET(POP_JUMP_IF_FALSE): {
3421 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 PyObject *cond = POP();
3423 int err;
3424 if (cond == Py_True) {
3425 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 FAST_DISPATCH();
3427 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 if (cond == Py_False) {
3429 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 JUMPTO(oparg);
3431 FAST_DISPATCH();
3432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 err = PyObject_IsTrue(cond);
3434 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003436 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 else if (err == 0)
3438 JUMPTO(oparg);
3439 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003442 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003443
Benjamin Petersonddd19492018-09-16 22:38:02 -07003444 case TARGET(POP_JUMP_IF_TRUE): {
3445 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 PyObject *cond = POP();
3447 int err;
3448 if (cond == Py_False) {
3449 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 FAST_DISPATCH();
3451 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 if (cond == Py_True) {
3453 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 JUMPTO(oparg);
3455 FAST_DISPATCH();
3456 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 err = PyObject_IsTrue(cond);
3458 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 JUMPTO(oparg);
3461 }
3462 else if (err == 0)
3463 ;
3464 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003468
Benjamin Petersonddd19492018-09-16 22:38:02 -07003469 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003470 PyObject *cond = TOP();
3471 int err;
3472 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003473 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003474 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 FAST_DISPATCH();
3476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 JUMPTO(oparg);
3479 FAST_DISPATCH();
3480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003483 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 }
3486 else if (err == 0)
3487 JUMPTO(oparg);
3488 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003489 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003491 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003492
Benjamin Petersonddd19492018-09-16 22:38:02 -07003493 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 PyObject *cond = TOP();
3495 int err;
3496 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003497 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 FAST_DISPATCH();
3500 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 JUMPTO(oparg);
3503 FAST_DISPATCH();
3504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 JUMPTO(oparg);
3508 }
3509 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003510 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 }
3513 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003514 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003516 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003517
Benjamin Petersonddd19492018-09-16 22:38:02 -07003518 case TARGET(JUMP_ABSOLUTE): {
3519 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003521#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 /* Enabling this path speeds-up all while and for-loops by bypassing
3523 the per-loop checks for signals. By default, this should be turned-off
3524 because it prevents detection of a control-break in tight loops like
3525 "while 1: pass". Compile with this option turned-on when you need
3526 the speed-up and do not need break checking inside tight loops (ones
3527 that contain only instructions ending with FAST_DISPATCH).
3528 */
3529 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003530#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003532#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003534
Benjamin Petersonddd19492018-09-16 22:38:02 -07003535 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003537 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003538 PyObject *iter = PyObject_GetIter(iterable);
3539 Py_DECREF(iterable);
3540 SET_TOP(iter);
3541 if (iter == NULL)
3542 goto error;
3543 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003544 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003545 DISPATCH();
3546 }
3547
Benjamin Petersonddd19492018-09-16 22:38:02 -07003548 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003549 /* before: [obj]; after [getiter(obj)] */
3550 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003551 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003552 if (PyCoro_CheckExact(iterable)) {
3553 /* `iterable` is a coroutine */
3554 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3555 /* and it is used in a 'yield from' expression of a
3556 regular generator. */
3557 Py_DECREF(iterable);
3558 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003559 _PyErr_SetString(tstate, PyExc_TypeError,
3560 "cannot 'yield from' a coroutine object "
3561 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003562 goto error;
3563 }
3564 }
3565 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003566 /* `iterable` is not a generator. */
3567 iter = PyObject_GetIter(iterable);
3568 Py_DECREF(iterable);
3569 SET_TOP(iter);
3570 if (iter == NULL)
3571 goto error;
3572 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003573 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 DISPATCH();
3575 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003576
Benjamin Petersonddd19492018-09-16 22:38:02 -07003577 case TARGET(FOR_ITER): {
3578 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003581 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 if (next != NULL) {
3583 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 PREDICT(STORE_FAST);
3585 PREDICT(UNPACK_SEQUENCE);
3586 DISPATCH();
3587 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003588 if (_PyErr_Occurred(tstate)) {
3589 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003590 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003591 }
3592 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003593 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003594 }
3595 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 }
3597 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003598 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 JUMPBY(oparg);
3601 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003603
Benjamin Petersonddd19492018-09-16 22:38:02 -07003604 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003605 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 STACK_LEVEL());
3607 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003608 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003609
Benjamin Petersonddd19492018-09-16 22:38:02 -07003610 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003611 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003612 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003613 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003614 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003615 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003616 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003617 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003618 }
3619 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3620 if (exit == NULL) {
3621 Py_DECREF(enter);
3622 goto error;
3623 }
Yury Selivanov75445082015-05-11 22:57:16 -04003624 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003625 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003626 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003627 Py_DECREF(enter);
3628 if (res == NULL)
3629 goto error;
3630 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003631 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003632 DISPATCH();
3633 }
3634
Benjamin Petersonddd19492018-09-16 22:38:02 -07003635 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003636 PyObject *res = POP();
3637 /* Setup the finally block before pushing the result
3638 of __aenter__ on the stack. */
3639 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3640 STACK_LEVEL());
3641 PUSH(res);
3642 DISPATCH();
3643 }
3644
Benjamin Petersonddd19492018-09-16 22:38:02 -07003645 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003646 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003647 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003648 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003649 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003650 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003651 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003652 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003653 }
3654 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003655 if (exit == NULL) {
3656 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003657 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003658 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003659 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003660 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003661 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003662 Py_DECREF(enter);
3663 if (res == NULL)
3664 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 /* Setup the finally block before pushing the result
3666 of __enter__ on the stack. */
3667 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3668 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003669
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003670 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 DISPATCH();
3672 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003673
Mark Shannonfee55262019-11-21 09:11:43 +00003674 case TARGET(WITH_EXCEPT_START): {
3675 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003677 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3678 - SEVENTH: the context.__exit__ bound method
3679 We call SEVENTH(TOP, SECOND, THIRD).
3680 Then we push again the TOP exception and the __exit__
3681 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003684 PyObject *exc, *val, *tb, *res;
3685
Victor Stinner842cfff2016-12-01 14:45:31 +01003686 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003687 val = SECOND();
3688 tb = THIRD();
3689 assert(exc != Py_None);
3690 assert(!PyLong_Check(exc));
3691 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003692 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003693 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003694 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003695 if (res == NULL)
3696 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003697
Yury Selivanov75445082015-05-11 22:57:16 -04003698 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003699 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003701
Benjamin Petersonddd19492018-09-16 22:38:02 -07003702 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003703 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003704 PyObject *name = GETITEM(names, oparg);
3705 PyObject *obj = TOP();
3706 PyObject *meth = NULL;
3707
3708 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3709
Yury Selivanovf2392132016-12-13 19:03:51 -05003710 if (meth == NULL) {
3711 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003712 goto error;
3713 }
3714
3715 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003716 /* We can bypass temporary bound method object.
3717 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003718
INADA Naoki015bce62017-01-16 17:23:30 +09003719 meth | self | arg1 | ... | argN
3720 */
3721 SET_TOP(meth);
3722 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003723 }
3724 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003725 /* meth is not an unbound method (but a regular attr, or
3726 something was returned by a descriptor protocol). Set
3727 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003728 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003729
3730 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003731 */
INADA Naoki015bce62017-01-16 17:23:30 +09003732 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003733 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003734 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003735 }
3736 DISPATCH();
3737 }
3738
Benjamin Petersonddd19492018-09-16 22:38:02 -07003739 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003740 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003741 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003742
3743 sp = stack_pointer;
3744
INADA Naoki015bce62017-01-16 17:23:30 +09003745 meth = PEEK(oparg + 2);
3746 if (meth == NULL) {
3747 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3748 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003749
3750 Stack layout:
3751
INADA Naoki015bce62017-01-16 17:23:30 +09003752 ... | NULL | callable | arg1 | ... | argN
3753 ^- TOP()
3754 ^- (-oparg)
3755 ^- (-oparg-1)
3756 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003757
Ville Skyttä49b27342017-08-03 09:00:59 +03003758 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003759 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003760 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003761 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003762 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003763 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003764 }
3765 else {
3766 /* This is a method call. Stack layout:
3767
INADA Naoki015bce62017-01-16 17:23:30 +09003768 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003769 ^- TOP()
3770 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003771 ^- (-oparg-1)
3772 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003773
INADA Naoki015bce62017-01-16 17:23:30 +09003774 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003775 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003776 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003777 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003778 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003779 stack_pointer = sp;
3780 }
3781
3782 PUSH(res);
3783 if (res == NULL)
3784 goto error;
3785 DISPATCH();
3786 }
3787
Benjamin Petersonddd19492018-09-16 22:38:02 -07003788 case TARGET(CALL_FUNCTION): {
3789 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003790 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003792 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003794 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003795 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003800
Benjamin Petersonddd19492018-09-16 22:38:02 -07003801 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003802 PyObject **sp, *res, *names;
3803
3804 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003805 assert(PyTuple_Check(names));
3806 assert(PyTuple_GET_SIZE(names) <= oparg);
3807 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003809 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003811 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003812 Py_DECREF(names);
3813
3814 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003815 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003816 }
3817 DISPATCH();
3818 }
3819
Benjamin Petersonddd19492018-09-16 22:38:02 -07003820 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003821 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003822 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003823 if (oparg & 0x01) {
3824 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003825 if (!PyDict_CheckExact(kwargs)) {
3826 PyObject *d = PyDict_New();
3827 if (d == NULL)
3828 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003829 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003830 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003831 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003832 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003833 goto error;
3834 }
3835 Py_DECREF(kwargs);
3836 kwargs = d;
3837 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003838 assert(PyDict_CheckExact(kwargs));
3839 }
3840 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003841 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003842 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003843 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003844 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003845 goto error;
3846 }
3847 Py_SETREF(callargs, PySequence_Tuple(callargs));
3848 if (callargs == NULL) {
3849 goto error;
3850 }
3851 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003852 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003853
Victor Stinner09532fe2019-05-10 23:39:09 +02003854 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003855 Py_DECREF(func);
3856 Py_DECREF(callargs);
3857 Py_XDECREF(kwargs);
3858
3859 SET_TOP(result);
3860 if (result == NULL) {
3861 goto error;
3862 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003863 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003865
Benjamin Petersonddd19492018-09-16 22:38:02 -07003866 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003867 PyObject *qualname = POP();
3868 PyObject *codeobj = POP();
3869 PyFunctionObject *func = (PyFunctionObject *)
3870 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003871
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003872 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003873 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003874 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003877
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003878 if (oparg & 0x08) {
3879 assert(PyTuple_CheckExact(TOP()));
3880 func ->func_closure = POP();
3881 }
3882 if (oparg & 0x04) {
3883 assert(PyDict_CheckExact(TOP()));
3884 func->func_annotations = POP();
3885 }
3886 if (oparg & 0x02) {
3887 assert(PyDict_CheckExact(TOP()));
3888 func->func_kwdefaults = POP();
3889 }
3890 if (oparg & 0x01) {
3891 assert(PyTuple_CheckExact(TOP()));
3892 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003894
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003895 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003896 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003898
Benjamin Petersonddd19492018-09-16 22:38:02 -07003899 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003900 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003902 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003904 step = NULL;
3905 stop = POP();
3906 start = TOP();
3907 slice = PySlice_New(start, stop, step);
3908 Py_DECREF(start);
3909 Py_DECREF(stop);
3910 Py_XDECREF(step);
3911 SET_TOP(slice);
3912 if (slice == NULL)
3913 goto error;
3914 DISPATCH();
3915 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003916
Benjamin Petersonddd19492018-09-16 22:38:02 -07003917 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003918 /* Handles f-string value formatting. */
3919 PyObject *result;
3920 PyObject *fmt_spec;
3921 PyObject *value;
3922 PyObject *(*conv_fn)(PyObject *);
3923 int which_conversion = oparg & FVC_MASK;
3924 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3925
3926 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003927 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003928
3929 /* See if any conversion is specified. */
3930 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003931 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003932 case FVC_STR: conv_fn = PyObject_Str; break;
3933 case FVC_REPR: conv_fn = PyObject_Repr; break;
3934 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003935 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003936 _PyErr_Format(tstate, PyExc_SystemError,
3937 "unexpected conversion flag %d",
3938 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003939 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003940 }
3941
3942 /* If there's a conversion function, call it and replace
3943 value with that result. Otherwise, just use value,
3944 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003945 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003946 result = conv_fn(value);
3947 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003948 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003949 Py_XDECREF(fmt_spec);
3950 goto error;
3951 }
3952 value = result;
3953 }
3954
3955 /* If value is a unicode object, and there's no fmt_spec,
3956 then we know the result of format(value) is value
3957 itself. In that case, skip calling format(). I plan to
3958 move this optimization in to PyObject_Format()
3959 itself. */
3960 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3961 /* Do nothing, just transfer ownership to result. */
3962 result = value;
3963 } else {
3964 /* Actually call format(). */
3965 result = PyObject_Format(value, fmt_spec);
3966 Py_DECREF(value);
3967 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003968 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003969 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003970 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003971 }
3972
Eric V. Smith135d5f42016-02-05 18:23:08 -05003973 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003974 DISPATCH();
3975 }
3976
Benjamin Petersonddd19492018-09-16 22:38:02 -07003977 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003978 int oldoparg = oparg;
3979 NEXTOPARG();
3980 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003982 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003983
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003984
Antoine Pitrou042b1282010-08-13 21:15:58 +00003985#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003987#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 default:
3989 fprintf(stderr,
3990 "XXX lineno: %d, opcode: %d\n",
3991 PyFrame_GetLineNumber(f),
3992 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003993 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003994 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003997
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003998 /* This should never be reached. Every opcode should end with DISPATCH()
3999 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004000 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004001
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004002error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004003 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004004#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004005 if (!_PyErr_Occurred(tstate)) {
4006 _PyErr_SetString(tstate, PyExc_SystemError,
4007 "error return without exception set");
4008 }
Victor Stinner365b6932013-07-12 00:11:58 +02004009#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004010 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004011#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004012
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004013 /* Log traceback info. */
4014 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004015
Mark Shannoncb9879b2020-07-17 11:44:23 +01004016 if (tstate->c_tracefunc != NULL) {
4017 /* Make sure state is set to FRAME_EXECUTING for tracing */
4018 assert(f->f_state == FRAME_EXECUTING);
4019 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004020 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
4021 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004022 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004023exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004024 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004025 /* Unwind stacks if an exception occurred */
4026 while (f->f_iblock > 0) {
4027 /* Pop the current block. */
4028 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (b->b_type == EXCEPT_HANDLER) {
4031 UNWIND_EXCEPT_HANDLER(b);
4032 continue;
4033 }
4034 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004035 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 PyObject *exc, *val, *tb;
4037 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004038 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 /* Beware, this invalidates all b->b_* fields */
4040 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004041 PUSH(exc_info->exc_traceback);
4042 PUSH(exc_info->exc_value);
4043 if (exc_info->exc_type != NULL) {
4044 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 }
4046 else {
4047 Py_INCREF(Py_None);
4048 PUSH(Py_None);
4049 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004050 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 /* Make the raw exception data
4052 available to the handler,
4053 so a program can emulate the
4054 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004055 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004056 if (tb != NULL)
4057 PyException_SetTraceback(val, tb);
4058 else
4059 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004061 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004063 exc_info->exc_value = val;
4064 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 if (tb == NULL)
4066 tb = Py_None;
4067 Py_INCREF(tb);
4068 PUSH(tb);
4069 PUSH(val);
4070 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004072 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004073 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004074 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004075 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004076 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004077 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 }
4079 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004080
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004081 /* End the loop as we still have an error */
4082 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004084
Pablo Galindof00828a2019-05-09 16:52:02 +01004085 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004086 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 /* Pop remaining stack entries. */
4089 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004090 PyObject *o = POP();
4091 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004093 f->f_stackdepth = 0;
4094 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004095exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004097 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004098 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
4099 tstate, f, PyTrace_RETURN, retval)) {
4100 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 }
4102 }
4103 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004104 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
4105 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004106 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
4108 }
4109 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004112exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004113 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4114 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004115 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004117
Victor Stinner0b72b232020-03-12 23:18:39 +01004118 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004119}
4120
Benjamin Petersonb204a422011-06-05 22:04:07 -05004121static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004122format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004123 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004124{
4125 int err;
4126 Py_ssize_t len = PyList_GET_SIZE(names);
4127 PyObject *name_str, *comma, *tail, *tmp;
4128
4129 assert(PyList_CheckExact(names));
4130 assert(len >= 1);
4131 /* Deal with the joys of natural language. */
4132 switch (len) {
4133 case 1:
4134 name_str = PyList_GET_ITEM(names, 0);
4135 Py_INCREF(name_str);
4136 break;
4137 case 2:
4138 name_str = PyUnicode_FromFormat("%U and %U",
4139 PyList_GET_ITEM(names, len - 2),
4140 PyList_GET_ITEM(names, len - 1));
4141 break;
4142 default:
4143 tail = PyUnicode_FromFormat(", %U, and %U",
4144 PyList_GET_ITEM(names, len - 2),
4145 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004146 if (tail == NULL)
4147 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004148 /* Chop off the last two objects in the list. This shouldn't actually
4149 fail, but we can't be too careful. */
4150 err = PyList_SetSlice(names, len - 2, len, NULL);
4151 if (err == -1) {
4152 Py_DECREF(tail);
4153 return;
4154 }
4155 /* Stitch everything up into a nice comma-separated list. */
4156 comma = PyUnicode_FromString(", ");
4157 if (comma == NULL) {
4158 Py_DECREF(tail);
4159 return;
4160 }
4161 tmp = PyUnicode_Join(comma, names);
4162 Py_DECREF(comma);
4163 if (tmp == NULL) {
4164 Py_DECREF(tail);
4165 return;
4166 }
4167 name_str = PyUnicode_Concat(tmp, tail);
4168 Py_DECREF(tmp);
4169 Py_DECREF(tail);
4170 break;
4171 }
4172 if (name_str == NULL)
4173 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004174 _PyErr_Format(tstate, PyExc_TypeError,
4175 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004176 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004177 len,
4178 kind,
4179 len == 1 ? "" : "s",
4180 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004181 Py_DECREF(name_str);
4182}
4183
4184static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004185missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4186 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004187 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004188{
Victor Stinner74319ae2016-08-25 00:04:09 +02004189 Py_ssize_t i, j = 0;
4190 Py_ssize_t start, end;
4191 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004192 const char *kind = positional ? "positional" : "keyword-only";
4193 PyObject *missing_names;
4194
4195 /* Compute the names of the arguments that are missing. */
4196 missing_names = PyList_New(missing);
4197 if (missing_names == NULL)
4198 return;
4199 if (positional) {
4200 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004201 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004202 }
4203 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004204 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004205 end = start + co->co_kwonlyargcount;
4206 }
4207 for (i = start; i < end; i++) {
4208 if (GETLOCAL(i) == NULL) {
4209 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4210 PyObject *name = PyObject_Repr(raw);
4211 if (name == NULL) {
4212 Py_DECREF(missing_names);
4213 return;
4214 }
4215 PyList_SET_ITEM(missing_names, j++, name);
4216 }
4217 }
4218 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004219 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004220 Py_DECREF(missing_names);
4221}
4222
4223static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004224too_many_positional(PyThreadState *tstate, PyCodeObject *co,
4225 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004226 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004227{
4228 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004229 Py_ssize_t kwonly_given = 0;
4230 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004231 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004232 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233
Benjamin Petersone109c702011-06-24 09:37:26 -05004234 assert((co->co_flags & CO_VARARGS) == 0);
4235 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004236 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004237 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004239 }
4240 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004241 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004242 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004243 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004244 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004245 }
4246 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004247 plural = (co_argcount != 1);
4248 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004249 }
4250 if (sig == NULL)
4251 return;
4252 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004253 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4254 kwonly_sig = PyUnicode_FromFormat(format,
4255 given != 1 ? "s" : "",
4256 kwonly_given,
4257 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004258 if (kwonly_sig == NULL) {
4259 Py_DECREF(sig);
4260 return;
4261 }
4262 }
4263 else {
4264 /* This will not fail. */
4265 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004266 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004267 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004268 _PyErr_Format(tstate, PyExc_TypeError,
4269 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004270 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004271 sig,
4272 plural ? "s" : "",
4273 given,
4274 kwonly_sig,
4275 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004276 Py_DECREF(sig);
4277 Py_DECREF(kwonly_sig);
4278}
4279
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004280static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004281positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004282 Py_ssize_t kwcount, PyObject* const* kwnames,
4283 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004284{
4285 int posonly_conflicts = 0;
4286 PyObject* posonly_names = PyList_New(0);
4287
4288 for(int k=0; k < co->co_posonlyargcount; k++){
4289 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4290
4291 for (int k2=0; k2<kwcount; k2++){
4292 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4293 PyObject* kwname = kwnames[k2];
4294 if (kwname == posonly_name){
4295 if(PyList_Append(posonly_names, kwname) != 0) {
4296 goto fail;
4297 }
4298 posonly_conflicts++;
4299 continue;
4300 }
4301
4302 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4303
4304 if ( cmp > 0) {
4305 if(PyList_Append(posonly_names, kwname) != 0) {
4306 goto fail;
4307 }
4308 posonly_conflicts++;
4309 } else if (cmp < 0) {
4310 goto fail;
4311 }
4312
4313 }
4314 }
4315 if (posonly_conflicts) {
4316 PyObject* comma = PyUnicode_FromString(", ");
4317 if (comma == NULL) {
4318 goto fail;
4319 }
4320 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4321 Py_DECREF(comma);
4322 if (error_names == NULL) {
4323 goto fail;
4324 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004325 _PyErr_Format(tstate, PyExc_TypeError,
4326 "%U() got some positional-only arguments passed"
4327 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004328 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004329 Py_DECREF(error_names);
4330 goto fail;
4331 }
4332
4333 Py_DECREF(posonly_names);
4334 return 0;
4335
4336fail:
4337 Py_XDECREF(posonly_names);
4338 return 1;
4339
4340}
4341
Guido van Rossumc2e20742006-02-27 22:32:47 +00004342/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004343 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004344 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004345
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004346PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004347_PyEval_EvalCode(PyThreadState *tstate,
4348 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004349 PyObject *const *args, Py_ssize_t argcount,
4350 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004351 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004352 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004353 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004354 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004355{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004356 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004357
Victor Stinner232dda62020-06-04 15:19:02 +02004358 PyCodeObject *co = (PyCodeObject*)_co;
4359
4360 if (!name) {
4361 name = co->co_name;
4362 }
4363 assert(name != NULL);
4364 assert(PyUnicode_Check(name));
4365
4366 if (!qualname) {
4367 qualname = name;
4368 }
4369 assert(qualname != NULL);
4370 assert(PyUnicode_Check(qualname));
4371
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004372 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004373 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004376 _PyErr_SetString(tstate, PyExc_SystemError,
4377 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 return NULL;
4379 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004380
Victor Stinnerc7020012016-08-16 23:40:29 +02004381 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004382 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004383 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004385 }
Victor Stinner232dda62020-06-04 15:19:02 +02004386 PyObject **fastlocals = f->f_localsplus;
4387 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004388
Victor Stinnerc7020012016-08-16 23:40:29 +02004389 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004390 PyObject *kwdict;
4391 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004392 if (co->co_flags & CO_VARKEYWORDS) {
4393 kwdict = PyDict_New();
4394 if (kwdict == NULL)
4395 goto fail;
4396 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004397 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004398 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004399 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004400 SETLOCAL(i, kwdict);
4401 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004402 else {
4403 kwdict = NULL;
4404 }
4405
Pablo Galindocd74e662019-06-01 18:08:04 +01004406 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004407 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004408 if (argcount > co->co_argcount) {
4409 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004410 }
4411 else {
4412 n = argcount;
4413 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004414 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004415 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004416 Py_INCREF(x);
4417 SETLOCAL(j, x);
4418 }
4419
Victor Stinnerc7020012016-08-16 23:40:29 +02004420 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004421 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004422 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004423 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004424 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004425 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004426 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004427 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004428
Serhiy Storchakab7281052016-09-12 00:52:40 +03004429 /* Handle keyword arguments passed as two strided arrays */
4430 kwcount *= kwstep;
4431 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004432 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004433 PyObject *keyword = kwnames[i];
4434 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004435 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004436
Benjamin Petersonb204a422011-06-05 22:04:07 -05004437 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004438 _PyErr_Format(tstate, PyExc_TypeError,
4439 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004440 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004441 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004443
Benjamin Petersonb204a422011-06-05 22:04:07 -05004444 /* Speed hack: do raw pointer compares. As names are
4445 normally interned this should almost always hit. */
4446 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004447 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004448 PyObject *varname = co_varnames[j];
4449 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004450 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004451 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004452 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004453
Benjamin Petersonb204a422011-06-05 22:04:07 -05004454 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004455 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004456 PyObject *varname = co_varnames[j];
4457 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004458 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004459 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004460 }
4461 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004463 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004464 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004465
Victor Stinner231d1f32017-01-11 02:12:06 +01004466 assert(j >= total_args);
4467 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004468
Victor Stinner438a12d2019-05-24 17:01:38 +02004469 if (co->co_posonlyargcount
4470 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004471 kwcount, kwnames,
4472 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004474 goto fail;
4475 }
4476
Victor Stinner438a12d2019-05-24 17:01:38 +02004477 _PyErr_Format(tstate, PyExc_TypeError,
4478 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004479 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004480 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004482
Christian Heimes0bd447f2013-07-20 14:48:10 +02004483 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4484 goto fail;
4485 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004486 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004487
Benjamin Petersonb204a422011-06-05 22:04:07 -05004488 kw_found:
4489 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004490 _PyErr_Format(tstate, PyExc_TypeError,
4491 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004492 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004493 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004495 Py_INCREF(value);
4496 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004498
4499 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004500 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004501 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4502 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 goto fail;
4504 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004505
4506 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004507 if (argcount < co->co_argcount) {
4508 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004509 Py_ssize_t missing = 0;
4510 for (i = argcount; i < m; i++) {
4511 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004512 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004513 }
4514 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004515 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004516 missing_arguments(tstate, co, missing, defcount, fastlocals,
4517 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004518 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004519 }
4520 if (n > m)
4521 i = n - m;
4522 else
4523 i = 0;
4524 for (; i < defcount; i++) {
4525 if (GETLOCAL(m+i) == NULL) {
4526 PyObject *def = defs[i];
4527 Py_INCREF(def);
4528 SETLOCAL(m+i, def);
4529 }
4530 }
4531 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004532
4533 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004534 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004535 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004536 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004537 if (GETLOCAL(i) != NULL)
4538 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004539 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004540 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004541 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004542 if (def) {
4543 Py_INCREF(def);
4544 SETLOCAL(i, def);
4545 continue;
4546 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004547 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004548 goto fail;
4549 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004550 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004551 missing++;
4552 }
4553 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004554 missing_arguments(tstate, co, missing, -1, fastlocals,
4555 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004556 goto fail;
4557 }
4558 }
4559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004561 vars into frame. */
4562 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004564 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004565 /* Possibly account for the cell variable being an argument. */
4566 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004567 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004568 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004569 /* Clear the local copy. */
4570 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004571 }
4572 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004573 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004574 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004575 if (c == NULL)
4576 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004577 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004579
4580 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004581 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4582 PyObject *o = PyTuple_GET_ITEM(closure, i);
4583 Py_INCREF(o);
4584 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004586
Yury Selivanoveb636452016-09-08 22:01:51 -07004587 /* Handle generator/coroutine/asynchronous generator */
4588 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004589 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004590 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 /* Don't need to keep the reference to f_back, it will be set
4593 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004594 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 /* Create a new generator that owns the ready to run frame
4597 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004598 if (is_coro) {
4599 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004600 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4601 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004602 } else {
4603 gen = PyGen_NewWithQualName(f, name, qualname);
4604 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004605 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004606 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004607 }
INADA Naoki9c157762016-12-26 18:52:46 +09004608
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004609 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004610
Yury Selivanov75445082015-05-11 22:57:16 -04004611 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004613
Victor Stinnerb9e68122019-11-14 12:20:46 +01004614 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004615
Thomas Woutersce272b62007-09-19 21:19:28 +00004616fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 /* decref'ing the frame can cause __del__ methods to get invoked,
4619 which can call back into Python. While we're done with the
4620 current Python frame (f), the associated C stack is still in use,
4621 so recursion_depth must be boosted for the duration.
4622 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004623 if (Py_REFCNT(f) > 1) {
4624 Py_DECREF(f);
4625 _PyObject_GC_TRACK(f);
4626 }
4627 else {
4628 ++tstate->recursion_depth;
4629 Py_DECREF(f);
4630 --tstate->recursion_depth;
4631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004633}
4634
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004635
4636PyObject *
4637_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4638 PyObject *const *args, Py_ssize_t argcount,
4639 PyObject *const *kwnames, PyObject *const *kwargs,
4640 Py_ssize_t kwcount, int kwstep,
4641 PyObject *const *defs, Py_ssize_t defcount,
4642 PyObject *kwdefs, PyObject *closure,
4643 PyObject *name, PyObject *qualname)
4644{
4645 PyThreadState *tstate = _PyThreadState_GET();
4646 return _PyEval_EvalCode(tstate, _co, globals, locals,
4647 args, argcount,
4648 kwnames, kwargs,
4649 kwcount, kwstep,
4650 defs, defcount,
4651 kwdefs, closure,
4652 name, qualname);
4653}
4654
Victor Stinner40ee3012014-06-16 15:59:28 +02004655PyObject *
4656PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004657 PyObject *const *args, int argcount,
4658 PyObject *const *kws, int kwcount,
4659 PyObject *const *defs, int defcount,
4660 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004661{
4662 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004663 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004664 kws, kws != NULL ? kws + 1 : NULL,
4665 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004666 defs, defcount,
4667 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004668 NULL, NULL);
4669}
Tim Peters5ca576e2001-06-18 22:08:13 +00004670
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004671static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004672special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004675 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004676 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004677 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 return NULL;
4679 }
4680 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004681}
4682
4683
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004684/* Logic for the raise statement (too complicated for inlining).
4685 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004686static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004687do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 if (exc == NULL) {
4692 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004693 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004695 type = exc_info->exc_type;
4696 value = exc_info->exc_value;
4697 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004698 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004699 _PyErr_SetString(tstate, PyExc_RuntimeError,
4700 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004701 return 0;
4702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 Py_XINCREF(type);
4704 Py_XINCREF(value);
4705 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004706 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004707 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 /* We support the following forms of raise:
4711 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004712 raise <instance>
4713 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 if (PyExceptionClass_Check(exc)) {
4716 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004717 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 if (value == NULL)
4719 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004720 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004721 _PyErr_Format(tstate, PyExc_TypeError,
4722 "calling %R should have returned an instance of "
4723 "BaseException, not %R",
4724 type, Py_TYPE(value));
4725 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004726 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 }
4728 else if (PyExceptionInstance_Check(exc)) {
4729 value = exc;
4730 type = PyExceptionInstance_Class(exc);
4731 Py_INCREF(type);
4732 }
4733 else {
4734 /* Not something you can raise. You get an exception
4735 anyway, just not what you specified :-) */
4736 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004737 _PyErr_SetString(tstate, PyExc_TypeError,
4738 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 goto raise_error;
4740 }
Collin Winter828f04a2007-08-31 00:04:24 +00004741
Serhiy Storchakac0191582016-09-27 11:37:10 +03004742 assert(type != NULL);
4743 assert(value != NULL);
4744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 if (cause) {
4746 PyObject *fixed_cause;
4747 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004748 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 if (fixed_cause == NULL)
4750 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004751 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004753 else if (PyExceptionInstance_Check(cause)) {
4754 fixed_cause = cause;
4755 }
4756 else if (cause == Py_None) {
4757 Py_DECREF(cause);
4758 fixed_cause = NULL;
4759 }
4760 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004761 _PyErr_SetString(tstate, PyExc_TypeError,
4762 "exception causes must derive from "
4763 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 goto raise_error;
4765 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004766 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 }
Collin Winter828f04a2007-08-31 00:04:24 +00004768
Victor Stinner438a12d2019-05-24 17:01:38 +02004769 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004770 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004771 Py_DECREF(value);
4772 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004773 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004774
4775raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 Py_XDECREF(value);
4777 Py_XDECREF(type);
4778 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004779 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004780}
4781
Tim Petersd6d010b2001-06-21 02:49:55 +00004782/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004783 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004784
Guido van Rossum0368b722007-05-11 16:50:42 +00004785 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4786 with a variable target.
4787*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004788
Barry Warsawe42b18f1997-08-25 22:13:04 +00004789static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004790unpack_iterable(PyThreadState *tstate, PyObject *v,
4791 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 int i = 0, j = 0;
4794 Py_ssize_t ll = 0;
4795 PyObject *it; /* iter(v) */
4796 PyObject *w;
4797 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004802 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004803 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004804 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004805 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004806 _PyErr_Format(tstate, PyExc_TypeError,
4807 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004808 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004809 }
4810 return 0;
4811 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 for (; i < argcnt; i++) {
4814 w = PyIter_Next(it);
4815 if (w == NULL) {
4816 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004817 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004818 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004819 _PyErr_Format(tstate, PyExc_ValueError,
4820 "not enough values to unpack "
4821 "(expected %d, got %d)",
4822 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004823 }
4824 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004825 _PyErr_Format(tstate, PyExc_ValueError,
4826 "not enough values to unpack "
4827 "(expected at least %d, got %d)",
4828 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004829 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 }
4831 goto Error;
4832 }
4833 *--sp = w;
4834 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 if (argcntafter == -1) {
4837 /* We better have exhausted the iterator now. */
4838 w = PyIter_Next(it);
4839 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004840 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 goto Error;
4842 Py_DECREF(it);
4843 return 1;
4844 }
4845 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004846 _PyErr_Format(tstate, PyExc_ValueError,
4847 "too many values to unpack (expected %d)",
4848 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 goto Error;
4850 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 l = PySequence_List(it);
4853 if (l == NULL)
4854 goto Error;
4855 *--sp = l;
4856 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 ll = PyList_GET_SIZE(l);
4859 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004860 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004861 "not enough values to unpack (expected at least %d, got %zd)",
4862 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 goto Error;
4864 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 /* Pop the "after-variable" args off the list. */
4867 for (j = argcntafter; j > 0; j--, i++) {
4868 *--sp = PyList_GET_ITEM(l, ll - j);
4869 }
4870 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004871 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 Py_DECREF(it);
4873 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004874
Tim Petersd6d010b2001-06-21 02:49:55 +00004875Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 for (; i > 0; i--, sp++)
4877 Py_DECREF(*sp);
4878 Py_XDECREF(it);
4879 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004880}
4881
4882
Guido van Rossum96a42c81992-01-12 02:29:51 +00004883#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004884static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004885prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004888 if (PyObject_Print(v, stdout, 0) != 0) {
4889 /* Don't know what else to do */
4890 _PyErr_Clear(tstate);
4891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 printf("\n");
4893 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004894}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004895#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004896
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004897static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004898call_exc_trace(Py_tracefunc func, PyObject *self,
4899 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004900{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004901 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004903 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 if (value == NULL) {
4905 value = Py_None;
4906 Py_INCREF(value);
4907 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004908 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004909 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 arg = PyTuple_Pack(3, type, value, traceback);
4911 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004912 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913 return;
4914 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004915 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004917 if (err == 0) {
4918 _PyErr_Restore(tstate, type, value, orig_traceback);
4919 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 else {
4921 Py_XDECREF(type);
4922 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004923 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004925}
4926
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004927static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004928call_trace_protected(Py_tracefunc func, PyObject *obj,
4929 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 PyObject *type, *value, *traceback;
4933 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004934 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004935 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 if (err == 0)
4937 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004938 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 return 0;
4940 }
4941 else {
4942 Py_XDECREF(type);
4943 Py_XDECREF(value);
4944 Py_XDECREF(traceback);
4945 return -1;
4946 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004947}
4948
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004949static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004950call_trace(Py_tracefunc func, PyObject *obj,
4951 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 int result;
4955 if (tstate->tracing)
4956 return 0;
4957 tstate->tracing++;
4958 tstate->use_tracing = 0;
4959 result = func(obj, frame, what, arg);
4960 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4961 || (tstate->c_profilefunc != NULL));
4962 tstate->tracing--;
4963 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004964}
4965
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004966PyObject *
4967_PyEval_CallTracing(PyObject *func, PyObject *args)
4968{
Victor Stinner50b48572018-11-01 01:51:40 +01004969 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 int save_tracing = tstate->tracing;
4971 int save_use_tracing = tstate->use_tracing;
4972 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 tstate->tracing = 0;
4975 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4976 || (tstate->c_profilefunc != NULL));
4977 result = PyObject_Call(func, args, NULL);
4978 tstate->tracing = save_tracing;
4979 tstate->use_tracing = save_use_tracing;
4980 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004981}
4982
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004983/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004984static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004985maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004986 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00004987 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 int result = 0;
4990 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 /* If the last instruction executed isn't in the current
4993 instruction window, reset the window.
4994 */
Mark Shannon877df852020-11-12 09:43:29 +00004995 line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Nick Coghlan5a851672017-09-08 10:14:16 +10004996 /* If the last instruction falls at the start of a line or if it
4997 represents a jump backwards, update the frame's line number and
4998 then call the trace function if we're tracing source lines.
4999 */
Mark Shannon877df852020-11-12 09:43:29 +00005000 if ((line != frame->f_lineno || frame->f_lasti < *instr_prev)) {
5001 if (line != -1) {
5002 frame->f_lineno = line;
5003 if (frame->f_trace_lines) {
5004 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
5005 }
Nick Coghlan5a851672017-09-08 10:14:16 +10005006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 }
George King20faa682017-10-18 17:44:22 -07005008 /* Always emit an opcode event if we're tracing all opcodes. */
5009 if (frame->f_trace_opcodes) {
5010 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
5011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 *instr_prev = frame->f_lasti;
5013 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005014}
5015
Victor Stinner309d7cc2020-03-13 16:39:12 +01005016int
5017_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5018{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005019 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005020 /* The caller must hold the GIL */
5021 assert(PyGILState_Check());
5022
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005023 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005024 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005025 PyThreadState *current_tstate = _PyThreadState_GET();
5026 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005027 return -1;
5028 }
5029
5030 PyObject *profileobj = tstate->c_profileobj;
5031
5032 tstate->c_profilefunc = NULL;
5033 tstate->c_profileobj = NULL;
5034 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5035 tstate->use_tracing = tstate->c_tracefunc != NULL;
5036 Py_XDECREF(profileobj);
5037
5038 Py_XINCREF(arg);
5039 tstate->c_profileobj = arg;
5040 tstate->c_profilefunc = func;
5041
5042 /* Flag that tracing or profiling is turned on */
5043 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5044 return 0;
5045}
5046
Fred Drake5755ce62001-06-27 19:19:46 +00005047void
5048PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005049{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005050 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005051 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005052 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005053 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5054 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005055}
5056
5057int
5058_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5059{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005060 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005061 /* The caller must hold the GIL */
5062 assert(PyGILState_Check());
5063
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005064 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005065 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005066 PyThreadState *current_tstate = _PyThreadState_GET();
5067 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005068 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005069 }
5070
Victor Stinnerda2914d2020-03-20 09:29:08 +01005071 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005072 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005073 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005074
5075 tstate->c_tracefunc = NULL;
5076 tstate->c_traceobj = NULL;
5077 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5078 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5079 Py_XDECREF(traceobj);
5080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005082 tstate->c_traceobj = arg;
5083 tstate->c_tracefunc = func;
5084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005086 tstate->use_tracing = ((func != NULL)
5087 || (tstate->c_profilefunc != NULL));
5088
5089 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005090}
5091
5092void
5093PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5094{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005095 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005096 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005097 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005098 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5099 }
Fred Draked0838392001-06-16 21:02:31 +00005100}
5101
Victor Stinner309d7cc2020-03-13 16:39:12 +01005102
Yury Selivanov75445082015-05-11 22:57:16 -04005103void
Victor Stinner838f2642019-06-13 22:41:23 +02005104_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005105{
5106 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005107 tstate->coroutine_origin_tracking_depth = new_depth;
5108}
5109
5110int
5111_PyEval_GetCoroutineOriginTrackingDepth(void)
5112{
Victor Stinner50b48572018-11-01 01:51:40 +01005113 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005114 return tstate->coroutine_origin_tracking_depth;
5115}
5116
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005117int
Yury Selivanoveb636452016-09-08 22:01:51 -07005118_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5119{
Victor Stinner50b48572018-11-01 01:51:40 +01005120 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005121
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005122 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005123 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005124 }
5125
Yury Selivanoveb636452016-09-08 22:01:51 -07005126 Py_XINCREF(firstiter);
5127 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005128 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005129}
5130
5131PyObject *
5132_PyEval_GetAsyncGenFirstiter(void)
5133{
Victor Stinner50b48572018-11-01 01:51:40 +01005134 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005135 return tstate->async_gen_firstiter;
5136}
5137
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005138int
Yury Selivanoveb636452016-09-08 22:01:51 -07005139_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5140{
Victor Stinner50b48572018-11-01 01:51:40 +01005141 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005142
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005143 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005144 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005145 }
5146
Yury Selivanoveb636452016-09-08 22:01:51 -07005147 Py_XINCREF(finalizer);
5148 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005149 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005150}
5151
5152PyObject *
5153_PyEval_GetAsyncGenFinalizer(void)
5154{
Victor Stinner50b48572018-11-01 01:51:40 +01005155 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005156 return tstate->async_gen_finalizer;
5157}
5158
Victor Stinner438a12d2019-05-24 17:01:38 +02005159PyFrameObject *
5160PyEval_GetFrame(void)
5161{
5162 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005163 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005164}
5165
Guido van Rossumb209a111997-04-29 18:18:01 +00005166PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005167PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005168{
Victor Stinner438a12d2019-05-24 17:01:38 +02005169 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005170 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005172 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005173 else
5174 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005175}
5176
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005177/* Convenience function to get a builtin from its name */
5178PyObject *
5179_PyEval_GetBuiltinId(_Py_Identifier *name)
5180{
Victor Stinner438a12d2019-05-24 17:01:38 +02005181 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005182 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5183 if (attr) {
5184 Py_INCREF(attr);
5185 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005186 else if (!_PyErr_Occurred(tstate)) {
5187 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005188 }
5189 return attr;
5190}
5191
Guido van Rossumb209a111997-04-29 18:18:01 +00005192PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005193PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005194{
Victor Stinner438a12d2019-05-24 17:01:38 +02005195 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005196 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005197 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005198 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005200 }
5201
Victor Stinner438a12d2019-05-24 17:01:38 +02005202 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005203 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005204 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005205
5206 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005208}
5209
Guido van Rossumb209a111997-04-29 18:18:01 +00005210PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005211PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005212{
Victor Stinner438a12d2019-05-24 17:01:38 +02005213 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005214 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005215 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005217 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005218
5219 assert(current_frame->f_globals != NULL);
5220 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005221}
5222
Guido van Rossum6135a871995-01-09 17:53:26 +00005223int
Tim Peters5ba58662001-07-16 02:29:45 +00005224PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005225{
Victor Stinner438a12d2019-05-24 17:01:38 +02005226 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005227 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 if (current_frame != NULL) {
5231 const int codeflags = current_frame->f_code->co_flags;
5232 const int compilerflags = codeflags & PyCF_MASK;
5233 if (compilerflags) {
5234 result = 1;
5235 cf->cf_flags |= compilerflags;
5236 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005237#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 if (codeflags & CO_GENERATOR_ALLOWED) {
5239 result = 1;
5240 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5241 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005242#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 }
5244 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005245}
5246
Guido van Rossum3f5da241990-12-20 15:06:42 +00005247
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005248const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005249PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 if (PyMethod_Check(func))
5252 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5253 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005254 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 else if (PyCFunction_Check(func))
5256 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5257 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005258 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005259}
5260
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005261const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005262PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 if (PyMethod_Check(func))
5265 return "()";
5266 else if (PyFunction_Check(func))
5267 return "()";
5268 else if (PyCFunction_Check(func))
5269 return "()";
5270 else
5271 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005272}
5273
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005274#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005275if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005276 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5277 tstate, tstate->frame, \
5278 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 x = NULL; \
5280 } \
5281 else { \
5282 x = call; \
5283 if (tstate->c_profilefunc != NULL) { \
5284 if (x == NULL) { \
5285 call_trace_protected(tstate->c_profilefunc, \
5286 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005287 tstate, tstate->frame, \
5288 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 /* XXX should pass (type, value, tb) */ \
5290 } else { \
5291 if (call_trace(tstate->c_profilefunc, \
5292 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005293 tstate, tstate->frame, \
5294 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 Py_DECREF(x); \
5296 x = NULL; \
5297 } \
5298 } \
5299 } \
5300 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005301} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 x = call; \
5303 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005304
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005305
5306static PyObject *
5307trace_call_function(PyThreadState *tstate,
5308 PyObject *func,
5309 PyObject **args, Py_ssize_t nargs,
5310 PyObject *kwnames)
5311{
5312 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005313 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005314 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005315 return x;
5316 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005317 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005318 /* We need to create a temporary bound method as argument
5319 for profiling.
5320
5321 If nargs == 0, then this cannot work because we have no
5322 "self". In any case, the call itself would raise
5323 TypeError (foo needs an argument), so we just skip
5324 profiling. */
5325 PyObject *self = args[0];
5326 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5327 if (func == NULL) {
5328 return NULL;
5329 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005330 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005331 args+1, nargs-1,
5332 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005333 Py_DECREF(func);
5334 return x;
5335 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005336 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005337}
5338
Victor Stinner415c5102017-01-11 00:54:57 +01005339/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5340 to reduce the stack consumption. */
5341Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005342call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005343{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005344 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 PyObject *func = *pfunc;
5346 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005347 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5348 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005349 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005350
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005351 if (tstate->use_tracing) {
5352 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005353 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005354 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005355 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005357
Victor Stinner438a12d2019-05-24 17:01:38 +02005358 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005359
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005360 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 while ((*pp_stack) > pfunc) {
5362 w = EXT_POP(*pp_stack);
5363 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005367}
5368
Jeremy Hylton52820442001-01-03 23:52:36 +00005369static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005370do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005371{
jdemeyere89de732018-09-19 12:06:20 +02005372 PyObject *result;
5373
scoder4c9ea092020-05-12 16:12:41 +02005374 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005375 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005376 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005378 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005379 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5380 if (nargs > 0 && tstate->use_tracing) {
5381 /* We need to create a temporary bound method as argument
5382 for profiling.
5383
5384 If nargs == 0, then this cannot work because we have no
5385 "self". In any case, the call itself would raise
5386 TypeError (foo needs an argument), so we just skip
5387 profiling. */
5388 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5389 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5390 if (func == NULL) {
5391 return NULL;
5392 }
5393
Victor Stinner4d231bc2019-11-14 13:36:21 +01005394 C_TRACE(result, _PyObject_FastCallDictTstate(
5395 tstate, func,
5396 &_PyTuple_ITEMS(callargs)[1],
5397 nargs - 1,
5398 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005399 Py_DECREF(func);
5400 return result;
5401 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005402 }
jdemeyere89de732018-09-19 12:06:20 +02005403 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005404}
5405
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005406/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005407 nb_index slot defined, and store in *pi.
5408 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005409 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005410 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005411*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005412int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005413_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005414{
Victor Stinner438a12d2019-05-24 17:01:38 +02005415 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005416 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005417 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005418 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005420 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005421 return 0;
5422 }
5423 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005424 _PyErr_SetString(tstate, PyExc_TypeError,
5425 "slice indices must be integers or "
5426 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 return 0;
5428 }
5429 *pi = x;
5430 }
5431 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005432}
5433
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005434int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005435_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005436{
Victor Stinner438a12d2019-05-24 17:01:38 +02005437 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005438 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005439 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005440 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005441 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005442 return 0;
5443 }
5444 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 _PyErr_SetString(tstate, PyExc_TypeError,
5446 "slice indices must be integers or "
5447 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005448 return 0;
5449 }
5450 *pi = x;
5451 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005452}
5453
Thomas Wouters52152252000-08-17 22:55:00 +00005454static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005455import_name(PyThreadState *tstate, PyFrameObject *f,
5456 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005457{
5458 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005459 PyObject *import_func, *res;
5460 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005461
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005462 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005463 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005464 if (!_PyErr_Occurred(tstate)) {
5465 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005466 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005467 return NULL;
5468 }
5469
5470 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005471 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005472 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005473 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005474 return NULL;
5475 }
5476 res = PyImport_ImportModuleLevelObject(
5477 name,
5478 f->f_globals,
5479 f->f_locals == NULL ? Py_None : f->f_locals,
5480 fromlist,
5481 ilevel);
5482 return res;
5483 }
5484
5485 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005486
5487 stack[0] = name;
5488 stack[1] = f->f_globals;
5489 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5490 stack[3] = fromlist;
5491 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005492 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005493 Py_DECREF(import_func);
5494 return res;
5495}
5496
5497static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005498import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005501 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005502
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005503 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005504 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005505 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005506 /* Issue #17636: in case this failed because of a circular relative
5507 import, try to fallback on reading the module directly from
5508 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005509 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005510 if (pkgname == NULL) {
5511 goto error;
5512 }
Oren Milman6db70332017-09-19 14:23:01 +03005513 if (!PyUnicode_Check(pkgname)) {
5514 Py_CLEAR(pkgname);
5515 goto error;
5516 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005517 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005518 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005519 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005520 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005521 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005522 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005523 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005524 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005525 goto error;
5526 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005527 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005529 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005530 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005531 if (pkgname == NULL) {
5532 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5533 if (pkgname_or_unknown == NULL) {
5534 Py_XDECREF(pkgpath);
5535 return NULL;
5536 }
5537 } else {
5538 pkgname_or_unknown = pkgname;
5539 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005540
5541 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005542 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005543 errmsg = PyUnicode_FromFormat(
5544 "cannot import name %R from %R (unknown location)",
5545 name, pkgname_or_unknown
5546 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005547 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005548 PyErr_SetImportError(errmsg, pkgname, NULL);
5549 }
5550 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005551 _Py_IDENTIFIER(__spec__);
5552 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005553 const char *fmt =
5554 _PyModuleSpec_IsInitializing(spec) ?
5555 "cannot import name %R from partially initialized module %R "
5556 "(most likely due to a circular import) (%S)" :
5557 "cannot import name %R from %R (%S)";
5558 Py_XDECREF(spec);
5559
5560 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005561 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005562 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005563 }
5564
Xiang Zhang4830f582017-03-21 11:13:42 +08005565 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005566 Py_XDECREF(pkgname_or_unknown);
5567 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005568 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005569}
Guido van Rossumac7be682001-01-17 15:42:30 +00005570
Thomas Wouters52152252000-08-17 22:55:00 +00005571static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005572import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005573{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005574 _Py_IDENTIFIER(__all__);
5575 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005576 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 int skip_leading_underscores = 0;
5578 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005579
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005580 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5581 return -1; /* Unexpected error */
5582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005584 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5585 return -1;
5586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005588 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005589 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 return -1;
5591 }
5592 all = PyMapping_Keys(dict);
5593 Py_DECREF(dict);
5594 if (all == NULL)
5595 return -1;
5596 skip_leading_underscores = 1;
5597 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 for (pos = 0, err = 0; ; pos++) {
5600 name = PySequence_GetItem(all, pos);
5601 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005602 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005604 }
5605 else {
5606 _PyErr_Clear(tstate);
5607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 break;
5609 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005610 if (!PyUnicode_Check(name)) {
5611 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5612 if (modname == NULL) {
5613 Py_DECREF(name);
5614 err = -1;
5615 break;
5616 }
5617 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005618 _PyErr_Format(tstate, PyExc_TypeError,
5619 "module __name__ must be a string, not %.100s",
5620 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005621 }
5622 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005623 _PyErr_Format(tstate, PyExc_TypeError,
5624 "%s in %U.%s must be str, not %.100s",
5625 skip_leading_underscores ? "Key" : "Item",
5626 modname,
5627 skip_leading_underscores ? "__dict__" : "__all__",
5628 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005629 }
5630 Py_DECREF(modname);
5631 Py_DECREF(name);
5632 err = -1;
5633 break;
5634 }
5635 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005636 if (PyUnicode_READY(name) == -1) {
5637 Py_DECREF(name);
5638 err = -1;
5639 break;
5640 }
5641 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5642 Py_DECREF(name);
5643 continue;
5644 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005645 }
5646 value = PyObject_GetAttr(v, name);
5647 if (value == NULL)
5648 err = -1;
5649 else if (PyDict_CheckExact(locals))
5650 err = PyDict_SetItem(locals, name, value);
5651 else
5652 err = PyObject_SetItem(locals, name, value);
5653 Py_DECREF(name);
5654 Py_XDECREF(value);
5655 if (err != 0)
5656 break;
5657 }
5658 Py_DECREF(all);
5659 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005660}
5661
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005662static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005663check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005664{
Victor Stinnera102ed72020-02-07 02:24:48 +01005665 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005666 /* check_args_iterable() may be called with a live exception:
5667 * clear it to prevent calling _PyObject_FunctionStr() with an
5668 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005669 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005670 PyObject *funcstr = _PyObject_FunctionStr(func);
5671 if (funcstr != NULL) {
5672 _PyErr_Format(tstate, PyExc_TypeError,
5673 "%U argument after * must be an iterable, not %.200s",
5674 funcstr, Py_TYPE(args)->tp_name);
5675 Py_DECREF(funcstr);
5676 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005677 return -1;
5678 }
5679 return 0;
5680}
5681
5682static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005683format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005684{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005685 /* _PyDict_MergeEx raises attribute
5686 * error (percolated from an attempt
5687 * to get 'keys' attribute) instead of
5688 * a type error if its second argument
5689 * is not a mapping.
5690 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005691 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005692 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005693 PyObject *funcstr = _PyObject_FunctionStr(func);
5694 if (funcstr != NULL) {
5695 _PyErr_Format(
5696 tstate, PyExc_TypeError,
5697 "%U argument after ** must be a mapping, not %.200s",
5698 funcstr, Py_TYPE(kwargs)->tp_name);
5699 Py_DECREF(funcstr);
5700 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005701 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005702 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005703 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005704 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005705 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005706 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005707 PyObject *funcstr = _PyObject_FunctionStr(func);
5708 if (funcstr != NULL) {
5709 PyObject *key = PyTuple_GET_ITEM(val, 0);
5710 _PyErr_Format(
5711 tstate, PyExc_TypeError,
5712 "%U got multiple values for keyword argument '%S'",
5713 funcstr, key);
5714 Py_DECREF(funcstr);
5715 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005716 Py_XDECREF(exc);
5717 Py_XDECREF(val);
5718 Py_XDECREF(tb);
5719 }
5720 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005721 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005722 }
5723 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005724}
5725
Guido van Rossumac7be682001-01-17 15:42:30 +00005726static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005727format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5728 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005730 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005732 if (!obj)
5733 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005734
Serhiy Storchaka06515832016-11-20 09:13:07 +02005735 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 if (!obj_str)
5737 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005738
Victor Stinner438a12d2019-05-24 17:01:38 +02005739 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005740}
Guido van Rossum950361c1997-01-24 13:49:28 +00005741
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005742static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005743format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005744{
5745 PyObject *name;
5746 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005747 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005748 return;
5749 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5750 name = PyTuple_GET_ITEM(co->co_cellvars,
5751 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005752 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005753 PyExc_UnboundLocalError,
5754 UNBOUNDLOCAL_ERROR_MSG,
5755 name);
5756 } else {
5757 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5758 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005759 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005760 UNBOUNDFREE_ERROR_MSG, name);
5761 }
5762}
5763
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005764static void
Mark Shannonfee55262019-11-21 09:11:43 +00005765format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005766{
5767 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5768 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005769 _PyErr_Format(tstate, PyExc_TypeError,
5770 "'async with' received an object from __aenter__ "
5771 "that does not implement __await__: %.100s",
5772 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005773 }
Mark Shannonfee55262019-11-21 09:11:43 +00005774 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 _PyErr_Format(tstate, PyExc_TypeError,
5776 "'async with' received an object from __aexit__ "
5777 "that does not implement __await__: %.100s",
5778 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005779 }
5780 }
5781}
5782
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005783static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005784unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005785 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005786{
5787 PyObject *res;
5788 if (Py_REFCNT(v) == 2) {
5789 /* In the common case, there are 2 references to the value
5790 * stored in 'variable' when the += is performed: one on the
5791 * value stack (in 'v') and one still stored in the
5792 * 'variable'. We try to delete the variable now to reduce
5793 * the refcnt to 1.
5794 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005795 int opcode, oparg;
5796 NEXTOPARG();
5797 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005798 case STORE_FAST:
5799 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005800 PyObject **fastlocals = f->f_localsplus;
5801 if (GETLOCAL(oparg) == v)
5802 SETLOCAL(oparg, NULL);
5803 break;
5804 }
5805 case STORE_DEREF:
5806 {
5807 PyObject **freevars = (f->f_localsplus +
5808 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005809 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005810 if (PyCell_GET(c) == v) {
5811 PyCell_SET(c, NULL);
5812 Py_DECREF(v);
5813 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005814 break;
5815 }
5816 case STORE_NAME:
5817 {
5818 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005819 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005820 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005821 if (locals && PyDict_CheckExact(locals)) {
5822 PyObject *w = PyDict_GetItemWithError(locals, name);
5823 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005824 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005825 {
5826 Py_DECREF(v);
5827 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005828 }
5829 }
5830 break;
5831 }
5832 }
5833 }
5834 res = v;
5835 PyUnicode_Append(&res, w);
5836 return res;
5837}
5838
Guido van Rossum950361c1997-01-24 13:49:28 +00005839#ifdef DYNAMIC_EXECUTION_PROFILE
5840
Skip Montanarof118cb12001-10-15 20:51:38 +00005841static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005842getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005844 int i;
5845 PyObject *l = PyList_New(256);
5846 if (l == NULL) return NULL;
5847 for (i = 0; i < 256; i++) {
5848 PyObject *x = PyLong_FromLong(a[i]);
5849 if (x == NULL) {
5850 Py_DECREF(l);
5851 return NULL;
5852 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005853 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005854 }
5855 for (i = 0; i < 256; i++)
5856 a[i] = 0;
5857 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005858}
5859
5860PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005861_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005862{
5863#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005864 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005865#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005866 int i;
5867 PyObject *l = PyList_New(257);
5868 if (l == NULL) return NULL;
5869 for (i = 0; i < 257; i++) {
5870 PyObject *x = getarray(dxpairs[i]);
5871 if (x == NULL) {
5872 Py_DECREF(l);
5873 return NULL;
5874 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005875 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005876 }
5877 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005878#endif
5879}
5880
5881#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005882
5883Py_ssize_t
5884_PyEval_RequestCodeExtraIndex(freefunc free)
5885{
Victor Stinner81a7be32020-04-14 15:14:01 +02005886 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005887 Py_ssize_t new_index;
5888
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005889 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005890 return -1;
5891 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005892 new_index = interp->co_extra_user_count++;
5893 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005894 return new_index;
5895}
Łukasz Langaa785c872016-09-09 17:37:37 -07005896
5897static void
5898dtrace_function_entry(PyFrameObject *f)
5899{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005900 const char *filename;
5901 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005902 int lineno;
5903
Victor Stinner6d86a232020-04-29 00:56:58 +02005904 PyCodeObject *code = f->f_code;
5905 filename = PyUnicode_AsUTF8(code->co_filename);
5906 funcname = PyUnicode_AsUTF8(code->co_name);
5907 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005908
Andy Lestere6be9b52020-02-11 20:28:35 -06005909 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005910}
5911
5912static void
5913dtrace_function_return(PyFrameObject *f)
5914{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005915 const char *filename;
5916 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005917 int lineno;
5918
Victor Stinner6d86a232020-04-29 00:56:58 +02005919 PyCodeObject *code = f->f_code;
5920 filename = PyUnicode_AsUTF8(code->co_filename);
5921 funcname = PyUnicode_AsUTF8(code->co_name);
5922 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005923
Andy Lestere6be9b52020-02-11 20:28:35 -06005924 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005925}
5926
5927/* DTrace equivalent of maybe_call_line_trace. */
5928static void
5929maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005930 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07005931{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005932 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005933
5934 /* If the last instruction executed isn't in the current
5935 instruction window, reset the window.
5936 */
Mark Shannon877df852020-11-12 09:43:29 +00005937 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07005938 /* If the last instruction falls at the start of a line or if
5939 it represents a jump backwards, update the frame's line
5940 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00005941 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
5942 if (line != -1) {
5943 frame->f_lineno = line;
5944 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5945 if (!co_filename)
5946 co_filename = "?";
5947 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5948 if (!co_name)
5949 co_name = "?";
5950 PyDTrace_LINE(co_filename, co_name, line);
5951 }
Łukasz Langaa785c872016-09-09 17:37:37 -07005952 }
5953 *instr_prev = frame->f_lasti;
5954}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005955
5956
5957/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5958 for the limited API. */
5959
5960#undef Py_EnterRecursiveCall
5961
5962int Py_EnterRecursiveCall(const char *where)
5963{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005964 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005965}
5966
5967#undef Py_LeaveRecursiveCall
5968
5969void Py_LeaveRecursiveCall(void)
5970{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005971 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005972}