blob: 762de577e6b550521e438af265b5799367aefcde [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->overflowed) {
818 if (tstate->recursion_depth > recursion_limit + 50) {
819 /* Overflowing while handling an overflow. Give up. */
820 Py_FatalError("Cannot recover from stack overflow.");
821 }
822 return 0;
823 }
824 if (tstate->recursion_depth > recursion_limit) {
825 --tstate->recursion_depth;
826 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200827 _PyErr_Format(tstate, PyExc_RecursionError,
828 "maximum recursion depth exceeded%s",
829 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 return -1;
831 }
832 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000833}
834
Victor Stinner09532fe2019-05-10 23:39:09 +0200835static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200836static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000837
Victor Stinnere225beb2019-06-03 18:14:24 +0200838#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000839
Guido van Rossum374a9221991-04-04 10:40:29 +0000840
Guido van Rossumb209a111997-04-29 18:18:01 +0000841PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000842PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 return PyEval_EvalCodeEx(co,
845 globals, locals,
846 (PyObject **)NULL, 0,
847 (PyObject **)NULL, 0,
848 (PyObject **)NULL, 0,
849 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000850}
851
852
853/* Interpreter main loop */
854
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000855PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100856PyEval_EvalFrame(PyFrameObject *f)
857{
Victor Stinner0b72b232020-03-12 23:18:39 +0100858 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100859 PyThreadState *tstate = _PyThreadState_GET();
860 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000861}
862
863PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000865{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100866 PyThreadState *tstate = _PyThreadState_GET();
867 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700868}
869
Victor Stinnerda2914d2020-03-20 09:29:08 +0100870
871/* Handle signals, pending calls, GIL drop request
872 and asynchronous exception */
873static int
874eval_frame_handle_pending(PyThreadState *tstate)
875{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100876 _PyRuntimeState * const runtime = &_PyRuntime;
877 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200878
879 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200880 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100881 if (handle_signals(tstate) != 0) {
882 return -1;
883 }
884 }
885
886 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200887 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100888 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
889 if (make_pending_calls(tstate) != 0) {
890 return -1;
891 }
892 }
893
894 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200895 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100896 /* Give another thread a chance */
897 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
898 Py_FatalError("tstate mix-up");
899 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200900 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100901
902 /* Other threads may run now */
903
904 take_gil(tstate);
905
Victor Stinnere838a932020-05-05 19:56:48 +0200906#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
907 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
908#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100909 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
910 Py_FatalError("orphan tstate");
911 }
Victor Stinnere838a932020-05-05 19:56:48 +0200912#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100913 }
914
915 /* Check for asynchronous exception. */
916 if (tstate->async_exc != NULL) {
917 PyObject *exc = tstate->async_exc;
918 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200919 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100920 _PyErr_SetNone(tstate, exc);
921 Py_DECREF(exc);
922 return -1;
923 }
924
925 return 0;
926}
927
Victor Stinnerc6944e72016-11-11 02:13:35 +0100928PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100929_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700930{
Victor Stinner3026cad2020-06-01 16:02:40 +0200931 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100932
Guido van Rossum950361c1997-01-24 13:49:28 +0000933#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000935#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200936 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300937 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200938 int opcode; /* Current opcode */
939 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200940 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100942 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100943 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 is true when the line being executed has changed. The
951 initial values are such as to make this false the first
952 time it is tested. */
953 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000954
Serhiy Storchakaab874002016-09-11 13:48:15 +0300955 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyObject *names;
957 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900958 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000959
Brett Cannon368b4b72012-04-02 12:17:59 -0400960#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200961 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400962#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200963
Antoine Pitroub52ec782009-01-25 16:34:23 +0000964/* Computed GOTOs, or
965 the-optimization-commonly-but-improperly-known-as-"threaded code"
966 using gcc's labels-as-values extension
967 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
968
969 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000971 combined with a lookup table of jump addresses. However, since the
972 indirect jump instruction is shared by all opcodes, the CPU will have a
973 hard time making the right prediction for where to jump next (actually,
974 it will be always wrong except in the uncommon case of a sequence of
975 several identical opcodes).
976
977 "Threaded code" in contrast, uses an explicit jump table and an explicit
978 indirect jump instruction at the end of each opcode. Since the jump
979 instruction is at a different address for each opcode, the CPU will make a
980 separate prediction for each of these instructions, which is equivalent to
981 predicting the second opcode of each opcode pair. These predictions have
982 a much better chance to turn out valid, especially in small bytecode loops.
983
984 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000986 and potentially many more instructions (depending on the pipeline width).
987 A correctly predicted branch, however, is nearly free.
988
989 At the time of this writing, the "threaded code" version is up to 15-20%
990 faster than the normal "switch" version, depending on the compiler and the
991 CPU architecture.
992
993 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
994 because it would render the measurements invalid.
995
996
997 NOTE: care must be taken that the compiler doesn't try to "optimize" the
998 indirect jumps by sharing them between all opcodes. Such optimizations
999 can be disabled on gcc by using the -fno-gcse flag (or possibly
1000 -fno-crossjumping).
1001*/
1002
Antoine Pitrou042b1282010-08-13 21:15:58 +00001003#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001004#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001005#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001006#endif
1007
Antoine Pitrou042b1282010-08-13 21:15:58 +00001008#ifdef HAVE_COMPUTED_GOTOS
1009 #ifndef USE_COMPUTED_GOTOS
1010 #define USE_COMPUTED_GOTOS 1
1011 #endif
1012#else
1013 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1014 #error "Computed gotos are not supported on this compiler."
1015 #endif
1016 #undef USE_COMPUTED_GOTOS
1017 #define USE_COMPUTED_GOTOS 0
1018#endif
1019
1020#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021/* Import the static jump table */
1022#include "opcode_targets.h"
1023
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001025 op: \
1026 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001027
Antoine Pitroub52ec782009-01-25 16:34:23 +00001028#ifdef LLTRACE
1029#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001031 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001033 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001034 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 } \
1036 goto fast_next_opcode; \
1037 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001038#else
1039#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001041 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001043 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001044 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 } \
1046 goto fast_next_opcode; \
1047 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001048#endif
1049
Victor Stinner09532fe2019-05-10 23:39:09 +02001050#define DISPATCH() \
1051 { \
1052 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1053 FAST_DISPATCH(); \
1054 } \
1055 continue; \
1056 }
1057
Antoine Pitroub52ec782009-01-25 16:34:23 +00001058#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001059#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001060#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001061#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001062#endif
1063
1064
Neal Norwitza81d2202002-07-14 00:27:26 +00001065/* Tuple access macros */
1066
1067#ifndef Py_DEBUG
1068#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1069#else
1070#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1071#endif
1072
Guido van Rossum374a9221991-04-04 10:40:29 +00001073/* Code access macros */
1074
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001075/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001076#define INSTR_OFFSET() \
1077 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001078#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001079 _Py_CODEUNIT word = *next_instr; \
1080 opcode = _Py_OPCODE(word); \
1081 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001082 next_instr++; \
1083 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001084#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1085#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001086
Raymond Hettingerf606f872003-03-16 03:11:04 +00001087/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 Some opcodes tend to come in pairs thus making it possible to
1089 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001090 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 Verifying the prediction costs a single high-speed test of a register
1093 variable against a constant. If the pairing was good, then the
1094 processor's own internal branch predication has a high likelihood of
1095 success, resulting in a nearly zero-overhead transition to the
1096 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001097 including its unpredictable switch-case branch. Combined with the
1098 processor's internal branch prediction, a successful PREDICT has the
1099 effect of making the two opcodes run as if they were a single new opcode
1100 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001101
Georg Brandl86b2fb92008-07-16 03:43:04 +00001102 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 predictions turned-on and interpret the results as if some opcodes
1104 had been combined or turn-off predictions so that the opcode frequency
1105 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001106
1107 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 the CPU to record separate branch prediction information for each
1109 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001110
Raymond Hettingerf606f872003-03-16 03:11:04 +00001111*/
1112
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001113#define PREDICT_ID(op) PRED_##op
1114
Antoine Pitrou042b1282010-08-13 21:15:58 +00001115#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001116#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001117#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001118#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001119 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001120 _Py_CODEUNIT word = *next_instr; \
1121 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001122 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001123 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001124 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001125 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001126 } \
1127 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001128#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001129#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001130
Raymond Hettingerf606f872003-03-16 03:11:04 +00001131
Guido van Rossum374a9221991-04-04 10:40:29 +00001132/* Stack manipulation macros */
1133
Martin v. Löwis18e16552006-02-15 17:27:45 +00001134/* The stack can grow at most MAXINT deep, as co_nlocals and
1135 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001136#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1137#define EMPTY() (STACK_LEVEL() == 0)
1138#define TOP() (stack_pointer[-1])
1139#define SECOND() (stack_pointer[-2])
1140#define THIRD() (stack_pointer[-3])
1141#define FOURTH() (stack_pointer[-4])
1142#define PEEK(n) (stack_pointer[-(n)])
1143#define SET_TOP(v) (stack_pointer[-1] = (v))
1144#define SET_SECOND(v) (stack_pointer[-2] = (v))
1145#define SET_THIRD(v) (stack_pointer[-3] = (v))
1146#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001147#define BASIC_STACKADJ(n) (stack_pointer += n)
1148#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1149#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001150
Guido van Rossum96a42c81992-01-12 02:29:51 +00001151#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001153 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001154 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001155#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001156 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001157#define STACK_GROW(n) do { \
1158 assert(n >= 0); \
1159 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001160 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001161 assert(STACK_LEVEL() <= co->co_stacksize); \
1162 } while (0)
1163#define STACK_SHRINK(n) do { \
1164 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001165 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001166 (void)(BASIC_STACKADJ(-n)); \
1167 assert(STACK_LEVEL() <= co->co_stacksize); \
1168 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001169#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001170 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001171 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001172#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001173#define PUSH(v) BASIC_PUSH(v)
1174#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001175#define STACK_GROW(n) BASIC_STACKADJ(n)
1176#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001177#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001178#endif
1179
Guido van Rossum681d79a1995-07-18 14:51:37 +00001180/* Local variable macros */
1181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001183
1184/* The SETLOCAL() macro must not DECREF the local variable in-place and
1185 then store the new value; it must copy the old value to a temporary
1186 value, then store the new value, and then DECREF the temporary value.
1187 This is because it is possible that during the DECREF the frame is
1188 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1189 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001191 GETLOCAL(i) = value; \
1192 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001193
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001194
1195#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 while (STACK_LEVEL() > (b)->b_level) { \
1197 PyObject *v = POP(); \
1198 Py_XDECREF(v); \
1199 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001200
1201#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001202 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001204 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1206 while (STACK_LEVEL() > (b)->b_level + 3) { \
1207 value = POP(); \
1208 Py_XDECREF(value); \
1209 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001210 exc_info = tstate->exc_info; \
1211 type = exc_info->exc_type; \
1212 value = exc_info->exc_value; \
1213 traceback = exc_info->exc_traceback; \
1214 exc_info->exc_type = POP(); \
1215 exc_info->exc_value = POP(); \
1216 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 Py_XDECREF(type); \
1218 Py_XDECREF(value); \
1219 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001220 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001221
Inada Naoki91234a12019-06-03 21:30:58 +09001222 /* macros for opcode cache */
1223#define OPCACHE_CHECK() \
1224 do { \
1225 co_opcache = NULL; \
1226 if (co->co_opcache != NULL) { \
1227 unsigned char co_opt_offset = \
1228 co->co_opcache_map[next_instr - first_instr]; \
1229 if (co_opt_offset > 0) { \
1230 assert(co_opt_offset <= co->co_opcache_size); \
1231 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1232 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001233 } \
1234 } \
1235 } while (0)
1236
1237#if OPCACHE_STATS
1238
1239#define OPCACHE_STAT_GLOBAL_HIT() \
1240 do { \
1241 if (co->co_opcache != NULL) opcache_global_hits++; \
1242 } while (0)
1243
1244#define OPCACHE_STAT_GLOBAL_MISS() \
1245 do { \
1246 if (co->co_opcache != NULL) opcache_global_misses++; \
1247 } while (0)
1248
1249#define OPCACHE_STAT_GLOBAL_OPT() \
1250 do { \
1251 if (co->co_opcache != NULL) opcache_global_opts++; \
1252 } while (0)
1253
1254#else /* OPCACHE_STATS */
1255
1256#define OPCACHE_STAT_GLOBAL_HIT()
1257#define OPCACHE_STAT_GLOBAL_MISS()
1258#define OPCACHE_STAT_GLOBAL_OPT()
1259
1260#endif
1261
Guido van Rossuma027efa1997-05-05 20:56:21 +00001262/* Start of code */
1263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001265 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001267 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if (tstate->use_tracing) {
1272 if (tstate->c_tracefunc != NULL) {
1273 /* tstate->c_tracefunc, if defined, is a
1274 function that will be called on *every* entry
1275 to a code block. Its return value, if not
1276 None, is a function that will be called at
1277 the start of each executed line of code.
1278 (Actually, the function must return itself
1279 in order to continue tracing.) The trace
1280 functions are called with three arguments:
1281 a pointer to the current frame, a string
1282 indicating why the function is called, and
1283 an argument which depends on the situation.
1284 The global trace function is also called
1285 whenever an exception is detected. */
1286 if (call_trace_protected(tstate->c_tracefunc,
1287 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001288 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* Trace function raised an error */
1290 goto exit_eval_frame;
1291 }
1292 }
1293 if (tstate->c_profilefunc != NULL) {
1294 /* Similar for c_profilefunc, except it needn't
1295 return itself and isn't called for "line" events */
1296 if (call_trace_protected(tstate->c_profilefunc,
1297 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001298 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* Profile function raised an error */
1300 goto exit_eval_frame;
1301 }
1302 }
1303 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001304
Łukasz Langaa785c872016-09-09 17:37:37 -07001305 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1306 dtrace_function_entry(f);
1307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 co = f->f_code;
1309 names = co->co_names;
1310 consts = co->co_consts;
1311 fastlocals = f->f_localsplus;
1312 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001313 assert(PyBytes_Check(co->co_code));
1314 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001315 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1316 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1317 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001318 /*
1319 f->f_lasti refers to the index of the last instruction,
1320 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001321
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001322 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001323 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 When the PREDICT() macros are enabled, some opcode pairs follow in
1326 direct succession without updating f->f_lasti. A successful
1327 prediction effectively links the two codes together as if they
1328 were a single new opcode; accordingly,f->f_lasti will point to
1329 the first code in the pair (for instance, GET_ITER followed by
1330 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001331 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001333 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001334 next_instr = first_instr;
1335 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001336 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1337 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001338 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001339 stack_pointer = f->f_valuestack + f->f_stackdepth;
1340 /* Set f->f_stackdepth to -1.
1341 * Update when returning or calling trace function.
1342 Having f_stackdepth <= 0 ensures that invalid
1343 values are not visible to the cycle GC.
1344 We choose -1 rather than 0 to assist debugging.
1345 */
1346 f->f_stackdepth = -1;
1347 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001348
Inada Naoki91234a12019-06-03 21:30:58 +09001349 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1350 co->co_opcache_flag++;
1351 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1352 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001353 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001354 }
1355#if OPCACHE_STATS
1356 opcache_code_objects_extra_mem +=
1357 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1358 sizeof(_PyOpcache) * co->co_opcache_size;
1359 opcache_code_objects++;
1360#endif
1361 }
1362 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001363
Tim Peters5ca576e2001-06-18 22:08:13 +00001364#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001365 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001366#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001368 if (throwflag) /* support for generator.throw() */
1369 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370
Victor Stinnerace47d72013-07-18 01:41:08 +02001371#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001372 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001373 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001374 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001375 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001376#endif
1377
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001378main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1381 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001382 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* Do periodic things. Doing this every time through
1385 the loop would add too much overhead, so we do it
1386 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001387 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 event needs attention (e.g. a signal handler or
1389 async I/O handler); see Py_AddPendingCall() and
1390 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Eric Snow7bda9de2019-03-08 17:25:54 -07001392 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001393 opcode = _Py_OPCODE(*next_instr);
1394 if (opcode == SETUP_FINALLY ||
1395 opcode == SETUP_WITH ||
1396 opcode == BEFORE_ASYNC_WITH ||
1397 opcode == YIELD_FROM) {
1398 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001399 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001400 - If we're about to enter the 'with:'. It will prevent
1401 emitting a resource warning in the common idiom
1402 'with open(path) as file:'.
1403 - If we're about to enter the 'async with:'.
1404 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001405 *very* useful, but might help in some cases and it's
1406 traditional)
1407 - If we're resuming a chain of nested 'yield from' or
1408 'await' calls, then each frame is parked with YIELD_FROM
1409 as its next opcode. If the user hit control-C we want to
1410 wait until we've reached the innermost frame before
1411 running the signal handler and raising KeyboardInterrupt
1412 (see bpo-30039).
1413 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 goto fast_next_opcode;
1415 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001416
Victor Stinnerda2914d2020-03-20 09:29:08 +01001417 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 }
1420 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 fast_next_opcode:
1423 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Łukasz Langaa785c872016-09-09 17:37:37 -07001425 if (PyDTrace_LINE_ENABLED())
1426 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001429
Victor Stinnerdab84232020-03-17 18:56:44 +01001430 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001431 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001433 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001435 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 err = maybe_call_line_trace(tstate->c_tracefunc,
1438 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001439 tstate, f,
1440 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* Reload possibly changed frame fields */
1442 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001443 stack_pointer = f->f_valuestack+f->f_stackdepth;
1444 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001451
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001452 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001453 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001454#ifdef DYNAMIC_EXECUTION_PROFILE
1455#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 dxpairs[lastopcode][opcode]++;
1457 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001460#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001461
Guido van Rossum96a42c81992-01-12 02:29:51 +00001462#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (lltrace) {
1466 if (HAS_ARG(opcode)) {
1467 printf("%d: %d, %d\n",
1468 f->f_lasti, opcode, oparg);
1469 }
1470 else {
1471 printf("%d: %d\n",
1472 f->f_lasti, opcode);
1473 }
1474 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001475#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001480 It is essential that any operation that fails must goto error
1481 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Petersonddd19492018-09-16 22:38:02 -07001483 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001486
Benjamin Petersonddd19492018-09-16 22:38:02 -07001487 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 PyObject *value = GETLOCAL(oparg);
1489 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001490 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 UNBOUNDLOCAL_ERROR_MSG,
1492 PyTuple_GetItem(co->co_varnames, oparg));
1493 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 Py_INCREF(value);
1496 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 }
1499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(LOAD_CONST): {
1501 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *value = GETITEM(consts, oparg);
1503 Py_INCREF(value);
1504 PUSH(value);
1505 FAST_DISPATCH();
1506 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001507
Benjamin Petersonddd19492018-09-16 22:38:02 -07001508 case TARGET(STORE_FAST): {
1509 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 PyObject *value = POP();
1511 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001514
Benjamin Petersonddd19492018-09-16 22:38:02 -07001515 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 PyObject *value = POP();
1517 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *top = TOP();
1523 PyObject *second = SECOND();
1524 SET_TOP(second);
1525 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Petersonddd19492018-09-16 22:38:02 -07001529 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 PyObject *top = TOP();
1531 PyObject *second = SECOND();
1532 PyObject *third = THIRD();
1533 SET_TOP(second);
1534 SET_SECOND(third);
1535 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001540 PyObject *top = TOP();
1541 PyObject *second = SECOND();
1542 PyObject *third = THIRD();
1543 PyObject *fourth = FOURTH();
1544 SET_TOP(second);
1545 SET_SECOND(third);
1546 SET_THIRD(fourth);
1547 SET_FOURTH(top);
1548 FAST_DISPATCH();
1549 }
1550
Benjamin Petersonddd19492018-09-16 22:38:02 -07001551 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001552 PyObject *top = TOP();
1553 Py_INCREF(top);
1554 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001557
Benjamin Petersonddd19492018-09-16 22:38:02 -07001558 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001561 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001563 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001564 SET_TOP(top);
1565 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001566 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001568
Benjamin Petersonddd19492018-09-16 22:38:02 -07001569 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 PyObject *value = TOP();
1571 PyObject *res = PyNumber_Positive(value);
1572 Py_DECREF(value);
1573 SET_TOP(res);
1574 if (res == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Benjamin Petersonddd19492018-09-16 22:38:02 -07001579 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 PyObject *value = TOP();
1581 PyObject *res = PyNumber_Negative(value);
1582 Py_DECREF(value);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Benjamin Petersonddd19492018-09-16 22:38:02 -07001589 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 PyObject *value = TOP();
1591 int err = PyObject_IsTrue(value);
1592 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (err == 0) {
1594 Py_INCREF(Py_True);
1595 SET_TOP(Py_True);
1596 DISPATCH();
1597 }
1598 else if (err > 0) {
1599 Py_INCREF(Py_False);
1600 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 DISPATCH();
1602 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001603 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 goto error;
1605 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Petersonddd19492018-09-16 22:38:02 -07001607 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 PyObject *value = TOP();
1609 PyObject *res = PyNumber_Invert(value);
1610 Py_DECREF(value);
1611 SET_TOP(res);
1612 if (res == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Benjamin Petersonddd19492018-09-16 22:38:02 -07001617 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 PyObject *exp = POP();
1619 PyObject *base = TOP();
1620 PyObject *res = PyNumber_Power(base, exp, Py_None);
1621 Py_DECREF(base);
1622 Py_DECREF(exp);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Benjamin Petersonddd19492018-09-16 22:38:02 -07001629 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 PyObject *right = POP();
1631 PyObject *left = TOP();
1632 PyObject *res = PyNumber_Multiply(left, right);
1633 Py_DECREF(left);
1634 Py_DECREF(right);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Petersonddd19492018-09-16 22:38:02 -07001641 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *res = PyNumber_MatrixMultiply(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(res);
1648 if (res == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
1652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *divisor = POP();
1655 PyObject *dividend = TOP();
1656 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1657 Py_DECREF(dividend);
1658 Py_DECREF(divisor);
1659 SET_TOP(quotient);
1660 if (quotient == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Benjamin Petersonddd19492018-09-16 22:38:02 -07001665 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 PyObject *divisor = POP();
1667 PyObject *dividend = TOP();
1668 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1669 Py_DECREF(dividend);
1670 Py_DECREF(divisor);
1671 SET_TOP(quotient);
1672 if (quotient == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001676
Benjamin Petersonddd19492018-09-16 22:38:02 -07001677 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 PyObject *divisor = POP();
1679 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001680 PyObject *res;
1681 if (PyUnicode_CheckExact(dividend) && (
1682 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1683 // fast path; string formatting, but not if the RHS is a str subclass
1684 // (see issue28598)
1685 res = PyUnicode_Format(dividend, divisor);
1686 } else {
1687 res = PyNumber_Remainder(dividend, divisor);
1688 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 Py_DECREF(divisor);
1690 Py_DECREF(dividend);
1691 SET_TOP(res);
1692 if (res == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 PyObject *right = POP();
1699 PyObject *left = TOP();
1700 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001701 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001702 CPython using bytecode, it is simply worthless.
1703 See http://bugs.python.org/issue21955 and
1704 http://bugs.python.org/issue10044 for the discussion. In short,
1705 no patch shown any impact on a realistic benchmark, only a minor
1706 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 if (PyUnicode_CheckExact(left) &&
1708 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001709 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001710 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001711 }
1712 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 sum = PyNumber_Add(left, right);
1714 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001715 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 Py_DECREF(right);
1717 SET_TOP(sum);
1718 if (sum == NULL)
1719 goto error;
1720 DISPATCH();
1721 }
1722
Benjamin Petersonddd19492018-09-16 22:38:02 -07001723 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 PyObject *right = POP();
1725 PyObject *left = TOP();
1726 PyObject *diff = PyNumber_Subtract(left, right);
1727 Py_DECREF(right);
1728 Py_DECREF(left);
1729 SET_TOP(diff);
1730 if (diff == NULL)
1731 goto error;
1732 DISPATCH();
1733 }
1734
Benjamin Petersonddd19492018-09-16 22:38:02 -07001735 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 PyObject *sub = POP();
1737 PyObject *container = TOP();
1738 PyObject *res = PyObject_GetItem(container, sub);
1739 Py_DECREF(container);
1740 Py_DECREF(sub);
1741 SET_TOP(res);
1742 if (res == NULL)
1743 goto error;
1744 DISPATCH();
1745 }
1746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 PyObject *right = POP();
1749 PyObject *left = TOP();
1750 PyObject *res = PyNumber_Lshift(left, right);
1751 Py_DECREF(left);
1752 Py_DECREF(right);
1753 SET_TOP(res);
1754 if (res == NULL)
1755 goto error;
1756 DISPATCH();
1757 }
1758
Benjamin Petersonddd19492018-09-16 22:38:02 -07001759 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 PyObject *right = POP();
1761 PyObject *left = TOP();
1762 PyObject *res = PyNumber_Rshift(left, right);
1763 Py_DECREF(left);
1764 Py_DECREF(right);
1765 SET_TOP(res);
1766 if (res == NULL)
1767 goto error;
1768 DISPATCH();
1769 }
1770
Benjamin Petersonddd19492018-09-16 22:38:02 -07001771 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 PyObject *right = POP();
1773 PyObject *left = TOP();
1774 PyObject *res = PyNumber_And(left, right);
1775 Py_DECREF(left);
1776 Py_DECREF(right);
1777 SET_TOP(res);
1778 if (res == NULL)
1779 goto error;
1780 DISPATCH();
1781 }
1782
Benjamin Petersonddd19492018-09-16 22:38:02 -07001783 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 PyObject *right = POP();
1785 PyObject *left = TOP();
1786 PyObject *res = PyNumber_Xor(left, right);
1787 Py_DECREF(left);
1788 Py_DECREF(right);
1789 SET_TOP(res);
1790 if (res == NULL)
1791 goto error;
1792 DISPATCH();
1793 }
1794
Benjamin Petersonddd19492018-09-16 22:38:02 -07001795 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001796 PyObject *right = POP();
1797 PyObject *left = TOP();
1798 PyObject *res = PyNumber_Or(left, right);
1799 Py_DECREF(left);
1800 Py_DECREF(right);
1801 SET_TOP(res);
1802 if (res == NULL)
1803 goto error;
1804 DISPATCH();
1805 }
1806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 PyObject *v = POP();
1809 PyObject *list = PEEK(oparg);
1810 int err;
1811 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 if (err != 0)
1814 goto error;
1815 PREDICT(JUMP_ABSOLUTE);
1816 DISPATCH();
1817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Benjamin Petersonddd19492018-09-16 22:38:02 -07001819 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001821 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 int err;
1823 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 if (err != 0)
1826 goto error;
1827 PREDICT(JUMP_ABSOLUTE);
1828 DISPATCH();
1829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Benjamin Petersonddd19492018-09-16 22:38:02 -07001831 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 PyObject *exp = POP();
1833 PyObject *base = TOP();
1834 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1835 Py_DECREF(base);
1836 Py_DECREF(exp);
1837 SET_TOP(res);
1838 if (res == NULL)
1839 goto error;
1840 DISPATCH();
1841 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001842
Benjamin Petersonddd19492018-09-16 22:38:02 -07001843 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 PyObject *right = POP();
1845 PyObject *left = TOP();
1846 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1847 Py_DECREF(left);
1848 Py_DECREF(right);
1849 SET_TOP(res);
1850 if (res == NULL)
1851 goto error;
1852 DISPATCH();
1853 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001854
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001856 PyObject *right = POP();
1857 PyObject *left = TOP();
1858 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1859 Py_DECREF(left);
1860 Py_DECREF(right);
1861 SET_TOP(res);
1862 if (res == NULL)
1863 goto error;
1864 DISPATCH();
1865 }
1866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *divisor = POP();
1869 PyObject *dividend = TOP();
1870 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1871 Py_DECREF(dividend);
1872 Py_DECREF(divisor);
1873 SET_TOP(quotient);
1874 if (quotient == NULL)
1875 goto error;
1876 DISPATCH();
1877 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *divisor = POP();
1881 PyObject *dividend = TOP();
1882 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1883 Py_DECREF(dividend);
1884 Py_DECREF(divisor);
1885 SET_TOP(quotient);
1886 if (quotient == NULL)
1887 goto error;
1888 DISPATCH();
1889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *right = POP();
1893 PyObject *left = TOP();
1894 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1895 Py_DECREF(left);
1896 Py_DECREF(right);
1897 SET_TOP(mod);
1898 if (mod == NULL)
1899 goto error;
1900 DISPATCH();
1901 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 PyObject *right = POP();
1905 PyObject *left = TOP();
1906 PyObject *sum;
1907 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001908 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001909 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001910 }
1911 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 sum = PyNumber_InPlaceAdd(left, right);
1913 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001914 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 Py_DECREF(right);
1916 SET_TOP(sum);
1917 if (sum == NULL)
1918 goto error;
1919 DISPATCH();
1920 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001921
Benjamin Petersonddd19492018-09-16 22:38:02 -07001922 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 PyObject *right = POP();
1924 PyObject *left = TOP();
1925 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1926 Py_DECREF(left);
1927 Py_DECREF(right);
1928 SET_TOP(diff);
1929 if (diff == NULL)
1930 goto error;
1931 DISPATCH();
1932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Benjamin Petersonddd19492018-09-16 22:38:02 -07001934 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 PyObject *right = POP();
1936 PyObject *left = TOP();
1937 PyObject *res = PyNumber_InPlaceLshift(left, right);
1938 Py_DECREF(left);
1939 Py_DECREF(right);
1940 SET_TOP(res);
1941 if (res == NULL)
1942 goto error;
1943 DISPATCH();
1944 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001945
Benjamin Petersonddd19492018-09-16 22:38:02 -07001946 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 PyObject *right = POP();
1948 PyObject *left = TOP();
1949 PyObject *res = PyNumber_InPlaceRshift(left, right);
1950 Py_DECREF(left);
1951 Py_DECREF(right);
1952 SET_TOP(res);
1953 if (res == NULL)
1954 goto error;
1955 DISPATCH();
1956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001957
Benjamin Petersonddd19492018-09-16 22:38:02 -07001958 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 PyObject *right = POP();
1960 PyObject *left = TOP();
1961 PyObject *res = PyNumber_InPlaceAnd(left, right);
1962 Py_DECREF(left);
1963 Py_DECREF(right);
1964 SET_TOP(res);
1965 if (res == NULL)
1966 goto error;
1967 DISPATCH();
1968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Benjamin Petersonddd19492018-09-16 22:38:02 -07001970 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 PyObject *right = POP();
1972 PyObject *left = TOP();
1973 PyObject *res = PyNumber_InPlaceXor(left, right);
1974 Py_DECREF(left);
1975 Py_DECREF(right);
1976 SET_TOP(res);
1977 if (res == NULL)
1978 goto error;
1979 DISPATCH();
1980 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Benjamin Petersonddd19492018-09-16 22:38:02 -07001982 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 PyObject *right = POP();
1984 PyObject *left = TOP();
1985 PyObject *res = PyNumber_InPlaceOr(left, right);
1986 Py_DECREF(left);
1987 Py_DECREF(right);
1988 SET_TOP(res);
1989 if (res == NULL)
1990 goto error;
1991 DISPATCH();
1992 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 PyObject *sub = TOP();
1996 PyObject *container = SECOND();
1997 PyObject *v = THIRD();
1998 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001999 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002000 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 Py_DECREF(container);
2004 Py_DECREF(sub);
2005 if (err != 0)
2006 goto error;
2007 DISPATCH();
2008 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002009
Benjamin Petersonddd19492018-09-16 22:38:02 -07002010 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 PyObject *sub = TOP();
2012 PyObject *container = SECOND();
2013 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002014 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002015 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 err = PyObject_DelItem(container, sub);
2017 Py_DECREF(container);
2018 Py_DECREF(sub);
2019 if (err != 0)
2020 goto error;
2021 DISPATCH();
2022 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002025 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002027 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002028 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002030 _PyErr_SetString(tstate, PyExc_RuntimeError,
2031 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 Py_DECREF(value);
2033 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002035 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 Py_DECREF(value);
2037 if (res == NULL)
2038 goto error;
2039 Py_DECREF(res);
2040 DISPATCH();
2041 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002042
Benjamin Petersonddd19492018-09-16 22:38:02 -07002043 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 switch (oparg) {
2046 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002048 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002051 /* fall through */
2052 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002053 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002054 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 break;
2057 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002058 _PyErr_SetString(tstate, PyExc_SystemError,
2059 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 break;
2061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 goto error;
2063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Benjamin Petersonddd19492018-09-16 22:38:02 -07002065 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002067 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002068 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002069 f->f_state = FRAME_RETURNED;
2070 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002071 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002073
Benjamin Petersonddd19492018-09-16 22:38:02 -07002074 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002075 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002076 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002077 PyObject *obj = TOP();
2078 PyTypeObject *type = Py_TYPE(obj);
2079
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002080 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002081 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002082 }
Yury Selivanov75445082015-05-11 22:57:16 -04002083
2084 if (getter != NULL) {
2085 iter = (*getter)(obj);
2086 Py_DECREF(obj);
2087 if (iter == NULL) {
2088 SET_TOP(NULL);
2089 goto error;
2090 }
2091 }
2092 else {
2093 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002094 _PyErr_Format(tstate, PyExc_TypeError,
2095 "'async for' requires an object with "
2096 "__aiter__ method, got %.100s",
2097 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002098 Py_DECREF(obj);
2099 goto error;
2100 }
2101
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002102 if (Py_TYPE(iter)->tp_as_async == NULL ||
2103 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002104
Yury Selivanov398ff912017-03-02 22:20:00 -05002105 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002106 _PyErr_Format(tstate, PyExc_TypeError,
2107 "'async for' received an object from __aiter__ "
2108 "that does not implement __anext__: %.100s",
2109 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002110 Py_DECREF(iter);
2111 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002112 }
2113
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002114 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002115 DISPATCH();
2116 }
2117
Benjamin Petersonddd19492018-09-16 22:38:02 -07002118 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002119 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002120 PyObject *next_iter = NULL;
2121 PyObject *awaitable = NULL;
2122 PyObject *aiter = TOP();
2123 PyTypeObject *type = Py_TYPE(aiter);
2124
Yury Selivanoveb636452016-09-08 22:01:51 -07002125 if (PyAsyncGen_CheckExact(aiter)) {
2126 awaitable = type->tp_as_async->am_anext(aiter);
2127 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002128 goto error;
2129 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002130 } else {
2131 if (type->tp_as_async != NULL){
2132 getter = type->tp_as_async->am_anext;
2133 }
Yury Selivanov75445082015-05-11 22:57:16 -04002134
Yury Selivanoveb636452016-09-08 22:01:51 -07002135 if (getter != NULL) {
2136 next_iter = (*getter)(aiter);
2137 if (next_iter == NULL) {
2138 goto error;
2139 }
2140 }
2141 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002142 _PyErr_Format(tstate, PyExc_TypeError,
2143 "'async for' requires an iterator with "
2144 "__anext__ method, got %.100s",
2145 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002146 goto error;
2147 }
Yury Selivanov75445082015-05-11 22:57:16 -04002148
Yury Selivanoveb636452016-09-08 22:01:51 -07002149 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2150 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002151 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002152 PyExc_TypeError,
2153 "'async for' received an invalid object "
2154 "from __anext__: %.100s",
2155 Py_TYPE(next_iter)->tp_name);
2156
2157 Py_DECREF(next_iter);
2158 goto error;
2159 } else {
2160 Py_DECREF(next_iter);
2161 }
2162 }
Yury Selivanov75445082015-05-11 22:57:16 -04002163
2164 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002165 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002166 DISPATCH();
2167 }
2168
Benjamin Petersonddd19492018-09-16 22:38:02 -07002169 case TARGET(GET_AWAITABLE): {
2170 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002171 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002172 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002173
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002174 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002175 int opcode_at_minus_3 = 0;
2176 if ((next_instr - first_instr) > 2) {
2177 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2178 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002179 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002180 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002181 _Py_OPCODE(next_instr[-2]));
2182 }
2183
Yury Selivanov75445082015-05-11 22:57:16 -04002184 Py_DECREF(iterable);
2185
Yury Selivanovc724bae2016-03-02 11:30:46 -05002186 if (iter != NULL && PyCoro_CheckExact(iter)) {
2187 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2188 if (yf != NULL) {
2189 /* `iter` is a coroutine object that is being
2190 awaited, `yf` is a pointer to the current awaitable
2191 being awaited on. */
2192 Py_DECREF(yf);
2193 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002194 _PyErr_SetString(tstate, PyExc_RuntimeError,
2195 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002196 /* The code below jumps to `error` if `iter` is NULL. */
2197 }
2198 }
2199
Yury Selivanov75445082015-05-11 22:57:16 -04002200 SET_TOP(iter); /* Even if it's NULL */
2201
2202 if (iter == NULL) {
2203 goto error;
2204 }
2205
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002206 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002207 DISPATCH();
2208 }
2209
Benjamin Petersonddd19492018-09-16 22:38:02 -07002210 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002212 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002213 PySendResult gen_status;
2214 if (tstate->c_tracefunc == NULL) {
2215 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002216 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002217 _Py_IDENTIFIER(send);
2218 if (v == Py_None && PyIter_Check(receiver)) {
2219 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002220 }
2221 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002222 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002223 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002224 if (retval == NULL) {
2225 if (tstate->c_tracefunc != NULL
2226 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2227 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2228 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2229 gen_status = PYGEN_RETURN;
2230 }
2231 else {
2232 gen_status = PYGEN_ERROR;
2233 }
2234 }
2235 else {
2236 gen_status = PYGEN_NEXT;
2237 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002238 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002240 if (gen_status == PYGEN_ERROR) {
2241 assert (retval == NULL);
2242 goto error;
2243 }
2244 if (gen_status == PYGEN_RETURN) {
2245 assert (retval != NULL);
2246
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002247 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002248 SET_TOP(retval);
2249 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002251 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002252 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002253 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002254 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002255 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002256 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002257 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002258 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002259 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002261
Benjamin Petersonddd19492018-09-16 22:38:02 -07002262 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002264
2265 if (co->co_flags & CO_ASYNC_GENERATOR) {
2266 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2267 Py_DECREF(retval);
2268 if (w == NULL) {
2269 retval = NULL;
2270 goto error;
2271 }
2272 retval = w;
2273 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002274 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002275 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002276 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002278
Benjamin Petersonddd19492018-09-16 22:38:02 -07002279 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002280 PyObject *type, *value, *traceback;
2281 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 PyTryBlock *b = PyFrame_BlockPop(f);
2283 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002284 _PyErr_SetString(tstate, PyExc_SystemError,
2285 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002288 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2289 STACK_LEVEL() <= (b)->b_level + 4);
2290 exc_info = tstate->exc_info;
2291 type = exc_info->exc_type;
2292 value = exc_info->exc_value;
2293 traceback = exc_info->exc_traceback;
2294 exc_info->exc_type = POP();
2295 exc_info->exc_value = POP();
2296 exc_info->exc_traceback = POP();
2297 Py_XDECREF(type);
2298 Py_XDECREF(value);
2299 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002302
Benjamin Petersonddd19492018-09-16 22:38:02 -07002303 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002304 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Mark Shannonfee55262019-11-21 09:11:43 +00002308 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002309 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002310 PyObject *val = POP();
2311 PyObject *tb = POP();
2312 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002313 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002314 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Benjamin Petersonddd19492018-09-16 22:38:02 -07002317 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002318 PyObject *exc = POP();
2319 assert(PyExceptionClass_Check(exc));
2320 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2321 PyTryBlock *b = PyFrame_BlockPop(f);
2322 assert(b->b_type == EXCEPT_HANDLER);
2323 Py_DECREF(exc);
2324 UNWIND_EXCEPT_HANDLER(b);
2325 Py_DECREF(POP());
2326 JUMPBY(oparg);
2327 FAST_DISPATCH();
2328 }
2329 else {
2330 PyObject *val = POP();
2331 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002332 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002333 goto exception_unwind;
2334 }
2335 }
2336
Zackery Spytzce6a0702019-08-25 03:44:09 -06002337 case TARGET(LOAD_ASSERTION_ERROR): {
2338 PyObject *value = PyExc_AssertionError;
2339 Py_INCREF(value);
2340 PUSH(value);
2341 FAST_DISPATCH();
2342 }
2343
Benjamin Petersonddd19492018-09-16 22:38:02 -07002344 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002345 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002346
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002348 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002349 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002351 if (!_PyErr_Occurred(tstate)) {
2352 _PyErr_SetString(tstate, PyExc_NameError,
2353 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002358 }
2359 else {
2360 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2361 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002362 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2364 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002365 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2366 _PyErr_SetString(tstate, PyExc_NameError,
2367 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002372 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002373 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyObject *name = GETITEM(names, oparg);
2377 PyObject *v = POP();
2378 PyObject *ns = f->f_locals;
2379 int err;
2380 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002381 _PyErr_Format(tstate, PyExc_SystemError,
2382 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 if (PyDict_CheckExact(ns))
2387 err = PyDict_SetItem(ns, name, v);
2388 else
2389 err = PyObject_SetItem(ns, name, v);
2390 Py_DECREF(v);
2391 if (err != 0)
2392 goto error;
2393 DISPATCH();
2394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Benjamin Petersonddd19492018-09-16 22:38:02 -07002396 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 PyObject *name = GETITEM(names, oparg);
2398 PyObject *ns = f->f_locals;
2399 int err;
2400 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 _PyErr_Format(tstate, PyExc_SystemError,
2402 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 err = PyObject_DelItem(ns, name);
2406 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002407 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 NAME_ERROR_MSG,
2409 name);
2410 goto error;
2411 }
2412 DISPATCH();
2413 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002414
Benjamin Petersonddd19492018-09-16 22:38:02 -07002415 case TARGET(UNPACK_SEQUENCE): {
2416 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 PyObject *seq = POP(), *item, **items;
2418 if (PyTuple_CheckExact(seq) &&
2419 PyTuple_GET_SIZE(seq) == oparg) {
2420 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 item = items[oparg];
2423 Py_INCREF(item);
2424 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 } else if (PyList_CheckExact(seq) &&
2427 PyList_GET_SIZE(seq) == oparg) {
2428 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 item = items[oparg];
2431 Py_INCREF(item);
2432 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002434 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002436 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 } else {
2438 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 Py_DECREF(seq);
2440 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002443 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002445
Benjamin Petersonddd19492018-09-16 22:38:02 -07002446 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2448 PyObject *seq = POP();
2449
Victor Stinner438a12d2019-05-24 17:01:38 +02002450 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 stack_pointer + totalargs)) {
2452 stack_pointer += totalargs;
2453 } else {
2454 Py_DECREF(seq);
2455 goto error;
2456 }
2457 Py_DECREF(seq);
2458 DISPATCH();
2459 }
2460
Benjamin Petersonddd19492018-09-16 22:38:02 -07002461 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 PyObject *name = GETITEM(names, oparg);
2463 PyObject *owner = TOP();
2464 PyObject *v = SECOND();
2465 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002466 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 Py_DECREF(owner);
2470 if (err != 0)
2471 goto error;
2472 DISPATCH();
2473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002474
Benjamin Petersonddd19492018-09-16 22:38:02 -07002475 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 PyObject *name = GETITEM(names, oparg);
2477 PyObject *owner = POP();
2478 int err;
2479 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2480 Py_DECREF(owner);
2481 if (err != 0)
2482 goto error;
2483 DISPATCH();
2484 }
2485
Benjamin Petersonddd19492018-09-16 22:38:02 -07002486 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 PyObject *name = GETITEM(names, oparg);
2488 PyObject *v = POP();
2489 int err;
2490 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 if (err != 0)
2493 goto error;
2494 DISPATCH();
2495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Benjamin Petersonddd19492018-09-16 22:38:02 -07002497 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 PyObject *name = GETITEM(names, oparg);
2499 int err;
2500 err = PyDict_DelItem(f->f_globals, name);
2501 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2503 format_exc_check_arg(tstate, PyExc_NameError,
2504 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002507 }
2508 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002510
Benjamin Petersonddd19492018-09-16 22:38:02 -07002511 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 PyObject *name = GETITEM(names, oparg);
2513 PyObject *locals = f->f_locals;
2514 PyObject *v;
2515 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002516 _PyErr_Format(tstate, PyExc_SystemError,
2517 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002521 v = PyDict_GetItemWithError(locals, name);
2522 if (v != NULL) {
2523 Py_INCREF(v);
2524 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002525 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002526 goto error;
2527 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 }
2529 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002531 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002532 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002533 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002534 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 }
2536 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002538 v = PyDict_GetItemWithError(f->f_globals, name);
2539 if (v != NULL) {
2540 Py_INCREF(v);
2541 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002543 goto error;
2544 }
2545 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002546 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002547 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002549 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002550 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002551 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002553 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002555 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002557 }
2558 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 v = PyObject_GetItem(f->f_builtins, name);
2560 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002561 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002562 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002563 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002567 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002574
Benjamin Petersonddd19492018-09-16 22:38:02 -07002575 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002576 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002578 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002579 && PyDict_CheckExact(f->f_builtins))
2580 {
Inada Naoki91234a12019-06-03 21:30:58 +09002581 OPCACHE_CHECK();
2582 if (co_opcache != NULL && co_opcache->optimized > 0) {
2583 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2584
2585 if (lg->globals_ver ==
2586 ((PyDictObject *)f->f_globals)->ma_version_tag
2587 && lg->builtins_ver ==
2588 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2589 {
2590 PyObject *ptr = lg->ptr;
2591 OPCACHE_STAT_GLOBAL_HIT();
2592 assert(ptr != NULL);
2593 Py_INCREF(ptr);
2594 PUSH(ptr);
2595 DISPATCH();
2596 }
2597 }
2598
2599 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002601 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 name);
2603 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002604 if (!_PyErr_OCCURRED()) {
2605 /* _PyDict_LoadGlobal() returns NULL without raising
2606 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002607 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002608 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 }
Inada Naoki91234a12019-06-03 21:30:58 +09002612
2613 if (co_opcache != NULL) {
2614 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2615
2616 if (co_opcache->optimized == 0) {
2617 /* Wasn't optimized before. */
2618 OPCACHE_STAT_GLOBAL_OPT();
2619 } else {
2620 OPCACHE_STAT_GLOBAL_MISS();
2621 }
2622
2623 co_opcache->optimized = 1;
2624 lg->globals_ver =
2625 ((PyDictObject *)f->f_globals)->ma_version_tag;
2626 lg->builtins_ver =
2627 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2628 lg->ptr = v; /* borrowed */
2629 }
2630
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002633 else {
2634 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002635
2636 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002637 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002638 v = PyObject_GetItem(f->f_globals, name);
2639 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002640 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002641 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002642 }
2643 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002644
Victor Stinnerb4efc962015-11-20 09:24:02 +01002645 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 v = PyObject_GetItem(f->f_builtins, name);
2647 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002648 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002649 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002650 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002651 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002652 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002654 }
2655 }
2656 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002660
Benjamin Petersonddd19492018-09-16 22:38:02 -07002661 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 PyObject *v = GETLOCAL(oparg);
2663 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 SETLOCAL(oparg, NULL);
2665 DISPATCH();
2666 }
2667 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002668 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 UNBOUNDLOCAL_ERROR_MSG,
2670 PyTuple_GetItem(co->co_varnames, oparg)
2671 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 goto error;
2673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002674
Benjamin Petersonddd19492018-09-16 22:38:02 -07002675 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002677 PyObject *oldobj = PyCell_GET(cell);
2678 if (oldobj != NULL) {
2679 PyCell_SET(cell, NULL);
2680 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002681 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002682 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002683 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 goto error;
2685 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002686
Benjamin Petersonddd19492018-09-16 22:38:02 -07002687 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 PyObject *cell = freevars[oparg];
2689 Py_INCREF(cell);
2690 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002693
Benjamin Petersonddd19492018-09-16 22:38:02 -07002694 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002695 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002696 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002697 assert(locals);
2698 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2699 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2700 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2701 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2702 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002703 value = PyDict_GetItemWithError(locals, name);
2704 if (value != NULL) {
2705 Py_INCREF(value);
2706 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002707 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002708 goto error;
2709 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002710 }
2711 else {
2712 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002713 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002714 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002715 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002716 }
2717 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002718 }
2719 }
2720 if (!value) {
2721 PyObject *cell = freevars[oparg];
2722 value = PyCell_GET(cell);
2723 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002724 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002725 goto error;
2726 }
2727 Py_INCREF(value);
2728 }
2729 PUSH(value);
2730 DISPATCH();
2731 }
2732
Benjamin Petersonddd19492018-09-16 22:38:02 -07002733 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 PyObject *cell = freevars[oparg];
2735 PyObject *value = PyCell_GET(cell);
2736 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002737 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 Py_INCREF(value);
2741 PUSH(value);
2742 DISPATCH();
2743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002744
Benjamin Petersonddd19492018-09-16 22:38:02 -07002745 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 PyObject *v = POP();
2747 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002748 PyObject *oldobj = PyCell_GET(cell);
2749 PyCell_SET(cell, v);
2750 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 DISPATCH();
2752 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002753
Benjamin Petersonddd19492018-09-16 22:38:02 -07002754 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002755 PyObject *str;
2756 PyObject *empty = PyUnicode_New(0, 0);
2757 if (empty == NULL) {
2758 goto error;
2759 }
2760 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2761 Py_DECREF(empty);
2762 if (str == NULL)
2763 goto error;
2764 while (--oparg >= 0) {
2765 PyObject *item = POP();
2766 Py_DECREF(item);
2767 }
2768 PUSH(str);
2769 DISPATCH();
2770 }
2771
Benjamin Petersonddd19492018-09-16 22:38:02 -07002772 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 PyObject *tup = PyTuple_New(oparg);
2774 if (tup == NULL)
2775 goto error;
2776 while (--oparg >= 0) {
2777 PyObject *item = POP();
2778 PyTuple_SET_ITEM(tup, oparg, item);
2779 }
2780 PUSH(tup);
2781 DISPATCH();
2782 }
2783
Benjamin Petersonddd19492018-09-16 22:38:02 -07002784 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 PyObject *list = PyList_New(oparg);
2786 if (list == NULL)
2787 goto error;
2788 while (--oparg >= 0) {
2789 PyObject *item = POP();
2790 PyList_SET_ITEM(list, oparg, item);
2791 }
2792 PUSH(list);
2793 DISPATCH();
2794 }
2795
Mark Shannon13bc1392020-01-23 09:25:17 +00002796 case TARGET(LIST_TO_TUPLE): {
2797 PyObject *list = POP();
2798 PyObject *tuple = PyList_AsTuple(list);
2799 Py_DECREF(list);
2800 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002802 }
2803 PUSH(tuple);
2804 DISPATCH();
2805 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806
Mark Shannon13bc1392020-01-23 09:25:17 +00002807 case TARGET(LIST_EXTEND): {
2808 PyObject *iterable = POP();
2809 PyObject *list = PEEK(oparg);
2810 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2811 if (none_val == NULL) {
2812 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002813 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002814 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002815 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002816 _PyErr_Format(tstate, PyExc_TypeError,
2817 "Value after * must be an iterable, not %.200s",
2818 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002820 Py_DECREF(iterable);
2821 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002822 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002823 Py_DECREF(none_val);
2824 Py_DECREF(iterable);
2825 DISPATCH();
2826 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002827
Mark Shannon13bc1392020-01-23 09:25:17 +00002828 case TARGET(SET_UPDATE): {
2829 PyObject *iterable = POP();
2830 PyObject *set = PEEK(oparg);
2831 int err = _PySet_Update(set, iterable);
2832 Py_DECREF(iterable);
2833 if (err < 0) {
2834 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002835 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002836 DISPATCH();
2837 }
2838
Benjamin Petersonddd19492018-09-16 22:38:02 -07002839 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 PyObject *set = PySet_New(NULL);
2841 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002842 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 if (set == NULL)
2844 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002845 for (i = oparg; i > 0; i--) {
2846 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 if (err == 0)
2848 err = PySet_Add(set, item);
2849 Py_DECREF(item);
2850 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002851 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 if (err != 0) {
2853 Py_DECREF(set);
2854 goto error;
2855 }
2856 PUSH(set);
2857 DISPATCH();
2858 }
2859
Benjamin Petersonddd19492018-09-16 22:38:02 -07002860 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002861 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2863 if (map == NULL)
2864 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002865 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002867 PyObject *key = PEEK(2*i);
2868 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002869 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002870 if (err != 0) {
2871 Py_DECREF(map);
2872 goto error;
2873 }
2874 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002875
2876 while (oparg--) {
2877 Py_DECREF(POP());
2878 Py_DECREF(POP());
2879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PUSH(map);
2881 DISPATCH();
2882 }
2883
Benjamin Petersonddd19492018-09-16 22:38:02 -07002884 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002885 _Py_IDENTIFIER(__annotations__);
2886 int err;
2887 PyObject *ann_dict;
2888 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002889 _PyErr_Format(tstate, PyExc_SystemError,
2890 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002891 goto error;
2892 }
2893 /* check if __annotations__ in locals()... */
2894 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002895 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002896 &PyId___annotations__);
2897 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002898 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002899 goto error;
2900 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002901 /* ...if not, create a new one */
2902 ann_dict = PyDict_New();
2903 if (ann_dict == NULL) {
2904 goto error;
2905 }
2906 err = _PyDict_SetItemId(f->f_locals,
2907 &PyId___annotations__, ann_dict);
2908 Py_DECREF(ann_dict);
2909 if (err != 0) {
2910 goto error;
2911 }
2912 }
2913 }
2914 else {
2915 /* do the same if locals() is not a dict */
2916 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2917 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002918 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002919 }
2920 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2921 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002922 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002923 goto error;
2924 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002925 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002926 ann_dict = PyDict_New();
2927 if (ann_dict == NULL) {
2928 goto error;
2929 }
2930 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2931 Py_DECREF(ann_dict);
2932 if (err != 0) {
2933 goto error;
2934 }
2935 }
2936 else {
2937 Py_DECREF(ann_dict);
2938 }
2939 }
2940 DISPATCH();
2941 }
2942
Benjamin Petersonddd19492018-09-16 22:38:02 -07002943 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002944 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002945 PyObject *map;
2946 PyObject *keys = TOP();
2947 if (!PyTuple_CheckExact(keys) ||
2948 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002949 _PyErr_SetString(tstate, PyExc_SystemError,
2950 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002951 goto error;
2952 }
2953 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2954 if (map == NULL) {
2955 goto error;
2956 }
2957 for (i = oparg; i > 0; i--) {
2958 int err;
2959 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2960 PyObject *value = PEEK(i + 1);
2961 err = PyDict_SetItem(map, key, value);
2962 if (err != 0) {
2963 Py_DECREF(map);
2964 goto error;
2965 }
2966 }
2967
2968 Py_DECREF(POP());
2969 while (oparg--) {
2970 Py_DECREF(POP());
2971 }
2972 PUSH(map);
2973 DISPATCH();
2974 }
2975
Mark Shannon8a4cd702020-01-27 09:57:45 +00002976 case TARGET(DICT_UPDATE): {
2977 PyObject *update = POP();
2978 PyObject *dict = PEEK(oparg);
2979 if (PyDict_Update(dict, update) < 0) {
2980 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2981 _PyErr_Format(tstate, PyExc_TypeError,
2982 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002983 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002984 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002985 Py_DECREF(update);
2986 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002987 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002988 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002989 DISPATCH();
2990 }
2991
Mark Shannon8a4cd702020-01-27 09:57:45 +00002992 case TARGET(DICT_MERGE): {
2993 PyObject *update = POP();
2994 PyObject *dict = PEEK(oparg);
2995
2996 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2997 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2998 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002999 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003000 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003001 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003002 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003003 DISPATCH();
3004 }
3005
Benjamin Petersonddd19492018-09-16 22:38:02 -07003006 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003007 PyObject *value = TOP();
3008 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 PyObject *map;
3010 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003011 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003012 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003014 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 Py_DECREF(value);
3016 Py_DECREF(key);
3017 if (err != 0)
3018 goto error;
3019 PREDICT(JUMP_ABSOLUTE);
3020 DISPATCH();
3021 }
3022
Benjamin Petersonddd19492018-09-16 22:38:02 -07003023 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 PyObject *name = GETITEM(names, oparg);
3025 PyObject *owner = TOP();
3026 PyObject *res = PyObject_GetAttr(owner, name);
3027 Py_DECREF(owner);
3028 SET_TOP(res);
3029 if (res == NULL)
3030 goto error;
3031 DISPATCH();
3032 }
3033
Benjamin Petersonddd19492018-09-16 22:38:02 -07003034 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003035 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 PyObject *right = POP();
3037 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003038 PyObject *res = PyObject_RichCompare(left, right, oparg);
3039 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 Py_DECREF(left);
3041 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 if (res == NULL)
3043 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 PREDICT(POP_JUMP_IF_FALSE);
3045 PREDICT(POP_JUMP_IF_TRUE);
3046 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003047 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003048
Mark Shannon9af0e472020-01-14 10:12:45 +00003049 case TARGET(IS_OP): {
3050 PyObject *right = POP();
3051 PyObject *left = TOP();
3052 int res = (left == right)^oparg;
3053 PyObject *b = res ? Py_True : Py_False;
3054 Py_INCREF(b);
3055 SET_TOP(b);
3056 Py_DECREF(left);
3057 Py_DECREF(right);
3058 PREDICT(POP_JUMP_IF_FALSE);
3059 PREDICT(POP_JUMP_IF_TRUE);
3060 FAST_DISPATCH();
3061 }
3062
3063 case TARGET(CONTAINS_OP): {
3064 PyObject *right = POP();
3065 PyObject *left = POP();
3066 int res = PySequence_Contains(right, left);
3067 Py_DECREF(left);
3068 Py_DECREF(right);
3069 if (res < 0) {
3070 goto error;
3071 }
3072 PyObject *b = (res^oparg) ? Py_True : Py_False;
3073 Py_INCREF(b);
3074 PUSH(b);
3075 PREDICT(POP_JUMP_IF_FALSE);
3076 PREDICT(POP_JUMP_IF_TRUE);
3077 FAST_DISPATCH();
3078 }
3079
3080#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3081 "BaseException is not allowed"
3082
3083 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3084 PyObject *right = POP();
3085 PyObject *left = POP();
3086 if (PyTuple_Check(right)) {
3087 Py_ssize_t i, length;
3088 length = PyTuple_GET_SIZE(right);
3089 for (i = 0; i < length; i++) {
3090 PyObject *exc = PyTuple_GET_ITEM(right, i);
3091 if (!PyExceptionClass_Check(exc)) {
3092 _PyErr_SetString(tstate, PyExc_TypeError,
3093 CANNOT_CATCH_MSG);
3094 Py_DECREF(left);
3095 Py_DECREF(right);
3096 goto error;
3097 }
3098 }
3099 }
3100 else {
3101 if (!PyExceptionClass_Check(right)) {
3102 _PyErr_SetString(tstate, PyExc_TypeError,
3103 CANNOT_CATCH_MSG);
3104 Py_DECREF(left);
3105 Py_DECREF(right);
3106 goto error;
3107 }
3108 }
3109 int res = PyErr_GivenExceptionMatches(left, right);
3110 Py_DECREF(left);
3111 Py_DECREF(right);
3112 if (res > 0) {
3113 /* Exception matches -- Do nothing */;
3114 }
3115 else if (res == 0) {
3116 JUMPTO(oparg);
3117 }
3118 else {
3119 goto error;
3120 }
3121 DISPATCH();
3122 }
3123
Benjamin Petersonddd19492018-09-16 22:38:02 -07003124 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003126 PyObject *fromlist = POP();
3127 PyObject *level = TOP();
3128 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003129 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003130 Py_DECREF(level);
3131 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 SET_TOP(res);
3133 if (res == NULL)
3134 goto error;
3135 DISPATCH();
3136 }
3137
Benjamin Petersonddd19492018-09-16 22:38:02 -07003138 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 PyObject *from = POP(), *locals;
3140 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003141 if (PyFrame_FastToLocalsWithError(f) < 0) {
3142 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003143 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003144 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003145
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 locals = f->f_locals;
3147 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003148 _PyErr_SetString(tstate, PyExc_SystemError,
3149 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003150 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003151 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003153 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 Py_DECREF(from);
3156 if (err != 0)
3157 goto error;
3158 DISPATCH();
3159 }
Guido van Rossum25831651993-05-19 14:50:45 +00003160
Benjamin Petersonddd19492018-09-16 22:38:02 -07003161 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 PyObject *name = GETITEM(names, oparg);
3163 PyObject *from = TOP();
3164 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003165 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 PUSH(res);
3167 if (res == NULL)
3168 goto error;
3169 DISPATCH();
3170 }
Thomas Wouters52152252000-08-17 22:55:00 +00003171
Benjamin Petersonddd19492018-09-16 22:38:02 -07003172 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 JUMPBY(oparg);
3174 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003176
Benjamin Petersonddd19492018-09-16 22:38:02 -07003177 case TARGET(POP_JUMP_IF_FALSE): {
3178 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 PyObject *cond = POP();
3180 int err;
3181 if (cond == Py_True) {
3182 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 FAST_DISPATCH();
3184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 if (cond == Py_False) {
3186 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 JUMPTO(oparg);
3188 FAST_DISPATCH();
3189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 err = PyObject_IsTrue(cond);
3191 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003193 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 else if (err == 0)
3195 JUMPTO(oparg);
3196 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003200
Benjamin Petersonddd19492018-09-16 22:38:02 -07003201 case TARGET(POP_JUMP_IF_TRUE): {
3202 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 PyObject *cond = POP();
3204 int err;
3205 if (cond == Py_False) {
3206 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 FAST_DISPATCH();
3208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 if (cond == Py_True) {
3210 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 JUMPTO(oparg);
3212 FAST_DISPATCH();
3213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 err = PyObject_IsTrue(cond);
3215 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 JUMPTO(oparg);
3218 }
3219 else if (err == 0)
3220 ;
3221 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003225
Benjamin Petersonddd19492018-09-16 22:38:02 -07003226 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003227 PyObject *cond = TOP();
3228 int err;
3229 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003230 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 FAST_DISPATCH();
3233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 JUMPTO(oparg);
3236 FAST_DISPATCH();
3237 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 if (err > 0) {
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 }
3243 else if (err == 0)
3244 JUMPTO(oparg);
3245 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003249
Benjamin Petersonddd19492018-09-16 22:38:02 -07003250 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 PyObject *cond = TOP();
3252 int err;
3253 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003254 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 FAST_DISPATCH();
3257 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 JUMPTO(oparg);
3260 FAST_DISPATCH();
3261 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 JUMPTO(oparg);
3265 }
3266 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003267 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 }
3270 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003274
Benjamin Petersonddd19492018-09-16 22:38:02 -07003275 case TARGET(JUMP_ABSOLUTE): {
3276 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003278#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 /* Enabling this path speeds-up all while and for-loops by bypassing
3280 the per-loop checks for signals. By default, this should be turned-off
3281 because it prevents detection of a control-break in tight loops like
3282 "while 1: pass". Compile with this option turned-on when you need
3283 the speed-up and do not need break checking inside tight loops (ones
3284 that contain only instructions ending with FAST_DISPATCH).
3285 */
3286 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003287#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003289#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003291
Benjamin Petersonddd19492018-09-16 22:38:02 -07003292 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003295 PyObject *iter = PyObject_GetIter(iterable);
3296 Py_DECREF(iterable);
3297 SET_TOP(iter);
3298 if (iter == NULL)
3299 goto error;
3300 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003301 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003302 DISPATCH();
3303 }
3304
Benjamin Petersonddd19492018-09-16 22:38:02 -07003305 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003306 /* before: [obj]; after [getiter(obj)] */
3307 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003308 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003309 if (PyCoro_CheckExact(iterable)) {
3310 /* `iterable` is a coroutine */
3311 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3312 /* and it is used in a 'yield from' expression of a
3313 regular generator. */
3314 Py_DECREF(iterable);
3315 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003316 _PyErr_SetString(tstate, PyExc_TypeError,
3317 "cannot 'yield from' a coroutine object "
3318 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003319 goto error;
3320 }
3321 }
3322 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003323 /* `iterable` is not a generator. */
3324 iter = PyObject_GetIter(iterable);
3325 Py_DECREF(iterable);
3326 SET_TOP(iter);
3327 if (iter == NULL)
3328 goto error;
3329 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003330 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 DISPATCH();
3332 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003333
Benjamin Petersonddd19492018-09-16 22:38:02 -07003334 case TARGET(FOR_ITER): {
3335 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003338 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 if (next != NULL) {
3340 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 PREDICT(STORE_FAST);
3342 PREDICT(UNPACK_SEQUENCE);
3343 DISPATCH();
3344 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003345 if (_PyErr_Occurred(tstate)) {
3346 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003348 }
3349 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003350 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003351 }
3352 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 }
3354 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003355 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 JUMPBY(oparg);
3358 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003360
Benjamin Petersonddd19492018-09-16 22:38:02 -07003361 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003362 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 STACK_LEVEL());
3364 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003365 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003366
Benjamin Petersonddd19492018-09-16 22:38:02 -07003367 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003368 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003369 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003370 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003371 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003372 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003373 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003374 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003375 }
3376 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3377 if (exit == NULL) {
3378 Py_DECREF(enter);
3379 goto error;
3380 }
Yury Selivanov75445082015-05-11 22:57:16 -04003381 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003382 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003383 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003384 Py_DECREF(enter);
3385 if (res == NULL)
3386 goto error;
3387 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003388 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003389 DISPATCH();
3390 }
3391
Benjamin Petersonddd19492018-09-16 22:38:02 -07003392 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003393 PyObject *res = POP();
3394 /* Setup the finally block before pushing the result
3395 of __aenter__ on the stack. */
3396 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3397 STACK_LEVEL());
3398 PUSH(res);
3399 DISPATCH();
3400 }
3401
Benjamin Petersonddd19492018-09-16 22:38:02 -07003402 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003403 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003404 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003406 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003408 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003409 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003410 }
3411 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003412 if (exit == NULL) {
3413 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003415 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003417 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003418 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 Py_DECREF(enter);
3420 if (res == NULL)
3421 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 /* Setup the finally block before pushing the result
3423 of __enter__ on the stack. */
3424 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3425 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 DISPATCH();
3429 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003430
Mark Shannonfee55262019-11-21 09:11:43 +00003431 case TARGET(WITH_EXCEPT_START): {
3432 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003434 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3435 - SEVENTH: the context.__exit__ bound method
3436 We call SEVENTH(TOP, SECOND, THIRD).
3437 Then we push again the TOP exception and the __exit__
3438 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003441 PyObject *exc, *val, *tb, *res;
3442
Victor Stinner842cfff2016-12-01 14:45:31 +01003443 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003444 val = SECOND();
3445 tb = THIRD();
3446 assert(exc != Py_None);
3447 assert(!PyLong_Check(exc));
3448 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003449 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003450 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003451 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 if (res == NULL)
3453 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003454
Yury Selivanov75445082015-05-11 22:57:16 -04003455 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003458
Benjamin Petersonddd19492018-09-16 22:38:02 -07003459 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003460 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003461 PyObject *name = GETITEM(names, oparg);
3462 PyObject *obj = TOP();
3463 PyObject *meth = NULL;
3464
3465 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3466
Yury Selivanovf2392132016-12-13 19:03:51 -05003467 if (meth == NULL) {
3468 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003469 goto error;
3470 }
3471
3472 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003473 /* We can bypass temporary bound method object.
3474 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003475
INADA Naoki015bce62017-01-16 17:23:30 +09003476 meth | self | arg1 | ... | argN
3477 */
3478 SET_TOP(meth);
3479 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003480 }
3481 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003482 /* meth is not an unbound method (but a regular attr, or
3483 something was returned by a descriptor protocol). Set
3484 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003485 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003486
3487 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 */
INADA Naoki015bce62017-01-16 17:23:30 +09003489 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003491 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003492 }
3493 DISPATCH();
3494 }
3495
Benjamin Petersonddd19492018-09-16 22:38:02 -07003496 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003497 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003498 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003499
3500 sp = stack_pointer;
3501
INADA Naoki015bce62017-01-16 17:23:30 +09003502 meth = PEEK(oparg + 2);
3503 if (meth == NULL) {
3504 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3505 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003506
3507 Stack layout:
3508
INADA Naoki015bce62017-01-16 17:23:30 +09003509 ... | NULL | callable | arg1 | ... | argN
3510 ^- TOP()
3511 ^- (-oparg)
3512 ^- (-oparg-1)
3513 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003514
Ville Skyttä49b27342017-08-03 09:00:59 +03003515 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003516 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003517 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003518 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003519 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003520 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003521 }
3522 else {
3523 /* This is a method call. Stack layout:
3524
INADA Naoki015bce62017-01-16 17:23:30 +09003525 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003526 ^- TOP()
3527 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003528 ^- (-oparg-1)
3529 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003530
INADA Naoki015bce62017-01-16 17:23:30 +09003531 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003532 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003533 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003534 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003535 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003536 stack_pointer = sp;
3537 }
3538
3539 PUSH(res);
3540 if (res == NULL)
3541 goto error;
3542 DISPATCH();
3543 }
3544
Benjamin Petersonddd19492018-09-16 22:38:02 -07003545 case TARGET(CALL_FUNCTION): {
3546 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003549 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003552 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003553 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003554 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003557
Benjamin Petersonddd19492018-09-16 22:38:02 -07003558 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003559 PyObject **sp, *res, *names;
3560
3561 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003562 assert(PyTuple_Check(names));
3563 assert(PyTuple_GET_SIZE(names) <= oparg);
3564 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003566 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003569 Py_DECREF(names);
3570
3571 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003572 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003573 }
3574 DISPATCH();
3575 }
3576
Benjamin Petersonddd19492018-09-16 22:38:02 -07003577 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003578 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003579 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003580 if (oparg & 0x01) {
3581 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003582 if (!PyDict_CheckExact(kwargs)) {
3583 PyObject *d = PyDict_New();
3584 if (d == NULL)
3585 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003586 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003587 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003588 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003589 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003590 goto error;
3591 }
3592 Py_DECREF(kwargs);
3593 kwargs = d;
3594 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003595 assert(PyDict_CheckExact(kwargs));
3596 }
3597 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003598 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003599 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003600 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003601 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003602 goto error;
3603 }
3604 Py_SETREF(callargs, PySequence_Tuple(callargs));
3605 if (callargs == NULL) {
3606 goto error;
3607 }
3608 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003609 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003610
Victor Stinner09532fe2019-05-10 23:39:09 +02003611 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003612 Py_DECREF(func);
3613 Py_DECREF(callargs);
3614 Py_XDECREF(kwargs);
3615
3616 SET_TOP(result);
3617 if (result == NULL) {
3618 goto error;
3619 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003620 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003622
Benjamin Petersonddd19492018-09-16 22:38:02 -07003623 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003624 PyObject *qualname = POP();
3625 PyObject *codeobj = POP();
3626 PyFunctionObject *func = (PyFunctionObject *)
3627 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003628
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003629 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003630 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003631 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003632 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003634
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003635 if (oparg & 0x08) {
3636 assert(PyTuple_CheckExact(TOP()));
3637 func ->func_closure = POP();
3638 }
3639 if (oparg & 0x04) {
3640 assert(PyDict_CheckExact(TOP()));
3641 func->func_annotations = POP();
3642 }
3643 if (oparg & 0x02) {
3644 assert(PyDict_CheckExact(TOP()));
3645 func->func_kwdefaults = POP();
3646 }
3647 if (oparg & 0x01) {
3648 assert(PyTuple_CheckExact(TOP()));
3649 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003651
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003652 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003653 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003655
Benjamin Petersonddd19492018-09-16 22:38:02 -07003656 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003657 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003659 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003661 step = NULL;
3662 stop = POP();
3663 start = TOP();
3664 slice = PySlice_New(start, stop, step);
3665 Py_DECREF(start);
3666 Py_DECREF(stop);
3667 Py_XDECREF(step);
3668 SET_TOP(slice);
3669 if (slice == NULL)
3670 goto error;
3671 DISPATCH();
3672 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003673
Benjamin Petersonddd19492018-09-16 22:38:02 -07003674 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003675 /* Handles f-string value formatting. */
3676 PyObject *result;
3677 PyObject *fmt_spec;
3678 PyObject *value;
3679 PyObject *(*conv_fn)(PyObject *);
3680 int which_conversion = oparg & FVC_MASK;
3681 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3682
3683 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003684 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003685
3686 /* See if any conversion is specified. */
3687 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003688 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003689 case FVC_STR: conv_fn = PyObject_Str; break;
3690 case FVC_REPR: conv_fn = PyObject_Repr; break;
3691 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003692 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003693 _PyErr_Format(tstate, PyExc_SystemError,
3694 "unexpected conversion flag %d",
3695 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003696 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003697 }
3698
3699 /* If there's a conversion function, call it and replace
3700 value with that result. Otherwise, just use value,
3701 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003702 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003703 result = conv_fn(value);
3704 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003705 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003706 Py_XDECREF(fmt_spec);
3707 goto error;
3708 }
3709 value = result;
3710 }
3711
3712 /* If value is a unicode object, and there's no fmt_spec,
3713 then we know the result of format(value) is value
3714 itself. In that case, skip calling format(). I plan to
3715 move this optimization in to PyObject_Format()
3716 itself. */
3717 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3718 /* Do nothing, just transfer ownership to result. */
3719 result = value;
3720 } else {
3721 /* Actually call format(). */
3722 result = PyObject_Format(value, fmt_spec);
3723 Py_DECREF(value);
3724 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003725 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003726 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003727 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003728 }
3729
Eric V. Smith135d5f42016-02-05 18:23:08 -05003730 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003731 DISPATCH();
3732 }
3733
Benjamin Petersonddd19492018-09-16 22:38:02 -07003734 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003735 int oldoparg = oparg;
3736 NEXTOPARG();
3737 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003739 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003740
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003741
Antoine Pitrou042b1282010-08-13 21:15:58 +00003742#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003744#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 default:
3746 fprintf(stderr,
3747 "XXX lineno: %d, opcode: %d\n",
3748 PyFrame_GetLineNumber(f),
3749 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003750 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003751 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003754
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003755 /* This should never be reached. Every opcode should end with DISPATCH()
3756 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003757 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003758
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003759error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003760 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003761#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003762 if (!_PyErr_Occurred(tstate)) {
3763 _PyErr_SetString(tstate, PyExc_SystemError,
3764 "error return without exception set");
3765 }
Victor Stinner365b6932013-07-12 00:11:58 +02003766#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003767 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003768#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003769
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003770 /* Log traceback info. */
3771 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003772
Mark Shannoncb9879b2020-07-17 11:44:23 +01003773 if (tstate->c_tracefunc != NULL) {
3774 /* Make sure state is set to FRAME_EXECUTING for tracing */
3775 assert(f->f_state == FRAME_EXECUTING);
3776 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003777 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3778 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01003779 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003780exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01003781 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003782 /* Unwind stacks if an exception occurred */
3783 while (f->f_iblock > 0) {
3784 /* Pop the current block. */
3785 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 if (b->b_type == EXCEPT_HANDLER) {
3788 UNWIND_EXCEPT_HANDLER(b);
3789 continue;
3790 }
3791 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003792 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 PyObject *exc, *val, *tb;
3794 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003795 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 /* Beware, this invalidates all b->b_* fields */
3797 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003798 PUSH(exc_info->exc_traceback);
3799 PUSH(exc_info->exc_value);
3800 if (exc_info->exc_type != NULL) {
3801 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 }
3803 else {
3804 Py_INCREF(Py_None);
3805 PUSH(Py_None);
3806 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003807 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 /* Make the raw exception data
3809 available to the handler,
3810 so a program can emulate the
3811 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003812 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003813 if (tb != NULL)
3814 PyException_SetTraceback(val, tb);
3815 else
3816 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003818 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003820 exc_info->exc_value = val;
3821 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 if (tb == NULL)
3823 tb = Py_None;
3824 Py_INCREF(tb);
3825 PUSH(tb);
3826 PUSH(val);
3827 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003829 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003830 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3831 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3832 /* Make sure that we trace line after exception if we are in a new execution
3833 * window or we don't need a line update and we are not in the first instruction
3834 * of the line. */
3835 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3836 instr_prev = INT_MAX;
3837 }
Mark Shannonfee55262019-11-21 09:11:43 +00003838 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003839 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01003840 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003841 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 }
3843 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003844
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003845 /* End the loop as we still have an error */
3846 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003848
Pablo Galindof00828a2019-05-09 16:52:02 +01003849 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003850 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 /* Pop remaining stack entries. */
3853 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003854 PyObject *o = POP();
3855 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01003857 f->f_stackdepth = 0;
3858 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003859exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003861 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003862 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3863 tstate, f, PyTrace_RETURN, retval)) {
3864 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 }
3866 }
3867 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003868 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3869 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003870 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 }
3872 }
3873 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003876exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003877 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3878 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003879 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003881
Victor Stinner0b72b232020-03-12 23:18:39 +01003882 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003883}
3884
Benjamin Petersonb204a422011-06-05 22:04:07 -05003885static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003886format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003887 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003888{
3889 int err;
3890 Py_ssize_t len = PyList_GET_SIZE(names);
3891 PyObject *name_str, *comma, *tail, *tmp;
3892
3893 assert(PyList_CheckExact(names));
3894 assert(len >= 1);
3895 /* Deal with the joys of natural language. */
3896 switch (len) {
3897 case 1:
3898 name_str = PyList_GET_ITEM(names, 0);
3899 Py_INCREF(name_str);
3900 break;
3901 case 2:
3902 name_str = PyUnicode_FromFormat("%U and %U",
3903 PyList_GET_ITEM(names, len - 2),
3904 PyList_GET_ITEM(names, len - 1));
3905 break;
3906 default:
3907 tail = PyUnicode_FromFormat(", %U, and %U",
3908 PyList_GET_ITEM(names, len - 2),
3909 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003910 if (tail == NULL)
3911 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003912 /* Chop off the last two objects in the list. This shouldn't actually
3913 fail, but we can't be too careful. */
3914 err = PyList_SetSlice(names, len - 2, len, NULL);
3915 if (err == -1) {
3916 Py_DECREF(tail);
3917 return;
3918 }
3919 /* Stitch everything up into a nice comma-separated list. */
3920 comma = PyUnicode_FromString(", ");
3921 if (comma == NULL) {
3922 Py_DECREF(tail);
3923 return;
3924 }
3925 tmp = PyUnicode_Join(comma, names);
3926 Py_DECREF(comma);
3927 if (tmp == NULL) {
3928 Py_DECREF(tail);
3929 return;
3930 }
3931 name_str = PyUnicode_Concat(tmp, tail);
3932 Py_DECREF(tmp);
3933 Py_DECREF(tail);
3934 break;
3935 }
3936 if (name_str == NULL)
3937 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003938 _PyErr_Format(tstate, PyExc_TypeError,
3939 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003940 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003941 len,
3942 kind,
3943 len == 1 ? "" : "s",
3944 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003945 Py_DECREF(name_str);
3946}
3947
3948static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003949missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3950 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003951 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003952{
Victor Stinner74319ae2016-08-25 00:04:09 +02003953 Py_ssize_t i, j = 0;
3954 Py_ssize_t start, end;
3955 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 const char *kind = positional ? "positional" : "keyword-only";
3957 PyObject *missing_names;
3958
3959 /* Compute the names of the arguments that are missing. */
3960 missing_names = PyList_New(missing);
3961 if (missing_names == NULL)
3962 return;
3963 if (positional) {
3964 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003965 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003966 }
3967 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003968 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003969 end = start + co->co_kwonlyargcount;
3970 }
3971 for (i = start; i < end; i++) {
3972 if (GETLOCAL(i) == NULL) {
3973 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3974 PyObject *name = PyObject_Repr(raw);
3975 if (name == NULL) {
3976 Py_DECREF(missing_names);
3977 return;
3978 }
3979 PyList_SET_ITEM(missing_names, j++, name);
3980 }
3981 }
3982 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003983 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003984 Py_DECREF(missing_names);
3985}
3986
3987static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003988too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3989 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003990 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991{
3992 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003993 Py_ssize_t kwonly_given = 0;
3994 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003996 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997
Benjamin Petersone109c702011-06-24 09:37:26 -05003998 assert((co->co_flags & CO_VARARGS) == 0);
3999 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004000 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004001 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004003 }
4004 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004005 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004006 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004008 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 }
4010 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004011 plural = (co_argcount != 1);
4012 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 }
4014 if (sig == NULL)
4015 return;
4016 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004017 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4018 kwonly_sig = PyUnicode_FromFormat(format,
4019 given != 1 ? "s" : "",
4020 kwonly_given,
4021 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 if (kwonly_sig == NULL) {
4023 Py_DECREF(sig);
4024 return;
4025 }
4026 }
4027 else {
4028 /* This will not fail. */
4029 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004030 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004032 _PyErr_Format(tstate, PyExc_TypeError,
4033 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004034 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004035 sig,
4036 plural ? "s" : "",
4037 given,
4038 kwonly_sig,
4039 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 Py_DECREF(sig);
4041 Py_DECREF(kwonly_sig);
4042}
4043
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004044static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004045positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004046 Py_ssize_t kwcount, PyObject* const* kwnames,
4047 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004048{
4049 int posonly_conflicts = 0;
4050 PyObject* posonly_names = PyList_New(0);
4051
4052 for(int k=0; k < co->co_posonlyargcount; k++){
4053 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4054
4055 for (int k2=0; k2<kwcount; k2++){
4056 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4057 PyObject* kwname = kwnames[k2];
4058 if (kwname == posonly_name){
4059 if(PyList_Append(posonly_names, kwname) != 0) {
4060 goto fail;
4061 }
4062 posonly_conflicts++;
4063 continue;
4064 }
4065
4066 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4067
4068 if ( cmp > 0) {
4069 if(PyList_Append(posonly_names, kwname) != 0) {
4070 goto fail;
4071 }
4072 posonly_conflicts++;
4073 } else if (cmp < 0) {
4074 goto fail;
4075 }
4076
4077 }
4078 }
4079 if (posonly_conflicts) {
4080 PyObject* comma = PyUnicode_FromString(", ");
4081 if (comma == NULL) {
4082 goto fail;
4083 }
4084 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4085 Py_DECREF(comma);
4086 if (error_names == NULL) {
4087 goto fail;
4088 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004089 _PyErr_Format(tstate, PyExc_TypeError,
4090 "%U() got some positional-only arguments passed"
4091 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004092 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004093 Py_DECREF(error_names);
4094 goto fail;
4095 }
4096
4097 Py_DECREF(posonly_names);
4098 return 0;
4099
4100fail:
4101 Py_XDECREF(posonly_names);
4102 return 1;
4103
4104}
4105
Guido van Rossumc2e20742006-02-27 22:32:47 +00004106/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004107 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004108 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004109
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004110PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004111_PyEval_EvalCode(PyThreadState *tstate,
4112 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004113 PyObject *const *args, Py_ssize_t argcount,
4114 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004115 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004116 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004117 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004118 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004119{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004120 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004121
Victor Stinner232dda62020-06-04 15:19:02 +02004122 PyCodeObject *co = (PyCodeObject*)_co;
4123
4124 if (!name) {
4125 name = co->co_name;
4126 }
4127 assert(name != NULL);
4128 assert(PyUnicode_Check(name));
4129
4130 if (!qualname) {
4131 qualname = name;
4132 }
4133 assert(qualname != NULL);
4134 assert(PyUnicode_Check(qualname));
4135
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004136 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004137 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004140 _PyErr_SetString(tstate, PyExc_SystemError,
4141 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 return NULL;
4143 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004144
Victor Stinnerc7020012016-08-16 23:40:29 +02004145 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004146 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004147 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004149 }
Victor Stinner232dda62020-06-04 15:19:02 +02004150 PyObject **fastlocals = f->f_localsplus;
4151 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004152
Victor Stinnerc7020012016-08-16 23:40:29 +02004153 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004154 PyObject *kwdict;
4155 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004156 if (co->co_flags & CO_VARKEYWORDS) {
4157 kwdict = PyDict_New();
4158 if (kwdict == NULL)
4159 goto fail;
4160 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004161 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004162 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004163 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 SETLOCAL(i, kwdict);
4165 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004166 else {
4167 kwdict = NULL;
4168 }
4169
Pablo Galindocd74e662019-06-01 18:08:04 +01004170 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004171 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004172 if (argcount > co->co_argcount) {
4173 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004174 }
4175 else {
4176 n = argcount;
4177 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004178 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004179 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004180 Py_INCREF(x);
4181 SETLOCAL(j, x);
4182 }
4183
Victor Stinnerc7020012016-08-16 23:40:29 +02004184 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004185 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004186 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004187 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004188 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004189 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004190 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004191 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004192
Serhiy Storchakab7281052016-09-12 00:52:40 +03004193 /* Handle keyword arguments passed as two strided arrays */
4194 kwcount *= kwstep;
4195 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004196 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004197 PyObject *keyword = kwnames[i];
4198 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004199 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004200
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004202 _PyErr_Format(tstate, PyExc_TypeError,
4203 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004204 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004205 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004207
Benjamin Petersonb204a422011-06-05 22:04:07 -05004208 /* Speed hack: do raw pointer compares. As names are
4209 normally interned this should almost always hit. */
4210 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004211 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004212 PyObject *varname = co_varnames[j];
4213 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004214 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004215 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004216 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004217
Benjamin Petersonb204a422011-06-05 22:04:07 -05004218 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004219 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004220 PyObject *varname = co_varnames[j];
4221 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004222 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004223 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004224 }
4225 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004227 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004228 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004229
Victor Stinner231d1f32017-01-11 02:12:06 +01004230 assert(j >= total_args);
4231 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004232
Victor Stinner438a12d2019-05-24 17:01:38 +02004233 if (co->co_posonlyargcount
4234 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004235 kwcount, kwnames,
4236 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004237 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004238 goto fail;
4239 }
4240
Victor Stinner438a12d2019-05-24 17:01:38 +02004241 _PyErr_Format(tstate, PyExc_TypeError,
4242 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004243 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004244 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004246
Christian Heimes0bd447f2013-07-20 14:48:10 +02004247 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4248 goto fail;
4249 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004250 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004251
Benjamin Petersonb204a422011-06-05 22:04:07 -05004252 kw_found:
4253 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004254 _PyErr_Format(tstate, PyExc_TypeError,
4255 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004256 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004257 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004259 Py_INCREF(value);
4260 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004262
4263 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004264 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004265 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4266 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 goto fail;
4268 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004269
4270 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004271 if (argcount < co->co_argcount) {
4272 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004273 Py_ssize_t missing = 0;
4274 for (i = argcount; i < m; i++) {
4275 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004276 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004277 }
4278 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004279 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004280 missing_arguments(tstate, co, missing, defcount, fastlocals,
4281 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004282 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004283 }
4284 if (n > m)
4285 i = n - m;
4286 else
4287 i = 0;
4288 for (; i < defcount; i++) {
4289 if (GETLOCAL(m+i) == NULL) {
4290 PyObject *def = defs[i];
4291 Py_INCREF(def);
4292 SETLOCAL(m+i, def);
4293 }
4294 }
4295 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004296
4297 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004298 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004299 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004300 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004301 if (GETLOCAL(i) != NULL)
4302 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004303 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004304 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004305 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004306 if (def) {
4307 Py_INCREF(def);
4308 SETLOCAL(i, def);
4309 continue;
4310 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004311 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004312 goto fail;
4313 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004314 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004315 missing++;
4316 }
4317 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004318 missing_arguments(tstate, co, missing, -1, fastlocals,
4319 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004320 goto fail;
4321 }
4322 }
4323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004325 vars into frame. */
4326 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004328 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004329 /* Possibly account for the cell variable being an argument. */
4330 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004331 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004332 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004333 /* Clear the local copy. */
4334 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004335 }
4336 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004337 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004338 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004339 if (c == NULL)
4340 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004341 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004343
4344 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004345 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4346 PyObject *o = PyTuple_GET_ITEM(closure, i);
4347 Py_INCREF(o);
4348 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004350
Yury Selivanoveb636452016-09-08 22:01:51 -07004351 /* Handle generator/coroutine/asynchronous generator */
4352 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004353 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004354 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 /* Don't need to keep the reference to f_back, it will be set
4357 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004358 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 /* Create a new generator that owns the ready to run frame
4361 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004362 if (is_coro) {
4363 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004364 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4365 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004366 } else {
4367 gen = PyGen_NewWithQualName(f, name, qualname);
4368 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004369 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004370 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004371 }
INADA Naoki9c157762016-12-26 18:52:46 +09004372
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004373 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004374
Yury Selivanov75445082015-05-11 22:57:16 -04004375 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004377
Victor Stinnerb9e68122019-11-14 12:20:46 +01004378 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004379
Thomas Woutersce272b62007-09-19 21:19:28 +00004380fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 /* decref'ing the frame can cause __del__ methods to get invoked,
4383 which can call back into Python. While we're done with the
4384 current Python frame (f), the associated C stack is still in use,
4385 so recursion_depth must be boosted for the duration.
4386 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004387 if (Py_REFCNT(f) > 1) {
4388 Py_DECREF(f);
4389 _PyObject_GC_TRACK(f);
4390 }
4391 else {
4392 ++tstate->recursion_depth;
4393 Py_DECREF(f);
4394 --tstate->recursion_depth;
4395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004397}
4398
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004399
4400PyObject *
4401_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4402 PyObject *const *args, Py_ssize_t argcount,
4403 PyObject *const *kwnames, PyObject *const *kwargs,
4404 Py_ssize_t kwcount, int kwstep,
4405 PyObject *const *defs, Py_ssize_t defcount,
4406 PyObject *kwdefs, PyObject *closure,
4407 PyObject *name, PyObject *qualname)
4408{
4409 PyThreadState *tstate = _PyThreadState_GET();
4410 return _PyEval_EvalCode(tstate, _co, globals, locals,
4411 args, argcount,
4412 kwnames, kwargs,
4413 kwcount, kwstep,
4414 defs, defcount,
4415 kwdefs, closure,
4416 name, qualname);
4417}
4418
Victor Stinner40ee3012014-06-16 15:59:28 +02004419PyObject *
4420PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004421 PyObject *const *args, int argcount,
4422 PyObject *const *kws, int kwcount,
4423 PyObject *const *defs, int defcount,
4424 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004425{
4426 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004427 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004428 kws, kws != NULL ? kws + 1 : NULL,
4429 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004430 defs, defcount,
4431 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004432 NULL, NULL);
4433}
Tim Peters5ca576e2001-06-18 22:08:13 +00004434
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004435static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004436special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004439 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004440 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004441 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 return NULL;
4443 }
4444 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004445}
4446
4447
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004448/* Logic for the raise statement (too complicated for inlining).
4449 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004450static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004451do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (exc == NULL) {
4456 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004457 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004459 type = exc_info->exc_type;
4460 value = exc_info->exc_value;
4461 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004462 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004463 _PyErr_SetString(tstate, PyExc_RuntimeError,
4464 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004465 return 0;
4466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 Py_XINCREF(type);
4468 Py_XINCREF(value);
4469 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004470 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004471 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 /* We support the following forms of raise:
4475 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004476 raise <instance>
4477 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 if (PyExceptionClass_Check(exc)) {
4480 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004481 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 if (value == NULL)
4483 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004484 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004485 _PyErr_Format(tstate, PyExc_TypeError,
4486 "calling %R should have returned an instance of "
4487 "BaseException, not %R",
4488 type, Py_TYPE(value));
4489 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
4492 else if (PyExceptionInstance_Check(exc)) {
4493 value = exc;
4494 type = PyExceptionInstance_Class(exc);
4495 Py_INCREF(type);
4496 }
4497 else {
4498 /* Not something you can raise. You get an exception
4499 anyway, just not what you specified :-) */
4500 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_SetString(tstate, PyExc_TypeError,
4502 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 goto raise_error;
4504 }
Collin Winter828f04a2007-08-31 00:04:24 +00004505
Serhiy Storchakac0191582016-09-27 11:37:10 +03004506 assert(type != NULL);
4507 assert(value != NULL);
4508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 if (cause) {
4510 PyObject *fixed_cause;
4511 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004512 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 if (fixed_cause == NULL)
4514 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004515 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004517 else if (PyExceptionInstance_Check(cause)) {
4518 fixed_cause = cause;
4519 }
4520 else if (cause == Py_None) {
4521 Py_DECREF(cause);
4522 fixed_cause = NULL;
4523 }
4524 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004525 _PyErr_SetString(tstate, PyExc_TypeError,
4526 "exception causes must derive from "
4527 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 goto raise_error;
4529 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004530 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 }
Collin Winter828f04a2007-08-31 00:04:24 +00004532
Victor Stinner438a12d2019-05-24 17:01:38 +02004533 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004534 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004535 Py_DECREF(value);
4536 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004537 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004538
4539raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 Py_XDECREF(value);
4541 Py_XDECREF(type);
4542 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004543 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004544}
4545
Tim Petersd6d010b2001-06-21 02:49:55 +00004546/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004547 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004548
Guido van Rossum0368b722007-05-11 16:50:42 +00004549 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4550 with a variable target.
4551*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004552
Barry Warsawe42b18f1997-08-25 22:13:04 +00004553static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004554unpack_iterable(PyThreadState *tstate, PyObject *v,
4555 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 int i = 0, j = 0;
4558 Py_ssize_t ll = 0;
4559 PyObject *it; /* iter(v) */
4560 PyObject *w;
4561 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004566 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004567 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004568 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004569 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004570 _PyErr_Format(tstate, PyExc_TypeError,
4571 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004572 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004573 }
4574 return 0;
4575 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 for (; i < argcnt; i++) {
4578 w = PyIter_Next(it);
4579 if (w == NULL) {
4580 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004581 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004582 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004583 _PyErr_Format(tstate, PyExc_ValueError,
4584 "not enough values to unpack "
4585 "(expected %d, got %d)",
4586 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004587 }
4588 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004589 _PyErr_Format(tstate, PyExc_ValueError,
4590 "not enough values to unpack "
4591 "(expected at least %d, got %d)",
4592 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
4595 goto Error;
4596 }
4597 *--sp = w;
4598 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 if (argcntafter == -1) {
4601 /* We better have exhausted the iterator now. */
4602 w = PyIter_Next(it);
4603 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004604 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 goto Error;
4606 Py_DECREF(it);
4607 return 1;
4608 }
4609 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004610 _PyErr_Format(tstate, PyExc_ValueError,
4611 "too many values to unpack (expected %d)",
4612 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 goto Error;
4614 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 l = PySequence_List(it);
4617 if (l == NULL)
4618 goto Error;
4619 *--sp = l;
4620 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 ll = PyList_GET_SIZE(l);
4623 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004624 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004625 "not enough values to unpack (expected at least %d, got %zd)",
4626 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 goto Error;
4628 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 /* Pop the "after-variable" args off the list. */
4631 for (j = argcntafter; j > 0; j--, i++) {
4632 *--sp = PyList_GET_ITEM(l, ll - j);
4633 }
4634 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004635 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 Py_DECREF(it);
4637 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004638
Tim Petersd6d010b2001-06-21 02:49:55 +00004639Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 for (; i > 0; i--, sp++)
4641 Py_DECREF(*sp);
4642 Py_XDECREF(it);
4643 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004644}
4645
4646
Guido van Rossum96a42c81992-01-12 02:29:51 +00004647#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004648static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004649prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004652 if (PyObject_Print(v, stdout, 0) != 0) {
4653 /* Don't know what else to do */
4654 _PyErr_Clear(tstate);
4655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 printf("\n");
4657 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004658}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004659#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004660
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004661static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004662call_exc_trace(Py_tracefunc func, PyObject *self,
4663 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004664{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004665 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004667 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 if (value == NULL) {
4669 value = Py_None;
4670 Py_INCREF(value);
4671 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004672 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004673 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 arg = PyTuple_Pack(3, type, value, traceback);
4675 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004676 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 return;
4678 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004679 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004681 if (err == 0) {
4682 _PyErr_Restore(tstate, type, value, orig_traceback);
4683 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 else {
4685 Py_XDECREF(type);
4686 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004687 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004689}
4690
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004691static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004692call_trace_protected(Py_tracefunc func, PyObject *obj,
4693 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 PyObject *type, *value, *traceback;
4697 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004698 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004699 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 if (err == 0)
4701 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004702 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 return 0;
4704 }
4705 else {
4706 Py_XDECREF(type);
4707 Py_XDECREF(value);
4708 Py_XDECREF(traceback);
4709 return -1;
4710 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004711}
4712
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004713static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004714call_trace(Py_tracefunc func, PyObject *obj,
4715 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 int result;
4719 if (tstate->tracing)
4720 return 0;
4721 tstate->tracing++;
4722 tstate->use_tracing = 0;
4723 result = func(obj, frame, what, arg);
4724 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4725 || (tstate->c_profilefunc != NULL));
4726 tstate->tracing--;
4727 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004728}
4729
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004730PyObject *
4731_PyEval_CallTracing(PyObject *func, PyObject *args)
4732{
Victor Stinner50b48572018-11-01 01:51:40 +01004733 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 int save_tracing = tstate->tracing;
4735 int save_use_tracing = tstate->use_tracing;
4736 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 tstate->tracing = 0;
4739 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4740 || (tstate->c_profilefunc != NULL));
4741 result = PyObject_Call(func, args, NULL);
4742 tstate->tracing = save_tracing;
4743 tstate->use_tracing = save_use_tracing;
4744 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004745}
4746
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004747/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004748static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004749maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004750 PyThreadState *tstate, PyFrameObject *frame,
4751 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 int result = 0;
4754 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 /* If the last instruction executed isn't in the current
4757 instruction window, reset the window.
4758 */
4759 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4760 PyAddrPair bounds;
4761 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4762 &bounds);
4763 *instr_lb = bounds.ap_lower;
4764 *instr_ub = bounds.ap_upper;
4765 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004766 /* If the last instruction falls at the start of a line or if it
4767 represents a jump backwards, update the frame's line number and
4768 then call the trace function if we're tracing source lines.
4769 */
4770 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004772 if (frame->f_trace_lines) {
4773 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 }
George King20faa682017-10-18 17:44:22 -07004776 /* Always emit an opcode event if we're tracing all opcodes. */
4777 if (frame->f_trace_opcodes) {
4778 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4779 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 *instr_prev = frame->f_lasti;
4781 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004782}
4783
Victor Stinner309d7cc2020-03-13 16:39:12 +01004784int
4785_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4786{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004787 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004788 /* The caller must hold the GIL */
4789 assert(PyGILState_Check());
4790
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004791 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004792 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004793 PyThreadState *current_tstate = _PyThreadState_GET();
4794 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004795 return -1;
4796 }
4797
4798 PyObject *profileobj = tstate->c_profileobj;
4799
4800 tstate->c_profilefunc = NULL;
4801 tstate->c_profileobj = NULL;
4802 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4803 tstate->use_tracing = tstate->c_tracefunc != NULL;
4804 Py_XDECREF(profileobj);
4805
4806 Py_XINCREF(arg);
4807 tstate->c_profileobj = arg;
4808 tstate->c_profilefunc = func;
4809
4810 /* Flag that tracing or profiling is turned on */
4811 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4812 return 0;
4813}
4814
Fred Drake5755ce62001-06-27 19:19:46 +00004815void
4816PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004817{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004818 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004819 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004820 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004821 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4822 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004823}
4824
4825int
4826_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4827{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004828 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004829 /* The caller must hold the GIL */
4830 assert(PyGILState_Check());
4831
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004832 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004833 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004834 PyThreadState *current_tstate = _PyThreadState_GET();
4835 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004836 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004837 }
4838
Victor Stinnerda2914d2020-03-20 09:29:08 +01004839 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004840 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004841 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004842
4843 tstate->c_tracefunc = NULL;
4844 tstate->c_traceobj = NULL;
4845 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4846 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4847 Py_XDECREF(traceobj);
4848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004850 tstate->c_traceobj = arg;
4851 tstate->c_tracefunc = func;
4852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004854 tstate->use_tracing = ((func != NULL)
4855 || (tstate->c_profilefunc != NULL));
4856
4857 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004858}
4859
4860void
4861PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4862{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004863 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004864 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004865 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004866 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4867 }
Fred Draked0838392001-06-16 21:02:31 +00004868}
4869
Victor Stinner309d7cc2020-03-13 16:39:12 +01004870
Yury Selivanov75445082015-05-11 22:57:16 -04004871void
Victor Stinner838f2642019-06-13 22:41:23 +02004872_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004873{
4874 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004875 tstate->coroutine_origin_tracking_depth = new_depth;
4876}
4877
4878int
4879_PyEval_GetCoroutineOriginTrackingDepth(void)
4880{
Victor Stinner50b48572018-11-01 01:51:40 +01004881 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004882 return tstate->coroutine_origin_tracking_depth;
4883}
4884
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004885int
Yury Selivanoveb636452016-09-08 22:01:51 -07004886_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4887{
Victor Stinner50b48572018-11-01 01:51:40 +01004888 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004889
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004890 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004891 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004892 }
4893
Yury Selivanoveb636452016-09-08 22:01:51 -07004894 Py_XINCREF(firstiter);
4895 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004896 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004897}
4898
4899PyObject *
4900_PyEval_GetAsyncGenFirstiter(void)
4901{
Victor Stinner50b48572018-11-01 01:51:40 +01004902 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004903 return tstate->async_gen_firstiter;
4904}
4905
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004906int
Yury Selivanoveb636452016-09-08 22:01:51 -07004907_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4908{
Victor Stinner50b48572018-11-01 01:51:40 +01004909 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004910
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004911 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004912 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004913 }
4914
Yury Selivanoveb636452016-09-08 22:01:51 -07004915 Py_XINCREF(finalizer);
4916 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004917 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004918}
4919
4920PyObject *
4921_PyEval_GetAsyncGenFinalizer(void)
4922{
Victor Stinner50b48572018-11-01 01:51:40 +01004923 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004924 return tstate->async_gen_finalizer;
4925}
4926
Victor Stinner438a12d2019-05-24 17:01:38 +02004927PyFrameObject *
4928PyEval_GetFrame(void)
4929{
4930 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004931 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004932}
4933
Guido van Rossumb209a111997-04-29 18:18:01 +00004934PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004935PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004936{
Victor Stinner438a12d2019-05-24 17:01:38 +02004937 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004938 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004940 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 else
4942 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004943}
4944
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004945/* Convenience function to get a builtin from its name */
4946PyObject *
4947_PyEval_GetBuiltinId(_Py_Identifier *name)
4948{
Victor Stinner438a12d2019-05-24 17:01:38 +02004949 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004950 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4951 if (attr) {
4952 Py_INCREF(attr);
4953 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004954 else if (!_PyErr_Occurred(tstate)) {
4955 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004956 }
4957 return attr;
4958}
4959
Guido van Rossumb209a111997-04-29 18:18:01 +00004960PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004961PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004962{
Victor Stinner438a12d2019-05-24 17:01:38 +02004963 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004964 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004965 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004966 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004967 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004968 }
4969
Victor Stinner438a12d2019-05-24 17:01:38 +02004970 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004971 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004972 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004973
4974 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004976}
4977
Guido van Rossumb209a111997-04-29 18:18:01 +00004978PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004979PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004980{
Victor Stinner438a12d2019-05-24 17:01:38 +02004981 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004982 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004983 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004985 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004986
4987 assert(current_frame->f_globals != NULL);
4988 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004989}
4990
Guido van Rossum6135a871995-01-09 17:53:26 +00004991int
Tim Peters5ba58662001-07-16 02:29:45 +00004992PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004993{
Victor Stinner438a12d2019-05-24 17:01:38 +02004994 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004995 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 if (current_frame != NULL) {
4999 const int codeflags = current_frame->f_code->co_flags;
5000 const int compilerflags = codeflags & PyCF_MASK;
5001 if (compilerflags) {
5002 result = 1;
5003 cf->cf_flags |= compilerflags;
5004 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005005#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 if (codeflags & CO_GENERATOR_ALLOWED) {
5007 result = 1;
5008 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5009 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005010#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 }
5012 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005013}
5014
Guido van Rossum3f5da241990-12-20 15:06:42 +00005015
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005016const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005017PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 if (PyMethod_Check(func))
5020 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5021 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005022 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 else if (PyCFunction_Check(func))
5024 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5025 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005026 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005027}
5028
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005029const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005030PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 if (PyMethod_Check(func))
5033 return "()";
5034 else if (PyFunction_Check(func))
5035 return "()";
5036 else if (PyCFunction_Check(func))
5037 return "()";
5038 else
5039 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005040}
5041
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005042#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005043if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005044 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5045 tstate, tstate->frame, \
5046 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 x = NULL; \
5048 } \
5049 else { \
5050 x = call; \
5051 if (tstate->c_profilefunc != NULL) { \
5052 if (x == NULL) { \
5053 call_trace_protected(tstate->c_profilefunc, \
5054 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005055 tstate, tstate->frame, \
5056 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 /* XXX should pass (type, value, tb) */ \
5058 } else { \
5059 if (call_trace(tstate->c_profilefunc, \
5060 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005061 tstate, tstate->frame, \
5062 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 Py_DECREF(x); \
5064 x = NULL; \
5065 } \
5066 } \
5067 } \
5068 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005069} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 x = call; \
5071 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005072
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005073
5074static PyObject *
5075trace_call_function(PyThreadState *tstate,
5076 PyObject *func,
5077 PyObject **args, Py_ssize_t nargs,
5078 PyObject *kwnames)
5079{
5080 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005081 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005082 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005083 return x;
5084 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005085 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005086 /* We need to create a temporary bound method as argument
5087 for profiling.
5088
5089 If nargs == 0, then this cannot work because we have no
5090 "self". In any case, the call itself would raise
5091 TypeError (foo needs an argument), so we just skip
5092 profiling. */
5093 PyObject *self = args[0];
5094 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5095 if (func == NULL) {
5096 return NULL;
5097 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005098 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005099 args+1, nargs-1,
5100 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005101 Py_DECREF(func);
5102 return x;
5103 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005104 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005105}
5106
Victor Stinner415c5102017-01-11 00:54:57 +01005107/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5108 to reduce the stack consumption. */
5109Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005110call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005111{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005112 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 PyObject *func = *pfunc;
5114 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005115 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5116 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005117 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005118
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005119 if (tstate->use_tracing) {
5120 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005121 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005122 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005123 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005125
Victor Stinner438a12d2019-05-24 17:01:38 +02005126 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005127
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005128 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 while ((*pp_stack) > pfunc) {
5130 w = EXT_POP(*pp_stack);
5131 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005135}
5136
Jeremy Hylton52820442001-01-03 23:52:36 +00005137static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005138do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005139{
jdemeyere89de732018-09-19 12:06:20 +02005140 PyObject *result;
5141
scoder4c9ea092020-05-12 16:12:41 +02005142 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005143 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005144 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005146 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005147 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5148 if (nargs > 0 && tstate->use_tracing) {
5149 /* We need to create a temporary bound method as argument
5150 for profiling.
5151
5152 If nargs == 0, then this cannot work because we have no
5153 "self". In any case, the call itself would raise
5154 TypeError (foo needs an argument), so we just skip
5155 profiling. */
5156 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5157 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5158 if (func == NULL) {
5159 return NULL;
5160 }
5161
Victor Stinner4d231bc2019-11-14 13:36:21 +01005162 C_TRACE(result, _PyObject_FastCallDictTstate(
5163 tstate, func,
5164 &_PyTuple_ITEMS(callargs)[1],
5165 nargs - 1,
5166 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005167 Py_DECREF(func);
5168 return result;
5169 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005170 }
jdemeyere89de732018-09-19 12:06:20 +02005171 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005172}
5173
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005174/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005175 nb_index slot defined, and store in *pi.
5176 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005177 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005178 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005179*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005180int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005181_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005182{
Victor Stinner438a12d2019-05-24 17:01:38 +02005183 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005184 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005186 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005188 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 return 0;
5190 }
5191 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005192 _PyErr_SetString(tstate, PyExc_TypeError,
5193 "slice indices must be integers or "
5194 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 return 0;
5196 }
5197 *pi = x;
5198 }
5199 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005200}
5201
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005202int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005203_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005204{
Victor Stinner438a12d2019-05-24 17:01:38 +02005205 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005206 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005207 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005208 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005209 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005210 return 0;
5211 }
5212 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005213 _PyErr_SetString(tstate, PyExc_TypeError,
5214 "slice indices must be integers or "
5215 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005216 return 0;
5217 }
5218 *pi = x;
5219 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005220}
5221
Thomas Wouters52152252000-08-17 22:55:00 +00005222static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005223import_name(PyThreadState *tstate, PyFrameObject *f,
5224 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005225{
5226 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005227 PyObject *import_func, *res;
5228 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005229
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005230 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005231 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005232 if (!_PyErr_Occurred(tstate)) {
5233 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005234 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005235 return NULL;
5236 }
5237
5238 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005239 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005240 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005241 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005242 return NULL;
5243 }
5244 res = PyImport_ImportModuleLevelObject(
5245 name,
5246 f->f_globals,
5247 f->f_locals == NULL ? Py_None : f->f_locals,
5248 fromlist,
5249 ilevel);
5250 return res;
5251 }
5252
5253 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005254
5255 stack[0] = name;
5256 stack[1] = f->f_globals;
5257 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5258 stack[3] = fromlist;
5259 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005260 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005261 Py_DECREF(import_func);
5262 return res;
5263}
5264
5265static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005266import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005269 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005270
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005271 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005272 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005273 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005274 /* Issue #17636: in case this failed because of a circular relative
5275 import, try to fallback on reading the module directly from
5276 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005277 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005278 if (pkgname == NULL) {
5279 goto error;
5280 }
Oren Milman6db70332017-09-19 14:23:01 +03005281 if (!PyUnicode_Check(pkgname)) {
5282 Py_CLEAR(pkgname);
5283 goto error;
5284 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005285 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005286 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005287 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005288 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005289 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005290 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005291 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005292 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005293 goto error;
5294 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005295 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005297 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005298 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005299 if (pkgname == NULL) {
5300 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5301 if (pkgname_or_unknown == NULL) {
5302 Py_XDECREF(pkgpath);
5303 return NULL;
5304 }
5305 } else {
5306 pkgname_or_unknown = pkgname;
5307 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005308
5309 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005310 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005311 errmsg = PyUnicode_FromFormat(
5312 "cannot import name %R from %R (unknown location)",
5313 name, pkgname_or_unknown
5314 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005315 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005316 PyErr_SetImportError(errmsg, pkgname, NULL);
5317 }
5318 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005319 _Py_IDENTIFIER(__spec__);
5320 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005321 const char *fmt =
5322 _PyModuleSpec_IsInitializing(spec) ?
5323 "cannot import name %R from partially initialized module %R "
5324 "(most likely due to a circular import) (%S)" :
5325 "cannot import name %R from %R (%S)";
5326 Py_XDECREF(spec);
5327
5328 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005329 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005330 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005331 }
5332
Xiang Zhang4830f582017-03-21 11:13:42 +08005333 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005334 Py_XDECREF(pkgname_or_unknown);
5335 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005336 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005337}
Guido van Rossumac7be682001-01-17 15:42:30 +00005338
Thomas Wouters52152252000-08-17 22:55:00 +00005339static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005340import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005341{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005342 _Py_IDENTIFIER(__all__);
5343 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005344 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 int skip_leading_underscores = 0;
5346 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005347
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005348 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5349 return -1; /* Unexpected error */
5350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005352 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5353 return -1;
5354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005355 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005357 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 return -1;
5359 }
5360 all = PyMapping_Keys(dict);
5361 Py_DECREF(dict);
5362 if (all == NULL)
5363 return -1;
5364 skip_leading_underscores = 1;
5365 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 for (pos = 0, err = 0; ; pos++) {
5368 name = PySequence_GetItem(all, pos);
5369 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005370 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005371 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005372 }
5373 else {
5374 _PyErr_Clear(tstate);
5375 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 break;
5377 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005378 if (!PyUnicode_Check(name)) {
5379 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5380 if (modname == NULL) {
5381 Py_DECREF(name);
5382 err = -1;
5383 break;
5384 }
5385 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005386 _PyErr_Format(tstate, PyExc_TypeError,
5387 "module __name__ must be a string, not %.100s",
5388 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005389 }
5390 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005391 _PyErr_Format(tstate, PyExc_TypeError,
5392 "%s in %U.%s must be str, not %.100s",
5393 skip_leading_underscores ? "Key" : "Item",
5394 modname,
5395 skip_leading_underscores ? "__dict__" : "__all__",
5396 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005397 }
5398 Py_DECREF(modname);
5399 Py_DECREF(name);
5400 err = -1;
5401 break;
5402 }
5403 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005404 if (PyUnicode_READY(name) == -1) {
5405 Py_DECREF(name);
5406 err = -1;
5407 break;
5408 }
5409 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5410 Py_DECREF(name);
5411 continue;
5412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 }
5414 value = PyObject_GetAttr(v, name);
5415 if (value == NULL)
5416 err = -1;
5417 else if (PyDict_CheckExact(locals))
5418 err = PyDict_SetItem(locals, name, value);
5419 else
5420 err = PyObject_SetItem(locals, name, value);
5421 Py_DECREF(name);
5422 Py_XDECREF(value);
5423 if (err != 0)
5424 break;
5425 }
5426 Py_DECREF(all);
5427 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005428}
5429
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005430static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005431check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005432{
Victor Stinnera102ed72020-02-07 02:24:48 +01005433 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005434 /* check_args_iterable() may be called with a live exception:
5435 * clear it to prevent calling _PyObject_FunctionStr() with an
5436 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005437 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005438 PyObject *funcstr = _PyObject_FunctionStr(func);
5439 if (funcstr != NULL) {
5440 _PyErr_Format(tstate, PyExc_TypeError,
5441 "%U argument after * must be an iterable, not %.200s",
5442 funcstr, Py_TYPE(args)->tp_name);
5443 Py_DECREF(funcstr);
5444 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005445 return -1;
5446 }
5447 return 0;
5448}
5449
5450static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005451format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005452{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005453 /* _PyDict_MergeEx raises attribute
5454 * error (percolated from an attempt
5455 * to get 'keys' attribute) instead of
5456 * a type error if its second argument
5457 * is not a mapping.
5458 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005459 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005460 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005461 PyObject *funcstr = _PyObject_FunctionStr(func);
5462 if (funcstr != NULL) {
5463 _PyErr_Format(
5464 tstate, PyExc_TypeError,
5465 "%U argument after ** must be a mapping, not %.200s",
5466 funcstr, Py_TYPE(kwargs)->tp_name);
5467 Py_DECREF(funcstr);
5468 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005469 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005470 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005471 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005472 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005473 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005474 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005475 PyObject *funcstr = _PyObject_FunctionStr(func);
5476 if (funcstr != NULL) {
5477 PyObject *key = PyTuple_GET_ITEM(val, 0);
5478 _PyErr_Format(
5479 tstate, PyExc_TypeError,
5480 "%U got multiple values for keyword argument '%S'",
5481 funcstr, key);
5482 Py_DECREF(funcstr);
5483 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005484 Py_XDECREF(exc);
5485 Py_XDECREF(val);
5486 Py_XDECREF(tb);
5487 }
5488 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005489 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005490 }
5491 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005492}
5493
Guido van Rossumac7be682001-01-17 15:42:30 +00005494static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005495format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5496 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005498 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 if (!obj)
5501 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005502
Serhiy Storchaka06515832016-11-20 09:13:07 +02005503 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 if (!obj_str)
5505 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005506
Victor Stinner438a12d2019-05-24 17:01:38 +02005507 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005508}
Guido van Rossum950361c1997-01-24 13:49:28 +00005509
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005510static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005511format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005512{
5513 PyObject *name;
5514 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005515 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005516 return;
5517 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5518 name = PyTuple_GET_ITEM(co->co_cellvars,
5519 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005520 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005521 PyExc_UnboundLocalError,
5522 UNBOUNDLOCAL_ERROR_MSG,
5523 name);
5524 } else {
5525 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5526 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005527 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005528 UNBOUNDFREE_ERROR_MSG, name);
5529 }
5530}
5531
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005532static void
Mark Shannonfee55262019-11-21 09:11:43 +00005533format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005534{
5535 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5536 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005537 _PyErr_Format(tstate, PyExc_TypeError,
5538 "'async with' received an object from __aenter__ "
5539 "that does not implement __await__: %.100s",
5540 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005541 }
Mark Shannonfee55262019-11-21 09:11:43 +00005542 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005543 _PyErr_Format(tstate, PyExc_TypeError,
5544 "'async with' received an object from __aexit__ "
5545 "that does not implement __await__: %.100s",
5546 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005547 }
5548 }
5549}
5550
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005551static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005552unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005553 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005554{
5555 PyObject *res;
5556 if (Py_REFCNT(v) == 2) {
5557 /* In the common case, there are 2 references to the value
5558 * stored in 'variable' when the += is performed: one on the
5559 * value stack (in 'v') and one still stored in the
5560 * 'variable'. We try to delete the variable now to reduce
5561 * the refcnt to 1.
5562 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005563 int opcode, oparg;
5564 NEXTOPARG();
5565 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005566 case STORE_FAST:
5567 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005568 PyObject **fastlocals = f->f_localsplus;
5569 if (GETLOCAL(oparg) == v)
5570 SETLOCAL(oparg, NULL);
5571 break;
5572 }
5573 case STORE_DEREF:
5574 {
5575 PyObject **freevars = (f->f_localsplus +
5576 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005577 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005578 if (PyCell_GET(c) == v) {
5579 PyCell_SET(c, NULL);
5580 Py_DECREF(v);
5581 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005582 break;
5583 }
5584 case STORE_NAME:
5585 {
5586 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005587 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005588 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005589 if (locals && PyDict_CheckExact(locals)) {
5590 PyObject *w = PyDict_GetItemWithError(locals, name);
5591 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005592 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005593 {
5594 Py_DECREF(v);
5595 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005596 }
5597 }
5598 break;
5599 }
5600 }
5601 }
5602 res = v;
5603 PyUnicode_Append(&res, w);
5604 return res;
5605}
5606
Guido van Rossum950361c1997-01-24 13:49:28 +00005607#ifdef DYNAMIC_EXECUTION_PROFILE
5608
Skip Montanarof118cb12001-10-15 20:51:38 +00005609static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005610getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005612 int i;
5613 PyObject *l = PyList_New(256);
5614 if (l == NULL) return NULL;
5615 for (i = 0; i < 256; i++) {
5616 PyObject *x = PyLong_FromLong(a[i]);
5617 if (x == NULL) {
5618 Py_DECREF(l);
5619 return NULL;
5620 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005621 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005622 }
5623 for (i = 0; i < 256; i++)
5624 a[i] = 0;
5625 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005626}
5627
5628PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005629_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005630{
5631#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005633#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005634 int i;
5635 PyObject *l = PyList_New(257);
5636 if (l == NULL) return NULL;
5637 for (i = 0; i < 257; i++) {
5638 PyObject *x = getarray(dxpairs[i]);
5639 if (x == NULL) {
5640 Py_DECREF(l);
5641 return NULL;
5642 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005643 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005644 }
5645 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005646#endif
5647}
5648
5649#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005650
5651Py_ssize_t
5652_PyEval_RequestCodeExtraIndex(freefunc free)
5653{
Victor Stinner81a7be32020-04-14 15:14:01 +02005654 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005655 Py_ssize_t new_index;
5656
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005657 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005658 return -1;
5659 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005660 new_index = interp->co_extra_user_count++;
5661 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005662 return new_index;
5663}
Łukasz Langaa785c872016-09-09 17:37:37 -07005664
5665static void
5666dtrace_function_entry(PyFrameObject *f)
5667{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005668 const char *filename;
5669 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005670 int lineno;
5671
Victor Stinner6d86a232020-04-29 00:56:58 +02005672 PyCodeObject *code = f->f_code;
5673 filename = PyUnicode_AsUTF8(code->co_filename);
5674 funcname = PyUnicode_AsUTF8(code->co_name);
5675 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005676
Andy Lestere6be9b52020-02-11 20:28:35 -06005677 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005678}
5679
5680static void
5681dtrace_function_return(PyFrameObject *f)
5682{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005683 const char *filename;
5684 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005685 int lineno;
5686
Victor Stinner6d86a232020-04-29 00:56:58 +02005687 PyCodeObject *code = f->f_code;
5688 filename = PyUnicode_AsUTF8(code->co_filename);
5689 funcname = PyUnicode_AsUTF8(code->co_name);
5690 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005691
Andy Lestere6be9b52020-02-11 20:28:35 -06005692 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005693}
5694
5695/* DTrace equivalent of maybe_call_line_trace. */
5696static void
5697maybe_dtrace_line(PyFrameObject *frame,
5698 int *instr_lb, int *instr_ub, int *instr_prev)
5699{
5700 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005701 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005702
5703 /* If the last instruction executed isn't in the current
5704 instruction window, reset the window.
5705 */
5706 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5707 PyAddrPair bounds;
5708 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5709 &bounds);
5710 *instr_lb = bounds.ap_lower;
5711 *instr_ub = bounds.ap_upper;
5712 }
5713 /* If the last instruction falls at the start of a line or if
5714 it represents a jump backwards, update the frame's line
5715 number and call the trace function. */
5716 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5717 frame->f_lineno = line;
5718 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5719 if (!co_filename)
5720 co_filename = "?";
5721 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5722 if (!co_name)
5723 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005724 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005725 }
5726 *instr_prev = frame->f_lasti;
5727}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005728
5729
5730/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5731 for the limited API. */
5732
5733#undef Py_EnterRecursiveCall
5734
5735int Py_EnterRecursiveCall(const char *where)
5736{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005737 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005738}
5739
5740#undef Py_LeaveRecursiveCall
5741
5742void Py_LeaveRecursiveCall(void)
5743{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005744 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005745}