blob: 6bd2d6bc13d86f124fba2deedda7981039006f8c [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 *,
69 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070070static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
71static 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
Inada Naoki91234a12019-06-03 21:30:58 +0900114#define OPCACHE_STATS 0 /* Enable stats */
115
116#if OPCACHE_STATS
117static size_t opcache_code_objects = 0;
118static size_t opcache_code_objects_extra_mem = 0;
119
120static size_t opcache_global_opts = 0;
121static size_t opcache_global_hits = 0;
122static size_t opcache_global_misses = 0;
123#endif
124
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100125
Victor Stinnerda2914d2020-03-20 09:29:08 +0100126#ifndef NDEBUG
127/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
128 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
129 when a thread continues to run after Python finalization, especially
130 daemon threads. */
131static int
132is_tstate_valid(PyThreadState *tstate)
133{
134 assert(!_PyMem_IsPtrFreed(tstate));
135 assert(!_PyMem_IsPtrFreed(tstate->interp));
136 return 1;
137}
138#endif
139
140
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000141/* This can set eval_breaker to 0 even though gil_drop_request became
142 1. We believe this is all right because the eval loop will release
143 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100144static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200145COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200146 struct _ceval_runtime_state *ceval,
147 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100148{
Victor Stinner299b8c62020-05-05 17:40:18 +0200149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200155 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100156}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200160SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100161{
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 struct _ceval_state *ceval2 = &interp->ceval;
163 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
164 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200169RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170{
Victor Stinner299b8c62020-05-05 17:40:18 +0200171 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
172 struct _ceval_state *ceval2 = &interp->ceval;
173 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
174 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Eric Snowfdf282d2019-01-11 14:26:55 -0700177
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200179SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180{
Victor Stinner299b8c62020-05-05 17:40:18 +0200181 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
184 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Eric Snowfdf282d2019-01-11 14:26:55 -0700186
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
196
197
198static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200199SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200203 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100204 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinner299b8c62020-05-05 17:40:18 +0200205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
207
208
209static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200210UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200214 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200215 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216}
217
218
219static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200220SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223 ceval2->pending.async_exc = 1;
224 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
225}
226
227
228static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200229UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100230{
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
232 struct _ceval_state *ceval2 = &interp->ceval;
233 ceval2->pending.async_exc = 0;
234 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236
237
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000240#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242
Victor Stinner3026cad2020-06-01 16:02:40 +0200243void _Py_NO_RETURN
244_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100245{
Victor Stinner3026cad2020-06-01 16:02:40 +0200246 _Py_FatalErrorFunc(func,
247 "the function must be called with the GIL held, "
248 "but the GIL is released "
249 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100250}
251
Victor Stinner7be4e352020-05-05 20:27:47 +0200252#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
253int
254_PyEval_ThreadsInitialized(PyInterpreterState *interp)
255{
256 return gil_created(&interp->ceval.gil);
257}
258
259int
260PyEval_ThreadsInitialized(void)
261{
262 // Fatal error if there is no current interpreter
263 PyInterpreterState *interp = PyInterpreterState_Get();
264 return _PyEval_ThreadsInitialized(interp);
265}
266#else
Tim Peters7f468f22004-10-11 02:40:51 +0000267int
Victor Stinner175a7042020-03-10 00:37:48 +0100268_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
269{
270 return gil_created(&runtime->ceval.gil);
271}
272
273int
Tim Peters7f468f22004-10-11 02:40:51 +0000274PyEval_ThreadsInitialized(void)
275{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100276 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100277 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000278}
Victor Stinner7be4e352020-05-05 20:27:47 +0200279#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000280
Victor Stinner111e4ee2020-03-09 21:24:14 +0100281PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200282_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283{
Victor Stinner7be4e352020-05-05 20:27:47 +0200284#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200285 if (!_Py_IsMainInterpreter(tstate)) {
286 /* Currently, the GIL is shared by all interpreters,
287 and only the main interpreter is responsible to create
288 and destroy it. */
289 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100290 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200291#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100292
Victor Stinner7be4e352020-05-05 20:27:47 +0200293#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
294 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
295#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200296 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200297#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200298 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100299
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200300 PyThread_init_thread();
301 create_gil(gil);
302
303 take_gil(tstate);
304
305 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100306 return _PyStatus_OK();
307}
308
309void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200310_PyEval_FiniGIL(PyThreadState *tstate)
311{
Victor Stinner7be4e352020-05-05 20:27:47 +0200312#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200313 if (!_Py_IsMainInterpreter(tstate)) {
314 /* Currently, the GIL is shared by all interpreters,
315 and only the main interpreter is responsible to create
316 and destroy it. */
317 return;
318 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200319#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200320
Victor Stinner7be4e352020-05-05 20:27:47 +0200321#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
322 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
323#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200324 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200325#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200326 if (!gil_created(gil)) {
327 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
328 yet: do nothing. */
329 return;
330 }
331
332 destroy_gil(gil);
333 assert(!gil_created(gil));
334}
335
336void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100337PyEval_InitThreads(void)
338{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100339 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000341
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000342void
Inada Naoki91234a12019-06-03 21:30:58 +0900343_PyEval_Fini(void)
344{
345#if OPCACHE_STATS
346 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
347 opcache_code_objects);
348
349 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
350 opcache_code_objects_extra_mem);
351
352 fprintf(stderr, "\n");
353
354 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
355 opcache_global_hits,
356 (int) (100.0 * opcache_global_hits /
357 (opcache_global_hits + opcache_global_misses)));
358
359 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
360 opcache_global_misses,
361 (int) (100.0 * opcache_global_misses /
362 (opcache_global_hits + opcache_global_misses)));
363
364 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
365 opcache_global_opts);
366
367 fprintf(stderr, "\n");
368#endif
369}
370
371void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000373{
Victor Stinner09532fe2019-05-10 23:39:09 +0200374 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200375 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200376 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100377
Victor Stinner85f5a692020-03-09 22:12:04 +0100378 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000379}
380
381void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383{
Victor Stinner09532fe2019-05-10 23:39:09 +0200384 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200385 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100387 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100388 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200389 struct _ceval_runtime_state *ceval = &runtime->ceval;
390 struct _ceval_state *ceval2 = &tstate->interp->ceval;
391 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392}
393
394void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100395_PyEval_ReleaseLock(PyThreadState *tstate)
396{
397 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200398 struct _ceval_state *ceval2 = &tstate->interp->ceval;
399 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100400}
401
402void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000404{
Victor Stinner3026cad2020-06-01 16:02:40 +0200405 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100406
Victor Stinner85f5a692020-03-09 22:12:04 +0100407 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200408
Victor Stinner85f5a692020-03-09 22:12:04 +0100409 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200410#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
411 (void)_PyThreadState_Swap(gilstate, tstate);
412#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100413 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100414 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200415 }
Victor Stinnere838a932020-05-05 19:56:48 +0200416#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000417}
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000421{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100422 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200423
Victor Stinner01b1cc12019-11-20 02:27:56 +0100424 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200425 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
426 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100427 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200428 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200429 struct _ceval_runtime_state *ceval = &runtime->ceval;
430 struct _ceval_state *ceval2 = &tstate->interp->ceval;
431 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000432}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000433
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900434#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200435/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200436 which are not running in the child process, and clear internal locks
437 which might be held by those threads. */
438PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200439_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000440{
Victor Stinner317bab02020-06-02 18:44:54 +0200441 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200442
443#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
444 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
445#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100446 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200447#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100448 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200449 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200450 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100451 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100452
453 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700454
Victor Stinner50e6e992020-03-19 02:41:21 +0100455 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900456 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200457 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700458 }
Jesse Nollera8513972008-07-17 16:49:17 +0000459
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200460 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100461 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200462 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000463}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900464#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000465
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000466/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600467 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000468
469void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100470_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000471{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200472 assert(is_tstate_valid(tstate));
473 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000474}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000475
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000476PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000477PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000478{
Victor Stinner09532fe2019-05-10 23:39:09 +0200479 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200480#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
481 PyThreadState *old_tstate = _PyThreadState_GET();
482 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
483#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200484 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200485#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200486 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100487
Victor Stinner0b1e3302020-05-05 16:14:31 +0200488 struct _ceval_runtime_state *ceval = &runtime->ceval;
489 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200490#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
491 assert(gil_created(&ceval2->gil));
492#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200493 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200494#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200495 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000497}
498
499void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000500PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000501{
Victor Stinner3026cad2020-06-01 16:02:40 +0200502 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100503
Victor Stinner85f5a692020-03-09 22:12:04 +0100504 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100505
Victor Stinner85f5a692020-03-09 22:12:04 +0100506 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
507 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000508}
509
510
Guido van Rossuma9672091994-09-14 13:31:22 +0000511/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
512 signal handlers or Mac I/O completion routines) can schedule calls
513 to a function to be called synchronously.
514 The synchronous function is called with one void* argument.
515 It should return 0 for success or -1 for failure -- failure should
516 be accompanied by an exception.
517
518 If registry succeeds, the registry function returns 0; if it fails
519 (e.g. due to too many pending calls) it returns -1 (without setting
520 an exception condition).
521
522 Note that because registry may occur from within signal handlers,
523 or other asynchronous events, calling malloc() is unsafe!
524
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 Any thread can schedule pending calls, but only the main thread
526 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000527 There is no facility to schedule calls to a particular thread, but
528 that should be easy to change, should that ever be required. In
529 that case, the static variables here should go into the python
530 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000531*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000532
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200533void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200534_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200535{
536 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100537 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200538 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200539 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200540}
541
Eric Snow5be45a62019-03-08 22:47:07 -0700542/* Push one item onto the queue while holding the lock. */
543static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200544_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600545 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700546{
Eric Snow842a2f02019-03-15 15:47:51 -0600547 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700548 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600549 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700550 return -1; /* Queue full */
551 }
Eric Snow842a2f02019-03-15 15:47:51 -0600552 pending->calls[i].func = func;
553 pending->calls[i].arg = arg;
554 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700555 return 0;
556}
557
558/* Pop one item off the queue while holding the lock. */
559static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200560_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600561 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700562{
Eric Snow842a2f02019-03-15 15:47:51 -0600563 int i = pending->first;
564 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700565 return; /* Queue empty */
566 }
567
Eric Snow842a2f02019-03-15 15:47:51 -0600568 *func = pending->calls[i].func;
569 *arg = pending->calls[i].arg;
570 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700571}
572
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200573/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000574 scheduling to be made from any thread, and even from an executing
575 callback.
576 */
577
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000578int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200579_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200580 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000581{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200582 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600583
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200584 /* Ensure that _PyEval_InitPendingCalls() was called
585 and that _PyEval_FiniPendingCalls() is not called yet. */
586 assert(pending->lock != NULL);
587
Eric Snow842a2f02019-03-15 15:47:51 -0600588 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200589 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600590 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700591
Victor Stinnere225beb2019-06-03 18:14:24 +0200592 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200593 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000595}
596
Victor Stinner09532fe2019-05-10 23:39:09 +0200597int
598Py_AddPendingCall(int (*func)(void *), void *arg)
599{
Victor Stinner50e6e992020-03-19 02:41:21 +0100600 /* Best-effort to support subinterpreters and calls with the GIL released.
601
602 First attempt _PyThreadState_GET() since it supports subinterpreters.
603
604 If the GIL is released, _PyThreadState_GET() returns NULL . In this
605 case, use PyGILState_GetThisThreadState() which works even if the GIL
606 is released.
607
608 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
609 see bpo-10915 and bpo-15751.
610
Victor Stinner8849e592020-03-18 19:28:53 +0100611 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100612 PyThreadState *tstate = _PyThreadState_GET();
613 if (tstate == NULL) {
614 tstate = PyGILState_GetThisThreadState();
615 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200616
617 PyInterpreterState *interp;
618 if (tstate != NULL) {
619 interp = tstate->interp;
620 }
621 else {
622 /* Last resort: use the main interpreter */
623 interp = _PyRuntime.interpreters.main;
624 }
625 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200626}
627
Eric Snowfdf282d2019-01-11 14:26:55 -0700628static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100629handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700630{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200631 assert(is_tstate_valid(tstate));
632 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700633 return 0;
634 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700635
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200636 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100637 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
638 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700640 return -1;
641 }
642 return 0;
643}
644
645static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100646make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000647{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200648 assert(is_tstate_valid(tstate));
649
Victor Stinnerd8316882020-03-20 14:50:35 +0100650 /* only execute pending calls on main thread */
651 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200652 return 0;
653 }
654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100656 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700657 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700659 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200660 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100661
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200662 /* unsignal before starting to call callbacks, so that any callback
663 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200664 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700665 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100668 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700669 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700670 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 void *arg = NULL;
672
673 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600674 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200675 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600676 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700677
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100678 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700679 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100680 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700681 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700682 res = func(arg);
683 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200684 goto error;
685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200687
Charles-François Natalif23339a2011-07-23 18:15:43 +0200688 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700689 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200690
691error:
692 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200693 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700694 return res;
695}
696
Eric Snow842a2f02019-03-15 15:47:51 -0600697void
Victor Stinner2b1df452020-01-13 18:46:59 +0100698_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600699{
Eric Snow842a2f02019-03-15 15:47:51 -0600700 assert(PyGILState_Check());
701
Victor Stinner50e6e992020-03-19 02:41:21 +0100702 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200703
Eric Snow842a2f02019-03-15 15:47:51 -0600704 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
705 return;
706 }
707
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100708 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200709 PyObject *exc, *val, *tb;
710 _PyErr_Fetch(tstate, &exc, &val, &tb);
711 PyErr_BadInternalCall();
712 _PyErr_ChainExceptions(exc, val, tb);
713 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600714 }
715}
716
Eric Snowfdf282d2019-01-11 14:26:55 -0700717/* Py_MakePendingCalls() is a simple wrapper for the sake
718 of backward-compatibility. */
719int
720Py_MakePendingCalls(void)
721{
722 assert(PyGILState_Check());
723
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100724 PyThreadState *tstate = _PyThreadState_GET();
725
Eric Snowfdf282d2019-01-11 14:26:55 -0700726 /* Python signal handler doesn't really queue a callback: it only signals
727 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100728 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700729 if (res != 0) {
730 return res;
731 }
732
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100733 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600734 if (res != 0) {
735 return res;
736 }
737
738 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000739}
740
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000741/* The interpreter's recursion limit */
742
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000743#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200744# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000745#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600746
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600747void
Victor Stinnerdab84232020-03-17 18:56:44 +0100748_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600749{
Victor Stinner7be4e352020-05-05 20:27:47 +0200750#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100751 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200752#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100753}
754
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200755int
Victor Stinnerdab84232020-03-17 18:56:44 +0100756_PyEval_InitState(struct _ceval_state *ceval)
757{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200758 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
759
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200760 struct _pending_calls *pending = &ceval->pending;
761 assert(pending->lock == NULL);
762
763 pending->lock = PyThread_allocate_lock();
764 if (pending->lock == NULL) {
765 return -1;
766 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200767
768#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
769 _gil_initialize(&ceval->gil);
770#endif
771
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200772 return 0;
773}
774
775void
776_PyEval_FiniState(struct _ceval_state *ceval)
777{
778 struct _pending_calls *pending = &ceval->pending;
779 if (pending->lock != NULL) {
780 PyThread_free_lock(pending->lock);
781 pending->lock = NULL;
782 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600783}
784
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000785int
786Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000787{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200788 PyInterpreterState *interp = _PyInterpreterState_GET();
789 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000790}
791
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000792void
793Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000794{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200795 PyThreadState *tstate = _PyThreadState_GET();
796 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000797}
798
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100799/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200800 if the recursion_depth reaches recursion_limit.
801 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000802 to guarantee that _Py_CheckRecursiveCall() is regularly called.
803 Without USE_STACKCHECK, there is no need for this. */
804int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100805_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000806{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200807 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000808
809#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700810 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (PyOS_CheckStack()) {
812 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200813 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 return -1;
815 }
pdox18967932017-10-25 23:03:01 -0700816#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (tstate->recursion_critical)
818 /* Somebody asked that we don't check for recursion. */
819 return 0;
820 if (tstate->overflowed) {
821 if (tstate->recursion_depth > recursion_limit + 50) {
822 /* Overflowing while handling an overflow. Give up. */
823 Py_FatalError("Cannot recover from stack overflow.");
824 }
825 return 0;
826 }
827 if (tstate->recursion_depth > recursion_limit) {
828 --tstate->recursion_depth;
829 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200830 _PyErr_Format(tstate, PyExc_RecursionError,
831 "maximum recursion depth exceeded%s",
832 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 return -1;
834 }
835 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000836}
837
Victor Stinner09532fe2019-05-10 23:39:09 +0200838static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200839static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000840
Victor Stinnere225beb2019-06-03 18:14:24 +0200841#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843
Guido van Rossumb209a111997-04-29 18:18:01 +0000844PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000845PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 return PyEval_EvalCodeEx(co,
848 globals, locals,
849 (PyObject **)NULL, 0,
850 (PyObject **)NULL, 0,
851 (PyObject **)NULL, 0,
852 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000853}
854
855
856/* Interpreter main loop */
857
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000858PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100859PyEval_EvalFrame(PyFrameObject *f)
860{
Victor Stinner0b72b232020-03-12 23:18:39 +0100861 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100862 PyThreadState *tstate = _PyThreadState_GET();
863 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000864}
865
866PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000868{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100869 PyThreadState *tstate = _PyThreadState_GET();
870 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700871}
872
Victor Stinnerda2914d2020-03-20 09:29:08 +0100873
874/* Handle signals, pending calls, GIL drop request
875 and asynchronous exception */
876static int
877eval_frame_handle_pending(PyThreadState *tstate)
878{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100879 _PyRuntimeState * const runtime = &_PyRuntime;
880 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200881
882 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200883 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100884 if (handle_signals(tstate) != 0) {
885 return -1;
886 }
887 }
888
889 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200890 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100891 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
892 if (make_pending_calls(tstate) != 0) {
893 return -1;
894 }
895 }
896
897 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200898 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100899 /* Give another thread a chance */
900 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
901 Py_FatalError("tstate mix-up");
902 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200903 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100904
905 /* Other threads may run now */
906
907 take_gil(tstate);
908
Victor Stinnere838a932020-05-05 19:56:48 +0200909#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
910 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
911#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100912 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
913 Py_FatalError("orphan tstate");
914 }
Victor Stinnere838a932020-05-05 19:56:48 +0200915#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100916 }
917
918 /* Check for asynchronous exception. */
919 if (tstate->async_exc != NULL) {
920 PyObject *exc = tstate->async_exc;
921 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200922 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100923 _PyErr_SetNone(tstate, exc);
924 Py_DECREF(exc);
925 return -1;
926 }
927
928 return 0;
929}
930
Victor Stinnerc6944e72016-11-11 02:13:35 +0100931PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100932_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700933{
Victor Stinner3026cad2020-06-01 16:02:40 +0200934 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100935
Guido van Rossum950361c1997-01-24 13:49:28 +0000936#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000938#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200939 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300940 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200941 int opcode; /* Current opcode */
942 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200943 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100945 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100946 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 is true when the line being executed has changed. The
954 initial values are such as to make this false the first
955 time it is tested. */
956 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000957
Serhiy Storchakaab874002016-09-11 13:48:15 +0300958 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyObject *names;
960 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900961 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000962
Brett Cannon368b4b72012-04-02 12:17:59 -0400963#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200964 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400965#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200966
Antoine Pitroub52ec782009-01-25 16:34:23 +0000967/* Computed GOTOs, or
968 the-optimization-commonly-but-improperly-known-as-"threaded code"
969 using gcc's labels-as-values extension
970 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
971
972 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000974 combined with a lookup table of jump addresses. However, since the
975 indirect jump instruction is shared by all opcodes, the CPU will have a
976 hard time making the right prediction for where to jump next (actually,
977 it will be always wrong except in the uncommon case of a sequence of
978 several identical opcodes).
979
980 "Threaded code" in contrast, uses an explicit jump table and an explicit
981 indirect jump instruction at the end of each opcode. Since the jump
982 instruction is at a different address for each opcode, the CPU will make a
983 separate prediction for each of these instructions, which is equivalent to
984 predicting the second opcode of each opcode pair. These predictions have
985 a much better chance to turn out valid, especially in small bytecode loops.
986
987 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000989 and potentially many more instructions (depending on the pipeline width).
990 A correctly predicted branch, however, is nearly free.
991
992 At the time of this writing, the "threaded code" version is up to 15-20%
993 faster than the normal "switch" version, depending on the compiler and the
994 CPU architecture.
995
996 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
997 because it would render the measurements invalid.
998
999
1000 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1001 indirect jumps by sharing them between all opcodes. Such optimizations
1002 can be disabled on gcc by using the -fno-gcse flag (or possibly
1003 -fno-crossjumping).
1004*/
1005
Antoine Pitrou042b1282010-08-13 21:15:58 +00001006#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001007#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001008#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001009#endif
1010
Antoine Pitrou042b1282010-08-13 21:15:58 +00001011#ifdef HAVE_COMPUTED_GOTOS
1012 #ifndef USE_COMPUTED_GOTOS
1013 #define USE_COMPUTED_GOTOS 1
1014 #endif
1015#else
1016 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1017 #error "Computed gotos are not supported on this compiler."
1018 #endif
1019 #undef USE_COMPUTED_GOTOS
1020 #define USE_COMPUTED_GOTOS 0
1021#endif
1022
1023#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024/* Import the static jump table */
1025#include "opcode_targets.h"
1026
Antoine Pitroub52ec782009-01-25 16:34:23 +00001027#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001028 op: \
1029 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001030
Antoine Pitroub52ec782009-01-25 16:34:23 +00001031#ifdef LLTRACE
1032#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001034 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001036 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 } \
1039 goto fast_next_opcode; \
1040 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001041#else
1042#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001044 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001046 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001047 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 } \
1049 goto fast_next_opcode; \
1050 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001051#endif
1052
Victor Stinner09532fe2019-05-10 23:39:09 +02001053#define DISPATCH() \
1054 { \
1055 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1056 FAST_DISPATCH(); \
1057 } \
1058 continue; \
1059 }
1060
Antoine Pitroub52ec782009-01-25 16:34:23 +00001061#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001062#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001063#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001064#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001065#endif
1066
1067
Neal Norwitza81d2202002-07-14 00:27:26 +00001068/* Tuple access macros */
1069
1070#ifndef Py_DEBUG
1071#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1072#else
1073#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1074#endif
1075
Guido van Rossum374a9221991-04-04 10:40:29 +00001076/* Code access macros */
1077
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001078/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001079#define INSTR_OFFSET() \
1080 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001081#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001082 _Py_CODEUNIT word = *next_instr; \
1083 opcode = _Py_OPCODE(word); \
1084 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001085 next_instr++; \
1086 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001087#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1088#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001089
Raymond Hettingerf606f872003-03-16 03:11:04 +00001090/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 Some opcodes tend to come in pairs thus making it possible to
1092 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001093 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 Verifying the prediction costs a single high-speed test of a register
1096 variable against a constant. If the pairing was good, then the
1097 processor's own internal branch predication has a high likelihood of
1098 success, resulting in a nearly zero-overhead transition to the
1099 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001100 including its unpredictable switch-case branch. Combined with the
1101 processor's internal branch prediction, a successful PREDICT has the
1102 effect of making the two opcodes run as if they were a single new opcode
1103 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001104
Georg Brandl86b2fb92008-07-16 03:43:04 +00001105 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 predictions turned-on and interpret the results as if some opcodes
1107 had been combined or turn-off predictions so that the opcode frequency
1108 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001109
1110 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 the CPU to record separate branch prediction information for each
1112 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001113
Raymond Hettingerf606f872003-03-16 03:11:04 +00001114*/
1115
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001116#define PREDICT_ID(op) PRED_##op
1117
Antoine Pitrou042b1282010-08-13 21:15:58 +00001118#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001119#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001120#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001121#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001122 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001123 _Py_CODEUNIT word = *next_instr; \
1124 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001125 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001126 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001127 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001128 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001129 } \
1130 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001131#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001132#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001133
Raymond Hettingerf606f872003-03-16 03:11:04 +00001134
Guido van Rossum374a9221991-04-04 10:40:29 +00001135/* Stack manipulation macros */
1136
Martin v. Löwis18e16552006-02-15 17:27:45 +00001137/* The stack can grow at most MAXINT deep, as co_nlocals and
1138 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001139#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1140#define EMPTY() (STACK_LEVEL() == 0)
1141#define TOP() (stack_pointer[-1])
1142#define SECOND() (stack_pointer[-2])
1143#define THIRD() (stack_pointer[-3])
1144#define FOURTH() (stack_pointer[-4])
1145#define PEEK(n) (stack_pointer[-(n)])
1146#define SET_TOP(v) (stack_pointer[-1] = (v))
1147#define SET_SECOND(v) (stack_pointer[-2] = (v))
1148#define SET_THIRD(v) (stack_pointer[-3] = (v))
1149#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001150#define BASIC_STACKADJ(n) (stack_pointer += n)
1151#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1152#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001153
Guido van Rossum96a42c81992-01-12 02:29:51 +00001154#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001156 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001157 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001158#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001159 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001160#define STACK_GROW(n) do { \
1161 assert(n >= 0); \
1162 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001163 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001164 assert(STACK_LEVEL() <= co->co_stacksize); \
1165 } while (0)
1166#define STACK_SHRINK(n) do { \
1167 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001168 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001169 (void)(BASIC_STACKADJ(-n)); \
1170 assert(STACK_LEVEL() <= co->co_stacksize); \
1171 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001172#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001173 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001174 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001175#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001176#define PUSH(v) BASIC_PUSH(v)
1177#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001178#define STACK_GROW(n) BASIC_STACKADJ(n)
1179#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001180#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001181#endif
1182
Guido van Rossum681d79a1995-07-18 14:51:37 +00001183/* Local variable macros */
1184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001186
1187/* The SETLOCAL() macro must not DECREF the local variable in-place and
1188 then store the new value; it must copy the old value to a temporary
1189 value, then store the new value, and then DECREF the temporary value.
1190 This is because it is possible that during the DECREF the frame is
1191 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1192 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001194 GETLOCAL(i) = value; \
1195 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001196
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001197
1198#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 while (STACK_LEVEL() > (b)->b_level) { \
1200 PyObject *v = POP(); \
1201 Py_XDECREF(v); \
1202 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001203
1204#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001205 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001207 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1209 while (STACK_LEVEL() > (b)->b_level + 3) { \
1210 value = POP(); \
1211 Py_XDECREF(value); \
1212 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001213 exc_info = tstate->exc_info; \
1214 type = exc_info->exc_type; \
1215 value = exc_info->exc_value; \
1216 traceback = exc_info->exc_traceback; \
1217 exc_info->exc_type = POP(); \
1218 exc_info->exc_value = POP(); \
1219 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 Py_XDECREF(type); \
1221 Py_XDECREF(value); \
1222 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001223 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001224
Inada Naoki91234a12019-06-03 21:30:58 +09001225 /* macros for opcode cache */
1226#define OPCACHE_CHECK() \
1227 do { \
1228 co_opcache = NULL; \
1229 if (co->co_opcache != NULL) { \
1230 unsigned char co_opt_offset = \
1231 co->co_opcache_map[next_instr - first_instr]; \
1232 if (co_opt_offset > 0) { \
1233 assert(co_opt_offset <= co->co_opcache_size); \
1234 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1235 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001236 } \
1237 } \
1238 } while (0)
1239
1240#if OPCACHE_STATS
1241
1242#define OPCACHE_STAT_GLOBAL_HIT() \
1243 do { \
1244 if (co->co_opcache != NULL) opcache_global_hits++; \
1245 } while (0)
1246
1247#define OPCACHE_STAT_GLOBAL_MISS() \
1248 do { \
1249 if (co->co_opcache != NULL) opcache_global_misses++; \
1250 } while (0)
1251
1252#define OPCACHE_STAT_GLOBAL_OPT() \
1253 do { \
1254 if (co->co_opcache != NULL) opcache_global_opts++; \
1255 } while (0)
1256
1257#else /* OPCACHE_STATS */
1258
1259#define OPCACHE_STAT_GLOBAL_HIT()
1260#define OPCACHE_STAT_GLOBAL_MISS()
1261#define OPCACHE_STAT_GLOBAL_OPT()
1262
1263#endif
1264
Guido van Rossuma027efa1997-05-05 20:56:21 +00001265/* Start of code */
1266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001268 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001270 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (tstate->use_tracing) {
1275 if (tstate->c_tracefunc != NULL) {
1276 /* tstate->c_tracefunc, if defined, is a
1277 function that will be called on *every* entry
1278 to a code block. Its return value, if not
1279 None, is a function that will be called at
1280 the start of each executed line of code.
1281 (Actually, the function must return itself
1282 in order to continue tracing.) The trace
1283 functions are called with three arguments:
1284 a pointer to the current frame, a string
1285 indicating why the function is called, and
1286 an argument which depends on the situation.
1287 The global trace function is also called
1288 whenever an exception is detected. */
1289 if (call_trace_protected(tstate->c_tracefunc,
1290 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001291 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* Trace function raised an error */
1293 goto exit_eval_frame;
1294 }
1295 }
1296 if (tstate->c_profilefunc != NULL) {
1297 /* Similar for c_profilefunc, except it needn't
1298 return itself and isn't called for "line" events */
1299 if (call_trace_protected(tstate->c_profilefunc,
1300 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001301 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* Profile function raised an error */
1303 goto exit_eval_frame;
1304 }
1305 }
1306 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001307
Łukasz Langaa785c872016-09-09 17:37:37 -07001308 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1309 dtrace_function_entry(f);
1310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 co = f->f_code;
1312 names = co->co_names;
1313 consts = co->co_consts;
1314 fastlocals = f->f_localsplus;
1315 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001316 assert(PyBytes_Check(co->co_code));
1317 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001318 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1319 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1320 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001321 /*
1322 f->f_lasti refers to the index of the last instruction,
1323 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001324
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001325 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001326 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 When the PREDICT() macros are enabled, some opcode pairs follow in
1329 direct succession without updating f->f_lasti. A successful
1330 prediction effectively links the two codes together as if they
1331 were a single new opcode; accordingly,f->f_lasti will point to
1332 the first code in the pair (for instance, GET_ITER followed by
1333 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001334 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001336 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001337 next_instr = first_instr;
1338 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001339 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1340 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001341 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001342 stack_pointer = f->f_valuestack + f->f_stackdepth;
1343 /* Set f->f_stackdepth to -1.
1344 * Update when returning or calling trace function.
1345 Having f_stackdepth <= 0 ensures that invalid
1346 values are not visible to the cycle GC.
1347 We choose -1 rather than 0 to assist debugging.
1348 */
1349 f->f_stackdepth = -1;
1350 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001351
Inada Naoki91234a12019-06-03 21:30:58 +09001352 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1353 co->co_opcache_flag++;
1354 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1355 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001356 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001357 }
1358#if OPCACHE_STATS
1359 opcache_code_objects_extra_mem +=
1360 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1361 sizeof(_PyOpcache) * co->co_opcache_size;
1362 opcache_code_objects++;
1363#endif
1364 }
1365 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001366
Tim Peters5ca576e2001-06-18 22:08:13 +00001367#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001368 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001369#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 if (throwflag) /* support for generator.throw() */
1372 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001373
Victor Stinnerace47d72013-07-18 01:41:08 +02001374#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001375 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001376 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001377 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001378 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001379#endif
1380
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001381main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1384 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001385 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* Do periodic things. Doing this every time through
1388 the loop would add too much overhead, so we do it
1389 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001390 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 event needs attention (e.g. a signal handler or
1392 async I/O handler); see Py_AddPendingCall() and
1393 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001394
Eric Snow7bda9de2019-03-08 17:25:54 -07001395 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001396 opcode = _Py_OPCODE(*next_instr);
1397 if (opcode == SETUP_FINALLY ||
1398 opcode == SETUP_WITH ||
1399 opcode == BEFORE_ASYNC_WITH ||
1400 opcode == YIELD_FROM) {
1401 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001402 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001403 - If we're about to enter the 'with:'. It will prevent
1404 emitting a resource warning in the common idiom
1405 'with open(path) as file:'.
1406 - If we're about to enter the 'async with:'.
1407 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001408 *very* useful, but might help in some cases and it's
1409 traditional)
1410 - If we're resuming a chain of nested 'yield from' or
1411 'await' calls, then each frame is parked with YIELD_FROM
1412 as its next opcode. If the user hit control-C we want to
1413 wait until we've reached the innermost frame before
1414 running the signal handler and raising KeyboardInterrupt
1415 (see bpo-30039).
1416 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 goto fast_next_opcode;
1418 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001419
Victor Stinnerda2914d2020-03-20 09:29:08 +01001420 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 }
1423 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 fast_next_opcode:
1426 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Łukasz Langaa785c872016-09-09 17:37:37 -07001428 if (PyDTrace_LINE_ENABLED())
1429 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001432
Victor Stinnerdab84232020-03-17 18:56:44 +01001433 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001434 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001436 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001438 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 err = maybe_call_line_trace(tstate->c_tracefunc,
1441 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001442 tstate, f,
1443 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 /* Reload possibly changed frame fields */
1445 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001446 stack_pointer = f->f_valuestack+f->f_stackdepth;
1447 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001454
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001455 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001456 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001457#ifdef DYNAMIC_EXECUTION_PROFILE
1458#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 dxpairs[lastopcode][opcode]++;
1460 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001461#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001463#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001464
Guido van Rossum96a42c81992-01-12 02:29:51 +00001465#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (lltrace) {
1469 if (HAS_ARG(opcode)) {
1470 printf("%d: %d, %d\n",
1471 f->f_lasti, opcode, oparg);
1472 }
1473 else {
1474 printf("%d: %d\n",
1475 f->f_lasti, opcode);
1476 }
1477 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001478#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001483 It is essential that any operation that fails must goto error
1484 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001489
Benjamin Petersonddd19492018-09-16 22:38:02 -07001490 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 PyObject *value = GETLOCAL(oparg);
1492 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001493 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 UNBOUNDLOCAL_ERROR_MSG,
1495 PyTuple_GetItem(co->co_varnames, oparg));
1496 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 Py_INCREF(value);
1499 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 }
1502
Benjamin Petersonddd19492018-09-16 22:38:02 -07001503 case TARGET(LOAD_CONST): {
1504 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 PyObject *value = GETITEM(consts, oparg);
1506 Py_INCREF(value);
1507 PUSH(value);
1508 FAST_DISPATCH();
1509 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001510
Benjamin Petersonddd19492018-09-16 22:38:02 -07001511 case TARGET(STORE_FAST): {
1512 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *value = POP();
1514 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *value = POP();
1520 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *top = TOP();
1526 PyObject *second = SECOND();
1527 SET_TOP(second);
1528 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Benjamin Petersonddd19492018-09-16 22:38:02 -07001532 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 PyObject *top = TOP();
1534 PyObject *second = SECOND();
1535 PyObject *third = THIRD();
1536 SET_TOP(second);
1537 SET_SECOND(third);
1538 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001543 PyObject *top = TOP();
1544 PyObject *second = SECOND();
1545 PyObject *third = THIRD();
1546 PyObject *fourth = FOURTH();
1547 SET_TOP(second);
1548 SET_SECOND(third);
1549 SET_THIRD(fourth);
1550 SET_FOURTH(top);
1551 FAST_DISPATCH();
1552 }
1553
Benjamin Petersonddd19492018-09-16 22:38:02 -07001554 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 PyObject *top = TOP();
1556 Py_INCREF(top);
1557 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001564 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001566 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 SET_TOP(top);
1568 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001569 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *value = TOP();
1574 PyObject *res = PyNumber_Positive(value);
1575 Py_DECREF(value);
1576 SET_TOP(res);
1577 if (res == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *value = TOP();
1584 PyObject *res = PyNumber_Negative(value);
1585 Py_DECREF(value);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *value = TOP();
1594 int err = PyObject_IsTrue(value);
1595 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 if (err == 0) {
1597 Py_INCREF(Py_True);
1598 SET_TOP(Py_True);
1599 DISPATCH();
1600 }
1601 else if (err > 0) {
1602 Py_INCREF(Py_False);
1603 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 DISPATCH();
1605 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001606 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 goto error;
1608 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Benjamin Petersonddd19492018-09-16 22:38:02 -07001610 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 PyObject *value = TOP();
1612 PyObject *res = PyNumber_Invert(value);
1613 Py_DECREF(value);
1614 SET_TOP(res);
1615 if (res == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *exp = POP();
1622 PyObject *base = TOP();
1623 PyObject *res = PyNumber_Power(base, exp, Py_None);
1624 Py_DECREF(base);
1625 Py_DECREF(exp);
1626 SET_TOP(res);
1627 if (res == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *right = POP();
1634 PyObject *left = TOP();
1635 PyObject *res = PyNumber_Multiply(left, right);
1636 Py_DECREF(left);
1637 Py_DECREF(right);
1638 SET_TOP(res);
1639 if (res == NULL)
1640 goto error;
1641 DISPATCH();
1642 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001645 PyObject *right = POP();
1646 PyObject *left = TOP();
1647 PyObject *res = PyNumber_MatrixMultiply(left, right);
1648 Py_DECREF(left);
1649 Py_DECREF(right);
1650 SET_TOP(res);
1651 if (res == NULL)
1652 goto error;
1653 DISPATCH();
1654 }
1655
Benjamin Petersonddd19492018-09-16 22:38:02 -07001656 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *divisor = POP();
1658 PyObject *dividend = TOP();
1659 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1660 Py_DECREF(dividend);
1661 Py_DECREF(divisor);
1662 SET_TOP(quotient);
1663 if (quotient == NULL)
1664 goto error;
1665 DISPATCH();
1666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Benjamin Petersonddd19492018-09-16 22:38:02 -07001668 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 PyObject *divisor = POP();
1670 PyObject *dividend = TOP();
1671 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1672 Py_DECREF(dividend);
1673 Py_DECREF(divisor);
1674 SET_TOP(quotient);
1675 if (quotient == NULL)
1676 goto error;
1677 DISPATCH();
1678 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001679
Benjamin Petersonddd19492018-09-16 22:38:02 -07001680 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 PyObject *divisor = POP();
1682 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001683 PyObject *res;
1684 if (PyUnicode_CheckExact(dividend) && (
1685 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1686 // fast path; string formatting, but not if the RHS is a str subclass
1687 // (see issue28598)
1688 res = PyUnicode_Format(dividend, divisor);
1689 } else {
1690 res = PyNumber_Remainder(dividend, divisor);
1691 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 Py_DECREF(divisor);
1693 Py_DECREF(dividend);
1694 SET_TOP(res);
1695 if (res == NULL)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 PyObject *right = POP();
1702 PyObject *left = TOP();
1703 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001704 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1705 CPython using bytecode, it is simply worthless.
1706 See http://bugs.python.org/issue21955 and
1707 http://bugs.python.org/issue10044 for the discussion. In short,
1708 no patch shown any impact on a realistic benchmark, only a minor
1709 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 if (PyUnicode_CheckExact(left) &&
1711 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001712 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001713 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001714 }
1715 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 sum = PyNumber_Add(left, right);
1717 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001718 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 Py_DECREF(right);
1720 SET_TOP(sum);
1721 if (sum == NULL)
1722 goto error;
1723 DISPATCH();
1724 }
1725
Benjamin Petersonddd19492018-09-16 22:38:02 -07001726 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 PyObject *right = POP();
1728 PyObject *left = TOP();
1729 PyObject *diff = PyNumber_Subtract(left, right);
1730 Py_DECREF(right);
1731 Py_DECREF(left);
1732 SET_TOP(diff);
1733 if (diff == NULL)
1734 goto error;
1735 DISPATCH();
1736 }
1737
Benjamin Petersonddd19492018-09-16 22:38:02 -07001738 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001739 PyObject *sub = POP();
1740 PyObject *container = TOP();
1741 PyObject *res = PyObject_GetItem(container, sub);
1742 Py_DECREF(container);
1743 Py_DECREF(sub);
1744 SET_TOP(res);
1745 if (res == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
1749
Benjamin Petersonddd19492018-09-16 22:38:02 -07001750 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 PyObject *right = POP();
1752 PyObject *left = TOP();
1753 PyObject *res = PyNumber_Lshift(left, right);
1754 Py_DECREF(left);
1755 Py_DECREF(right);
1756 SET_TOP(res);
1757 if (res == NULL)
1758 goto error;
1759 DISPATCH();
1760 }
1761
Benjamin Petersonddd19492018-09-16 22:38:02 -07001762 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 PyObject *right = POP();
1764 PyObject *left = TOP();
1765 PyObject *res = PyNumber_Rshift(left, right);
1766 Py_DECREF(left);
1767 Py_DECREF(right);
1768 SET_TOP(res);
1769 if (res == NULL)
1770 goto error;
1771 DISPATCH();
1772 }
1773
Benjamin Petersonddd19492018-09-16 22:38:02 -07001774 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 PyObject *right = POP();
1776 PyObject *left = TOP();
1777 PyObject *res = PyNumber_And(left, right);
1778 Py_DECREF(left);
1779 Py_DECREF(right);
1780 SET_TOP(res);
1781 if (res == NULL)
1782 goto error;
1783 DISPATCH();
1784 }
1785
Benjamin Petersonddd19492018-09-16 22:38:02 -07001786 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 PyObject *right = POP();
1788 PyObject *left = TOP();
1789 PyObject *res = PyNumber_Xor(left, right);
1790 Py_DECREF(left);
1791 Py_DECREF(right);
1792 SET_TOP(res);
1793 if (res == NULL)
1794 goto error;
1795 DISPATCH();
1796 }
1797
Benjamin Petersonddd19492018-09-16 22:38:02 -07001798 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001799 PyObject *right = POP();
1800 PyObject *left = TOP();
1801 PyObject *res = PyNumber_Or(left, right);
1802 Py_DECREF(left);
1803 Py_DECREF(right);
1804 SET_TOP(res);
1805 if (res == NULL)
1806 goto error;
1807 DISPATCH();
1808 }
1809
Benjamin Petersonddd19492018-09-16 22:38:02 -07001810 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 PyObject *v = POP();
1812 PyObject *list = PEEK(oparg);
1813 int err;
1814 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 if (err != 0)
1817 goto error;
1818 PREDICT(JUMP_ABSOLUTE);
1819 DISPATCH();
1820 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001821
Benjamin Petersonddd19492018-09-16 22:38:02 -07001822 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001824 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 int err;
1826 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 if (err != 0)
1829 goto error;
1830 PREDICT(JUMP_ABSOLUTE);
1831 DISPATCH();
1832 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Benjamin Petersonddd19492018-09-16 22:38:02 -07001834 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 PyObject *exp = POP();
1836 PyObject *base = TOP();
1837 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1838 Py_DECREF(base);
1839 Py_DECREF(exp);
1840 SET_TOP(res);
1841 if (res == NULL)
1842 goto error;
1843 DISPATCH();
1844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Benjamin Petersonddd19492018-09-16 22:38:02 -07001846 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 PyObject *right = POP();
1848 PyObject *left = TOP();
1849 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1850 Py_DECREF(left);
1851 Py_DECREF(right);
1852 SET_TOP(res);
1853 if (res == NULL)
1854 goto error;
1855 DISPATCH();
1856 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Benjamin Petersonddd19492018-09-16 22:38:02 -07001858 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001859 PyObject *right = POP();
1860 PyObject *left = TOP();
1861 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1862 Py_DECREF(left);
1863 Py_DECREF(right);
1864 SET_TOP(res);
1865 if (res == NULL)
1866 goto error;
1867 DISPATCH();
1868 }
1869
Benjamin Petersonddd19492018-09-16 22:38:02 -07001870 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 PyObject *divisor = POP();
1872 PyObject *dividend = TOP();
1873 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1874 Py_DECREF(dividend);
1875 Py_DECREF(divisor);
1876 SET_TOP(quotient);
1877 if (quotient == NULL)
1878 goto error;
1879 DISPATCH();
1880 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Benjamin Petersonddd19492018-09-16 22:38:02 -07001882 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 PyObject *divisor = POP();
1884 PyObject *dividend = TOP();
1885 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1886 Py_DECREF(dividend);
1887 Py_DECREF(divisor);
1888 SET_TOP(quotient);
1889 if (quotient == NULL)
1890 goto error;
1891 DISPATCH();
1892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Benjamin Petersonddd19492018-09-16 22:38:02 -07001894 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 PyObject *right = POP();
1896 PyObject *left = TOP();
1897 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1898 Py_DECREF(left);
1899 Py_DECREF(right);
1900 SET_TOP(mod);
1901 if (mod == NULL)
1902 goto error;
1903 DISPATCH();
1904 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Benjamin Petersonddd19492018-09-16 22:38:02 -07001906 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 PyObject *right = POP();
1908 PyObject *left = TOP();
1909 PyObject *sum;
1910 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001911 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001912 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001913 }
1914 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 sum = PyNumber_InPlaceAdd(left, right);
1916 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001917 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 Py_DECREF(right);
1919 SET_TOP(sum);
1920 if (sum == NULL)
1921 goto error;
1922 DISPATCH();
1923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Petersonddd19492018-09-16 22:38:02 -07001925 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 PyObject *right = POP();
1927 PyObject *left = TOP();
1928 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1929 Py_DECREF(left);
1930 Py_DECREF(right);
1931 SET_TOP(diff);
1932 if (diff == NULL)
1933 goto error;
1934 DISPATCH();
1935 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001936
Benjamin Petersonddd19492018-09-16 22:38:02 -07001937 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 PyObject *right = POP();
1939 PyObject *left = TOP();
1940 PyObject *res = PyNumber_InPlaceLshift(left, right);
1941 Py_DECREF(left);
1942 Py_DECREF(right);
1943 SET_TOP(res);
1944 if (res == NULL)
1945 goto error;
1946 DISPATCH();
1947 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Benjamin Petersonddd19492018-09-16 22:38:02 -07001949 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 PyObject *right = POP();
1951 PyObject *left = TOP();
1952 PyObject *res = PyNumber_InPlaceRshift(left, right);
1953 Py_DECREF(left);
1954 Py_DECREF(right);
1955 SET_TOP(res);
1956 if (res == NULL)
1957 goto error;
1958 DISPATCH();
1959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 PyObject *right = POP();
1963 PyObject *left = TOP();
1964 PyObject *res = PyNumber_InPlaceAnd(left, right);
1965 Py_DECREF(left);
1966 Py_DECREF(right);
1967 SET_TOP(res);
1968 if (res == NULL)
1969 goto error;
1970 DISPATCH();
1971 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Benjamin Petersonddd19492018-09-16 22:38:02 -07001973 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 PyObject *right = POP();
1975 PyObject *left = TOP();
1976 PyObject *res = PyNumber_InPlaceXor(left, right);
1977 Py_DECREF(left);
1978 Py_DECREF(right);
1979 SET_TOP(res);
1980 if (res == NULL)
1981 goto error;
1982 DISPATCH();
1983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 PyObject *right = POP();
1987 PyObject *left = TOP();
1988 PyObject *res = PyNumber_InPlaceOr(left, right);
1989 Py_DECREF(left);
1990 Py_DECREF(right);
1991 SET_TOP(res);
1992 if (res == NULL)
1993 goto error;
1994 DISPATCH();
1995 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001996
Benjamin Petersonddd19492018-09-16 22:38:02 -07001997 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 PyObject *sub = TOP();
1999 PyObject *container = SECOND();
2000 PyObject *v = THIRD();
2001 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002002 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002003 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 Py_DECREF(container);
2007 Py_DECREF(sub);
2008 if (err != 0)
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(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 PyObject *sub = TOP();
2015 PyObject *container = SECOND();
2016 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002017 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002018 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 err = PyObject_DelItem(container, sub);
2020 Py_DECREF(container);
2021 Py_DECREF(sub);
2022 if (err != 0)
2023 goto error;
2024 DISPATCH();
2025 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002026
Benjamin Petersonddd19492018-09-16 22:38:02 -07002027 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002028 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002030 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002031 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002033 _PyErr_SetString(tstate, PyExc_RuntimeError,
2034 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 Py_DECREF(value);
2036 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002038 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 Py_DECREF(value);
2040 if (res == NULL)
2041 goto error;
2042 Py_DECREF(res);
2043 DISPATCH();
2044 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 switch (oparg) {
2049 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002051 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002054 /* fall through */
2055 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002056 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002057 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 break;
2060 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002061 _PyErr_SetString(tstate, PyExc_SystemError,
2062 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 break;
2064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 goto error;
2066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Benjamin Petersonddd19492018-09-16 22:38:02 -07002068 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002070 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002071 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002072 f->f_state = FRAME_RETURNED;
2073 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002074 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002076
Benjamin Petersonddd19492018-09-16 22:38:02 -07002077 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002078 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002079 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002080 PyObject *obj = TOP();
2081 PyTypeObject *type = Py_TYPE(obj);
2082
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002083 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002084 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002085 }
Yury Selivanov75445082015-05-11 22:57:16 -04002086
2087 if (getter != NULL) {
2088 iter = (*getter)(obj);
2089 Py_DECREF(obj);
2090 if (iter == NULL) {
2091 SET_TOP(NULL);
2092 goto error;
2093 }
2094 }
2095 else {
2096 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002097 _PyErr_Format(tstate, PyExc_TypeError,
2098 "'async for' requires an object with "
2099 "__aiter__ method, got %.100s",
2100 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002101 Py_DECREF(obj);
2102 goto error;
2103 }
2104
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002105 if (Py_TYPE(iter)->tp_as_async == NULL ||
2106 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002107
Yury Selivanov398ff912017-03-02 22:20:00 -05002108 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002109 _PyErr_Format(tstate, PyExc_TypeError,
2110 "'async for' received an object from __aiter__ "
2111 "that does not implement __anext__: %.100s",
2112 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002113 Py_DECREF(iter);
2114 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002115 }
2116
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002117 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002118 DISPATCH();
2119 }
2120
Benjamin Petersonddd19492018-09-16 22:38:02 -07002121 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002122 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002123 PyObject *next_iter = NULL;
2124 PyObject *awaitable = NULL;
2125 PyObject *aiter = TOP();
2126 PyTypeObject *type = Py_TYPE(aiter);
2127
Yury Selivanoveb636452016-09-08 22:01:51 -07002128 if (PyAsyncGen_CheckExact(aiter)) {
2129 awaitable = type->tp_as_async->am_anext(aiter);
2130 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002131 goto error;
2132 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002133 } else {
2134 if (type->tp_as_async != NULL){
2135 getter = type->tp_as_async->am_anext;
2136 }
Yury Selivanov75445082015-05-11 22:57:16 -04002137
Yury Selivanoveb636452016-09-08 22:01:51 -07002138 if (getter != NULL) {
2139 next_iter = (*getter)(aiter);
2140 if (next_iter == NULL) {
2141 goto error;
2142 }
2143 }
2144 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002145 _PyErr_Format(tstate, PyExc_TypeError,
2146 "'async for' requires an iterator with "
2147 "__anext__ method, got %.100s",
2148 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002149 goto error;
2150 }
Yury Selivanov75445082015-05-11 22:57:16 -04002151
Yury Selivanoveb636452016-09-08 22:01:51 -07002152 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2153 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002154 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002155 PyExc_TypeError,
2156 "'async for' received an invalid object "
2157 "from __anext__: %.100s",
2158 Py_TYPE(next_iter)->tp_name);
2159
2160 Py_DECREF(next_iter);
2161 goto error;
2162 } else {
2163 Py_DECREF(next_iter);
2164 }
2165 }
Yury Selivanov75445082015-05-11 22:57:16 -04002166
2167 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002168 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002169 DISPATCH();
2170 }
2171
Benjamin Petersonddd19492018-09-16 22:38:02 -07002172 case TARGET(GET_AWAITABLE): {
2173 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002174 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002175 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002176
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002177 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002178 int opcode_at_minus_3 = 0;
2179 if ((next_instr - first_instr) > 2) {
2180 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2181 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002182 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002183 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002184 _Py_OPCODE(next_instr[-2]));
2185 }
2186
Yury Selivanov75445082015-05-11 22:57:16 -04002187 Py_DECREF(iterable);
2188
Yury Selivanovc724bae2016-03-02 11:30:46 -05002189 if (iter != NULL && PyCoro_CheckExact(iter)) {
2190 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2191 if (yf != NULL) {
2192 /* `iter` is a coroutine object that is being
2193 awaited, `yf` is a pointer to the current awaitable
2194 being awaited on. */
2195 Py_DECREF(yf);
2196 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002197 _PyErr_SetString(tstate, PyExc_RuntimeError,
2198 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002199 /* The code below jumps to `error` if `iter` is NULL. */
2200 }
2201 }
2202
Yury Selivanov75445082015-05-11 22:57:16 -04002203 SET_TOP(iter); /* Even if it's NULL */
2204
2205 if (iter == NULL) {
2206 goto error;
2207 }
2208
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002209 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002210 DISPATCH();
2211 }
2212
Benjamin Petersonddd19492018-09-16 22:38:02 -07002213 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002215 PyObject *receiver = TOP();
Vladimir Matveev2b053612020-09-18 18:38:38 -07002216 int is_gen_or_coro = PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver);
2217 int gen_status;
2218 if (tstate->c_tracefunc == NULL && is_gen_or_coro) {
2219 gen_status = PyGen_Send((PyGenObject *)receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002220 } else {
Vladimir Matveev2b053612020-09-18 18:38:38 -07002221 if (is_gen_or_coro) {
2222 retval = _PyGen_Send((PyGenObject *)receiver, v);
2223 }
2224 else {
2225 _Py_IDENTIFIER(send);
2226 if (v == Py_None) {
2227 retval = Py_TYPE(receiver)->tp_iternext(receiver);
2228 }
2229 else {
2230 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
2231 }
2232 }
2233
2234 if (retval == NULL) {
2235 if (tstate->c_tracefunc != NULL
2236 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2237 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2238 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2239 gen_status = PYGEN_RETURN;
2240 }
2241 else {
2242 gen_status = PYGEN_ERROR;
2243 }
2244 }
2245 else {
2246 gen_status = PYGEN_NEXT;
2247 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002250 if (gen_status == PYGEN_ERROR) {
2251 assert (retval == NULL);
2252 goto error;
2253 }
2254 if (gen_status == PYGEN_RETURN) {
2255 assert (retval != NULL);
2256
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002257 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002258 SET_TOP(retval);
2259 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002261 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002262 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002263 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002264 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002265 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002266 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002267 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002268 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002269 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002271
Benjamin Petersonddd19492018-09-16 22:38:02 -07002272 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002274
2275 if (co->co_flags & CO_ASYNC_GENERATOR) {
2276 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2277 Py_DECREF(retval);
2278 if (w == NULL) {
2279 retval = NULL;
2280 goto error;
2281 }
2282 retval = w;
2283 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002284 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002285 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002286 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002288
Benjamin Petersonddd19492018-09-16 22:38:02 -07002289 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002290 PyObject *type, *value, *traceback;
2291 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 PyTryBlock *b = PyFrame_BlockPop(f);
2293 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002294 _PyErr_SetString(tstate, PyExc_SystemError,
2295 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002298 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2299 STACK_LEVEL() <= (b)->b_level + 4);
2300 exc_info = tstate->exc_info;
2301 type = exc_info->exc_type;
2302 value = exc_info->exc_value;
2303 traceback = exc_info->exc_traceback;
2304 exc_info->exc_type = POP();
2305 exc_info->exc_value = POP();
2306 exc_info->exc_traceback = POP();
2307 Py_XDECREF(type);
2308 Py_XDECREF(value);
2309 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002312
Benjamin Petersonddd19492018-09-16 22:38:02 -07002313 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002314 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002317
Mark Shannonfee55262019-11-21 09:11:43 +00002318 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002319 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002320 PyObject *val = POP();
2321 PyObject *tb = POP();
2322 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002323 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002324 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002328 PyObject *exc = POP();
2329 assert(PyExceptionClass_Check(exc));
2330 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2331 PyTryBlock *b = PyFrame_BlockPop(f);
2332 assert(b->b_type == EXCEPT_HANDLER);
2333 Py_DECREF(exc);
2334 UNWIND_EXCEPT_HANDLER(b);
2335 Py_DECREF(POP());
2336 JUMPBY(oparg);
2337 FAST_DISPATCH();
2338 }
2339 else {
2340 PyObject *val = POP();
2341 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002343 goto exception_unwind;
2344 }
2345 }
2346
Zackery Spytzce6a0702019-08-25 03:44:09 -06002347 case TARGET(LOAD_ASSERTION_ERROR): {
2348 PyObject *value = PyExc_AssertionError;
2349 Py_INCREF(value);
2350 PUSH(value);
2351 FAST_DISPATCH();
2352 }
2353
Benjamin Petersonddd19492018-09-16 22:38:02 -07002354 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002355 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002356
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002358 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002359 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002361 if (!_PyErr_Occurred(tstate)) {
2362 _PyErr_SetString(tstate, PyExc_NameError,
2363 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002366 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002368 }
2369 else {
2370 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2371 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002372 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2374 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2376 _PyErr_SetString(tstate, PyExc_NameError,
2377 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002382 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002384
Benjamin Petersonddd19492018-09-16 22:38:02 -07002385 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 PyObject *name = GETITEM(names, oparg);
2387 PyObject *v = POP();
2388 PyObject *ns = f->f_locals;
2389 int err;
2390 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002391 _PyErr_Format(tstate, PyExc_SystemError,
2392 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 if (PyDict_CheckExact(ns))
2397 err = PyDict_SetItem(ns, name, v);
2398 else
2399 err = PyObject_SetItem(ns, name, v);
2400 Py_DECREF(v);
2401 if (err != 0)
2402 goto error;
2403 DISPATCH();
2404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 PyObject *name = GETITEM(names, oparg);
2408 PyObject *ns = f->f_locals;
2409 int err;
2410 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002411 _PyErr_Format(tstate, PyExc_SystemError,
2412 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 err = PyObject_DelItem(ns, name);
2416 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002417 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 NAME_ERROR_MSG,
2419 name);
2420 goto error;
2421 }
2422 DISPATCH();
2423 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002424
Benjamin Petersonddd19492018-09-16 22:38:02 -07002425 case TARGET(UNPACK_SEQUENCE): {
2426 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *seq = POP(), *item, **items;
2428 if (PyTuple_CheckExact(seq) &&
2429 PyTuple_GET_SIZE(seq) == oparg) {
2430 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 item = items[oparg];
2433 Py_INCREF(item);
2434 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 } else if (PyList_CheckExact(seq) &&
2437 PyList_GET_SIZE(seq) == oparg) {
2438 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 item = items[oparg];
2441 Py_INCREF(item);
2442 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002444 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002446 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 } else {
2448 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 Py_DECREF(seq);
2450 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002453 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002455
Benjamin Petersonddd19492018-09-16 22:38:02 -07002456 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2458 PyObject *seq = POP();
2459
Victor Stinner438a12d2019-05-24 17:01:38 +02002460 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 stack_pointer + totalargs)) {
2462 stack_pointer += totalargs;
2463 } else {
2464 Py_DECREF(seq);
2465 goto error;
2466 }
2467 Py_DECREF(seq);
2468 DISPATCH();
2469 }
2470
Benjamin Petersonddd19492018-09-16 22:38:02 -07002471 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 PyObject *name = GETITEM(names, oparg);
2473 PyObject *owner = TOP();
2474 PyObject *v = SECOND();
2475 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002476 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 Py_DECREF(owner);
2480 if (err != 0)
2481 goto error;
2482 DISPATCH();
2483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002484
Benjamin Petersonddd19492018-09-16 22:38:02 -07002485 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 PyObject *name = GETITEM(names, oparg);
2487 PyObject *owner = POP();
2488 int err;
2489 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2490 Py_DECREF(owner);
2491 if (err != 0)
2492 goto error;
2493 DISPATCH();
2494 }
2495
Benjamin Petersonddd19492018-09-16 22:38:02 -07002496 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 PyObject *name = GETITEM(names, oparg);
2498 PyObject *v = POP();
2499 int err;
2500 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 if (err != 0)
2503 goto error;
2504 DISPATCH();
2505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002506
Benjamin Petersonddd19492018-09-16 22:38:02 -07002507 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 PyObject *name = GETITEM(names, oparg);
2509 int err;
2510 err = PyDict_DelItem(f->f_globals, name);
2511 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002512 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2513 format_exc_check_arg(tstate, PyExc_NameError,
2514 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002515 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002517 }
2518 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002520
Benjamin Petersonddd19492018-09-16 22:38:02 -07002521 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002522 PyObject *name = GETITEM(names, oparg);
2523 PyObject *locals = f->f_locals;
2524 PyObject *v;
2525 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002526 _PyErr_Format(tstate, PyExc_SystemError,
2527 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002531 v = PyDict_GetItemWithError(locals, name);
2532 if (v != NULL) {
2533 Py_INCREF(v);
2534 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002535 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002536 goto error;
2537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 }
2539 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002541 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002543 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 }
2546 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002548 v = PyDict_GetItemWithError(f->f_globals, name);
2549 if (v != NULL) {
2550 Py_INCREF(v);
2551 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002552 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002553 goto error;
2554 }
2555 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002556 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002557 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002559 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002560 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002561 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002567 }
2568 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 v = PyObject_GetItem(f->f_builtins, name);
2570 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002571 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002572 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002573 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002575 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002577 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002584
Benjamin Petersonddd19492018-09-16 22:38:02 -07002585 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002586 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002588 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002589 && PyDict_CheckExact(f->f_builtins))
2590 {
Inada Naoki91234a12019-06-03 21:30:58 +09002591 OPCACHE_CHECK();
2592 if (co_opcache != NULL && co_opcache->optimized > 0) {
2593 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2594
2595 if (lg->globals_ver ==
2596 ((PyDictObject *)f->f_globals)->ma_version_tag
2597 && lg->builtins_ver ==
2598 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2599 {
2600 PyObject *ptr = lg->ptr;
2601 OPCACHE_STAT_GLOBAL_HIT();
2602 assert(ptr != NULL);
2603 Py_INCREF(ptr);
2604 PUSH(ptr);
2605 DISPATCH();
2606 }
2607 }
2608
2609 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002611 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 name);
2613 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002614 if (!_PyErr_OCCURRED()) {
2615 /* _PyDict_LoadGlobal() returns NULL without raising
2616 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002617 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002618 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002619 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 }
Inada Naoki91234a12019-06-03 21:30:58 +09002622
2623 if (co_opcache != NULL) {
2624 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2625
2626 if (co_opcache->optimized == 0) {
2627 /* Wasn't optimized before. */
2628 OPCACHE_STAT_GLOBAL_OPT();
2629 } else {
2630 OPCACHE_STAT_GLOBAL_MISS();
2631 }
2632
2633 co_opcache->optimized = 1;
2634 lg->globals_ver =
2635 ((PyDictObject *)f->f_globals)->ma_version_tag;
2636 lg->builtins_ver =
2637 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2638 lg->ptr = v; /* borrowed */
2639 }
2640
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002643 else {
2644 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002645
2646 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002647 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 v = PyObject_GetItem(f->f_globals, name);
2649 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002650 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002651 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002652 }
2653 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002654
Victor Stinnerb4efc962015-11-20 09:24:02 +01002655 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 v = PyObject_GetItem(f->f_builtins, name);
2657 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002658 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002659 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002660 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002661 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002662 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002664 }
2665 }
2666 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002670
Benjamin Petersonddd19492018-09-16 22:38:02 -07002671 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 PyObject *v = GETLOCAL(oparg);
2673 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 SETLOCAL(oparg, NULL);
2675 DISPATCH();
2676 }
2677 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002678 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 UNBOUNDLOCAL_ERROR_MSG,
2680 PyTuple_GetItem(co->co_varnames, oparg)
2681 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 goto error;
2683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002684
Benjamin Petersonddd19492018-09-16 22:38:02 -07002685 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002687 PyObject *oldobj = PyCell_GET(cell);
2688 if (oldobj != NULL) {
2689 PyCell_SET(cell, NULL);
2690 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002691 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002692 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002693 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 goto error;
2695 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002696
Benjamin Petersonddd19492018-09-16 22:38:02 -07002697 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 PyObject *cell = freevars[oparg];
2699 Py_INCREF(cell);
2700 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002703
Benjamin Petersonddd19492018-09-16 22:38:02 -07002704 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002705 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002706 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002707 assert(locals);
2708 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2709 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2710 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2711 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2712 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002713 value = PyDict_GetItemWithError(locals, name);
2714 if (value != NULL) {
2715 Py_INCREF(value);
2716 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002717 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002718 goto error;
2719 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002720 }
2721 else {
2722 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002723 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002724 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002725 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002726 }
2727 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002728 }
2729 }
2730 if (!value) {
2731 PyObject *cell = freevars[oparg];
2732 value = PyCell_GET(cell);
2733 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002734 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002735 goto error;
2736 }
2737 Py_INCREF(value);
2738 }
2739 PUSH(value);
2740 DISPATCH();
2741 }
2742
Benjamin Petersonddd19492018-09-16 22:38:02 -07002743 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 PyObject *cell = freevars[oparg];
2745 PyObject *value = PyCell_GET(cell);
2746 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002747 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 Py_INCREF(value);
2751 PUSH(value);
2752 DISPATCH();
2753 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002754
Benjamin Petersonddd19492018-09-16 22:38:02 -07002755 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 PyObject *v = POP();
2757 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002758 PyObject *oldobj = PyCell_GET(cell);
2759 PyCell_SET(cell, v);
2760 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 DISPATCH();
2762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002763
Benjamin Petersonddd19492018-09-16 22:38:02 -07002764 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002765 PyObject *str;
2766 PyObject *empty = PyUnicode_New(0, 0);
2767 if (empty == NULL) {
2768 goto error;
2769 }
2770 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2771 Py_DECREF(empty);
2772 if (str == NULL)
2773 goto error;
2774 while (--oparg >= 0) {
2775 PyObject *item = POP();
2776 Py_DECREF(item);
2777 }
2778 PUSH(str);
2779 DISPATCH();
2780 }
2781
Benjamin Petersonddd19492018-09-16 22:38:02 -07002782 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 PyObject *tup = PyTuple_New(oparg);
2784 if (tup == NULL)
2785 goto error;
2786 while (--oparg >= 0) {
2787 PyObject *item = POP();
2788 PyTuple_SET_ITEM(tup, oparg, item);
2789 }
2790 PUSH(tup);
2791 DISPATCH();
2792 }
2793
Benjamin Petersonddd19492018-09-16 22:38:02 -07002794 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 PyObject *list = PyList_New(oparg);
2796 if (list == NULL)
2797 goto error;
2798 while (--oparg >= 0) {
2799 PyObject *item = POP();
2800 PyList_SET_ITEM(list, oparg, item);
2801 }
2802 PUSH(list);
2803 DISPATCH();
2804 }
2805
Mark Shannon13bc1392020-01-23 09:25:17 +00002806 case TARGET(LIST_TO_TUPLE): {
2807 PyObject *list = POP();
2808 PyObject *tuple = PyList_AsTuple(list);
2809 Py_DECREF(list);
2810 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002812 }
2813 PUSH(tuple);
2814 DISPATCH();
2815 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816
Mark Shannon13bc1392020-01-23 09:25:17 +00002817 case TARGET(LIST_EXTEND): {
2818 PyObject *iterable = POP();
2819 PyObject *list = PEEK(oparg);
2820 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2821 if (none_val == NULL) {
2822 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002823 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002824 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002825 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002826 _PyErr_Format(tstate, PyExc_TypeError,
2827 "Value after * must be an iterable, not %.200s",
2828 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002829 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002830 Py_DECREF(iterable);
2831 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002832 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002833 Py_DECREF(none_val);
2834 Py_DECREF(iterable);
2835 DISPATCH();
2836 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837
Mark Shannon13bc1392020-01-23 09:25:17 +00002838 case TARGET(SET_UPDATE): {
2839 PyObject *iterable = POP();
2840 PyObject *set = PEEK(oparg);
2841 int err = _PySet_Update(set, iterable);
2842 Py_DECREF(iterable);
2843 if (err < 0) {
2844 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002845 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002846 DISPATCH();
2847 }
2848
Benjamin Petersonddd19492018-09-16 22:38:02 -07002849 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 PyObject *set = PySet_New(NULL);
2851 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002852 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 if (set == NULL)
2854 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002855 for (i = oparg; i > 0; i--) {
2856 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 if (err == 0)
2858 err = PySet_Add(set, item);
2859 Py_DECREF(item);
2860 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002861 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 if (err != 0) {
2863 Py_DECREF(set);
2864 goto error;
2865 }
2866 PUSH(set);
2867 DISPATCH();
2868 }
2869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002871 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2873 if (map == NULL)
2874 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002875 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002876 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002877 PyObject *key = PEEK(2*i);
2878 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002879 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002880 if (err != 0) {
2881 Py_DECREF(map);
2882 goto error;
2883 }
2884 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002885
2886 while (oparg--) {
2887 Py_DECREF(POP());
2888 Py_DECREF(POP());
2889 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 PUSH(map);
2891 DISPATCH();
2892 }
2893
Benjamin Petersonddd19492018-09-16 22:38:02 -07002894 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002895 _Py_IDENTIFIER(__annotations__);
2896 int err;
2897 PyObject *ann_dict;
2898 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002899 _PyErr_Format(tstate, PyExc_SystemError,
2900 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002901 goto error;
2902 }
2903 /* check if __annotations__ in locals()... */
2904 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002905 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002906 &PyId___annotations__);
2907 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002908 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002909 goto error;
2910 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002911 /* ...if not, create a new one */
2912 ann_dict = PyDict_New();
2913 if (ann_dict == NULL) {
2914 goto error;
2915 }
2916 err = _PyDict_SetItemId(f->f_locals,
2917 &PyId___annotations__, ann_dict);
2918 Py_DECREF(ann_dict);
2919 if (err != 0) {
2920 goto error;
2921 }
2922 }
2923 }
2924 else {
2925 /* do the same if locals() is not a dict */
2926 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2927 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002928 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002929 }
2930 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2931 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002932 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002933 goto error;
2934 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002935 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002936 ann_dict = PyDict_New();
2937 if (ann_dict == NULL) {
2938 goto error;
2939 }
2940 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2941 Py_DECREF(ann_dict);
2942 if (err != 0) {
2943 goto error;
2944 }
2945 }
2946 else {
2947 Py_DECREF(ann_dict);
2948 }
2949 }
2950 DISPATCH();
2951 }
2952
Benjamin Petersonddd19492018-09-16 22:38:02 -07002953 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002954 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002955 PyObject *map;
2956 PyObject *keys = TOP();
2957 if (!PyTuple_CheckExact(keys) ||
2958 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002959 _PyErr_SetString(tstate, PyExc_SystemError,
2960 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002961 goto error;
2962 }
2963 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2964 if (map == NULL) {
2965 goto error;
2966 }
2967 for (i = oparg; i > 0; i--) {
2968 int err;
2969 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2970 PyObject *value = PEEK(i + 1);
2971 err = PyDict_SetItem(map, key, value);
2972 if (err != 0) {
2973 Py_DECREF(map);
2974 goto error;
2975 }
2976 }
2977
2978 Py_DECREF(POP());
2979 while (oparg--) {
2980 Py_DECREF(POP());
2981 }
2982 PUSH(map);
2983 DISPATCH();
2984 }
2985
Mark Shannon8a4cd702020-01-27 09:57:45 +00002986 case TARGET(DICT_UPDATE): {
2987 PyObject *update = POP();
2988 PyObject *dict = PEEK(oparg);
2989 if (PyDict_Update(dict, update) < 0) {
2990 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2991 _PyErr_Format(tstate, PyExc_TypeError,
2992 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002993 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002994 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002995 Py_DECREF(update);
2996 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002998 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002999 DISPATCH();
3000 }
3001
Mark Shannon8a4cd702020-01-27 09:57:45 +00003002 case TARGET(DICT_MERGE): {
3003 PyObject *update = POP();
3004 PyObject *dict = PEEK(oparg);
3005
3006 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3007 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3008 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003009 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003010 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003011 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003012 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003013 DISPATCH();
3014 }
3015
Benjamin Petersonddd19492018-09-16 22:38:02 -07003016 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003017 PyObject *value = TOP();
3018 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 PyObject *map;
3020 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003021 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003022 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003024 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 Py_DECREF(value);
3026 Py_DECREF(key);
3027 if (err != 0)
3028 goto error;
3029 PREDICT(JUMP_ABSOLUTE);
3030 DISPATCH();
3031 }
3032
Benjamin Petersonddd19492018-09-16 22:38:02 -07003033 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 PyObject *name = GETITEM(names, oparg);
3035 PyObject *owner = TOP();
3036 PyObject *res = PyObject_GetAttr(owner, name);
3037 Py_DECREF(owner);
3038 SET_TOP(res);
3039 if (res == NULL)
3040 goto error;
3041 DISPATCH();
3042 }
3043
Benjamin Petersonddd19492018-09-16 22:38:02 -07003044 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003045 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PyObject *right = POP();
3047 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003048 PyObject *res = PyObject_RichCompare(left, right, oparg);
3049 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 Py_DECREF(left);
3051 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 if (res == NULL)
3053 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 PREDICT(POP_JUMP_IF_FALSE);
3055 PREDICT(POP_JUMP_IF_TRUE);
3056 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003057 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003058
Mark Shannon9af0e472020-01-14 10:12:45 +00003059 case TARGET(IS_OP): {
3060 PyObject *right = POP();
3061 PyObject *left = TOP();
3062 int res = (left == right)^oparg;
3063 PyObject *b = res ? Py_True : Py_False;
3064 Py_INCREF(b);
3065 SET_TOP(b);
3066 Py_DECREF(left);
3067 Py_DECREF(right);
3068 PREDICT(POP_JUMP_IF_FALSE);
3069 PREDICT(POP_JUMP_IF_TRUE);
3070 FAST_DISPATCH();
3071 }
3072
3073 case TARGET(CONTAINS_OP): {
3074 PyObject *right = POP();
3075 PyObject *left = POP();
3076 int res = PySequence_Contains(right, left);
3077 Py_DECREF(left);
3078 Py_DECREF(right);
3079 if (res < 0) {
3080 goto error;
3081 }
3082 PyObject *b = (res^oparg) ? Py_True : Py_False;
3083 Py_INCREF(b);
3084 PUSH(b);
3085 PREDICT(POP_JUMP_IF_FALSE);
3086 PREDICT(POP_JUMP_IF_TRUE);
3087 FAST_DISPATCH();
3088 }
3089
3090#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3091 "BaseException is not allowed"
3092
3093 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3094 PyObject *right = POP();
3095 PyObject *left = POP();
3096 if (PyTuple_Check(right)) {
3097 Py_ssize_t i, length;
3098 length = PyTuple_GET_SIZE(right);
3099 for (i = 0; i < length; i++) {
3100 PyObject *exc = PyTuple_GET_ITEM(right, i);
3101 if (!PyExceptionClass_Check(exc)) {
3102 _PyErr_SetString(tstate, PyExc_TypeError,
3103 CANNOT_CATCH_MSG);
3104 Py_DECREF(left);
3105 Py_DECREF(right);
3106 goto error;
3107 }
3108 }
3109 }
3110 else {
3111 if (!PyExceptionClass_Check(right)) {
3112 _PyErr_SetString(tstate, PyExc_TypeError,
3113 CANNOT_CATCH_MSG);
3114 Py_DECREF(left);
3115 Py_DECREF(right);
3116 goto error;
3117 }
3118 }
3119 int res = PyErr_GivenExceptionMatches(left, right);
3120 Py_DECREF(left);
3121 Py_DECREF(right);
3122 if (res > 0) {
3123 /* Exception matches -- Do nothing */;
3124 }
3125 else if (res == 0) {
3126 JUMPTO(oparg);
3127 }
3128 else {
3129 goto error;
3130 }
3131 DISPATCH();
3132 }
3133
Benjamin Petersonddd19492018-09-16 22:38:02 -07003134 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003136 PyObject *fromlist = POP();
3137 PyObject *level = TOP();
3138 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003139 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003140 Py_DECREF(level);
3141 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 SET_TOP(res);
3143 if (res == NULL)
3144 goto error;
3145 DISPATCH();
3146 }
3147
Benjamin Petersonddd19492018-09-16 22:38:02 -07003148 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 PyObject *from = POP(), *locals;
3150 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003151 if (PyFrame_FastToLocalsWithError(f) < 0) {
3152 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003153 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003154 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003155
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 locals = f->f_locals;
3157 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003158 _PyErr_SetString(tstate, PyExc_SystemError,
3159 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003160 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003163 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 Py_DECREF(from);
3166 if (err != 0)
3167 goto error;
3168 DISPATCH();
3169 }
Guido van Rossum25831651993-05-19 14:50:45 +00003170
Benjamin Petersonddd19492018-09-16 22:38:02 -07003171 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 PyObject *name = GETITEM(names, oparg);
3173 PyObject *from = TOP();
3174 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003175 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 PUSH(res);
3177 if (res == NULL)
3178 goto error;
3179 DISPATCH();
3180 }
Thomas Wouters52152252000-08-17 22:55:00 +00003181
Benjamin Petersonddd19492018-09-16 22:38:02 -07003182 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 JUMPBY(oparg);
3184 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003186
Benjamin Petersonddd19492018-09-16 22:38:02 -07003187 case TARGET(POP_JUMP_IF_FALSE): {
3188 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 PyObject *cond = POP();
3190 int err;
3191 if (cond == Py_True) {
3192 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 FAST_DISPATCH();
3194 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 if (cond == Py_False) {
3196 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 JUMPTO(oparg);
3198 FAST_DISPATCH();
3199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 err = PyObject_IsTrue(cond);
3201 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003203 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 else if (err == 0)
3205 JUMPTO(oparg);
3206 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003210
Benjamin Petersonddd19492018-09-16 22:38:02 -07003211 case TARGET(POP_JUMP_IF_TRUE): {
3212 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 PyObject *cond = POP();
3214 int err;
3215 if (cond == Py_False) {
3216 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 FAST_DISPATCH();
3218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 if (cond == Py_True) {
3220 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 JUMPTO(oparg);
3222 FAST_DISPATCH();
3223 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 err = PyObject_IsTrue(cond);
3225 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 JUMPTO(oparg);
3228 }
3229 else if (err == 0)
3230 ;
3231 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003235
Benjamin Petersonddd19492018-09-16 22:38:02 -07003236 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 PyObject *cond = TOP();
3238 int err;
3239 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003240 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 FAST_DISPATCH();
3243 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 JUMPTO(oparg);
3246 FAST_DISPATCH();
3247 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003250 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 }
3253 else if (err == 0)
3254 JUMPTO(oparg);
3255 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003259
Benjamin Petersonddd19492018-09-16 22:38:02 -07003260 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 PyObject *cond = TOP();
3262 int err;
3263 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003264 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 FAST_DISPATCH();
3267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 JUMPTO(oparg);
3270 FAST_DISPATCH();
3271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 JUMPTO(oparg);
3275 }
3276 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003277 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 }
3280 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003284
Benjamin Petersonddd19492018-09-16 22:38:02 -07003285 case TARGET(JUMP_ABSOLUTE): {
3286 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003288#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 /* Enabling this path speeds-up all while and for-loops by bypassing
3290 the per-loop checks for signals. By default, this should be turned-off
3291 because it prevents detection of a control-break in tight loops like
3292 "while 1: pass". Compile with this option turned-on when you need
3293 the speed-up and do not need break checking inside tight loops (ones
3294 that contain only instructions ending with FAST_DISPATCH).
3295 */
3296 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003297#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003299#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003305 PyObject *iter = PyObject_GetIter(iterable);
3306 Py_DECREF(iterable);
3307 SET_TOP(iter);
3308 if (iter == NULL)
3309 goto error;
3310 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003311 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003312 DISPATCH();
3313 }
3314
Benjamin Petersonddd19492018-09-16 22:38:02 -07003315 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003316 /* before: [obj]; after [getiter(obj)] */
3317 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003318 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003319 if (PyCoro_CheckExact(iterable)) {
3320 /* `iterable` is a coroutine */
3321 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3322 /* and it is used in a 'yield from' expression of a
3323 regular generator. */
3324 Py_DECREF(iterable);
3325 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003326 _PyErr_SetString(tstate, PyExc_TypeError,
3327 "cannot 'yield from' a coroutine object "
3328 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003329 goto error;
3330 }
3331 }
3332 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003333 /* `iterable` is not a generator. */
3334 iter = PyObject_GetIter(iterable);
3335 Py_DECREF(iterable);
3336 SET_TOP(iter);
3337 if (iter == NULL)
3338 goto error;
3339 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003340 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 DISPATCH();
3342 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003343
Benjamin Petersonddd19492018-09-16 22:38:02 -07003344 case TARGET(FOR_ITER): {
3345 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003348 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 if (next != NULL) {
3350 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 PREDICT(STORE_FAST);
3352 PREDICT(UNPACK_SEQUENCE);
3353 DISPATCH();
3354 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003355 if (_PyErr_Occurred(tstate)) {
3356 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003358 }
3359 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003360 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003361 }
3362 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 }
3364 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003365 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 JUMPBY(oparg);
3368 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003369 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003370
Benjamin Petersonddd19492018-09-16 22:38:02 -07003371 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003372 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 STACK_LEVEL());
3374 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003376
Benjamin Petersonddd19492018-09-16 22:38:02 -07003377 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003378 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003379 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003380 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003381 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003382 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003383 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003384 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003385 }
3386 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3387 if (exit == NULL) {
3388 Py_DECREF(enter);
3389 goto error;
3390 }
Yury Selivanov75445082015-05-11 22:57:16 -04003391 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003392 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003393 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003394 Py_DECREF(enter);
3395 if (res == NULL)
3396 goto error;
3397 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003398 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003399 DISPATCH();
3400 }
3401
Benjamin Petersonddd19492018-09-16 22:38:02 -07003402 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003403 PyObject *res = POP();
3404 /* Setup the finally block before pushing the result
3405 of __aenter__ on the stack. */
3406 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3407 STACK_LEVEL());
3408 PUSH(res);
3409 DISPATCH();
3410 }
3411
Benjamin Petersonddd19492018-09-16 22:38:02 -07003412 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003413 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003414 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003416 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003417 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003418 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003419 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003420 }
3421 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003422 if (exit == NULL) {
3423 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003425 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003426 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003428 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003429 Py_DECREF(enter);
3430 if (res == NULL)
3431 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 /* Setup the finally block before pushing the result
3433 of __enter__ on the stack. */
3434 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3435 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003436
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 DISPATCH();
3439 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003440
Mark Shannonfee55262019-11-21 09:11:43 +00003441 case TARGET(WITH_EXCEPT_START): {
3442 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003444 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3445 - SEVENTH: the context.__exit__ bound method
3446 We call SEVENTH(TOP, SECOND, THIRD).
3447 Then we push again the TOP exception and the __exit__
3448 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003451 PyObject *exc, *val, *tb, *res;
3452
Victor Stinner842cfff2016-12-01 14:45:31 +01003453 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003454 val = SECOND();
3455 tb = THIRD();
3456 assert(exc != Py_None);
3457 assert(!PyLong_Check(exc));
3458 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003459 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003460 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003461 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003462 if (res == NULL)
3463 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003464
Yury Selivanov75445082015-05-11 22:57:16 -04003465 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003468
Benjamin Petersonddd19492018-09-16 22:38:02 -07003469 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003470 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003471 PyObject *name = GETITEM(names, oparg);
3472 PyObject *obj = TOP();
3473 PyObject *meth = NULL;
3474
3475 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3476
Yury Selivanovf2392132016-12-13 19:03:51 -05003477 if (meth == NULL) {
3478 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003479 goto error;
3480 }
3481
3482 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003483 /* We can bypass temporary bound method object.
3484 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003485
INADA Naoki015bce62017-01-16 17:23:30 +09003486 meth | self | arg1 | ... | argN
3487 */
3488 SET_TOP(meth);
3489 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 }
3491 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003492 /* meth is not an unbound method (but a regular attr, or
3493 something was returned by a descriptor protocol). Set
3494 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003495 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003496
3497 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003498 */
INADA Naoki015bce62017-01-16 17:23:30 +09003499 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003500 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003501 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003502 }
3503 DISPATCH();
3504 }
3505
Benjamin Petersonddd19492018-09-16 22:38:02 -07003506 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003507 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003508 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003509
3510 sp = stack_pointer;
3511
INADA Naoki015bce62017-01-16 17:23:30 +09003512 meth = PEEK(oparg + 2);
3513 if (meth == NULL) {
3514 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3515 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003516
3517 Stack layout:
3518
INADA Naoki015bce62017-01-16 17:23:30 +09003519 ... | NULL | callable | arg1 | ... | argN
3520 ^- TOP()
3521 ^- (-oparg)
3522 ^- (-oparg-1)
3523 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003524
Ville Skyttä49b27342017-08-03 09:00:59 +03003525 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003526 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003527 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003528 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003529 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003530 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003531 }
3532 else {
3533 /* This is a method call. Stack layout:
3534
INADA Naoki015bce62017-01-16 17:23:30 +09003535 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003536 ^- TOP()
3537 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003538 ^- (-oparg-1)
3539 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003540
INADA Naoki015bce62017-01-16 17:23:30 +09003541 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003542 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003543 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003544 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003545 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003546 stack_pointer = sp;
3547 }
3548
3549 PUSH(res);
3550 if (res == NULL)
3551 goto error;
3552 DISPATCH();
3553 }
3554
Benjamin Petersonddd19492018-09-16 22:38:02 -07003555 case TARGET(CALL_FUNCTION): {
3556 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003557 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003559 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003562 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003565 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003567
Benjamin Petersonddd19492018-09-16 22:38:02 -07003568 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003569 PyObject **sp, *res, *names;
3570
3571 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003572 assert(PyTuple_Check(names));
3573 assert(PyTuple_GET_SIZE(names) <= oparg);
3574 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003576 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003579 Py_DECREF(names);
3580
3581 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003583 }
3584 DISPATCH();
3585 }
3586
Benjamin Petersonddd19492018-09-16 22:38:02 -07003587 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003588 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003589 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003590 if (oparg & 0x01) {
3591 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003592 if (!PyDict_CheckExact(kwargs)) {
3593 PyObject *d = PyDict_New();
3594 if (d == NULL)
3595 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003596 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003597 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003598 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003599 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003600 goto error;
3601 }
3602 Py_DECREF(kwargs);
3603 kwargs = d;
3604 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003605 assert(PyDict_CheckExact(kwargs));
3606 }
3607 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003608 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003609 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003610 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003611 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003612 goto error;
3613 }
3614 Py_SETREF(callargs, PySequence_Tuple(callargs));
3615 if (callargs == NULL) {
3616 goto error;
3617 }
3618 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003619 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003620
Victor Stinner09532fe2019-05-10 23:39:09 +02003621 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003622 Py_DECREF(func);
3623 Py_DECREF(callargs);
3624 Py_XDECREF(kwargs);
3625
3626 SET_TOP(result);
3627 if (result == NULL) {
3628 goto error;
3629 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003630 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003632
Benjamin Petersonddd19492018-09-16 22:38:02 -07003633 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003634 PyObject *qualname = POP();
3635 PyObject *codeobj = POP();
3636 PyFunctionObject *func = (PyFunctionObject *)
3637 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003638
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003639 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003640 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003641 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003642 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003644
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003645 if (oparg & 0x08) {
3646 assert(PyTuple_CheckExact(TOP()));
3647 func ->func_closure = POP();
3648 }
3649 if (oparg & 0x04) {
3650 assert(PyDict_CheckExact(TOP()));
3651 func->func_annotations = POP();
3652 }
3653 if (oparg & 0x02) {
3654 assert(PyDict_CheckExact(TOP()));
3655 func->func_kwdefaults = POP();
3656 }
3657 if (oparg & 0x01) {
3658 assert(PyTuple_CheckExact(TOP()));
3659 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003661
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003662 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003663 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003665
Benjamin Petersonddd19492018-09-16 22:38:02 -07003666 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003667 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003669 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 step = NULL;
3672 stop = POP();
3673 start = TOP();
3674 slice = PySlice_New(start, stop, step);
3675 Py_DECREF(start);
3676 Py_DECREF(stop);
3677 Py_XDECREF(step);
3678 SET_TOP(slice);
3679 if (slice == NULL)
3680 goto error;
3681 DISPATCH();
3682 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003683
Benjamin Petersonddd19492018-09-16 22:38:02 -07003684 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003685 /* Handles f-string value formatting. */
3686 PyObject *result;
3687 PyObject *fmt_spec;
3688 PyObject *value;
3689 PyObject *(*conv_fn)(PyObject *);
3690 int which_conversion = oparg & FVC_MASK;
3691 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3692
3693 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003694 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003695
3696 /* See if any conversion is specified. */
3697 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003698 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003699 case FVC_STR: conv_fn = PyObject_Str; break;
3700 case FVC_REPR: conv_fn = PyObject_Repr; break;
3701 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003702 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003703 _PyErr_Format(tstate, PyExc_SystemError,
3704 "unexpected conversion flag %d",
3705 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003706 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003707 }
3708
3709 /* If there's a conversion function, call it and replace
3710 value with that result. Otherwise, just use value,
3711 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003712 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003713 result = conv_fn(value);
3714 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003715 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003716 Py_XDECREF(fmt_spec);
3717 goto error;
3718 }
3719 value = result;
3720 }
3721
3722 /* If value is a unicode object, and there's no fmt_spec,
3723 then we know the result of format(value) is value
3724 itself. In that case, skip calling format(). I plan to
3725 move this optimization in to PyObject_Format()
3726 itself. */
3727 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3728 /* Do nothing, just transfer ownership to result. */
3729 result = value;
3730 } else {
3731 /* Actually call format(). */
3732 result = PyObject_Format(value, fmt_spec);
3733 Py_DECREF(value);
3734 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003735 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003736 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003737 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003738 }
3739
Eric V. Smith135d5f42016-02-05 18:23:08 -05003740 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003741 DISPATCH();
3742 }
3743
Benjamin Petersonddd19492018-09-16 22:38:02 -07003744 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003745 int oldoparg = oparg;
3746 NEXTOPARG();
3747 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003749 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003750
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003751
Antoine Pitrou042b1282010-08-13 21:15:58 +00003752#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003754#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 default:
3756 fprintf(stderr,
3757 "XXX lineno: %d, opcode: %d\n",
3758 PyFrame_GetLineNumber(f),
3759 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003760 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003761 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003764
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003765 /* This should never be reached. Every opcode should end with DISPATCH()
3766 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003767 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003769error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003770 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003771#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003772 if (!_PyErr_Occurred(tstate)) {
3773 _PyErr_SetString(tstate, PyExc_SystemError,
3774 "error return without exception set");
3775 }
Victor Stinner365b6932013-07-12 00:11:58 +02003776#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003777 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003778#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003779
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 /* Log traceback info. */
3781 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003782
Mark Shannoncb9879b2020-07-17 11:44:23 +01003783 if (tstate->c_tracefunc != NULL) {
3784 /* Make sure state is set to FRAME_EXECUTING for tracing */
3785 assert(f->f_state == FRAME_EXECUTING);
3786 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003787 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3788 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01003789 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003790exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01003791 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003792 /* Unwind stacks if an exception occurred */
3793 while (f->f_iblock > 0) {
3794 /* Pop the current block. */
3795 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 if (b->b_type == EXCEPT_HANDLER) {
3798 UNWIND_EXCEPT_HANDLER(b);
3799 continue;
3800 }
3801 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003802 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 PyObject *exc, *val, *tb;
3804 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003805 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 /* Beware, this invalidates all b->b_* fields */
3807 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003808 PUSH(exc_info->exc_traceback);
3809 PUSH(exc_info->exc_value);
3810 if (exc_info->exc_type != NULL) {
3811 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 }
3813 else {
3814 Py_INCREF(Py_None);
3815 PUSH(Py_None);
3816 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003817 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 /* Make the raw exception data
3819 available to the handler,
3820 so a program can emulate the
3821 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003822 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003823 if (tb != NULL)
3824 PyException_SetTraceback(val, tb);
3825 else
3826 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003828 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003830 exc_info->exc_value = val;
3831 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 if (tb == NULL)
3833 tb = Py_None;
3834 Py_INCREF(tb);
3835 PUSH(tb);
3836 PUSH(val);
3837 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003839 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003840 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3841 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3842 /* Make sure that we trace line after exception if we are in a new execution
3843 * window or we don't need a line update and we are not in the first instruction
3844 * of the line. */
3845 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3846 instr_prev = INT_MAX;
3847 }
Mark Shannonfee55262019-11-21 09:11:43 +00003848 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003849 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01003850 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003851 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 }
3853 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003854
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003855 /* End the loop as we still have an error */
3856 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003858
Pablo Galindof00828a2019-05-09 16:52:02 +01003859 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003860 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 /* Pop remaining stack entries. */
3863 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003864 PyObject *o = POP();
3865 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01003867 f->f_stackdepth = 0;
3868 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003869exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003871 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003872 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3873 tstate, f, PyTrace_RETURN, retval)) {
3874 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
3876 }
3877 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003878 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3879 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003880 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 }
3882 }
3883 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003886exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003887 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3888 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003889 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003891
Victor Stinner0b72b232020-03-12 23:18:39 +01003892 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003893}
3894
Benjamin Petersonb204a422011-06-05 22:04:07 -05003895static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003896format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003897 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003898{
3899 int err;
3900 Py_ssize_t len = PyList_GET_SIZE(names);
3901 PyObject *name_str, *comma, *tail, *tmp;
3902
3903 assert(PyList_CheckExact(names));
3904 assert(len >= 1);
3905 /* Deal with the joys of natural language. */
3906 switch (len) {
3907 case 1:
3908 name_str = PyList_GET_ITEM(names, 0);
3909 Py_INCREF(name_str);
3910 break;
3911 case 2:
3912 name_str = PyUnicode_FromFormat("%U and %U",
3913 PyList_GET_ITEM(names, len - 2),
3914 PyList_GET_ITEM(names, len - 1));
3915 break;
3916 default:
3917 tail = PyUnicode_FromFormat(", %U, and %U",
3918 PyList_GET_ITEM(names, len - 2),
3919 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003920 if (tail == NULL)
3921 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003922 /* Chop off the last two objects in the list. This shouldn't actually
3923 fail, but we can't be too careful. */
3924 err = PyList_SetSlice(names, len - 2, len, NULL);
3925 if (err == -1) {
3926 Py_DECREF(tail);
3927 return;
3928 }
3929 /* Stitch everything up into a nice comma-separated list. */
3930 comma = PyUnicode_FromString(", ");
3931 if (comma == NULL) {
3932 Py_DECREF(tail);
3933 return;
3934 }
3935 tmp = PyUnicode_Join(comma, names);
3936 Py_DECREF(comma);
3937 if (tmp == NULL) {
3938 Py_DECREF(tail);
3939 return;
3940 }
3941 name_str = PyUnicode_Concat(tmp, tail);
3942 Py_DECREF(tmp);
3943 Py_DECREF(tail);
3944 break;
3945 }
3946 if (name_str == NULL)
3947 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003948 _PyErr_Format(tstate, PyExc_TypeError,
3949 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003950 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003951 len,
3952 kind,
3953 len == 1 ? "" : "s",
3954 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003955 Py_DECREF(name_str);
3956}
3957
3958static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003959missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3960 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003961 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003962{
Victor Stinner74319ae2016-08-25 00:04:09 +02003963 Py_ssize_t i, j = 0;
3964 Py_ssize_t start, end;
3965 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003966 const char *kind = positional ? "positional" : "keyword-only";
3967 PyObject *missing_names;
3968
3969 /* Compute the names of the arguments that are missing. */
3970 missing_names = PyList_New(missing);
3971 if (missing_names == NULL)
3972 return;
3973 if (positional) {
3974 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003975 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003976 }
3977 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003978 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003979 end = start + co->co_kwonlyargcount;
3980 }
3981 for (i = start; i < end; i++) {
3982 if (GETLOCAL(i) == NULL) {
3983 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3984 PyObject *name = PyObject_Repr(raw);
3985 if (name == NULL) {
3986 Py_DECREF(missing_names);
3987 return;
3988 }
3989 PyList_SET_ITEM(missing_names, j++, name);
3990 }
3991 }
3992 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003993 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003994 Py_DECREF(missing_names);
3995}
3996
3997static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003998too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3999 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004000 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004001{
4002 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004003 Py_ssize_t kwonly_given = 0;
4004 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004006 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007
Benjamin Petersone109c702011-06-24 09:37:26 -05004008 assert((co->co_flags & CO_VARARGS) == 0);
4009 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004010 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004011 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004013 }
4014 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004015 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004016 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004018 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 }
4020 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004021 plural = (co_argcount != 1);
4022 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 }
4024 if (sig == NULL)
4025 return;
4026 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004027 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4028 kwonly_sig = PyUnicode_FromFormat(format,
4029 given != 1 ? "s" : "",
4030 kwonly_given,
4031 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 if (kwonly_sig == NULL) {
4033 Py_DECREF(sig);
4034 return;
4035 }
4036 }
4037 else {
4038 /* This will not fail. */
4039 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004040 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004041 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004042 _PyErr_Format(tstate, PyExc_TypeError,
4043 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004044 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004045 sig,
4046 plural ? "s" : "",
4047 given,
4048 kwonly_sig,
4049 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 Py_DECREF(sig);
4051 Py_DECREF(kwonly_sig);
4052}
4053
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004054static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004055positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004056 Py_ssize_t kwcount, PyObject* const* kwnames,
4057 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004058{
4059 int posonly_conflicts = 0;
4060 PyObject* posonly_names = PyList_New(0);
4061
4062 for(int k=0; k < co->co_posonlyargcount; k++){
4063 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4064
4065 for (int k2=0; k2<kwcount; k2++){
4066 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4067 PyObject* kwname = kwnames[k2];
4068 if (kwname == posonly_name){
4069 if(PyList_Append(posonly_names, kwname) != 0) {
4070 goto fail;
4071 }
4072 posonly_conflicts++;
4073 continue;
4074 }
4075
4076 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4077
4078 if ( cmp > 0) {
4079 if(PyList_Append(posonly_names, kwname) != 0) {
4080 goto fail;
4081 }
4082 posonly_conflicts++;
4083 } else if (cmp < 0) {
4084 goto fail;
4085 }
4086
4087 }
4088 }
4089 if (posonly_conflicts) {
4090 PyObject* comma = PyUnicode_FromString(", ");
4091 if (comma == NULL) {
4092 goto fail;
4093 }
4094 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4095 Py_DECREF(comma);
4096 if (error_names == NULL) {
4097 goto fail;
4098 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004099 _PyErr_Format(tstate, PyExc_TypeError,
4100 "%U() got some positional-only arguments passed"
4101 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004102 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004103 Py_DECREF(error_names);
4104 goto fail;
4105 }
4106
4107 Py_DECREF(posonly_names);
4108 return 0;
4109
4110fail:
4111 Py_XDECREF(posonly_names);
4112 return 1;
4113
4114}
4115
Guido van Rossumc2e20742006-02-27 22:32:47 +00004116/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004117 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004118 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004119
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004120PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004121_PyEval_EvalCode(PyThreadState *tstate,
4122 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004123 PyObject *const *args, Py_ssize_t argcount,
4124 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004125 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004126 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004127 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004128 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004129{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004130 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004131
Victor Stinner232dda62020-06-04 15:19:02 +02004132 PyCodeObject *co = (PyCodeObject*)_co;
4133
4134 if (!name) {
4135 name = co->co_name;
4136 }
4137 assert(name != NULL);
4138 assert(PyUnicode_Check(name));
4139
4140 if (!qualname) {
4141 qualname = name;
4142 }
4143 assert(qualname != NULL);
4144 assert(PyUnicode_Check(qualname));
4145
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004146 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004147 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004150 _PyErr_SetString(tstate, PyExc_SystemError,
4151 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 return NULL;
4153 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004154
Victor Stinnerc7020012016-08-16 23:40:29 +02004155 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004156 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004157 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004159 }
Victor Stinner232dda62020-06-04 15:19:02 +02004160 PyObject **fastlocals = f->f_localsplus;
4161 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004162
Victor Stinnerc7020012016-08-16 23:40:29 +02004163 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004164 PyObject *kwdict;
4165 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004166 if (co->co_flags & CO_VARKEYWORDS) {
4167 kwdict = PyDict_New();
4168 if (kwdict == NULL)
4169 goto fail;
4170 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004171 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004172 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004173 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004174 SETLOCAL(i, kwdict);
4175 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004176 else {
4177 kwdict = NULL;
4178 }
4179
Pablo Galindocd74e662019-06-01 18:08:04 +01004180 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004181 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004182 if (argcount > co->co_argcount) {
4183 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004184 }
4185 else {
4186 n = argcount;
4187 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004188 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004189 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004190 Py_INCREF(x);
4191 SETLOCAL(j, x);
4192 }
4193
Victor Stinnerc7020012016-08-16 23:40:29 +02004194 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004196 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004197 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004198 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004199 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004200 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004202
Serhiy Storchakab7281052016-09-12 00:52:40 +03004203 /* Handle keyword arguments passed as two strided arrays */
4204 kwcount *= kwstep;
4205 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004206 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004207 PyObject *keyword = kwnames[i];
4208 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004209 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004210
Benjamin Petersonb204a422011-06-05 22:04:07 -05004211 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004212 _PyErr_Format(tstate, PyExc_TypeError,
4213 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004214 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004215 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004217
Benjamin Petersonb204a422011-06-05 22:04:07 -05004218 /* Speed hack: do raw pointer compares. As names are
4219 normally interned this should almost always hit. */
4220 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004221 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004222 PyObject *varname = co_varnames[j];
4223 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004224 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004225 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004226 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004227
Benjamin Petersonb204a422011-06-05 22:04:07 -05004228 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004229 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004230 PyObject *varname = co_varnames[j];
4231 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004232 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004234 }
4235 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004237 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004239
Victor Stinner231d1f32017-01-11 02:12:06 +01004240 assert(j >= total_args);
4241 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004242
Victor Stinner438a12d2019-05-24 17:01:38 +02004243 if (co->co_posonlyargcount
4244 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004245 kwcount, kwnames,
4246 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004247 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004248 goto fail;
4249 }
4250
Victor Stinner438a12d2019-05-24 17:01:38 +02004251 _PyErr_Format(tstate, PyExc_TypeError,
4252 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004253 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004254 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004256
Christian Heimes0bd447f2013-07-20 14:48:10 +02004257 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4258 goto fail;
4259 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004260 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004261
Benjamin Petersonb204a422011-06-05 22:04:07 -05004262 kw_found:
4263 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004264 _PyErr_Format(tstate, PyExc_TypeError,
4265 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004266 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004267 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004269 Py_INCREF(value);
4270 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004272
4273 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004274 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004275 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4276 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 goto fail;
4278 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004279
4280 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004281 if (argcount < co->co_argcount) {
4282 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004283 Py_ssize_t missing = 0;
4284 for (i = argcount; i < m; i++) {
4285 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004286 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004287 }
4288 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004289 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004290 missing_arguments(tstate, co, missing, defcount, fastlocals,
4291 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004292 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004293 }
4294 if (n > m)
4295 i = n - m;
4296 else
4297 i = 0;
4298 for (; i < defcount; i++) {
4299 if (GETLOCAL(m+i) == NULL) {
4300 PyObject *def = defs[i];
4301 Py_INCREF(def);
4302 SETLOCAL(m+i, def);
4303 }
4304 }
4305 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004306
4307 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004308 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004309 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004310 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004311 if (GETLOCAL(i) != NULL)
4312 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004313 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004314 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004315 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004316 if (def) {
4317 Py_INCREF(def);
4318 SETLOCAL(i, def);
4319 continue;
4320 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004321 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004322 goto fail;
4323 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004324 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004325 missing++;
4326 }
4327 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004328 missing_arguments(tstate, co, missing, -1, fastlocals,
4329 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004330 goto fail;
4331 }
4332 }
4333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004335 vars into frame. */
4336 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004338 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004339 /* Possibly account for the cell variable being an argument. */
4340 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004341 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004342 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004343 /* Clear the local copy. */
4344 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004345 }
4346 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004347 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004348 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004349 if (c == NULL)
4350 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004351 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004353
4354 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004355 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4356 PyObject *o = PyTuple_GET_ITEM(closure, i);
4357 Py_INCREF(o);
4358 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004360
Yury Selivanoveb636452016-09-08 22:01:51 -07004361 /* Handle generator/coroutine/asynchronous generator */
4362 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004363 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004364 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 /* Don't need to keep the reference to f_back, it will be set
4367 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004368 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 /* Create a new generator that owns the ready to run frame
4371 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004372 if (is_coro) {
4373 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004374 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4375 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004376 } else {
4377 gen = PyGen_NewWithQualName(f, name, qualname);
4378 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004379 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004380 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004381 }
INADA Naoki9c157762016-12-26 18:52:46 +09004382
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004383 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004384
Yury Selivanov75445082015-05-11 22:57:16 -04004385 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004387
Victor Stinnerb9e68122019-11-14 12:20:46 +01004388 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004389
Thomas Woutersce272b62007-09-19 21:19:28 +00004390fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 /* decref'ing the frame can cause __del__ methods to get invoked,
4393 which can call back into Python. While we're done with the
4394 current Python frame (f), the associated C stack is still in use,
4395 so recursion_depth must be boosted for the duration.
4396 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004397 if (Py_REFCNT(f) > 1) {
4398 Py_DECREF(f);
4399 _PyObject_GC_TRACK(f);
4400 }
4401 else {
4402 ++tstate->recursion_depth;
4403 Py_DECREF(f);
4404 --tstate->recursion_depth;
4405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004407}
4408
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004409
4410PyObject *
4411_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4412 PyObject *const *args, Py_ssize_t argcount,
4413 PyObject *const *kwnames, PyObject *const *kwargs,
4414 Py_ssize_t kwcount, int kwstep,
4415 PyObject *const *defs, Py_ssize_t defcount,
4416 PyObject *kwdefs, PyObject *closure,
4417 PyObject *name, PyObject *qualname)
4418{
4419 PyThreadState *tstate = _PyThreadState_GET();
4420 return _PyEval_EvalCode(tstate, _co, globals, locals,
4421 args, argcount,
4422 kwnames, kwargs,
4423 kwcount, kwstep,
4424 defs, defcount,
4425 kwdefs, closure,
4426 name, qualname);
4427}
4428
Victor Stinner40ee3012014-06-16 15:59:28 +02004429PyObject *
4430PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004431 PyObject *const *args, int argcount,
4432 PyObject *const *kws, int kwcount,
4433 PyObject *const *defs, int defcount,
4434 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004435{
4436 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004437 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004438 kws, kws != NULL ? kws + 1 : NULL,
4439 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004440 defs, defcount,
4441 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004442 NULL, NULL);
4443}
Tim Peters5ca576e2001-06-18 22:08:13 +00004444
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004445static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004446special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004449 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004450 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004451 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 return NULL;
4453 }
4454 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004455}
4456
4457
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004458/* Logic for the raise statement (too complicated for inlining).
4459 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004460static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004461do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 if (exc == NULL) {
4466 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004467 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004469 type = exc_info->exc_type;
4470 value = exc_info->exc_value;
4471 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004472 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 _PyErr_SetString(tstate, PyExc_RuntimeError,
4474 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004475 return 0;
4476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 Py_XINCREF(type);
4478 Py_XINCREF(value);
4479 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004481 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 /* We support the following forms of raise:
4485 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004486 raise <instance>
4487 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (PyExceptionClass_Check(exc)) {
4490 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004491 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 if (value == NULL)
4493 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004494 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004495 _PyErr_Format(tstate, PyExc_TypeError,
4496 "calling %R should have returned an instance of "
4497 "BaseException, not %R",
4498 type, Py_TYPE(value));
4499 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 }
4502 else if (PyExceptionInstance_Check(exc)) {
4503 value = exc;
4504 type = PyExceptionInstance_Class(exc);
4505 Py_INCREF(type);
4506 }
4507 else {
4508 /* Not something you can raise. You get an exception
4509 anyway, just not what you specified :-) */
4510 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004511 _PyErr_SetString(tstate, PyExc_TypeError,
4512 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 goto raise_error;
4514 }
Collin Winter828f04a2007-08-31 00:04:24 +00004515
Serhiy Storchakac0191582016-09-27 11:37:10 +03004516 assert(type != NULL);
4517 assert(value != NULL);
4518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 if (cause) {
4520 PyObject *fixed_cause;
4521 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004522 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 if (fixed_cause == NULL)
4524 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004525 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004527 else if (PyExceptionInstance_Check(cause)) {
4528 fixed_cause = cause;
4529 }
4530 else if (cause == Py_None) {
4531 Py_DECREF(cause);
4532 fixed_cause = NULL;
4533 }
4534 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004535 _PyErr_SetString(tstate, PyExc_TypeError,
4536 "exception causes must derive from "
4537 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 goto raise_error;
4539 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004540 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 }
Collin Winter828f04a2007-08-31 00:04:24 +00004542
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004544 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004545 Py_DECREF(value);
4546 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004547 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004548
4549raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 Py_XDECREF(value);
4551 Py_XDECREF(type);
4552 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004553 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004554}
4555
Tim Petersd6d010b2001-06-21 02:49:55 +00004556/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004557 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004558
Guido van Rossum0368b722007-05-11 16:50:42 +00004559 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4560 with a variable target.
4561*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004562
Barry Warsawe42b18f1997-08-25 22:13:04 +00004563static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004564unpack_iterable(PyThreadState *tstate, PyObject *v,
4565 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 int i = 0, j = 0;
4568 Py_ssize_t ll = 0;
4569 PyObject *it; /* iter(v) */
4570 PyObject *w;
4571 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004576 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004577 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004578 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004579 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004580 _PyErr_Format(tstate, PyExc_TypeError,
4581 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004582 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004583 }
4584 return 0;
4585 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 for (; i < argcnt; i++) {
4588 w = PyIter_Next(it);
4589 if (w == NULL) {
4590 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004591 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004592 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004593 _PyErr_Format(tstate, PyExc_ValueError,
4594 "not enough values to unpack "
4595 "(expected %d, got %d)",
4596 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004597 }
4598 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004599 _PyErr_Format(tstate, PyExc_ValueError,
4600 "not enough values to unpack "
4601 "(expected at least %d, got %d)",
4602 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 }
4605 goto Error;
4606 }
4607 *--sp = w;
4608 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 if (argcntafter == -1) {
4611 /* We better have exhausted the iterator now. */
4612 w = PyIter_Next(it);
4613 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004614 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 goto Error;
4616 Py_DECREF(it);
4617 return 1;
4618 }
4619 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004620 _PyErr_Format(tstate, PyExc_ValueError,
4621 "too many values to unpack (expected %d)",
4622 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 goto Error;
4624 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 l = PySequence_List(it);
4627 if (l == NULL)
4628 goto Error;
4629 *--sp = l;
4630 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 ll = PyList_GET_SIZE(l);
4633 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004634 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004635 "not enough values to unpack (expected at least %d, got %zd)",
4636 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 goto Error;
4638 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 /* Pop the "after-variable" args off the list. */
4641 for (j = argcntafter; j > 0; j--, i++) {
4642 *--sp = PyList_GET_ITEM(l, ll - j);
4643 }
4644 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004645 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 Py_DECREF(it);
4647 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004648
Tim Petersd6d010b2001-06-21 02:49:55 +00004649Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 for (; i > 0; i--, sp++)
4651 Py_DECREF(*sp);
4652 Py_XDECREF(it);
4653 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004654}
4655
4656
Guido van Rossum96a42c81992-01-12 02:29:51 +00004657#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004658static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004659prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004662 if (PyObject_Print(v, stdout, 0) != 0) {
4663 /* Don't know what else to do */
4664 _PyErr_Clear(tstate);
4665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 printf("\n");
4667 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004668}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004669#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004670
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004671static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004672call_exc_trace(Py_tracefunc func, PyObject *self,
4673 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004674{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004675 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004677 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 if (value == NULL) {
4679 value = Py_None;
4680 Py_INCREF(value);
4681 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004682 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004683 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 arg = PyTuple_Pack(3, type, value, traceback);
4685 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004686 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 return;
4688 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004689 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004691 if (err == 0) {
4692 _PyErr_Restore(tstate, type, value, orig_traceback);
4693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 else {
4695 Py_XDECREF(type);
4696 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004697 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004699}
4700
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004701static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004702call_trace_protected(Py_tracefunc func, PyObject *obj,
4703 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 PyObject *type, *value, *traceback;
4707 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004708 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004709 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 if (err == 0)
4711 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004712 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 return 0;
4714 }
4715 else {
4716 Py_XDECREF(type);
4717 Py_XDECREF(value);
4718 Py_XDECREF(traceback);
4719 return -1;
4720 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004721}
4722
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004723static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004724call_trace(Py_tracefunc func, PyObject *obj,
4725 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 int result;
4729 if (tstate->tracing)
4730 return 0;
4731 tstate->tracing++;
4732 tstate->use_tracing = 0;
4733 result = func(obj, frame, what, arg);
4734 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4735 || (tstate->c_profilefunc != NULL));
4736 tstate->tracing--;
4737 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004738}
4739
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004740PyObject *
4741_PyEval_CallTracing(PyObject *func, PyObject *args)
4742{
Victor Stinner50b48572018-11-01 01:51:40 +01004743 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 int save_tracing = tstate->tracing;
4745 int save_use_tracing = tstate->use_tracing;
4746 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 tstate->tracing = 0;
4749 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4750 || (tstate->c_profilefunc != NULL));
4751 result = PyObject_Call(func, args, NULL);
4752 tstate->tracing = save_tracing;
4753 tstate->use_tracing = save_use_tracing;
4754 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004755}
4756
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004757/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004758static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004759maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004760 PyThreadState *tstate, PyFrameObject *frame,
4761 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 int result = 0;
4764 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 /* If the last instruction executed isn't in the current
4767 instruction window, reset the window.
4768 */
4769 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4770 PyAddrPair bounds;
4771 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4772 &bounds);
4773 *instr_lb = bounds.ap_lower;
4774 *instr_ub = bounds.ap_upper;
4775 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004776 /* If the last instruction falls at the start of a line or if it
4777 represents a jump backwards, update the frame's line number and
4778 then call the trace function if we're tracing source lines.
4779 */
4780 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004782 if (frame->f_trace_lines) {
4783 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 }
George King20faa682017-10-18 17:44:22 -07004786 /* Always emit an opcode event if we're tracing all opcodes. */
4787 if (frame->f_trace_opcodes) {
4788 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4789 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 *instr_prev = frame->f_lasti;
4791 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004792}
4793
Victor Stinner309d7cc2020-03-13 16:39:12 +01004794int
4795_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4796{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004797 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004798 /* The caller must hold the GIL */
4799 assert(PyGILState_Check());
4800
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004801 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004802 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004803 PyThreadState *current_tstate = _PyThreadState_GET();
4804 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004805 return -1;
4806 }
4807
4808 PyObject *profileobj = tstate->c_profileobj;
4809
4810 tstate->c_profilefunc = NULL;
4811 tstate->c_profileobj = NULL;
4812 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4813 tstate->use_tracing = tstate->c_tracefunc != NULL;
4814 Py_XDECREF(profileobj);
4815
4816 Py_XINCREF(arg);
4817 tstate->c_profileobj = arg;
4818 tstate->c_profilefunc = func;
4819
4820 /* Flag that tracing or profiling is turned on */
4821 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4822 return 0;
4823}
4824
Fred Drake5755ce62001-06-27 19:19:46 +00004825void
4826PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004827{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004828 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004829 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004830 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004831 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4832 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004833}
4834
4835int
4836_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4837{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004838 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004839 /* The caller must hold the GIL */
4840 assert(PyGILState_Check());
4841
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004842 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004843 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004844 PyThreadState *current_tstate = _PyThreadState_GET();
4845 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004846 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004847 }
4848
Victor Stinnerda2914d2020-03-20 09:29:08 +01004849 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004850 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004851 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004852
4853 tstate->c_tracefunc = NULL;
4854 tstate->c_traceobj = NULL;
4855 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4856 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4857 Py_XDECREF(traceobj);
4858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004860 tstate->c_traceobj = arg;
4861 tstate->c_tracefunc = func;
4862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004864 tstate->use_tracing = ((func != NULL)
4865 || (tstate->c_profilefunc != NULL));
4866
4867 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004868}
4869
4870void
4871PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4872{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004873 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004874 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004875 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004876 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4877 }
Fred Draked0838392001-06-16 21:02:31 +00004878}
4879
Victor Stinner309d7cc2020-03-13 16:39:12 +01004880
Yury Selivanov75445082015-05-11 22:57:16 -04004881void
Victor Stinner838f2642019-06-13 22:41:23 +02004882_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004883{
4884 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004885 tstate->coroutine_origin_tracking_depth = new_depth;
4886}
4887
4888int
4889_PyEval_GetCoroutineOriginTrackingDepth(void)
4890{
Victor Stinner50b48572018-11-01 01:51:40 +01004891 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004892 return tstate->coroutine_origin_tracking_depth;
4893}
4894
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004895int
Yury Selivanoveb636452016-09-08 22:01:51 -07004896_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4897{
Victor Stinner50b48572018-11-01 01:51:40 +01004898 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004899
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004900 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004901 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004902 }
4903
Yury Selivanoveb636452016-09-08 22:01:51 -07004904 Py_XINCREF(firstiter);
4905 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004906 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004907}
4908
4909PyObject *
4910_PyEval_GetAsyncGenFirstiter(void)
4911{
Victor Stinner50b48572018-11-01 01:51:40 +01004912 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004913 return tstate->async_gen_firstiter;
4914}
4915
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004916int
Yury Selivanoveb636452016-09-08 22:01:51 -07004917_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4918{
Victor Stinner50b48572018-11-01 01:51:40 +01004919 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004920
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004921 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004922 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004923 }
4924
Yury Selivanoveb636452016-09-08 22:01:51 -07004925 Py_XINCREF(finalizer);
4926 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004927 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004928}
4929
4930PyObject *
4931_PyEval_GetAsyncGenFinalizer(void)
4932{
Victor Stinner50b48572018-11-01 01:51:40 +01004933 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004934 return tstate->async_gen_finalizer;
4935}
4936
Victor Stinner438a12d2019-05-24 17:01:38 +02004937PyFrameObject *
4938PyEval_GetFrame(void)
4939{
4940 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004941 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004942}
4943
Guido van Rossumb209a111997-04-29 18:18:01 +00004944PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004945PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004946{
Victor Stinner438a12d2019-05-24 17:01:38 +02004947 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004948 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004950 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 else
4952 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004953}
4954
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004955/* Convenience function to get a builtin from its name */
4956PyObject *
4957_PyEval_GetBuiltinId(_Py_Identifier *name)
4958{
Victor Stinner438a12d2019-05-24 17:01:38 +02004959 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004960 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4961 if (attr) {
4962 Py_INCREF(attr);
4963 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004964 else if (!_PyErr_Occurred(tstate)) {
4965 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004966 }
4967 return attr;
4968}
4969
Guido van Rossumb209a111997-04-29 18:18:01 +00004970PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004971PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004972{
Victor Stinner438a12d2019-05-24 17:01:38 +02004973 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004974 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004975 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004976 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004978 }
4979
Victor Stinner438a12d2019-05-24 17:01:38 +02004980 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004981 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004982 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004983
4984 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004986}
4987
Guido van Rossumb209a111997-04-29 18:18:01 +00004988PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004989PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004990{
Victor Stinner438a12d2019-05-24 17:01:38 +02004991 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004992 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004993 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004995 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004996
4997 assert(current_frame->f_globals != NULL);
4998 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004999}
5000
Guido van Rossum6135a871995-01-09 17:53:26 +00005001int
Tim Peters5ba58662001-07-16 02:29:45 +00005002PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005003{
Victor Stinner438a12d2019-05-24 17:01:38 +02005004 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005005 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (current_frame != NULL) {
5009 const int codeflags = current_frame->f_code->co_flags;
5010 const int compilerflags = codeflags & PyCF_MASK;
5011 if (compilerflags) {
5012 result = 1;
5013 cf->cf_flags |= compilerflags;
5014 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005015#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 if (codeflags & CO_GENERATOR_ALLOWED) {
5017 result = 1;
5018 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5019 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005020#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 }
5022 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005023}
5024
Guido van Rossum3f5da241990-12-20 15:06:42 +00005025
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005026const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005027PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 if (PyMethod_Check(func))
5030 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5031 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005032 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 else if (PyCFunction_Check(func))
5034 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5035 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005036 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005037}
5038
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005039const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005040PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 if (PyMethod_Check(func))
5043 return "()";
5044 else if (PyFunction_Check(func))
5045 return "()";
5046 else if (PyCFunction_Check(func))
5047 return "()";
5048 else
5049 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005050}
5051
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005052#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005053if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005054 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5055 tstate, tstate->frame, \
5056 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 x = NULL; \
5058 } \
5059 else { \
5060 x = call; \
5061 if (tstate->c_profilefunc != NULL) { \
5062 if (x == NULL) { \
5063 call_trace_protected(tstate->c_profilefunc, \
5064 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005065 tstate, tstate->frame, \
5066 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005067 /* XXX should pass (type, value, tb) */ \
5068 } else { \
5069 if (call_trace(tstate->c_profilefunc, \
5070 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005071 tstate, tstate->frame, \
5072 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 Py_DECREF(x); \
5074 x = NULL; \
5075 } \
5076 } \
5077 } \
5078 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005079} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 x = call; \
5081 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005082
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005083
5084static PyObject *
5085trace_call_function(PyThreadState *tstate,
5086 PyObject *func,
5087 PyObject **args, Py_ssize_t nargs,
5088 PyObject *kwnames)
5089{
5090 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005091 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005092 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005093 return x;
5094 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005095 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005096 /* We need to create a temporary bound method as argument
5097 for profiling.
5098
5099 If nargs == 0, then this cannot work because we have no
5100 "self". In any case, the call itself would raise
5101 TypeError (foo needs an argument), so we just skip
5102 profiling. */
5103 PyObject *self = args[0];
5104 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5105 if (func == NULL) {
5106 return NULL;
5107 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005108 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005109 args+1, nargs-1,
5110 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005111 Py_DECREF(func);
5112 return x;
5113 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005114 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005115}
5116
Victor Stinner415c5102017-01-11 00:54:57 +01005117/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5118 to reduce the stack consumption. */
5119Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005120call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005121{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005122 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 PyObject *func = *pfunc;
5124 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005125 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5126 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005127 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005128
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005129 if (tstate->use_tracing) {
5130 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005131 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005132 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005133 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005135
Victor Stinner438a12d2019-05-24 17:01:38 +02005136 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005137
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005138 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 while ((*pp_stack) > pfunc) {
5140 w = EXT_POP(*pp_stack);
5141 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005145}
5146
Jeremy Hylton52820442001-01-03 23:52:36 +00005147static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005148do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005149{
jdemeyere89de732018-09-19 12:06:20 +02005150 PyObject *result;
5151
scoder4c9ea092020-05-12 16:12:41 +02005152 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005153 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005154 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005156 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005157 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5158 if (nargs > 0 && tstate->use_tracing) {
5159 /* We need to create a temporary bound method as argument
5160 for profiling.
5161
5162 If nargs == 0, then this cannot work because we have no
5163 "self". In any case, the call itself would raise
5164 TypeError (foo needs an argument), so we just skip
5165 profiling. */
5166 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5167 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5168 if (func == NULL) {
5169 return NULL;
5170 }
5171
Victor Stinner4d231bc2019-11-14 13:36:21 +01005172 C_TRACE(result, _PyObject_FastCallDictTstate(
5173 tstate, func,
5174 &_PyTuple_ITEMS(callargs)[1],
5175 nargs - 1,
5176 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005177 Py_DECREF(func);
5178 return result;
5179 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005180 }
jdemeyere89de732018-09-19 12:06:20 +02005181 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005182}
5183
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005184/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005185 nb_index slot defined, and store in *pi.
5186 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005187 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005188 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005189*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005190int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005191_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005192{
Victor Stinner438a12d2019-05-24 17:01:38 +02005193 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005194 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005196 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005198 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 return 0;
5200 }
5201 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005202 _PyErr_SetString(tstate, PyExc_TypeError,
5203 "slice indices must be integers or "
5204 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 return 0;
5206 }
5207 *pi = x;
5208 }
5209 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005210}
5211
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005212int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005213_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005214{
Victor Stinner438a12d2019-05-24 17:01:38 +02005215 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005216 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005217 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005218 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005219 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005220 return 0;
5221 }
5222 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005223 _PyErr_SetString(tstate, PyExc_TypeError,
5224 "slice indices must be integers or "
5225 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005226 return 0;
5227 }
5228 *pi = x;
5229 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005230}
5231
Thomas Wouters52152252000-08-17 22:55:00 +00005232static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005233import_name(PyThreadState *tstate, PyFrameObject *f,
5234 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005235{
5236 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005237 PyObject *import_func, *res;
5238 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005239
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005240 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005241 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005242 if (!_PyErr_Occurred(tstate)) {
5243 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005244 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005245 return NULL;
5246 }
5247
5248 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005249 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005250 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005251 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005252 return NULL;
5253 }
5254 res = PyImport_ImportModuleLevelObject(
5255 name,
5256 f->f_globals,
5257 f->f_locals == NULL ? Py_None : f->f_locals,
5258 fromlist,
5259 ilevel);
5260 return res;
5261 }
5262
5263 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005264
5265 stack[0] = name;
5266 stack[1] = f->f_globals;
5267 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5268 stack[3] = fromlist;
5269 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005270 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005271 Py_DECREF(import_func);
5272 return res;
5273}
5274
5275static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005276import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005279 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005280
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005281 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005282 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005283 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005284 /* Issue #17636: in case this failed because of a circular relative
5285 import, try to fallback on reading the module directly from
5286 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005287 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005288 if (pkgname == NULL) {
5289 goto error;
5290 }
Oren Milman6db70332017-09-19 14:23:01 +03005291 if (!PyUnicode_Check(pkgname)) {
5292 Py_CLEAR(pkgname);
5293 goto error;
5294 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005295 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005296 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005297 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005298 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005299 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005300 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005301 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005302 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005303 goto error;
5304 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005305 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005307 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005308 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005309 if (pkgname == NULL) {
5310 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5311 if (pkgname_or_unknown == NULL) {
5312 Py_XDECREF(pkgpath);
5313 return NULL;
5314 }
5315 } else {
5316 pkgname_or_unknown = pkgname;
5317 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005318
5319 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005320 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005321 errmsg = PyUnicode_FromFormat(
5322 "cannot import name %R from %R (unknown location)",
5323 name, pkgname_or_unknown
5324 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005325 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005326 PyErr_SetImportError(errmsg, pkgname, NULL);
5327 }
5328 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005329 _Py_IDENTIFIER(__spec__);
5330 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005331 const char *fmt =
5332 _PyModuleSpec_IsInitializing(spec) ?
5333 "cannot import name %R from partially initialized module %R "
5334 "(most likely due to a circular import) (%S)" :
5335 "cannot import name %R from %R (%S)";
5336 Py_XDECREF(spec);
5337
5338 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005339 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005340 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005341 }
5342
Xiang Zhang4830f582017-03-21 11:13:42 +08005343 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005344 Py_XDECREF(pkgname_or_unknown);
5345 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005346 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005347}
Guido van Rossumac7be682001-01-17 15:42:30 +00005348
Thomas Wouters52152252000-08-17 22:55:00 +00005349static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005350import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005351{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005352 _Py_IDENTIFIER(__all__);
5353 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005354 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005355 int skip_leading_underscores = 0;
5356 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005357
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005358 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5359 return -1; /* Unexpected error */
5360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005362 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5363 return -1;
5364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005367 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 return -1;
5369 }
5370 all = PyMapping_Keys(dict);
5371 Py_DECREF(dict);
5372 if (all == NULL)
5373 return -1;
5374 skip_leading_underscores = 1;
5375 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 for (pos = 0, err = 0; ; pos++) {
5378 name = PySequence_GetItem(all, pos);
5379 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005382 }
5383 else {
5384 _PyErr_Clear(tstate);
5385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 break;
5387 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005388 if (!PyUnicode_Check(name)) {
5389 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5390 if (modname == NULL) {
5391 Py_DECREF(name);
5392 err = -1;
5393 break;
5394 }
5395 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005396 _PyErr_Format(tstate, PyExc_TypeError,
5397 "module __name__ must be a string, not %.100s",
5398 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005399 }
5400 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005401 _PyErr_Format(tstate, PyExc_TypeError,
5402 "%s in %U.%s must be str, not %.100s",
5403 skip_leading_underscores ? "Key" : "Item",
5404 modname,
5405 skip_leading_underscores ? "__dict__" : "__all__",
5406 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005407 }
5408 Py_DECREF(modname);
5409 Py_DECREF(name);
5410 err = -1;
5411 break;
5412 }
5413 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005414 if (PyUnicode_READY(name) == -1) {
5415 Py_DECREF(name);
5416 err = -1;
5417 break;
5418 }
5419 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5420 Py_DECREF(name);
5421 continue;
5422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 }
5424 value = PyObject_GetAttr(v, name);
5425 if (value == NULL)
5426 err = -1;
5427 else if (PyDict_CheckExact(locals))
5428 err = PyDict_SetItem(locals, name, value);
5429 else
5430 err = PyObject_SetItem(locals, name, value);
5431 Py_DECREF(name);
5432 Py_XDECREF(value);
5433 if (err != 0)
5434 break;
5435 }
5436 Py_DECREF(all);
5437 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005438}
5439
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005440static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005441check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005442{
Victor Stinnera102ed72020-02-07 02:24:48 +01005443 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005444 /* check_args_iterable() may be called with a live exception:
5445 * clear it to prevent calling _PyObject_FunctionStr() with an
5446 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005447 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005448 PyObject *funcstr = _PyObject_FunctionStr(func);
5449 if (funcstr != NULL) {
5450 _PyErr_Format(tstate, PyExc_TypeError,
5451 "%U argument after * must be an iterable, not %.200s",
5452 funcstr, Py_TYPE(args)->tp_name);
5453 Py_DECREF(funcstr);
5454 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005455 return -1;
5456 }
5457 return 0;
5458}
5459
5460static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005461format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005462{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005463 /* _PyDict_MergeEx raises attribute
5464 * error (percolated from an attempt
5465 * to get 'keys' attribute) instead of
5466 * a type error if its second argument
5467 * is not a mapping.
5468 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005469 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005470 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005471 PyObject *funcstr = _PyObject_FunctionStr(func);
5472 if (funcstr != NULL) {
5473 _PyErr_Format(
5474 tstate, PyExc_TypeError,
5475 "%U argument after ** must be a mapping, not %.200s",
5476 funcstr, Py_TYPE(kwargs)->tp_name);
5477 Py_DECREF(funcstr);
5478 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005479 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005480 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005481 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005482 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005483 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005484 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005485 PyObject *funcstr = _PyObject_FunctionStr(func);
5486 if (funcstr != NULL) {
5487 PyObject *key = PyTuple_GET_ITEM(val, 0);
5488 _PyErr_Format(
5489 tstate, PyExc_TypeError,
5490 "%U got multiple values for keyword argument '%S'",
5491 funcstr, key);
5492 Py_DECREF(funcstr);
5493 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005494 Py_XDECREF(exc);
5495 Py_XDECREF(val);
5496 Py_XDECREF(tb);
5497 }
5498 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005499 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005500 }
5501 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005502}
5503
Guido van Rossumac7be682001-01-17 15:42:30 +00005504static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005505format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5506 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005508 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005510 if (!obj)
5511 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005512
Serhiy Storchaka06515832016-11-20 09:13:07 +02005513 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005514 if (!obj_str)
5515 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005516
Victor Stinner438a12d2019-05-24 17:01:38 +02005517 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005518}
Guido van Rossum950361c1997-01-24 13:49:28 +00005519
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005520static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005521format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005522{
5523 PyObject *name;
5524 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005525 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005526 return;
5527 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5528 name = PyTuple_GET_ITEM(co->co_cellvars,
5529 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005530 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005531 PyExc_UnboundLocalError,
5532 UNBOUNDLOCAL_ERROR_MSG,
5533 name);
5534 } else {
5535 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5536 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005537 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005538 UNBOUNDFREE_ERROR_MSG, name);
5539 }
5540}
5541
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005542static void
Mark Shannonfee55262019-11-21 09:11:43 +00005543format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005544{
5545 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5546 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005547 _PyErr_Format(tstate, PyExc_TypeError,
5548 "'async with' received an object from __aenter__ "
5549 "that does not implement __await__: %.100s",
5550 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005551 }
Mark Shannonfee55262019-11-21 09:11:43 +00005552 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005553 _PyErr_Format(tstate, PyExc_TypeError,
5554 "'async with' received an object from __aexit__ "
5555 "that does not implement __await__: %.100s",
5556 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005557 }
5558 }
5559}
5560
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005561static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005562unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005563 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005564{
5565 PyObject *res;
5566 if (Py_REFCNT(v) == 2) {
5567 /* In the common case, there are 2 references to the value
5568 * stored in 'variable' when the += is performed: one on the
5569 * value stack (in 'v') and one still stored in the
5570 * 'variable'. We try to delete the variable now to reduce
5571 * the refcnt to 1.
5572 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005573 int opcode, oparg;
5574 NEXTOPARG();
5575 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005576 case STORE_FAST:
5577 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005578 PyObject **fastlocals = f->f_localsplus;
5579 if (GETLOCAL(oparg) == v)
5580 SETLOCAL(oparg, NULL);
5581 break;
5582 }
5583 case STORE_DEREF:
5584 {
5585 PyObject **freevars = (f->f_localsplus +
5586 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005587 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005588 if (PyCell_GET(c) == v) {
5589 PyCell_SET(c, NULL);
5590 Py_DECREF(v);
5591 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005592 break;
5593 }
5594 case STORE_NAME:
5595 {
5596 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005597 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005598 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005599 if (locals && PyDict_CheckExact(locals)) {
5600 PyObject *w = PyDict_GetItemWithError(locals, name);
5601 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005602 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005603 {
5604 Py_DECREF(v);
5605 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005606 }
5607 }
5608 break;
5609 }
5610 }
5611 }
5612 res = v;
5613 PyUnicode_Append(&res, w);
5614 return res;
5615}
5616
Guido van Rossum950361c1997-01-24 13:49:28 +00005617#ifdef DYNAMIC_EXECUTION_PROFILE
5618
Skip Montanarof118cb12001-10-15 20:51:38 +00005619static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005620getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005622 int i;
5623 PyObject *l = PyList_New(256);
5624 if (l == NULL) return NULL;
5625 for (i = 0; i < 256; i++) {
5626 PyObject *x = PyLong_FromLong(a[i]);
5627 if (x == NULL) {
5628 Py_DECREF(l);
5629 return NULL;
5630 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005631 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 }
5633 for (i = 0; i < 256; i++)
5634 a[i] = 0;
5635 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005636}
5637
5638PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005639_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005640{
5641#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005642 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005643#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005644 int i;
5645 PyObject *l = PyList_New(257);
5646 if (l == NULL) return NULL;
5647 for (i = 0; i < 257; i++) {
5648 PyObject *x = getarray(dxpairs[i]);
5649 if (x == NULL) {
5650 Py_DECREF(l);
5651 return NULL;
5652 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005653 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005654 }
5655 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005656#endif
5657}
5658
5659#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005660
5661Py_ssize_t
5662_PyEval_RequestCodeExtraIndex(freefunc free)
5663{
Victor Stinner81a7be32020-04-14 15:14:01 +02005664 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005665 Py_ssize_t new_index;
5666
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005667 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005668 return -1;
5669 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005670 new_index = interp->co_extra_user_count++;
5671 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005672 return new_index;
5673}
Łukasz Langaa785c872016-09-09 17:37:37 -07005674
5675static void
5676dtrace_function_entry(PyFrameObject *f)
5677{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005678 const char *filename;
5679 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005680 int lineno;
5681
Victor Stinner6d86a232020-04-29 00:56:58 +02005682 PyCodeObject *code = f->f_code;
5683 filename = PyUnicode_AsUTF8(code->co_filename);
5684 funcname = PyUnicode_AsUTF8(code->co_name);
5685 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005686
Andy Lestere6be9b52020-02-11 20:28:35 -06005687 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005688}
5689
5690static void
5691dtrace_function_return(PyFrameObject *f)
5692{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005693 const char *filename;
5694 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005695 int lineno;
5696
Victor Stinner6d86a232020-04-29 00:56:58 +02005697 PyCodeObject *code = f->f_code;
5698 filename = PyUnicode_AsUTF8(code->co_filename);
5699 funcname = PyUnicode_AsUTF8(code->co_name);
5700 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005701
Andy Lestere6be9b52020-02-11 20:28:35 -06005702 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005703}
5704
5705/* DTrace equivalent of maybe_call_line_trace. */
5706static void
5707maybe_dtrace_line(PyFrameObject *frame,
5708 int *instr_lb, int *instr_ub, int *instr_prev)
5709{
5710 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005711 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005712
5713 /* If the last instruction executed isn't in the current
5714 instruction window, reset the window.
5715 */
5716 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5717 PyAddrPair bounds;
5718 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5719 &bounds);
5720 *instr_lb = bounds.ap_lower;
5721 *instr_ub = bounds.ap_upper;
5722 }
5723 /* If the last instruction falls at the start of a line or if
5724 it represents a jump backwards, update the frame's line
5725 number and call the trace function. */
5726 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5727 frame->f_lineno = line;
5728 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5729 if (!co_filename)
5730 co_filename = "?";
5731 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5732 if (!co_name)
5733 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005734 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005735 }
5736 *instr_prev = frame->f_lasti;
5737}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005738
5739
5740/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5741 for the limited API. */
5742
5743#undef Py_EnterRecursiveCall
5744
5745int Py_EnterRecursiveCall(const char *where)
5746{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005747 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005748}
5749
5750#undef Py_LeaveRecursiveCall
5751
5752void Py_LeaveRecursiveCall(void)
5753{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005754 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005755}