blob: f747faaebf024a3a3ba0505088355f329e90a110 [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
744#define Py_DEFAULT_RECURSION_LIMIT 1000
745#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600746
Eric Snow05351c12017-09-05 21:43:08 -0700747int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000748
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600749void
Victor Stinnerdab84232020-03-17 18:56:44 +0100750_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600751{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600752 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinner7be4e352020-05-05 20:27:47 +0200753#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100754 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200755#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100756}
757
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200758int
Victor Stinnerdab84232020-03-17 18:56:44 +0100759_PyEval_InitState(struct _ceval_state *ceval)
760{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200761 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
762
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200763 struct _pending_calls *pending = &ceval->pending;
764 assert(pending->lock == NULL);
765
766 pending->lock = PyThread_allocate_lock();
767 if (pending->lock == NULL) {
768 return -1;
769 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200770
771#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
772 _gil_initialize(&ceval->gil);
773#endif
774
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200775 return 0;
776}
777
778void
779_PyEval_FiniState(struct _ceval_state *ceval)
780{
781 struct _pending_calls *pending = &ceval->pending;
782 if (pending->lock != NULL) {
783 PyThread_free_lock(pending->lock);
784 pending->lock = NULL;
785 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600786}
787
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000788int
789Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000790{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200791 PyInterpreterState *interp = _PyInterpreterState_GET();
792 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000793}
794
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000795void
796Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000797{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200798 PyThreadState *tstate = _PyThreadState_GET();
799 tstate->interp->ceval.recursion_limit = new_limit;
800 if (_Py_IsMainInterpreter(tstate)) {
801 _Py_CheckRecursionLimit = new_limit;
802 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000803}
804
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100805/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000806 if the recursion_depth reaches _Py_CheckRecursionLimit.
807 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
808 to guarantee that _Py_CheckRecursiveCall() is regularly called.
809 Without USE_STACKCHECK, there is no need for this. */
810int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100811_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000812{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200813 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000814
815#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700816 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (PyOS_CheckStack()) {
818 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200819 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 return -1;
821 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200822 if (_Py_IsMainInterpreter(tstate)) {
823 /* Needed for ABI backwards-compatibility (see bpo-31857) */
824 _Py_CheckRecursionLimit = recursion_limit;
825 }
pdox18967932017-10-25 23:03:01 -0700826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (tstate->recursion_critical)
828 /* Somebody asked that we don't check for recursion. */
829 return 0;
830 if (tstate->overflowed) {
831 if (tstate->recursion_depth > recursion_limit + 50) {
832 /* Overflowing while handling an overflow. Give up. */
833 Py_FatalError("Cannot recover from stack overflow.");
834 }
835 return 0;
836 }
837 if (tstate->recursion_depth > recursion_limit) {
838 --tstate->recursion_depth;
839 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200840 _PyErr_Format(tstate, PyExc_RecursionError,
841 "maximum recursion depth exceeded%s",
842 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 return -1;
844 }
845 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000846}
847
Victor Stinner09532fe2019-05-10 23:39:09 +0200848static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200849static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000850
Victor Stinnere225beb2019-06-03 18:14:24 +0200851#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000852
Guido van Rossum374a9221991-04-04 10:40:29 +0000853
Guido van Rossumb209a111997-04-29 18:18:01 +0000854PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000855PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 return PyEval_EvalCodeEx(co,
858 globals, locals,
859 (PyObject **)NULL, 0,
860 (PyObject **)NULL, 0,
861 (PyObject **)NULL, 0,
862 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000863}
864
865
866/* Interpreter main loop */
867
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000868PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100869PyEval_EvalFrame(PyFrameObject *f)
870{
Victor Stinner0b72b232020-03-12 23:18:39 +0100871 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100872 PyThreadState *tstate = _PyThreadState_GET();
873 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000874}
875
876PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000878{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100879 PyThreadState *tstate = _PyThreadState_GET();
880 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700881}
882
Victor Stinnerda2914d2020-03-20 09:29:08 +0100883
884/* Handle signals, pending calls, GIL drop request
885 and asynchronous exception */
886static int
887eval_frame_handle_pending(PyThreadState *tstate)
888{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100889 _PyRuntimeState * const runtime = &_PyRuntime;
890 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200891
892 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200893 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100894 if (handle_signals(tstate) != 0) {
895 return -1;
896 }
897 }
898
899 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200900 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100901 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
902 if (make_pending_calls(tstate) != 0) {
903 return -1;
904 }
905 }
906
907 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200908 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100909 /* Give another thread a chance */
910 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
911 Py_FatalError("tstate mix-up");
912 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200913 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100914
915 /* Other threads may run now */
916
917 take_gil(tstate);
918
Victor Stinnere838a932020-05-05 19:56:48 +0200919#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
920 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
921#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100922 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
923 Py_FatalError("orphan tstate");
924 }
Victor Stinnere838a932020-05-05 19:56:48 +0200925#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100926 }
927
928 /* Check for asynchronous exception. */
929 if (tstate->async_exc != NULL) {
930 PyObject *exc = tstate->async_exc;
931 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200932 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100933 _PyErr_SetNone(tstate, exc);
934 Py_DECREF(exc);
935 return -1;
936 }
937
938 return 0;
939}
940
Victor Stinnerc6944e72016-11-11 02:13:35 +0100941PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100942_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700943{
Victor Stinner3026cad2020-06-01 16:02:40 +0200944 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100945
Guido van Rossum950361c1997-01-24 13:49:28 +0000946#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000948#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200949 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300950 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200951 int opcode; /* Current opcode */
952 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200953 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100955 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100956 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 is true when the line being executed has changed. The
964 initial values are such as to make this false the first
965 time it is tested. */
966 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000967
Serhiy Storchakaab874002016-09-11 13:48:15 +0300968 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 PyObject *names;
970 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900971 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000972
Brett Cannon368b4b72012-04-02 12:17:59 -0400973#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200974 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400975#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200976
Antoine Pitroub52ec782009-01-25 16:34:23 +0000977/* Computed GOTOs, or
978 the-optimization-commonly-but-improperly-known-as-"threaded code"
979 using gcc's labels-as-values extension
980 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
981
982 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000984 combined with a lookup table of jump addresses. However, since the
985 indirect jump instruction is shared by all opcodes, the CPU will have a
986 hard time making the right prediction for where to jump next (actually,
987 it will be always wrong except in the uncommon case of a sequence of
988 several identical opcodes).
989
990 "Threaded code" in contrast, uses an explicit jump table and an explicit
991 indirect jump instruction at the end of each opcode. Since the jump
992 instruction is at a different address for each opcode, the CPU will make a
993 separate prediction for each of these instructions, which is equivalent to
994 predicting the second opcode of each opcode pair. These predictions have
995 a much better chance to turn out valid, especially in small bytecode loops.
996
997 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999 and potentially many more instructions (depending on the pipeline width).
1000 A correctly predicted branch, however, is nearly free.
1001
1002 At the time of this writing, the "threaded code" version is up to 15-20%
1003 faster than the normal "switch" version, depending on the compiler and the
1004 CPU architecture.
1005
1006 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1007 because it would render the measurements invalid.
1008
1009
1010 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1011 indirect jumps by sharing them between all opcodes. Such optimizations
1012 can be disabled on gcc by using the -fno-gcse flag (or possibly
1013 -fno-crossjumping).
1014*/
1015
Antoine Pitrou042b1282010-08-13 21:15:58 +00001016#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001018#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019#endif
1020
Antoine Pitrou042b1282010-08-13 21:15:58 +00001021#ifdef HAVE_COMPUTED_GOTOS
1022 #ifndef USE_COMPUTED_GOTOS
1023 #define USE_COMPUTED_GOTOS 1
1024 #endif
1025#else
1026 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1027 #error "Computed gotos are not supported on this compiler."
1028 #endif
1029 #undef USE_COMPUTED_GOTOS
1030 #define USE_COMPUTED_GOTOS 0
1031#endif
1032
1033#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001034/* Import the static jump table */
1035#include "opcode_targets.h"
1036
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001038 op: \
1039 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001040
Antoine Pitroub52ec782009-01-25 16:34:23 +00001041#ifdef LLTRACE
1042#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001044 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001046 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001047 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 } \
1049 goto fast_next_opcode; \
1050 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001051#else
1052#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001054 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001056 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001057 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 } \
1059 goto fast_next_opcode; \
1060 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001061#endif
1062
Victor Stinner09532fe2019-05-10 23:39:09 +02001063#define DISPATCH() \
1064 { \
1065 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1066 FAST_DISPATCH(); \
1067 } \
1068 continue; \
1069 }
1070
Antoine Pitroub52ec782009-01-25 16:34:23 +00001071#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001072#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001073#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001074#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001075#endif
1076
1077
Neal Norwitza81d2202002-07-14 00:27:26 +00001078/* Tuple access macros */
1079
1080#ifndef Py_DEBUG
1081#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1082#else
1083#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1084#endif
1085
Guido van Rossum374a9221991-04-04 10:40:29 +00001086/* Code access macros */
1087
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001088/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001089#define INSTR_OFFSET() \
1090 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001091#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001092 _Py_CODEUNIT word = *next_instr; \
1093 opcode = _Py_OPCODE(word); \
1094 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001095 next_instr++; \
1096 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001097#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1098#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001099
Raymond Hettingerf606f872003-03-16 03:11:04 +00001100/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 Some opcodes tend to come in pairs thus making it possible to
1102 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001103 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 Verifying the prediction costs a single high-speed test of a register
1106 variable against a constant. If the pairing was good, then the
1107 processor's own internal branch predication has a high likelihood of
1108 success, resulting in a nearly zero-overhead transition to the
1109 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001110 including its unpredictable switch-case branch. Combined with the
1111 processor's internal branch prediction, a successful PREDICT has the
1112 effect of making the two opcodes run as if they were a single new opcode
1113 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001114
Georg Brandl86b2fb92008-07-16 03:43:04 +00001115 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 predictions turned-on and interpret the results as if some opcodes
1117 had been combined or turn-off predictions so that the opcode frequency
1118 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001119
1120 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 the CPU to record separate branch prediction information for each
1122 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001123
Raymond Hettingerf606f872003-03-16 03:11:04 +00001124*/
1125
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001126#define PREDICT_ID(op) PRED_##op
1127
Antoine Pitrou042b1282010-08-13 21:15:58 +00001128#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001129#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001130#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001131#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001132 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001133 _Py_CODEUNIT word = *next_instr; \
1134 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001135 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001136 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001137 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001138 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001139 } \
1140 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001141#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001142#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001143
Raymond Hettingerf606f872003-03-16 03:11:04 +00001144
Guido van Rossum374a9221991-04-04 10:40:29 +00001145/* Stack manipulation macros */
1146
Martin v. Löwis18e16552006-02-15 17:27:45 +00001147/* The stack can grow at most MAXINT deep, as co_nlocals and
1148 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001149#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1150#define EMPTY() (STACK_LEVEL() == 0)
1151#define TOP() (stack_pointer[-1])
1152#define SECOND() (stack_pointer[-2])
1153#define THIRD() (stack_pointer[-3])
1154#define FOURTH() (stack_pointer[-4])
1155#define PEEK(n) (stack_pointer[-(n)])
1156#define SET_TOP(v) (stack_pointer[-1] = (v))
1157#define SET_SECOND(v) (stack_pointer[-2] = (v))
1158#define SET_THIRD(v) (stack_pointer[-3] = (v))
1159#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001160#define BASIC_STACKADJ(n) (stack_pointer += n)
1161#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1162#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001163
Guido van Rossum96a42c81992-01-12 02:29:51 +00001164#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001166 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001167 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001168#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001169 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001170#define STACK_GROW(n) do { \
1171 assert(n >= 0); \
1172 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001173 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001174 assert(STACK_LEVEL() <= co->co_stacksize); \
1175 } while (0)
1176#define STACK_SHRINK(n) do { \
1177 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001178 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001179 (void)(BASIC_STACKADJ(-n)); \
1180 assert(STACK_LEVEL() <= co->co_stacksize); \
1181 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001182#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001183 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001184 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001185#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001186#define PUSH(v) BASIC_PUSH(v)
1187#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001188#define STACK_GROW(n) BASIC_STACKADJ(n)
1189#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001190#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001191#endif
1192
Guido van Rossum681d79a1995-07-18 14:51:37 +00001193/* Local variable macros */
1194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001196
1197/* The SETLOCAL() macro must not DECREF the local variable in-place and
1198 then store the new value; it must copy the old value to a temporary
1199 value, then store the new value, and then DECREF the temporary value.
1200 This is because it is possible that during the DECREF the frame is
1201 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1202 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001204 GETLOCAL(i) = value; \
1205 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001206
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001207
1208#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 while (STACK_LEVEL() > (b)->b_level) { \
1210 PyObject *v = POP(); \
1211 Py_XDECREF(v); \
1212 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001213
1214#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001215 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001217 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1219 while (STACK_LEVEL() > (b)->b_level + 3) { \
1220 value = POP(); \
1221 Py_XDECREF(value); \
1222 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001223 exc_info = tstate->exc_info; \
1224 type = exc_info->exc_type; \
1225 value = exc_info->exc_value; \
1226 traceback = exc_info->exc_traceback; \
1227 exc_info->exc_type = POP(); \
1228 exc_info->exc_value = POP(); \
1229 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_XDECREF(type); \
1231 Py_XDECREF(value); \
1232 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001233 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001234
Inada Naoki91234a12019-06-03 21:30:58 +09001235 /* macros for opcode cache */
1236#define OPCACHE_CHECK() \
1237 do { \
1238 co_opcache = NULL; \
1239 if (co->co_opcache != NULL) { \
1240 unsigned char co_opt_offset = \
1241 co->co_opcache_map[next_instr - first_instr]; \
1242 if (co_opt_offset > 0) { \
1243 assert(co_opt_offset <= co->co_opcache_size); \
1244 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1245 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001246 } \
1247 } \
1248 } while (0)
1249
1250#if OPCACHE_STATS
1251
1252#define OPCACHE_STAT_GLOBAL_HIT() \
1253 do { \
1254 if (co->co_opcache != NULL) opcache_global_hits++; \
1255 } while (0)
1256
1257#define OPCACHE_STAT_GLOBAL_MISS() \
1258 do { \
1259 if (co->co_opcache != NULL) opcache_global_misses++; \
1260 } while (0)
1261
1262#define OPCACHE_STAT_GLOBAL_OPT() \
1263 do { \
1264 if (co->co_opcache != NULL) opcache_global_opts++; \
1265 } while (0)
1266
1267#else /* OPCACHE_STATS */
1268
1269#define OPCACHE_STAT_GLOBAL_HIT()
1270#define OPCACHE_STAT_GLOBAL_MISS()
1271#define OPCACHE_STAT_GLOBAL_OPT()
1272
1273#endif
1274
Guido van Rossuma027efa1997-05-05 20:56:21 +00001275/* Start of code */
1276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001278 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001280 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (tstate->use_tracing) {
1285 if (tstate->c_tracefunc != NULL) {
1286 /* tstate->c_tracefunc, if defined, is a
1287 function that will be called on *every* entry
1288 to a code block. Its return value, if not
1289 None, is a function that will be called at
1290 the start of each executed line of code.
1291 (Actually, the function must return itself
1292 in order to continue tracing.) The trace
1293 functions are called with three arguments:
1294 a pointer to the current frame, a string
1295 indicating why the function is called, and
1296 an argument which depends on the situation.
1297 The global trace function is also called
1298 whenever an exception is detected. */
1299 if (call_trace_protected(tstate->c_tracefunc,
1300 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001301 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* Trace function raised an error */
1303 goto exit_eval_frame;
1304 }
1305 }
1306 if (tstate->c_profilefunc != NULL) {
1307 /* Similar for c_profilefunc, except it needn't
1308 return itself and isn't called for "line" events */
1309 if (call_trace_protected(tstate->c_profilefunc,
1310 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001311 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Profile function raised an error */
1313 goto exit_eval_frame;
1314 }
1315 }
1316 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001317
Łukasz Langaa785c872016-09-09 17:37:37 -07001318 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1319 dtrace_function_entry(f);
1320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 co = f->f_code;
1322 names = co->co_names;
1323 consts = co->co_consts;
1324 fastlocals = f->f_localsplus;
1325 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001326 assert(PyBytes_Check(co->co_code));
1327 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001328 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1329 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1330 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001331 /*
1332 f->f_lasti refers to the index of the last instruction,
1333 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001335 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001336 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 When the PREDICT() macros are enabled, some opcode pairs follow in
1339 direct succession without updating f->f_lasti. A successful
1340 prediction effectively links the two codes together as if they
1341 were a single new opcode; accordingly,f->f_lasti will point to
1342 the first code in the pair (for instance, GET_ITER followed by
1343 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001344 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001346 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001347 next_instr = first_instr;
1348 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001349 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1350 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001351 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001352 stack_pointer = f->f_valuestack + f->f_stackdepth;
1353 /* Set f->f_stackdepth to -1.
1354 * Update when returning or calling trace function.
1355 Having f_stackdepth <= 0 ensures that invalid
1356 values are not visible to the cycle GC.
1357 We choose -1 rather than 0 to assist debugging.
1358 */
1359 f->f_stackdepth = -1;
1360 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001361
Inada Naoki91234a12019-06-03 21:30:58 +09001362 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1363 co->co_opcache_flag++;
1364 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1365 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001366 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001367 }
1368#if OPCACHE_STATS
1369 opcache_code_objects_extra_mem +=
1370 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1371 sizeof(_PyOpcache) * co->co_opcache_size;
1372 opcache_code_objects++;
1373#endif
1374 }
1375 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001376
Tim Peters5ca576e2001-06-18 22:08:13 +00001377#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001378 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001379#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 if (throwflag) /* support for generator.throw() */
1382 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001383
Victor Stinnerace47d72013-07-18 01:41:08 +02001384#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001385 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001386 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001387 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001388 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001389#endif
1390
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001391main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1394 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001395 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* Do periodic things. Doing this every time through
1398 the loop would add too much overhead, so we do it
1399 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001400 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 event needs attention (e.g. a signal handler or
1402 async I/O handler); see Py_AddPendingCall() and
1403 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Eric Snow7bda9de2019-03-08 17:25:54 -07001405 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001406 opcode = _Py_OPCODE(*next_instr);
1407 if (opcode == SETUP_FINALLY ||
1408 opcode == SETUP_WITH ||
1409 opcode == BEFORE_ASYNC_WITH ||
1410 opcode == YIELD_FROM) {
1411 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001412 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001413 - If we're about to enter the 'with:'. It will prevent
1414 emitting a resource warning in the common idiom
1415 'with open(path) as file:'.
1416 - If we're about to enter the 'async with:'.
1417 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001418 *very* useful, but might help in some cases and it's
1419 traditional)
1420 - If we're resuming a chain of nested 'yield from' or
1421 'await' calls, then each frame is parked with YIELD_FROM
1422 as its next opcode. If the user hit control-C we want to
1423 wait until we've reached the innermost frame before
1424 running the signal handler and raising KeyboardInterrupt
1425 (see bpo-30039).
1426 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 goto fast_next_opcode;
1428 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001429
Victor Stinnerda2914d2020-03-20 09:29:08 +01001430 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 }
1433 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 fast_next_opcode:
1436 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Łukasz Langaa785c872016-09-09 17:37:37 -07001438 if (PyDTrace_LINE_ENABLED())
1439 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001442
Victor Stinnerdab84232020-03-17 18:56:44 +01001443 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001444 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* see maybe_call_line_trace
1447 for expository comments */
Mark Shannoncb9879b2020-07-17 11:44:23 +01001448 f->f_stackdepth = stack_pointer-f->f_valuestack;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 err = maybe_call_line_trace(tstate->c_tracefunc,
1451 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001452 tstate, f,
1453 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 /* Reload possibly changed frame fields */
1455 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001456 stack_pointer = f->f_valuestack+f->f_stackdepth;
1457 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001464
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001465 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001466 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001467#ifdef DYNAMIC_EXECUTION_PROFILE
1468#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 dxpairs[lastopcode][opcode]++;
1470 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001471#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001473#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001474
Guido van Rossum96a42c81992-01-12 02:29:51 +00001475#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (lltrace) {
1479 if (HAS_ARG(opcode)) {
1480 printf("%d: %d, %d\n",
1481 f->f_lasti, opcode, oparg);
1482 }
1483 else {
1484 printf("%d: %d\n",
1485 f->f_lasti, opcode);
1486 }
1487 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001488#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001493 It is essential that any operation that fails must goto error
1494 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *value = GETLOCAL(oparg);
1502 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001503 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 UNBOUNDLOCAL_ERROR_MSG,
1505 PyTuple_GetItem(co->co_varnames, oparg));
1506 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 Py_INCREF(value);
1509 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 }
1512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(LOAD_CONST): {
1514 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 PyObject *value = GETITEM(consts, oparg);
1516 Py_INCREF(value);
1517 PUSH(value);
1518 FAST_DISPATCH();
1519 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(STORE_FAST): {
1522 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *value = POP();
1524 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *value = POP();
1530 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Benjamin Petersonddd19492018-09-16 22:38:02 -07001534 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 PyObject *top = TOP();
1536 PyObject *second = SECOND();
1537 SET_TOP(second);
1538 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *top = TOP();
1544 PyObject *second = SECOND();
1545 PyObject *third = THIRD();
1546 SET_TOP(second);
1547 SET_SECOND(third);
1548 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001551
Benjamin Petersonddd19492018-09-16 22:38:02 -07001552 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001553 PyObject *top = TOP();
1554 PyObject *second = SECOND();
1555 PyObject *third = THIRD();
1556 PyObject *fourth = FOURTH();
1557 SET_TOP(second);
1558 SET_SECOND(third);
1559 SET_THIRD(fourth);
1560 SET_FOURTH(top);
1561 FAST_DISPATCH();
1562 }
1563
Benjamin Petersonddd19492018-09-16 22:38:02 -07001564 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 PyObject *top = TOP();
1566 Py_INCREF(top);
1567 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Benjamin Petersonddd19492018-09-16 22:38:02 -07001571 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001574 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001576 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 SET_TOP(top);
1578 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001579 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *value = TOP();
1584 PyObject *res = PyNumber_Positive(value);
1585 Py_DECREF(value);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *value = TOP();
1594 PyObject *res = PyNumber_Negative(value);
1595 Py_DECREF(value);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *value = TOP();
1604 int err = PyObject_IsTrue(value);
1605 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (err == 0) {
1607 Py_INCREF(Py_True);
1608 SET_TOP(Py_True);
1609 DISPATCH();
1610 }
1611 else if (err > 0) {
1612 Py_INCREF(Py_False);
1613 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 DISPATCH();
1615 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001616 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 goto error;
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *value = TOP();
1622 PyObject *res = PyNumber_Invert(value);
1623 Py_DECREF(value);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Petersonddd19492018-09-16 22:38:02 -07001630 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 PyObject *exp = POP();
1632 PyObject *base = TOP();
1633 PyObject *res = PyNumber_Power(base, exp, Py_None);
1634 Py_DECREF(base);
1635 Py_DECREF(exp);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Benjamin Petersonddd19492018-09-16 22:38:02 -07001642 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Multiply(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Petersonddd19492018-09-16 22:38:02 -07001654 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_MatrixMultiply(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *divisor = POP();
1668 PyObject *dividend = TOP();
1669 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1670 Py_DECREF(dividend);
1671 Py_DECREF(divisor);
1672 SET_TOP(quotient);
1673 if (quotient == NULL)
1674 goto error;
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Petersonddd19492018-09-16 22:38:02 -07001678 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 PyObject *divisor = POP();
1680 PyObject *dividend = TOP();
1681 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1682 Py_DECREF(dividend);
1683 Py_DECREF(divisor);
1684 SET_TOP(quotient);
1685 if (quotient == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001689
Benjamin Petersonddd19492018-09-16 22:38:02 -07001690 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 PyObject *divisor = POP();
1692 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001693 PyObject *res;
1694 if (PyUnicode_CheckExact(dividend) && (
1695 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1696 // fast path; string formatting, but not if the RHS is a str subclass
1697 // (see issue28598)
1698 res = PyUnicode_Format(dividend, divisor);
1699 } else {
1700 res = PyNumber_Remainder(dividend, divisor);
1701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 Py_DECREF(divisor);
1703 Py_DECREF(dividend);
1704 SET_TOP(res);
1705 if (res == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *right = POP();
1712 PyObject *left = TOP();
1713 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001714 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1715 CPython using bytecode, it is simply worthless.
1716 See http://bugs.python.org/issue21955 and
1717 http://bugs.python.org/issue10044 for the discussion. In short,
1718 no patch shown any impact on a realistic benchmark, only a minor
1719 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 if (PyUnicode_CheckExact(left) &&
1721 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001722 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001723 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001724 }
1725 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 sum = PyNumber_Add(left, right);
1727 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 Py_DECREF(right);
1730 SET_TOP(sum);
1731 if (sum == NULL)
1732 goto error;
1733 DISPATCH();
1734 }
1735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *right = POP();
1738 PyObject *left = TOP();
1739 PyObject *diff = PyNumber_Subtract(left, right);
1740 Py_DECREF(right);
1741 Py_DECREF(left);
1742 SET_TOP(diff);
1743 if (diff == NULL)
1744 goto error;
1745 DISPATCH();
1746 }
1747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 PyObject *sub = POP();
1750 PyObject *container = TOP();
1751 PyObject *res = PyObject_GetItem(container, sub);
1752 Py_DECREF(container);
1753 Py_DECREF(sub);
1754 SET_TOP(res);
1755 if (res == NULL)
1756 goto error;
1757 DISPATCH();
1758 }
1759
Benjamin Petersonddd19492018-09-16 22:38:02 -07001760 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 PyObject *right = POP();
1762 PyObject *left = TOP();
1763 PyObject *res = PyNumber_Lshift(left, right);
1764 Py_DECREF(left);
1765 Py_DECREF(right);
1766 SET_TOP(res);
1767 if (res == NULL)
1768 goto error;
1769 DISPATCH();
1770 }
1771
Benjamin Petersonddd19492018-09-16 22:38:02 -07001772 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 PyObject *right = POP();
1774 PyObject *left = TOP();
1775 PyObject *res = PyNumber_Rshift(left, right);
1776 Py_DECREF(left);
1777 Py_DECREF(right);
1778 SET_TOP(res);
1779 if (res == NULL)
1780 goto error;
1781 DISPATCH();
1782 }
1783
Benjamin Petersonddd19492018-09-16 22:38:02 -07001784 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *right = POP();
1786 PyObject *left = TOP();
1787 PyObject *res = PyNumber_And(left, right);
1788 Py_DECREF(left);
1789 Py_DECREF(right);
1790 SET_TOP(res);
1791 if (res == NULL)
1792 goto error;
1793 DISPATCH();
1794 }
1795
Benjamin Petersonddd19492018-09-16 22:38:02 -07001796 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 PyObject *right = POP();
1798 PyObject *left = TOP();
1799 PyObject *res = PyNumber_Xor(left, right);
1800 Py_DECREF(left);
1801 Py_DECREF(right);
1802 SET_TOP(res);
1803 if (res == NULL)
1804 goto error;
1805 DISPATCH();
1806 }
1807
Benjamin Petersonddd19492018-09-16 22:38:02 -07001808 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 PyObject *right = POP();
1810 PyObject *left = TOP();
1811 PyObject *res = PyNumber_Or(left, right);
1812 Py_DECREF(left);
1813 Py_DECREF(right);
1814 SET_TOP(res);
1815 if (res == NULL)
1816 goto error;
1817 DISPATCH();
1818 }
1819
Benjamin Petersonddd19492018-09-16 22:38:02 -07001820 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 PyObject *v = POP();
1822 PyObject *list = PEEK(oparg);
1823 int err;
1824 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 if (err != 0)
1827 goto error;
1828 PREDICT(JUMP_ABSOLUTE);
1829 DISPATCH();
1830 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001831
Benjamin Petersonddd19492018-09-16 22:38:02 -07001832 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001834 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 int err;
1836 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 if (err != 0)
1839 goto error;
1840 PREDICT(JUMP_ABSOLUTE);
1841 DISPATCH();
1842 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Benjamin Petersonddd19492018-09-16 22:38:02 -07001844 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 PyObject *exp = POP();
1846 PyObject *base = TOP();
1847 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1848 Py_DECREF(base);
1849 Py_DECREF(exp);
1850 SET_TOP(res);
1851 if (res == NULL)
1852 goto error;
1853 DISPATCH();
1854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 PyObject *right = POP();
1858 PyObject *left = TOP();
1859 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1860 Py_DECREF(left);
1861 Py_DECREF(right);
1862 SET_TOP(res);
1863 if (res == NULL)
1864 goto error;
1865 DISPATCH();
1866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Benjamin Petersonddd19492018-09-16 22:38:02 -07001868 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001869 PyObject *right = POP();
1870 PyObject *left = TOP();
1871 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1872 Py_DECREF(left);
1873 Py_DECREF(right);
1874 SET_TOP(res);
1875 if (res == NULL)
1876 goto error;
1877 DISPATCH();
1878 }
1879
Benjamin Petersonddd19492018-09-16 22:38:02 -07001880 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 PyObject *divisor = POP();
1882 PyObject *dividend = TOP();
1883 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1884 Py_DECREF(dividend);
1885 Py_DECREF(divisor);
1886 SET_TOP(quotient);
1887 if (quotient == NULL)
1888 goto error;
1889 DISPATCH();
1890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *divisor = POP();
1894 PyObject *dividend = TOP();
1895 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1896 Py_DECREF(dividend);
1897 Py_DECREF(divisor);
1898 SET_TOP(quotient);
1899 if (quotient == NULL)
1900 goto error;
1901 DISPATCH();
1902 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001903
Benjamin Petersonddd19492018-09-16 22:38:02 -07001904 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 PyObject *right = POP();
1906 PyObject *left = TOP();
1907 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1908 Py_DECREF(left);
1909 Py_DECREF(right);
1910 SET_TOP(mod);
1911 if (mod == NULL)
1912 goto error;
1913 DISPATCH();
1914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 PyObject *right = POP();
1918 PyObject *left = TOP();
1919 PyObject *sum;
1920 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001921 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001922 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001923 }
1924 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 sum = PyNumber_InPlaceAdd(left, right);
1926 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 Py_DECREF(right);
1929 SET_TOP(sum);
1930 if (sum == NULL)
1931 goto error;
1932 DISPATCH();
1933 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Benjamin Petersonddd19492018-09-16 22:38:02 -07001935 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 PyObject *right = POP();
1937 PyObject *left = TOP();
1938 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1939 Py_DECREF(left);
1940 Py_DECREF(right);
1941 SET_TOP(diff);
1942 if (diff == NULL)
1943 goto error;
1944 DISPATCH();
1945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 PyObject *right = POP();
1949 PyObject *left = TOP();
1950 PyObject *res = PyNumber_InPlaceLshift(left, right);
1951 Py_DECREF(left);
1952 Py_DECREF(right);
1953 SET_TOP(res);
1954 if (res == NULL)
1955 goto error;
1956 DISPATCH();
1957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Benjamin Petersonddd19492018-09-16 22:38:02 -07001959 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PyObject *right = POP();
1961 PyObject *left = TOP();
1962 PyObject *res = PyNumber_InPlaceRshift(left, right);
1963 Py_DECREF(left);
1964 Py_DECREF(right);
1965 SET_TOP(res);
1966 if (res == NULL)
1967 goto error;
1968 DISPATCH();
1969 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001970
Benjamin Petersonddd19492018-09-16 22:38:02 -07001971 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 PyObject *right = POP();
1973 PyObject *left = TOP();
1974 PyObject *res = PyNumber_InPlaceAnd(left, right);
1975 Py_DECREF(left);
1976 Py_DECREF(right);
1977 SET_TOP(res);
1978 if (res == NULL)
1979 goto error;
1980 DISPATCH();
1981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Benjamin Petersonddd19492018-09-16 22:38:02 -07001983 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 PyObject *right = POP();
1985 PyObject *left = TOP();
1986 PyObject *res = PyNumber_InPlaceXor(left, right);
1987 Py_DECREF(left);
1988 Py_DECREF(right);
1989 SET_TOP(res);
1990 if (res == NULL)
1991 goto error;
1992 DISPATCH();
1993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Benjamin Petersonddd19492018-09-16 22:38:02 -07001995 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 PyObject *right = POP();
1997 PyObject *left = TOP();
1998 PyObject *res = PyNumber_InPlaceOr(left, right);
1999 Py_DECREF(left);
2000 Py_DECREF(right);
2001 SET_TOP(res);
2002 if (res == NULL)
2003 goto error;
2004 DISPATCH();
2005 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002006
Benjamin Petersonddd19492018-09-16 22:38:02 -07002007 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 PyObject *sub = TOP();
2009 PyObject *container = SECOND();
2010 PyObject *v = THIRD();
2011 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002012 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002013 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 Py_DECREF(container);
2017 Py_DECREF(sub);
2018 if (err != 0)
2019 goto error;
2020 DISPATCH();
2021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Benjamin Petersonddd19492018-09-16 22:38:02 -07002023 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 PyObject *sub = TOP();
2025 PyObject *container = SECOND();
2026 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002027 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002028 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 err = PyObject_DelItem(container, sub);
2030 Py_DECREF(container);
2031 Py_DECREF(sub);
2032 if (err != 0)
2033 goto error;
2034 DISPATCH();
2035 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002036
Benjamin Petersonddd19492018-09-16 22:38:02 -07002037 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002038 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002040 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002041 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002043 _PyErr_SetString(tstate, PyExc_RuntimeError,
2044 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 Py_DECREF(value);
2046 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002048 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 Py_DECREF(value);
2050 if (res == NULL)
2051 goto error;
2052 Py_DECREF(res);
2053 DISPATCH();
2054 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002055
Benjamin Petersonddd19492018-09-16 22:38:02 -07002056 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 switch (oparg) {
2059 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002061 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002064 /* fall through */
2065 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002066 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002067 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 break;
2070 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002071 _PyErr_SetString(tstate, PyExc_SystemError,
2072 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 break;
2074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 goto error;
2076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Benjamin Petersonddd19492018-09-16 22:38:02 -07002078 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002080 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002081 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002082 f->f_state = FRAME_RETURNED;
2083 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002084 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002086
Benjamin Petersonddd19492018-09-16 22:38:02 -07002087 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002088 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002089 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002090 PyObject *obj = TOP();
2091 PyTypeObject *type = Py_TYPE(obj);
2092
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002093 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002094 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002095 }
Yury Selivanov75445082015-05-11 22:57:16 -04002096
2097 if (getter != NULL) {
2098 iter = (*getter)(obj);
2099 Py_DECREF(obj);
2100 if (iter == NULL) {
2101 SET_TOP(NULL);
2102 goto error;
2103 }
2104 }
2105 else {
2106 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002107 _PyErr_Format(tstate, PyExc_TypeError,
2108 "'async for' requires an object with "
2109 "__aiter__ method, got %.100s",
2110 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002111 Py_DECREF(obj);
2112 goto error;
2113 }
2114
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002115 if (Py_TYPE(iter)->tp_as_async == NULL ||
2116 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002117
Yury Selivanov398ff912017-03-02 22:20:00 -05002118 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002119 _PyErr_Format(tstate, PyExc_TypeError,
2120 "'async for' received an object from __aiter__ "
2121 "that does not implement __anext__: %.100s",
2122 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002123 Py_DECREF(iter);
2124 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002125 }
2126
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002127 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002128 DISPATCH();
2129 }
2130
Benjamin Petersonddd19492018-09-16 22:38:02 -07002131 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002132 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002133 PyObject *next_iter = NULL;
2134 PyObject *awaitable = NULL;
2135 PyObject *aiter = TOP();
2136 PyTypeObject *type = Py_TYPE(aiter);
2137
Yury Selivanoveb636452016-09-08 22:01:51 -07002138 if (PyAsyncGen_CheckExact(aiter)) {
2139 awaitable = type->tp_as_async->am_anext(aiter);
2140 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002141 goto error;
2142 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002143 } else {
2144 if (type->tp_as_async != NULL){
2145 getter = type->tp_as_async->am_anext;
2146 }
Yury Selivanov75445082015-05-11 22:57:16 -04002147
Yury Selivanoveb636452016-09-08 22:01:51 -07002148 if (getter != NULL) {
2149 next_iter = (*getter)(aiter);
2150 if (next_iter == NULL) {
2151 goto error;
2152 }
2153 }
2154 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002155 _PyErr_Format(tstate, PyExc_TypeError,
2156 "'async for' requires an iterator with "
2157 "__anext__ method, got %.100s",
2158 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002159 goto error;
2160 }
Yury Selivanov75445082015-05-11 22:57:16 -04002161
Yury Selivanoveb636452016-09-08 22:01:51 -07002162 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2163 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002164 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002165 PyExc_TypeError,
2166 "'async for' received an invalid object "
2167 "from __anext__: %.100s",
2168 Py_TYPE(next_iter)->tp_name);
2169
2170 Py_DECREF(next_iter);
2171 goto error;
2172 } else {
2173 Py_DECREF(next_iter);
2174 }
2175 }
Yury Selivanov75445082015-05-11 22:57:16 -04002176
2177 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002178 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002179 DISPATCH();
2180 }
2181
Benjamin Petersonddd19492018-09-16 22:38:02 -07002182 case TARGET(GET_AWAITABLE): {
2183 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002184 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002185 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002186
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002187 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002188 int opcode_at_minus_3 = 0;
2189 if ((next_instr - first_instr) > 2) {
2190 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2191 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002192 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002193 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002194 _Py_OPCODE(next_instr[-2]));
2195 }
2196
Yury Selivanov75445082015-05-11 22:57:16 -04002197 Py_DECREF(iterable);
2198
Yury Selivanovc724bae2016-03-02 11:30:46 -05002199 if (iter != NULL && PyCoro_CheckExact(iter)) {
2200 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2201 if (yf != NULL) {
2202 /* `iter` is a coroutine object that is being
2203 awaited, `yf` is a pointer to the current awaitable
2204 being awaited on. */
2205 Py_DECREF(yf);
2206 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002207 _PyErr_SetString(tstate, PyExc_RuntimeError,
2208 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002209 /* The code below jumps to `error` if `iter` is NULL. */
2210 }
2211 }
2212
Yury Selivanov75445082015-05-11 22:57:16 -04002213 SET_TOP(iter); /* Even if it's NULL */
2214
2215 if (iter == NULL) {
2216 goto error;
2217 }
2218
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002219 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002220 DISPATCH();
2221 }
2222
Benjamin Petersonddd19492018-09-16 22:38:02 -07002223 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002225 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002227 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2228 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002229 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002230 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002232 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002233 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002234 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002235 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 Py_DECREF(v);
2237 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002238 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002239 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002240 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002241 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002242 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 if (err < 0)
2244 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002245 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 SET_TOP(val);
2247 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002248 }
Martin Panter95f53c12016-07-18 08:23:26 +00002249 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002250 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002251 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002252 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002253 f->f_state = FRAME_SUSPENDED;
2254 f->f_stackdepth = stack_pointer-f->f_valuestack;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002255 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002257
Benjamin Petersonddd19492018-09-16 22:38:02 -07002258 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002260
2261 if (co->co_flags & CO_ASYNC_GENERATOR) {
2262 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2263 Py_DECREF(retval);
2264 if (w == NULL) {
2265 retval = NULL;
2266 goto error;
2267 }
2268 retval = w;
2269 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002270 f->f_state = FRAME_SUSPENDED;
2271 f->f_stackdepth = stack_pointer-f->f_valuestack;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002272 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002274
Benjamin Petersonddd19492018-09-16 22:38:02 -07002275 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002276 PyObject *type, *value, *traceback;
2277 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 PyTryBlock *b = PyFrame_BlockPop(f);
2279 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002280 _PyErr_SetString(tstate, PyExc_SystemError,
2281 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002284 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2285 STACK_LEVEL() <= (b)->b_level + 4);
2286 exc_info = tstate->exc_info;
2287 type = exc_info->exc_type;
2288 value = exc_info->exc_value;
2289 traceback = exc_info->exc_traceback;
2290 exc_info->exc_type = POP();
2291 exc_info->exc_value = POP();
2292 exc_info->exc_traceback = POP();
2293 Py_XDECREF(type);
2294 Py_XDECREF(value);
2295 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002298
Benjamin Petersonddd19492018-09-16 22:38:02 -07002299 case TARGET(POP_BLOCK): {
2300 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002301 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Mark Shannonfee55262019-11-21 09:11:43 +00002305 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002306 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002307 PyObject *val = POP();
2308 PyObject *tb = POP();
2309 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002310 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002311 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Benjamin Petersonddd19492018-09-16 22:38:02 -07002314 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002315 PyObject *exc = POP();
2316 assert(PyExceptionClass_Check(exc));
2317 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2318 PyTryBlock *b = PyFrame_BlockPop(f);
2319 assert(b->b_type == EXCEPT_HANDLER);
2320 Py_DECREF(exc);
2321 UNWIND_EXCEPT_HANDLER(b);
2322 Py_DECREF(POP());
2323 JUMPBY(oparg);
2324 FAST_DISPATCH();
2325 }
2326 else {
2327 PyObject *val = POP();
2328 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002329 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002330 goto exception_unwind;
2331 }
2332 }
2333
Zackery Spytzce6a0702019-08-25 03:44:09 -06002334 case TARGET(LOAD_ASSERTION_ERROR): {
2335 PyObject *value = PyExc_AssertionError;
2336 Py_INCREF(value);
2337 PUSH(value);
2338 FAST_DISPATCH();
2339 }
2340
Benjamin Petersonddd19492018-09-16 22:38:02 -07002341 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002342 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002343
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002345 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002346 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002348 if (!_PyErr_Occurred(tstate)) {
2349 _PyErr_SetString(tstate, PyExc_NameError,
2350 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002351 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002355 }
2356 else {
2357 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2358 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002359 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2361 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002362 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2363 _PyErr_SetString(tstate, PyExc_NameError,
2364 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002369 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002371
Benjamin Petersonddd19492018-09-16 22:38:02 -07002372 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 PyObject *name = GETITEM(names, oparg);
2374 PyObject *v = POP();
2375 PyObject *ns = f->f_locals;
2376 int err;
2377 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002378 _PyErr_Format(tstate, PyExc_SystemError,
2379 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 if (PyDict_CheckExact(ns))
2384 err = PyDict_SetItem(ns, name, v);
2385 else
2386 err = PyObject_SetItem(ns, name, v);
2387 Py_DECREF(v);
2388 if (err != 0)
2389 goto error;
2390 DISPATCH();
2391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Benjamin Petersonddd19492018-09-16 22:38:02 -07002393 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 PyObject *name = GETITEM(names, oparg);
2395 PyObject *ns = f->f_locals;
2396 int err;
2397 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002398 _PyErr_Format(tstate, PyExc_SystemError,
2399 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 err = PyObject_DelItem(ns, name);
2403 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002404 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 NAME_ERROR_MSG,
2406 name);
2407 goto error;
2408 }
2409 DISPATCH();
2410 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002411
Benjamin Petersonddd19492018-09-16 22:38:02 -07002412 case TARGET(UNPACK_SEQUENCE): {
2413 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 PyObject *seq = POP(), *item, **items;
2415 if (PyTuple_CheckExact(seq) &&
2416 PyTuple_GET_SIZE(seq) == oparg) {
2417 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 item = items[oparg];
2420 Py_INCREF(item);
2421 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 } else if (PyList_CheckExact(seq) &&
2424 PyList_GET_SIZE(seq) == oparg) {
2425 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 item = items[oparg];
2428 Py_INCREF(item);
2429 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002431 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002433 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 } else {
2435 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 Py_DECREF(seq);
2437 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002440 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2445 PyObject *seq = POP();
2446
Victor Stinner438a12d2019-05-24 17:01:38 +02002447 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 stack_pointer + totalargs)) {
2449 stack_pointer += totalargs;
2450 } else {
2451 Py_DECREF(seq);
2452 goto error;
2453 }
2454 Py_DECREF(seq);
2455 DISPATCH();
2456 }
2457
Benjamin Petersonddd19492018-09-16 22:38:02 -07002458 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 PyObject *name = GETITEM(names, oparg);
2460 PyObject *owner = TOP();
2461 PyObject *v = SECOND();
2462 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002463 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 Py_DECREF(owner);
2467 if (err != 0)
2468 goto error;
2469 DISPATCH();
2470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002471
Benjamin Petersonddd19492018-09-16 22:38:02 -07002472 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 PyObject *name = GETITEM(names, oparg);
2474 PyObject *owner = POP();
2475 int err;
2476 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2477 Py_DECREF(owner);
2478 if (err != 0)
2479 goto error;
2480 DISPATCH();
2481 }
2482
Benjamin Petersonddd19492018-09-16 22:38:02 -07002483 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 PyObject *name = GETITEM(names, oparg);
2485 PyObject *v = POP();
2486 int err;
2487 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 if (err != 0)
2490 goto error;
2491 DISPATCH();
2492 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002493
Benjamin Petersonddd19492018-09-16 22:38:02 -07002494 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PyObject *name = GETITEM(names, oparg);
2496 int err;
2497 err = PyDict_DelItem(f->f_globals, name);
2498 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002499 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2500 format_exc_check_arg(tstate, PyExc_NameError,
2501 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002502 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002504 }
2505 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Benjamin Petersonddd19492018-09-16 22:38:02 -07002508 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 PyObject *name = GETITEM(names, oparg);
2510 PyObject *locals = f->f_locals;
2511 PyObject *v;
2512 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002513 _PyErr_Format(tstate, PyExc_SystemError,
2514 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002518 v = PyDict_GetItemWithError(locals, name);
2519 if (v != NULL) {
2520 Py_INCREF(v);
2521 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002522 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002523 goto error;
2524 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 }
2526 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002528 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002530 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002531 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 }
2533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002535 v = PyDict_GetItemWithError(f->f_globals, name);
2536 if (v != NULL) {
2537 Py_INCREF(v);
2538 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002539 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002540 goto error;
2541 }
2542 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002543 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002544 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002546 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002547 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002548 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002550 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002554 }
2555 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 v = PyObject_GetItem(f->f_builtins, name);
2557 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002558 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002559 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002562 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002564 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002571
Benjamin Petersonddd19492018-09-16 22:38:02 -07002572 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002573 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002575 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002576 && PyDict_CheckExact(f->f_builtins))
2577 {
Inada Naoki91234a12019-06-03 21:30:58 +09002578 OPCACHE_CHECK();
2579 if (co_opcache != NULL && co_opcache->optimized > 0) {
2580 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2581
2582 if (lg->globals_ver ==
2583 ((PyDictObject *)f->f_globals)->ma_version_tag
2584 && lg->builtins_ver ==
2585 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2586 {
2587 PyObject *ptr = lg->ptr;
2588 OPCACHE_STAT_GLOBAL_HIT();
2589 assert(ptr != NULL);
2590 Py_INCREF(ptr);
2591 PUSH(ptr);
2592 DISPATCH();
2593 }
2594 }
2595
2596 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002598 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 name);
2600 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002601 if (!_PyErr_OCCURRED()) {
2602 /* _PyDict_LoadGlobal() returns NULL without raising
2603 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002604 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002605 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002606 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 }
Inada Naoki91234a12019-06-03 21:30:58 +09002609
2610 if (co_opcache != NULL) {
2611 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2612
2613 if (co_opcache->optimized == 0) {
2614 /* Wasn't optimized before. */
2615 OPCACHE_STAT_GLOBAL_OPT();
2616 } else {
2617 OPCACHE_STAT_GLOBAL_MISS();
2618 }
2619
2620 co_opcache->optimized = 1;
2621 lg->globals_ver =
2622 ((PyDictObject *)f->f_globals)->ma_version_tag;
2623 lg->builtins_ver =
2624 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2625 lg->ptr = v; /* borrowed */
2626 }
2627
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002630 else {
2631 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002632
2633 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002634 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 v = PyObject_GetItem(f->f_globals, name);
2636 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002637 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002638 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002639 }
2640 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002641
Victor Stinnerb4efc962015-11-20 09:24:02 +01002642 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002643 v = PyObject_GetItem(f->f_builtins, name);
2644 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002646 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002648 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002649 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002651 }
2652 }
2653 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002657
Benjamin Petersonddd19492018-09-16 22:38:02 -07002658 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 PyObject *v = GETLOCAL(oparg);
2660 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 SETLOCAL(oparg, NULL);
2662 DISPATCH();
2663 }
2664 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002665 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 UNBOUNDLOCAL_ERROR_MSG,
2667 PyTuple_GetItem(co->co_varnames, oparg)
2668 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 goto error;
2670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002674 PyObject *oldobj = PyCell_GET(cell);
2675 if (oldobj != NULL) {
2676 PyCell_SET(cell, NULL);
2677 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002678 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002679 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002680 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 goto error;
2682 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002683
Benjamin Petersonddd19492018-09-16 22:38:02 -07002684 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 PyObject *cell = freevars[oparg];
2686 Py_INCREF(cell);
2687 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002690
Benjamin Petersonddd19492018-09-16 22:38:02 -07002691 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002692 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002693 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002694 assert(locals);
2695 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2696 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2697 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2698 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2699 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002700 value = PyDict_GetItemWithError(locals, name);
2701 if (value != NULL) {
2702 Py_INCREF(value);
2703 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002704 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002705 goto error;
2706 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002707 }
2708 else {
2709 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002710 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002711 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002712 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002713 }
2714 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002715 }
2716 }
2717 if (!value) {
2718 PyObject *cell = freevars[oparg];
2719 value = PyCell_GET(cell);
2720 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002721 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002722 goto error;
2723 }
2724 Py_INCREF(value);
2725 }
2726 PUSH(value);
2727 DISPATCH();
2728 }
2729
Benjamin Petersonddd19492018-09-16 22:38:02 -07002730 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 PyObject *cell = freevars[oparg];
2732 PyObject *value = PyCell_GET(cell);
2733 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002734 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 Py_INCREF(value);
2738 PUSH(value);
2739 DISPATCH();
2740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002741
Benjamin Petersonddd19492018-09-16 22:38:02 -07002742 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 PyObject *v = POP();
2744 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002745 PyObject *oldobj = PyCell_GET(cell);
2746 PyCell_SET(cell, v);
2747 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 DISPATCH();
2749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002750
Benjamin Petersonddd19492018-09-16 22:38:02 -07002751 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002752 PyObject *str;
2753 PyObject *empty = PyUnicode_New(0, 0);
2754 if (empty == NULL) {
2755 goto error;
2756 }
2757 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2758 Py_DECREF(empty);
2759 if (str == NULL)
2760 goto error;
2761 while (--oparg >= 0) {
2762 PyObject *item = POP();
2763 Py_DECREF(item);
2764 }
2765 PUSH(str);
2766 DISPATCH();
2767 }
2768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 PyObject *tup = PyTuple_New(oparg);
2771 if (tup == NULL)
2772 goto error;
2773 while (--oparg >= 0) {
2774 PyObject *item = POP();
2775 PyTuple_SET_ITEM(tup, oparg, item);
2776 }
2777 PUSH(tup);
2778 DISPATCH();
2779 }
2780
Benjamin Petersonddd19492018-09-16 22:38:02 -07002781 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 PyObject *list = PyList_New(oparg);
2783 if (list == NULL)
2784 goto error;
2785 while (--oparg >= 0) {
2786 PyObject *item = POP();
2787 PyList_SET_ITEM(list, oparg, item);
2788 }
2789 PUSH(list);
2790 DISPATCH();
2791 }
2792
Mark Shannon13bc1392020-01-23 09:25:17 +00002793 case TARGET(LIST_TO_TUPLE): {
2794 PyObject *list = POP();
2795 PyObject *tuple = PyList_AsTuple(list);
2796 Py_DECREF(list);
2797 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002798 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002799 }
2800 PUSH(tuple);
2801 DISPATCH();
2802 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002803
Mark Shannon13bc1392020-01-23 09:25:17 +00002804 case TARGET(LIST_EXTEND): {
2805 PyObject *iterable = POP();
2806 PyObject *list = PEEK(oparg);
2807 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2808 if (none_val == NULL) {
2809 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002810 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002811 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002812 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002813 _PyErr_Format(tstate, PyExc_TypeError,
2814 "Value after * must be an iterable, not %.200s",
2815 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002817 Py_DECREF(iterable);
2818 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002820 Py_DECREF(none_val);
2821 Py_DECREF(iterable);
2822 DISPATCH();
2823 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002824
Mark Shannon13bc1392020-01-23 09:25:17 +00002825 case TARGET(SET_UPDATE): {
2826 PyObject *iterable = POP();
2827 PyObject *set = PEEK(oparg);
2828 int err = _PySet_Update(set, iterable);
2829 Py_DECREF(iterable);
2830 if (err < 0) {
2831 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002832 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002833 DISPATCH();
2834 }
2835
Benjamin Petersonddd19492018-09-16 22:38:02 -07002836 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 PyObject *set = PySet_New(NULL);
2838 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002839 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 if (set == NULL)
2841 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002842 for (i = oparg; i > 0; i--) {
2843 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 if (err == 0)
2845 err = PySet_Add(set, item);
2846 Py_DECREF(item);
2847 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002848 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 if (err != 0) {
2850 Py_DECREF(set);
2851 goto error;
2852 }
2853 PUSH(set);
2854 DISPATCH();
2855 }
2856
Benjamin Petersonddd19492018-09-16 22:38:02 -07002857 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002858 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2860 if (map == NULL)
2861 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002862 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002864 PyObject *key = PEEK(2*i);
2865 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002867 if (err != 0) {
2868 Py_DECREF(map);
2869 goto error;
2870 }
2871 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002872
2873 while (oparg--) {
2874 Py_DECREF(POP());
2875 Py_DECREF(POP());
2876 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 PUSH(map);
2878 DISPATCH();
2879 }
2880
Benjamin Petersonddd19492018-09-16 22:38:02 -07002881 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002882 _Py_IDENTIFIER(__annotations__);
2883 int err;
2884 PyObject *ann_dict;
2885 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002886 _PyErr_Format(tstate, PyExc_SystemError,
2887 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002888 goto error;
2889 }
2890 /* check if __annotations__ in locals()... */
2891 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002892 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002893 &PyId___annotations__);
2894 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002895 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002896 goto error;
2897 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002898 /* ...if not, create a new one */
2899 ann_dict = PyDict_New();
2900 if (ann_dict == NULL) {
2901 goto error;
2902 }
2903 err = _PyDict_SetItemId(f->f_locals,
2904 &PyId___annotations__, ann_dict);
2905 Py_DECREF(ann_dict);
2906 if (err != 0) {
2907 goto error;
2908 }
2909 }
2910 }
2911 else {
2912 /* do the same if locals() is not a dict */
2913 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2914 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002915 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002916 }
2917 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2918 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002919 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002920 goto error;
2921 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002922 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002923 ann_dict = PyDict_New();
2924 if (ann_dict == NULL) {
2925 goto error;
2926 }
2927 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2928 Py_DECREF(ann_dict);
2929 if (err != 0) {
2930 goto error;
2931 }
2932 }
2933 else {
2934 Py_DECREF(ann_dict);
2935 }
2936 }
2937 DISPATCH();
2938 }
2939
Benjamin Petersonddd19492018-09-16 22:38:02 -07002940 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002941 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002942 PyObject *map;
2943 PyObject *keys = TOP();
2944 if (!PyTuple_CheckExact(keys) ||
2945 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002946 _PyErr_SetString(tstate, PyExc_SystemError,
2947 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002948 goto error;
2949 }
2950 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2951 if (map == NULL) {
2952 goto error;
2953 }
2954 for (i = oparg; i > 0; i--) {
2955 int err;
2956 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2957 PyObject *value = PEEK(i + 1);
2958 err = PyDict_SetItem(map, key, value);
2959 if (err != 0) {
2960 Py_DECREF(map);
2961 goto error;
2962 }
2963 }
2964
2965 Py_DECREF(POP());
2966 while (oparg--) {
2967 Py_DECREF(POP());
2968 }
2969 PUSH(map);
2970 DISPATCH();
2971 }
2972
Mark Shannon8a4cd702020-01-27 09:57:45 +00002973 case TARGET(DICT_UPDATE): {
2974 PyObject *update = POP();
2975 PyObject *dict = PEEK(oparg);
2976 if (PyDict_Update(dict, update) < 0) {
2977 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2978 _PyErr_Format(tstate, PyExc_TypeError,
2979 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002980 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002981 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002982 Py_DECREF(update);
2983 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002984 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002985 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002986 DISPATCH();
2987 }
2988
Mark Shannon8a4cd702020-01-27 09:57:45 +00002989 case TARGET(DICT_MERGE): {
2990 PyObject *update = POP();
2991 PyObject *dict = PEEK(oparg);
2992
2993 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2994 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2995 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002996 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002997 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002998 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002999 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003000 DISPATCH();
3001 }
3002
Benjamin Petersonddd19492018-09-16 22:38:02 -07003003 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003004 PyObject *value = TOP();
3005 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 PyObject *map;
3007 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003008 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003009 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003011 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 Py_DECREF(value);
3013 Py_DECREF(key);
3014 if (err != 0)
3015 goto error;
3016 PREDICT(JUMP_ABSOLUTE);
3017 DISPATCH();
3018 }
3019
Benjamin Petersonddd19492018-09-16 22:38:02 -07003020 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 PyObject *name = GETITEM(names, oparg);
3022 PyObject *owner = TOP();
3023 PyObject *res = PyObject_GetAttr(owner, name);
3024 Py_DECREF(owner);
3025 SET_TOP(res);
3026 if (res == NULL)
3027 goto error;
3028 DISPATCH();
3029 }
3030
Benjamin Petersonddd19492018-09-16 22:38:02 -07003031 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003032 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 PyObject *right = POP();
3034 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003035 PyObject *res = PyObject_RichCompare(left, right, oparg);
3036 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 Py_DECREF(left);
3038 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 if (res == NULL)
3040 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 PREDICT(POP_JUMP_IF_FALSE);
3042 PREDICT(POP_JUMP_IF_TRUE);
3043 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003044 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003045
Mark Shannon9af0e472020-01-14 10:12:45 +00003046 case TARGET(IS_OP): {
3047 PyObject *right = POP();
3048 PyObject *left = TOP();
3049 int res = (left == right)^oparg;
3050 PyObject *b = res ? Py_True : Py_False;
3051 Py_INCREF(b);
3052 SET_TOP(b);
3053 Py_DECREF(left);
3054 Py_DECREF(right);
3055 PREDICT(POP_JUMP_IF_FALSE);
3056 PREDICT(POP_JUMP_IF_TRUE);
3057 FAST_DISPATCH();
3058 }
3059
3060 case TARGET(CONTAINS_OP): {
3061 PyObject *right = POP();
3062 PyObject *left = POP();
3063 int res = PySequence_Contains(right, left);
3064 Py_DECREF(left);
3065 Py_DECREF(right);
3066 if (res < 0) {
3067 goto error;
3068 }
3069 PyObject *b = (res^oparg) ? Py_True : Py_False;
3070 Py_INCREF(b);
3071 PUSH(b);
3072 PREDICT(POP_JUMP_IF_FALSE);
3073 PREDICT(POP_JUMP_IF_TRUE);
3074 FAST_DISPATCH();
3075 }
3076
3077#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3078 "BaseException is not allowed"
3079
3080 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3081 PyObject *right = POP();
3082 PyObject *left = POP();
3083 if (PyTuple_Check(right)) {
3084 Py_ssize_t i, length;
3085 length = PyTuple_GET_SIZE(right);
3086 for (i = 0; i < length; i++) {
3087 PyObject *exc = PyTuple_GET_ITEM(right, i);
3088 if (!PyExceptionClass_Check(exc)) {
3089 _PyErr_SetString(tstate, PyExc_TypeError,
3090 CANNOT_CATCH_MSG);
3091 Py_DECREF(left);
3092 Py_DECREF(right);
3093 goto error;
3094 }
3095 }
3096 }
3097 else {
3098 if (!PyExceptionClass_Check(right)) {
3099 _PyErr_SetString(tstate, PyExc_TypeError,
3100 CANNOT_CATCH_MSG);
3101 Py_DECREF(left);
3102 Py_DECREF(right);
3103 goto error;
3104 }
3105 }
3106 int res = PyErr_GivenExceptionMatches(left, right);
3107 Py_DECREF(left);
3108 Py_DECREF(right);
3109 if (res > 0) {
3110 /* Exception matches -- Do nothing */;
3111 }
3112 else if (res == 0) {
3113 JUMPTO(oparg);
3114 }
3115 else {
3116 goto error;
3117 }
3118 DISPATCH();
3119 }
3120
Benjamin Petersonddd19492018-09-16 22:38:02 -07003121 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003123 PyObject *fromlist = POP();
3124 PyObject *level = TOP();
3125 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003126 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003127 Py_DECREF(level);
3128 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 SET_TOP(res);
3130 if (res == NULL)
3131 goto error;
3132 DISPATCH();
3133 }
3134
Benjamin Petersonddd19492018-09-16 22:38:02 -07003135 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 PyObject *from = POP(), *locals;
3137 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003138 if (PyFrame_FastToLocalsWithError(f) < 0) {
3139 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003140 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003141 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 locals = f->f_locals;
3144 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003145 _PyErr_SetString(tstate, PyExc_SystemError,
3146 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003147 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003150 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 Py_DECREF(from);
3153 if (err != 0)
3154 goto error;
3155 DISPATCH();
3156 }
Guido van Rossum25831651993-05-19 14:50:45 +00003157
Benjamin Petersonddd19492018-09-16 22:38:02 -07003158 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PyObject *name = GETITEM(names, oparg);
3160 PyObject *from = TOP();
3161 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003162 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 PUSH(res);
3164 if (res == NULL)
3165 goto error;
3166 DISPATCH();
3167 }
Thomas Wouters52152252000-08-17 22:55:00 +00003168
Benjamin Petersonddd19492018-09-16 22:38:02 -07003169 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 JUMPBY(oparg);
3171 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003173
Benjamin Petersonddd19492018-09-16 22:38:02 -07003174 case TARGET(POP_JUMP_IF_FALSE): {
3175 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 PyObject *cond = POP();
3177 int err;
3178 if (cond == Py_True) {
3179 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 FAST_DISPATCH();
3181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 if (cond == Py_False) {
3183 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 JUMPTO(oparg);
3185 FAST_DISPATCH();
3186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 err = PyObject_IsTrue(cond);
3188 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003190 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 else if (err == 0)
3192 JUMPTO(oparg);
3193 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003197
Benjamin Petersonddd19492018-09-16 22:38:02 -07003198 case TARGET(POP_JUMP_IF_TRUE): {
3199 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 PyObject *cond = POP();
3201 int err;
3202 if (cond == Py_False) {
3203 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 FAST_DISPATCH();
3205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 if (cond == Py_True) {
3207 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 JUMPTO(oparg);
3209 FAST_DISPATCH();
3210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 err = PyObject_IsTrue(cond);
3212 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 JUMPTO(oparg);
3215 }
3216 else if (err == 0)
3217 ;
3218 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003222
Benjamin Petersonddd19492018-09-16 22:38:02 -07003223 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 PyObject *cond = TOP();
3225 int err;
3226 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003227 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 FAST_DISPATCH();
3230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 JUMPTO(oparg);
3233 FAST_DISPATCH();
3234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003237 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 }
3240 else if (err == 0)
3241 JUMPTO(oparg);
3242 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003246
Benjamin Petersonddd19492018-09-16 22:38:02 -07003247 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 PyObject *cond = TOP();
3249 int err;
3250 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003251 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 FAST_DISPATCH();
3254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 JUMPTO(oparg);
3257 FAST_DISPATCH();
3258 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 JUMPTO(oparg);
3262 }
3263 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003264 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 }
3267 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003271
Benjamin Petersonddd19492018-09-16 22:38:02 -07003272 case TARGET(JUMP_ABSOLUTE): {
3273 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003275#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* Enabling this path speeds-up all while and for-loops by bypassing
3277 the per-loop checks for signals. By default, this should be turned-off
3278 because it prevents detection of a control-break in tight loops like
3279 "while 1: pass". Compile with this option turned-on when you need
3280 the speed-up and do not need break checking inside tight loops (ones
3281 that contain only instructions ending with FAST_DISPATCH).
3282 */
3283 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003284#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003286#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003288
Benjamin Petersonddd19492018-09-16 22:38:02 -07003289 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003292 PyObject *iter = PyObject_GetIter(iterable);
3293 Py_DECREF(iterable);
3294 SET_TOP(iter);
3295 if (iter == NULL)
3296 goto error;
3297 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003298 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003299 DISPATCH();
3300 }
3301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003303 /* before: [obj]; after [getiter(obj)] */
3304 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003305 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003306 if (PyCoro_CheckExact(iterable)) {
3307 /* `iterable` is a coroutine */
3308 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3309 /* and it is used in a 'yield from' expression of a
3310 regular generator. */
3311 Py_DECREF(iterable);
3312 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003313 _PyErr_SetString(tstate, PyExc_TypeError,
3314 "cannot 'yield from' a coroutine object "
3315 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003316 goto error;
3317 }
3318 }
3319 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003320 /* `iterable` is not a generator. */
3321 iter = PyObject_GetIter(iterable);
3322 Py_DECREF(iterable);
3323 SET_TOP(iter);
3324 if (iter == NULL)
3325 goto error;
3326 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003327 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 DISPATCH();
3329 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003330
Benjamin Petersonddd19492018-09-16 22:38:02 -07003331 case TARGET(FOR_ITER): {
3332 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003335 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003336 if (next != NULL) {
3337 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 PREDICT(STORE_FAST);
3339 PREDICT(UNPACK_SEQUENCE);
3340 DISPATCH();
3341 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003342 if (_PyErr_Occurred(tstate)) {
3343 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003345 }
3346 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003347 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003348 }
3349 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
3351 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003352 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003355 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003358
Benjamin Petersonddd19492018-09-16 22:38:02 -07003359 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003360 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 STACK_LEVEL());
3362 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003364
Benjamin Petersonddd19492018-09-16 22:38:02 -07003365 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003366 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003367 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003368 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003369 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003370 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003371 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003372 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003373 }
3374 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3375 if (exit == NULL) {
3376 Py_DECREF(enter);
3377 goto error;
3378 }
Yury Selivanov75445082015-05-11 22:57:16 -04003379 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003380 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003381 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003382 Py_DECREF(enter);
3383 if (res == NULL)
3384 goto error;
3385 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003386 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003387 DISPATCH();
3388 }
3389
Benjamin Petersonddd19492018-09-16 22:38:02 -07003390 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003391 PyObject *res = POP();
3392 /* Setup the finally block before pushing the result
3393 of __aenter__ on the stack. */
3394 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3395 STACK_LEVEL());
3396 PUSH(res);
3397 DISPATCH();
3398 }
3399
Benjamin Petersonddd19492018-09-16 22:38:02 -07003400 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003401 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003402 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003404 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003406 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003407 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003408 }
3409 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003410 if (exit == NULL) {
3411 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003413 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003416 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003417 Py_DECREF(enter);
3418 if (res == NULL)
3419 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 /* Setup the finally block before pushing the result
3421 of __enter__ on the stack. */
3422 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3423 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003424
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003425 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 DISPATCH();
3427 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003428
Mark Shannonfee55262019-11-21 09:11:43 +00003429 case TARGET(WITH_EXCEPT_START): {
3430 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003432 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3433 - SEVENTH: the context.__exit__ bound method
3434 We call SEVENTH(TOP, SECOND, THIRD).
3435 Then we push again the TOP exception and the __exit__
3436 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003439 PyObject *exc, *val, *tb, *res;
3440
Victor Stinner842cfff2016-12-01 14:45:31 +01003441 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003442 val = SECOND();
3443 tb = THIRD();
3444 assert(exc != Py_None);
3445 assert(!PyLong_Check(exc));
3446 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003447 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003448 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003449 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 if (res == NULL)
3451 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003452
Yury Selivanov75445082015-05-11 22:57:16 -04003453 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003456
Benjamin Petersonddd19492018-09-16 22:38:02 -07003457 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003458 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003459 PyObject *name = GETITEM(names, oparg);
3460 PyObject *obj = TOP();
3461 PyObject *meth = NULL;
3462
3463 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3464
Yury Selivanovf2392132016-12-13 19:03:51 -05003465 if (meth == NULL) {
3466 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003467 goto error;
3468 }
3469
3470 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003471 /* We can bypass temporary bound method object.
3472 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003473
INADA Naoki015bce62017-01-16 17:23:30 +09003474 meth | self | arg1 | ... | argN
3475 */
3476 SET_TOP(meth);
3477 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003478 }
3479 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003480 /* meth is not an unbound method (but a regular attr, or
3481 something was returned by a descriptor protocol). Set
3482 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003483 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003484
3485 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 */
INADA Naoki015bce62017-01-16 17:23:30 +09003487 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003489 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 }
3491 DISPATCH();
3492 }
3493
Benjamin Petersonddd19492018-09-16 22:38:02 -07003494 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003495 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003496 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003497
3498 sp = stack_pointer;
3499
INADA Naoki015bce62017-01-16 17:23:30 +09003500 meth = PEEK(oparg + 2);
3501 if (meth == NULL) {
3502 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3503 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003504
3505 Stack layout:
3506
INADA Naoki015bce62017-01-16 17:23:30 +09003507 ... | NULL | callable | arg1 | ... | argN
3508 ^- TOP()
3509 ^- (-oparg)
3510 ^- (-oparg-1)
3511 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003512
Ville Skyttä49b27342017-08-03 09:00:59 +03003513 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003514 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003515 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003516 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003517 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003518 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003519 }
3520 else {
3521 /* This is a method call. Stack layout:
3522
INADA Naoki015bce62017-01-16 17:23:30 +09003523 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003524 ^- TOP()
3525 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003526 ^- (-oparg-1)
3527 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003528
INADA Naoki015bce62017-01-16 17:23:30 +09003529 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003530 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003531 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003532 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003533 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003534 stack_pointer = sp;
3535 }
3536
3537 PUSH(res);
3538 if (res == NULL)
3539 goto error;
3540 DISPATCH();
3541 }
3542
Benjamin Petersonddd19492018-09-16 22:38:02 -07003543 case TARGET(CALL_FUNCTION): {
3544 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003545 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003547 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003550 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003553 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003555
Benjamin Petersonddd19492018-09-16 22:38:02 -07003556 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003557 PyObject **sp, *res, *names;
3558
3559 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003560 assert(PyTuple_Check(names));
3561 assert(PyTuple_GET_SIZE(names) <= oparg);
3562 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003564 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003567 Py_DECREF(names);
3568
3569 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003570 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003571 }
3572 DISPATCH();
3573 }
3574
Benjamin Petersonddd19492018-09-16 22:38:02 -07003575 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003576 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003577 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003578 if (oparg & 0x01) {
3579 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003580 if (!PyDict_CheckExact(kwargs)) {
3581 PyObject *d = PyDict_New();
3582 if (d == NULL)
3583 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003584 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003585 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003586 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003587 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003588 goto error;
3589 }
3590 Py_DECREF(kwargs);
3591 kwargs = d;
3592 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003593 assert(PyDict_CheckExact(kwargs));
3594 }
3595 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003596 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003597 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003598 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003599 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003600 goto error;
3601 }
3602 Py_SETREF(callargs, PySequence_Tuple(callargs));
3603 if (callargs == NULL) {
3604 goto error;
3605 }
3606 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003607 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003608
Victor Stinner09532fe2019-05-10 23:39:09 +02003609 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003610 Py_DECREF(func);
3611 Py_DECREF(callargs);
3612 Py_XDECREF(kwargs);
3613
3614 SET_TOP(result);
3615 if (result == NULL) {
3616 goto error;
3617 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003618 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003620
Benjamin Petersonddd19492018-09-16 22:38:02 -07003621 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003622 PyObject *qualname = POP();
3623 PyObject *codeobj = POP();
3624 PyFunctionObject *func = (PyFunctionObject *)
3625 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003626
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003627 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003628 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003629 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003630 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003632
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003633 if (oparg & 0x08) {
3634 assert(PyTuple_CheckExact(TOP()));
3635 func ->func_closure = POP();
3636 }
3637 if (oparg & 0x04) {
3638 assert(PyDict_CheckExact(TOP()));
3639 func->func_annotations = POP();
3640 }
3641 if (oparg & 0x02) {
3642 assert(PyDict_CheckExact(TOP()));
3643 func->func_kwdefaults = POP();
3644 }
3645 if (oparg & 0x01) {
3646 assert(PyTuple_CheckExact(TOP()));
3647 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003649
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003650 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003651 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003653
Benjamin Petersonddd19492018-09-16 22:38:02 -07003654 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003655 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003657 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003659 step = NULL;
3660 stop = POP();
3661 start = TOP();
3662 slice = PySlice_New(start, stop, step);
3663 Py_DECREF(start);
3664 Py_DECREF(stop);
3665 Py_XDECREF(step);
3666 SET_TOP(slice);
3667 if (slice == NULL)
3668 goto error;
3669 DISPATCH();
3670 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003671
Benjamin Petersonddd19492018-09-16 22:38:02 -07003672 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003673 /* Handles f-string value formatting. */
3674 PyObject *result;
3675 PyObject *fmt_spec;
3676 PyObject *value;
3677 PyObject *(*conv_fn)(PyObject *);
3678 int which_conversion = oparg & FVC_MASK;
3679 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3680
3681 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003682 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003683
3684 /* See if any conversion is specified. */
3685 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003686 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003687 case FVC_STR: conv_fn = PyObject_Str; break;
3688 case FVC_REPR: conv_fn = PyObject_Repr; break;
3689 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003690 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003691 _PyErr_Format(tstate, PyExc_SystemError,
3692 "unexpected conversion flag %d",
3693 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003694 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003695 }
3696
3697 /* If there's a conversion function, call it and replace
3698 value with that result. Otherwise, just use value,
3699 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003700 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003701 result = conv_fn(value);
3702 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003703 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003704 Py_XDECREF(fmt_spec);
3705 goto error;
3706 }
3707 value = result;
3708 }
3709
3710 /* If value is a unicode object, and there's no fmt_spec,
3711 then we know the result of format(value) is value
3712 itself. In that case, skip calling format(). I plan to
3713 move this optimization in to PyObject_Format()
3714 itself. */
3715 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3716 /* Do nothing, just transfer ownership to result. */
3717 result = value;
3718 } else {
3719 /* Actually call format(). */
3720 result = PyObject_Format(value, fmt_spec);
3721 Py_DECREF(value);
3722 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003723 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003724 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003725 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003726 }
3727
Eric V. Smith135d5f42016-02-05 18:23:08 -05003728 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003729 DISPATCH();
3730 }
3731
Benjamin Petersonddd19492018-09-16 22:38:02 -07003732 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003733 int oldoparg = oparg;
3734 NEXTOPARG();
3735 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003737 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003738
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003739
Antoine Pitrou042b1282010-08-13 21:15:58 +00003740#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003742#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 default:
3744 fprintf(stderr,
3745 "XXX lineno: %d, opcode: %d\n",
3746 PyFrame_GetLineNumber(f),
3747 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003748 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003749 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003752
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003753 /* This should never be reached. Every opcode should end with DISPATCH()
3754 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003755 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003756
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003757error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003758 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003759#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003760 if (!_PyErr_Occurred(tstate)) {
3761 _PyErr_SetString(tstate, PyExc_SystemError,
3762 "error return without exception set");
3763 }
Victor Stinner365b6932013-07-12 00:11:58 +02003764#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003765 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003766#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003767
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003768 /* Log traceback info. */
3769 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003770
Mark Shannoncb9879b2020-07-17 11:44:23 +01003771 if (tstate->c_tracefunc != NULL) {
3772 /* Make sure state is set to FRAME_EXECUTING for tracing */
3773 assert(f->f_state == FRAME_EXECUTING);
3774 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003775 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3776 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01003777 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003778exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01003779 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003780 /* Unwind stacks if an exception occurred */
3781 while (f->f_iblock > 0) {
3782 /* Pop the current block. */
3783 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 if (b->b_type == EXCEPT_HANDLER) {
3786 UNWIND_EXCEPT_HANDLER(b);
3787 continue;
3788 }
3789 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003790 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 PyObject *exc, *val, *tb;
3792 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003793 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 /* Beware, this invalidates all b->b_* fields */
3795 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003796 PUSH(exc_info->exc_traceback);
3797 PUSH(exc_info->exc_value);
3798 if (exc_info->exc_type != NULL) {
3799 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
3801 else {
3802 Py_INCREF(Py_None);
3803 PUSH(Py_None);
3804 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003805 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 /* Make the raw exception data
3807 available to the handler,
3808 so a program can emulate the
3809 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003810 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003811 if (tb != NULL)
3812 PyException_SetTraceback(val, tb);
3813 else
3814 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003816 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003818 exc_info->exc_value = val;
3819 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 if (tb == NULL)
3821 tb = Py_None;
3822 Py_INCREF(tb);
3823 PUSH(tb);
3824 PUSH(val);
3825 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003827 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003828 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3829 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3830 /* Make sure that we trace line after exception if we are in a new execution
3831 * window or we don't need a line update and we are not in the first instruction
3832 * of the line. */
3833 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3834 instr_prev = INT_MAX;
3835 }
Mark Shannonfee55262019-11-21 09:11:43 +00003836 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003837 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01003838 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003839 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 }
3841 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003842
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003843 /* End the loop as we still have an error */
3844 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003846
Pablo Galindof00828a2019-05-09 16:52:02 +01003847 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003848 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 /* Pop remaining stack entries. */
3851 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003852 PyObject *o = POP();
3853 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01003855 f->f_stackdepth = 0;
3856 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003857exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003859 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003860 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3861 tstate, f, PyTrace_RETURN, retval)) {
3862 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 }
3864 }
3865 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003866 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3867 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003868 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 }
3870 }
3871 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003874exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003875 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3876 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003877 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003879
Victor Stinner0b72b232020-03-12 23:18:39 +01003880 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003881}
3882
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003884format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003885 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003886{
3887 int err;
3888 Py_ssize_t len = PyList_GET_SIZE(names);
3889 PyObject *name_str, *comma, *tail, *tmp;
3890
3891 assert(PyList_CheckExact(names));
3892 assert(len >= 1);
3893 /* Deal with the joys of natural language. */
3894 switch (len) {
3895 case 1:
3896 name_str = PyList_GET_ITEM(names, 0);
3897 Py_INCREF(name_str);
3898 break;
3899 case 2:
3900 name_str = PyUnicode_FromFormat("%U and %U",
3901 PyList_GET_ITEM(names, len - 2),
3902 PyList_GET_ITEM(names, len - 1));
3903 break;
3904 default:
3905 tail = PyUnicode_FromFormat(", %U, and %U",
3906 PyList_GET_ITEM(names, len - 2),
3907 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003908 if (tail == NULL)
3909 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 /* Chop off the last two objects in the list. This shouldn't actually
3911 fail, but we can't be too careful. */
3912 err = PyList_SetSlice(names, len - 2, len, NULL);
3913 if (err == -1) {
3914 Py_DECREF(tail);
3915 return;
3916 }
3917 /* Stitch everything up into a nice comma-separated list. */
3918 comma = PyUnicode_FromString(", ");
3919 if (comma == NULL) {
3920 Py_DECREF(tail);
3921 return;
3922 }
3923 tmp = PyUnicode_Join(comma, names);
3924 Py_DECREF(comma);
3925 if (tmp == NULL) {
3926 Py_DECREF(tail);
3927 return;
3928 }
3929 name_str = PyUnicode_Concat(tmp, tail);
3930 Py_DECREF(tmp);
3931 Py_DECREF(tail);
3932 break;
3933 }
3934 if (name_str == NULL)
3935 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003936 _PyErr_Format(tstate, PyExc_TypeError,
3937 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003938 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003939 len,
3940 kind,
3941 len == 1 ? "" : "s",
3942 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003943 Py_DECREF(name_str);
3944}
3945
3946static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003947missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3948 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003949 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003950{
Victor Stinner74319ae2016-08-25 00:04:09 +02003951 Py_ssize_t i, j = 0;
3952 Py_ssize_t start, end;
3953 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003954 const char *kind = positional ? "positional" : "keyword-only";
3955 PyObject *missing_names;
3956
3957 /* Compute the names of the arguments that are missing. */
3958 missing_names = PyList_New(missing);
3959 if (missing_names == NULL)
3960 return;
3961 if (positional) {
3962 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003963 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003964 }
3965 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003966 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003967 end = start + co->co_kwonlyargcount;
3968 }
3969 for (i = start; i < end; i++) {
3970 if (GETLOCAL(i) == NULL) {
3971 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3972 PyObject *name = PyObject_Repr(raw);
3973 if (name == NULL) {
3974 Py_DECREF(missing_names);
3975 return;
3976 }
3977 PyList_SET_ITEM(missing_names, j++, name);
3978 }
3979 }
3980 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003981 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003982 Py_DECREF(missing_names);
3983}
3984
3985static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003986too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3987 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003988 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003989{
3990 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003991 Py_ssize_t kwonly_given = 0;
3992 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003994 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995
Benjamin Petersone109c702011-06-24 09:37:26 -05003996 assert((co->co_flags & CO_VARARGS) == 0);
3997 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003998 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003999 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004001 }
4002 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004003 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004004 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004006 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 }
4008 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004009 plural = (co_argcount != 1);
4010 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 }
4012 if (sig == NULL)
4013 return;
4014 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004015 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4016 kwonly_sig = PyUnicode_FromFormat(format,
4017 given != 1 ? "s" : "",
4018 kwonly_given,
4019 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 if (kwonly_sig == NULL) {
4021 Py_DECREF(sig);
4022 return;
4023 }
4024 }
4025 else {
4026 /* This will not fail. */
4027 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004028 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004030 _PyErr_Format(tstate, PyExc_TypeError,
4031 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004032 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004033 sig,
4034 plural ? "s" : "",
4035 given,
4036 kwonly_sig,
4037 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 Py_DECREF(sig);
4039 Py_DECREF(kwonly_sig);
4040}
4041
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004042static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004043positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004044 Py_ssize_t kwcount, PyObject* const* kwnames,
4045 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004046{
4047 int posonly_conflicts = 0;
4048 PyObject* posonly_names = PyList_New(0);
4049
4050 for(int k=0; k < co->co_posonlyargcount; k++){
4051 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4052
4053 for (int k2=0; k2<kwcount; k2++){
4054 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4055 PyObject* kwname = kwnames[k2];
4056 if (kwname == posonly_name){
4057 if(PyList_Append(posonly_names, kwname) != 0) {
4058 goto fail;
4059 }
4060 posonly_conflicts++;
4061 continue;
4062 }
4063
4064 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4065
4066 if ( cmp > 0) {
4067 if(PyList_Append(posonly_names, kwname) != 0) {
4068 goto fail;
4069 }
4070 posonly_conflicts++;
4071 } else if (cmp < 0) {
4072 goto fail;
4073 }
4074
4075 }
4076 }
4077 if (posonly_conflicts) {
4078 PyObject* comma = PyUnicode_FromString(", ");
4079 if (comma == NULL) {
4080 goto fail;
4081 }
4082 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4083 Py_DECREF(comma);
4084 if (error_names == NULL) {
4085 goto fail;
4086 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004087 _PyErr_Format(tstate, PyExc_TypeError,
4088 "%U() got some positional-only arguments passed"
4089 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004090 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004091 Py_DECREF(error_names);
4092 goto fail;
4093 }
4094
4095 Py_DECREF(posonly_names);
4096 return 0;
4097
4098fail:
4099 Py_XDECREF(posonly_names);
4100 return 1;
4101
4102}
4103
Guido van Rossumc2e20742006-02-27 22:32:47 +00004104/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004105 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004106 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004107
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004108PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004109_PyEval_EvalCode(PyThreadState *tstate,
4110 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004111 PyObject *const *args, Py_ssize_t argcount,
4112 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004113 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004114 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004115 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004116 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004117{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004118 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004119
Victor Stinner232dda62020-06-04 15:19:02 +02004120 PyCodeObject *co = (PyCodeObject*)_co;
4121
4122 if (!name) {
4123 name = co->co_name;
4124 }
4125 assert(name != NULL);
4126 assert(PyUnicode_Check(name));
4127
4128 if (!qualname) {
4129 qualname = name;
4130 }
4131 assert(qualname != NULL);
4132 assert(PyUnicode_Check(qualname));
4133
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004134 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004135 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004138 _PyErr_SetString(tstate, PyExc_SystemError,
4139 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 return NULL;
4141 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004142
Victor Stinnerc7020012016-08-16 23:40:29 +02004143 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004144 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004145 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004147 }
Victor Stinner232dda62020-06-04 15:19:02 +02004148 PyObject **fastlocals = f->f_localsplus;
4149 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004150
Victor Stinnerc7020012016-08-16 23:40:29 +02004151 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004152 PyObject *kwdict;
4153 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004154 if (co->co_flags & CO_VARKEYWORDS) {
4155 kwdict = PyDict_New();
4156 if (kwdict == NULL)
4157 goto fail;
4158 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004159 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004160 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004161 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004162 SETLOCAL(i, kwdict);
4163 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004164 else {
4165 kwdict = NULL;
4166 }
4167
Pablo Galindocd74e662019-06-01 18:08:04 +01004168 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004169 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004170 if (argcount > co->co_argcount) {
4171 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004172 }
4173 else {
4174 n = argcount;
4175 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004176 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004177 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004178 Py_INCREF(x);
4179 SETLOCAL(j, x);
4180 }
4181
Victor Stinnerc7020012016-08-16 23:40:29 +02004182 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004183 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004184 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004185 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004186 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004187 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004188 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004189 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004190
Serhiy Storchakab7281052016-09-12 00:52:40 +03004191 /* Handle keyword arguments passed as two strided arrays */
4192 kwcount *= kwstep;
4193 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004194 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004195 PyObject *keyword = kwnames[i];
4196 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004197 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004198
Benjamin Petersonb204a422011-06-05 22:04:07 -05004199 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004200 _PyErr_Format(tstate, PyExc_TypeError,
4201 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004202 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004205
Benjamin Petersonb204a422011-06-05 22:04:07 -05004206 /* Speed hack: do raw pointer compares. As names are
4207 normally interned this should almost always hit. */
4208 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004209 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004210 PyObject *varname = co_varnames[j];
4211 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004212 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004213 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004214 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004215
Benjamin Petersonb204a422011-06-05 22:04:07 -05004216 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004217 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004218 PyObject *varname = co_varnames[j];
4219 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004220 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004221 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004222 }
4223 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004225 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004226 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004227
Victor Stinner231d1f32017-01-11 02:12:06 +01004228 assert(j >= total_args);
4229 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004230
Victor Stinner438a12d2019-05-24 17:01:38 +02004231 if (co->co_posonlyargcount
4232 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004233 kwcount, kwnames,
4234 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004235 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004236 goto fail;
4237 }
4238
Victor Stinner438a12d2019-05-24 17:01:38 +02004239 _PyErr_Format(tstate, PyExc_TypeError,
4240 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004241 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004242 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004244
Christian Heimes0bd447f2013-07-20 14:48:10 +02004245 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4246 goto fail;
4247 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004248 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004249
Benjamin Petersonb204a422011-06-05 22:04:07 -05004250 kw_found:
4251 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004252 _PyErr_Format(tstate, PyExc_TypeError,
4253 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004254 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004255 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004257 Py_INCREF(value);
4258 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004260
4261 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004262 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004263 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4264 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 goto fail;
4266 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004267
4268 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004269 if (argcount < co->co_argcount) {
4270 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004271 Py_ssize_t missing = 0;
4272 for (i = argcount; i < m; i++) {
4273 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004274 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004275 }
4276 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004277 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004278 missing_arguments(tstate, co, missing, defcount, fastlocals,
4279 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004280 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004281 }
4282 if (n > m)
4283 i = n - m;
4284 else
4285 i = 0;
4286 for (; i < defcount; i++) {
4287 if (GETLOCAL(m+i) == NULL) {
4288 PyObject *def = defs[i];
4289 Py_INCREF(def);
4290 SETLOCAL(m+i, def);
4291 }
4292 }
4293 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004294
4295 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004296 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004297 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004298 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004299 if (GETLOCAL(i) != NULL)
4300 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004301 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004302 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004303 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004304 if (def) {
4305 Py_INCREF(def);
4306 SETLOCAL(i, def);
4307 continue;
4308 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004309 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004310 goto fail;
4311 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004312 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004313 missing++;
4314 }
4315 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004316 missing_arguments(tstate, co, missing, -1, fastlocals,
4317 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004318 goto fail;
4319 }
4320 }
4321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004323 vars into frame. */
4324 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004326 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004327 /* Possibly account for the cell variable being an argument. */
4328 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004329 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004330 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004331 /* Clear the local copy. */
4332 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004333 }
4334 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004335 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004336 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004337 if (c == NULL)
4338 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004339 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004341
4342 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004343 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4344 PyObject *o = PyTuple_GET_ITEM(closure, i);
4345 Py_INCREF(o);
4346 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004348
Yury Selivanoveb636452016-09-08 22:01:51 -07004349 /* Handle generator/coroutine/asynchronous generator */
4350 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004351 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004352 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 /* Don't need to keep the reference to f_back, it will be set
4355 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004356 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 /* Create a new generator that owns the ready to run frame
4359 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004360 if (is_coro) {
4361 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004362 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4363 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004364 } else {
4365 gen = PyGen_NewWithQualName(f, name, qualname);
4366 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004367 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004368 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004369 }
INADA Naoki9c157762016-12-26 18:52:46 +09004370
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004371 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004372
Yury Selivanov75445082015-05-11 22:57:16 -04004373 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004375
Victor Stinnerb9e68122019-11-14 12:20:46 +01004376 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004377
Thomas Woutersce272b62007-09-19 21:19:28 +00004378fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 /* decref'ing the frame can cause __del__ methods to get invoked,
4381 which can call back into Python. While we're done with the
4382 current Python frame (f), the associated C stack is still in use,
4383 so recursion_depth must be boosted for the duration.
4384 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004385 if (Py_REFCNT(f) > 1) {
4386 Py_DECREF(f);
4387 _PyObject_GC_TRACK(f);
4388 }
4389 else {
4390 ++tstate->recursion_depth;
4391 Py_DECREF(f);
4392 --tstate->recursion_depth;
4393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004395}
4396
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004397
4398PyObject *
4399_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4400 PyObject *const *args, Py_ssize_t argcount,
4401 PyObject *const *kwnames, PyObject *const *kwargs,
4402 Py_ssize_t kwcount, int kwstep,
4403 PyObject *const *defs, Py_ssize_t defcount,
4404 PyObject *kwdefs, PyObject *closure,
4405 PyObject *name, PyObject *qualname)
4406{
4407 PyThreadState *tstate = _PyThreadState_GET();
4408 return _PyEval_EvalCode(tstate, _co, globals, locals,
4409 args, argcount,
4410 kwnames, kwargs,
4411 kwcount, kwstep,
4412 defs, defcount,
4413 kwdefs, closure,
4414 name, qualname);
4415}
4416
Victor Stinner40ee3012014-06-16 15:59:28 +02004417PyObject *
4418PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004419 PyObject *const *args, int argcount,
4420 PyObject *const *kws, int kwcount,
4421 PyObject *const *defs, int defcount,
4422 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004423{
4424 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004425 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004426 kws, kws != NULL ? kws + 1 : NULL,
4427 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004428 defs, defcount,
4429 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004430 NULL, NULL);
4431}
Tim Peters5ca576e2001-06-18 22:08:13 +00004432
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004433static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004434special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004437 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004438 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004439 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 return NULL;
4441 }
4442 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004443}
4444
4445
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004446/* Logic for the raise statement (too complicated for inlining).
4447 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004448static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004449do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 if (exc == NULL) {
4454 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004455 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004457 type = exc_info->exc_type;
4458 value = exc_info->exc_value;
4459 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004460 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004461 _PyErr_SetString(tstate, PyExc_RuntimeError,
4462 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004463 return 0;
4464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 Py_XINCREF(type);
4466 Py_XINCREF(value);
4467 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004468 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004469 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 /* We support the following forms of raise:
4473 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004474 raise <instance>
4475 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 if (PyExceptionClass_Check(exc)) {
4478 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004479 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 if (value == NULL)
4481 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004482 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004483 _PyErr_Format(tstate, PyExc_TypeError,
4484 "calling %R should have returned an instance of "
4485 "BaseException, not %R",
4486 type, Py_TYPE(value));
4487 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 }
4490 else if (PyExceptionInstance_Check(exc)) {
4491 value = exc;
4492 type = PyExceptionInstance_Class(exc);
4493 Py_INCREF(type);
4494 }
4495 else {
4496 /* Not something you can raise. You get an exception
4497 anyway, just not what you specified :-) */
4498 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004499 _PyErr_SetString(tstate, PyExc_TypeError,
4500 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 goto raise_error;
4502 }
Collin Winter828f04a2007-08-31 00:04:24 +00004503
Serhiy Storchakac0191582016-09-27 11:37:10 +03004504 assert(type != NULL);
4505 assert(value != NULL);
4506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 if (cause) {
4508 PyObject *fixed_cause;
4509 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004510 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 if (fixed_cause == NULL)
4512 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004513 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004515 else if (PyExceptionInstance_Check(cause)) {
4516 fixed_cause = cause;
4517 }
4518 else if (cause == Py_None) {
4519 Py_DECREF(cause);
4520 fixed_cause = NULL;
4521 }
4522 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 _PyErr_SetString(tstate, PyExc_TypeError,
4524 "exception causes must derive from "
4525 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 goto raise_error;
4527 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004528 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 }
Collin Winter828f04a2007-08-31 00:04:24 +00004530
Victor Stinner438a12d2019-05-24 17:01:38 +02004531 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004532 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004533 Py_DECREF(value);
4534 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004535 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004536
4537raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 Py_XDECREF(value);
4539 Py_XDECREF(type);
4540 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004541 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004542}
4543
Tim Petersd6d010b2001-06-21 02:49:55 +00004544/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004545 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004546
Guido van Rossum0368b722007-05-11 16:50:42 +00004547 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4548 with a variable target.
4549*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004550
Barry Warsawe42b18f1997-08-25 22:13:04 +00004551static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004552unpack_iterable(PyThreadState *tstate, PyObject *v,
4553 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 int i = 0, j = 0;
4556 Py_ssize_t ll = 0;
4557 PyObject *it; /* iter(v) */
4558 PyObject *w;
4559 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004564 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004565 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004566 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004567 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004568 _PyErr_Format(tstate, PyExc_TypeError,
4569 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004570 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004571 }
4572 return 0;
4573 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 for (; i < argcnt; i++) {
4576 w = PyIter_Next(it);
4577 if (w == NULL) {
4578 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004579 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004580 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004581 _PyErr_Format(tstate, PyExc_ValueError,
4582 "not enough values to unpack "
4583 "(expected %d, got %d)",
4584 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004585 }
4586 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004587 _PyErr_Format(tstate, PyExc_ValueError,
4588 "not enough values to unpack "
4589 "(expected at least %d, got %d)",
4590 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004591 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 }
4593 goto Error;
4594 }
4595 *--sp = w;
4596 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 if (argcntafter == -1) {
4599 /* We better have exhausted the iterator now. */
4600 w = PyIter_Next(it);
4601 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004602 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 goto Error;
4604 Py_DECREF(it);
4605 return 1;
4606 }
4607 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004608 _PyErr_Format(tstate, PyExc_ValueError,
4609 "too many values to unpack (expected %d)",
4610 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 goto Error;
4612 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 l = PySequence_List(it);
4615 if (l == NULL)
4616 goto Error;
4617 *--sp = l;
4618 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 ll = PyList_GET_SIZE(l);
4621 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004622 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004623 "not enough values to unpack (expected at least %d, got %zd)",
4624 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 goto Error;
4626 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 /* Pop the "after-variable" args off the list. */
4629 for (j = argcntafter; j > 0; j--, i++) {
4630 *--sp = PyList_GET_ITEM(l, ll - j);
4631 }
4632 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004633 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 Py_DECREF(it);
4635 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004636
Tim Petersd6d010b2001-06-21 02:49:55 +00004637Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 for (; i > 0; i--, sp++)
4639 Py_DECREF(*sp);
4640 Py_XDECREF(it);
4641 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004642}
4643
4644
Guido van Rossum96a42c81992-01-12 02:29:51 +00004645#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004646static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004647prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004650 if (PyObject_Print(v, stdout, 0) != 0) {
4651 /* Don't know what else to do */
4652 _PyErr_Clear(tstate);
4653 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 printf("\n");
4655 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004656}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004657#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004658
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004659static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004660call_exc_trace(Py_tracefunc func, PyObject *self,
4661 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004662{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004663 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004665 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 if (value == NULL) {
4667 value = Py_None;
4668 Py_INCREF(value);
4669 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004670 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004671 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 arg = PyTuple_Pack(3, type, value, traceback);
4673 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004674 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 return;
4676 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004677 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004679 if (err == 0) {
4680 _PyErr_Restore(tstate, type, value, orig_traceback);
4681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 else {
4683 Py_XDECREF(type);
4684 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004685 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004687}
4688
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004689static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004690call_trace_protected(Py_tracefunc func, PyObject *obj,
4691 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 PyObject *type, *value, *traceback;
4695 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004696 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004697 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 if (err == 0)
4699 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004700 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 return 0;
4702 }
4703 else {
4704 Py_XDECREF(type);
4705 Py_XDECREF(value);
4706 Py_XDECREF(traceback);
4707 return -1;
4708 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004709}
4710
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004711static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004712call_trace(Py_tracefunc func, PyObject *obj,
4713 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 int result;
4717 if (tstate->tracing)
4718 return 0;
4719 tstate->tracing++;
4720 tstate->use_tracing = 0;
4721 result = func(obj, frame, what, arg);
4722 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4723 || (tstate->c_profilefunc != NULL));
4724 tstate->tracing--;
4725 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004726}
4727
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004728PyObject *
4729_PyEval_CallTracing(PyObject *func, PyObject *args)
4730{
Victor Stinner50b48572018-11-01 01:51:40 +01004731 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 int save_tracing = tstate->tracing;
4733 int save_use_tracing = tstate->use_tracing;
4734 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 tstate->tracing = 0;
4737 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4738 || (tstate->c_profilefunc != NULL));
4739 result = PyObject_Call(func, args, NULL);
4740 tstate->tracing = save_tracing;
4741 tstate->use_tracing = save_use_tracing;
4742 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004743}
4744
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004745/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004746static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004747maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004748 PyThreadState *tstate, PyFrameObject *frame,
4749 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 int result = 0;
4752 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 /* If the last instruction executed isn't in the current
4755 instruction window, reset the window.
4756 */
4757 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4758 PyAddrPair bounds;
4759 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4760 &bounds);
4761 *instr_lb = bounds.ap_lower;
4762 *instr_ub = bounds.ap_upper;
4763 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004764 /* If the last instruction falls at the start of a line or if it
4765 represents a jump backwards, update the frame's line number and
4766 then call the trace function if we're tracing source lines.
4767 */
4768 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004770 if (frame->f_trace_lines) {
4771 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4772 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 }
George King20faa682017-10-18 17:44:22 -07004774 /* Always emit an opcode event if we're tracing all opcodes. */
4775 if (frame->f_trace_opcodes) {
4776 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4777 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 *instr_prev = frame->f_lasti;
4779 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004780}
4781
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782int
4783_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4784{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004785 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004786 /* The caller must hold the GIL */
4787 assert(PyGILState_Check());
4788
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004789 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004790 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004791 PyThreadState *current_tstate = _PyThreadState_GET();
4792 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004793 return -1;
4794 }
4795
4796 PyObject *profileobj = tstate->c_profileobj;
4797
4798 tstate->c_profilefunc = NULL;
4799 tstate->c_profileobj = NULL;
4800 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4801 tstate->use_tracing = tstate->c_tracefunc != NULL;
4802 Py_XDECREF(profileobj);
4803
4804 Py_XINCREF(arg);
4805 tstate->c_profileobj = arg;
4806 tstate->c_profilefunc = func;
4807
4808 /* Flag that tracing or profiling is turned on */
4809 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4810 return 0;
4811}
4812
Fred Drake5755ce62001-06-27 19:19:46 +00004813void
4814PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004815{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004816 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004817 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004818 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004819 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4820 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004821}
4822
4823int
4824_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4825{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004826 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004827 /* The caller must hold the GIL */
4828 assert(PyGILState_Check());
4829
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004830 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004831 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004832 PyThreadState *current_tstate = _PyThreadState_GET();
4833 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004834 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004835 }
4836
Victor Stinnerda2914d2020-03-20 09:29:08 +01004837 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004838 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004839 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004840
4841 tstate->c_tracefunc = NULL;
4842 tstate->c_traceobj = NULL;
4843 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4844 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4845 Py_XDECREF(traceobj);
4846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004848 tstate->c_traceobj = arg;
4849 tstate->c_tracefunc = func;
4850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004852 tstate->use_tracing = ((func != NULL)
4853 || (tstate->c_profilefunc != NULL));
4854
4855 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004856}
4857
4858void
4859PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4860{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004861 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004862 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004863 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004864 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4865 }
Fred Draked0838392001-06-16 21:02:31 +00004866}
4867
Victor Stinner309d7cc2020-03-13 16:39:12 +01004868
Yury Selivanov75445082015-05-11 22:57:16 -04004869void
Victor Stinner838f2642019-06-13 22:41:23 +02004870_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004871{
4872 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004873 tstate->coroutine_origin_tracking_depth = new_depth;
4874}
4875
4876int
4877_PyEval_GetCoroutineOriginTrackingDepth(void)
4878{
Victor Stinner50b48572018-11-01 01:51:40 +01004879 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004880 return tstate->coroutine_origin_tracking_depth;
4881}
4882
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004883int
Yury Selivanoveb636452016-09-08 22:01:51 -07004884_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4885{
Victor Stinner50b48572018-11-01 01:51:40 +01004886 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004887
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004888 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004889 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004890 }
4891
Yury Selivanoveb636452016-09-08 22:01:51 -07004892 Py_XINCREF(firstiter);
4893 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004894 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004895}
4896
4897PyObject *
4898_PyEval_GetAsyncGenFirstiter(void)
4899{
Victor Stinner50b48572018-11-01 01:51:40 +01004900 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004901 return tstate->async_gen_firstiter;
4902}
4903
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004904int
Yury Selivanoveb636452016-09-08 22:01:51 -07004905_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4906{
Victor Stinner50b48572018-11-01 01:51:40 +01004907 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004908
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004909 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004910 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004911 }
4912
Yury Selivanoveb636452016-09-08 22:01:51 -07004913 Py_XINCREF(finalizer);
4914 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004915 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004916}
4917
4918PyObject *
4919_PyEval_GetAsyncGenFinalizer(void)
4920{
Victor Stinner50b48572018-11-01 01:51:40 +01004921 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004922 return tstate->async_gen_finalizer;
4923}
4924
Victor Stinner438a12d2019-05-24 17:01:38 +02004925PyFrameObject *
4926PyEval_GetFrame(void)
4927{
4928 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004929 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004930}
4931
Guido van Rossumb209a111997-04-29 18:18:01 +00004932PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004933PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004934{
Victor Stinner438a12d2019-05-24 17:01:38 +02004935 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004936 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004938 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 else
4940 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004941}
4942
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004943/* Convenience function to get a builtin from its name */
4944PyObject *
4945_PyEval_GetBuiltinId(_Py_Identifier *name)
4946{
Victor Stinner438a12d2019-05-24 17:01:38 +02004947 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004948 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4949 if (attr) {
4950 Py_INCREF(attr);
4951 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004952 else if (!_PyErr_Occurred(tstate)) {
4953 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004954 }
4955 return attr;
4956}
4957
Guido van Rossumb209a111997-04-29 18:18:01 +00004958PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004959PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004960{
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004962 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004963 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004964 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004966 }
4967
Victor Stinner438a12d2019-05-24 17:01:38 +02004968 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004969 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004970 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004971
4972 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004974}
4975
Guido van Rossumb209a111997-04-29 18:18:01 +00004976PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004977PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004978{
Victor Stinner438a12d2019-05-24 17:01:38 +02004979 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004980 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004981 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004983 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004984
4985 assert(current_frame->f_globals != NULL);
4986 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004987}
4988
Guido van Rossum6135a871995-01-09 17:53:26 +00004989int
Tim Peters5ba58662001-07-16 02:29:45 +00004990PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004991{
Victor Stinner438a12d2019-05-24 17:01:38 +02004992 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004993 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 if (current_frame != NULL) {
4997 const int codeflags = current_frame->f_code->co_flags;
4998 const int compilerflags = codeflags & PyCF_MASK;
4999 if (compilerflags) {
5000 result = 1;
5001 cf->cf_flags |= compilerflags;
5002 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005003#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 if (codeflags & CO_GENERATOR_ALLOWED) {
5005 result = 1;
5006 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5007 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005008#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 }
5010 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005011}
5012
Guido van Rossum3f5da241990-12-20 15:06:42 +00005013
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005014const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005015PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 if (PyMethod_Check(func))
5018 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5019 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005020 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 else if (PyCFunction_Check(func))
5022 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5023 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005024 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005025}
5026
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005027const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005028PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 if (PyMethod_Check(func))
5031 return "()";
5032 else if (PyFunction_Check(func))
5033 return "()";
5034 else if (PyCFunction_Check(func))
5035 return "()";
5036 else
5037 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005038}
5039
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005040#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005041if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005042 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5043 tstate, tstate->frame, \
5044 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 x = NULL; \
5046 } \
5047 else { \
5048 x = call; \
5049 if (tstate->c_profilefunc != NULL) { \
5050 if (x == NULL) { \
5051 call_trace_protected(tstate->c_profilefunc, \
5052 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005053 tstate, tstate->frame, \
5054 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 /* XXX should pass (type, value, tb) */ \
5056 } else { \
5057 if (call_trace(tstate->c_profilefunc, \
5058 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005059 tstate, tstate->frame, \
5060 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 Py_DECREF(x); \
5062 x = NULL; \
5063 } \
5064 } \
5065 } \
5066 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005067} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 x = call; \
5069 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005070
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005071
5072static PyObject *
5073trace_call_function(PyThreadState *tstate,
5074 PyObject *func,
5075 PyObject **args, Py_ssize_t nargs,
5076 PyObject *kwnames)
5077{
5078 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005079 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005080 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005081 return x;
5082 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005083 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005084 /* We need to create a temporary bound method as argument
5085 for profiling.
5086
5087 If nargs == 0, then this cannot work because we have no
5088 "self". In any case, the call itself would raise
5089 TypeError (foo needs an argument), so we just skip
5090 profiling. */
5091 PyObject *self = args[0];
5092 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5093 if (func == NULL) {
5094 return NULL;
5095 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005096 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005097 args+1, nargs-1,
5098 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005099 Py_DECREF(func);
5100 return x;
5101 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005102 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005103}
5104
Victor Stinner415c5102017-01-11 00:54:57 +01005105/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5106 to reduce the stack consumption. */
5107Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005108call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005109{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005110 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 PyObject *func = *pfunc;
5112 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005113 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5114 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005115 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005116
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005117 if (tstate->use_tracing) {
5118 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005119 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005120 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005121 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005123
Victor Stinner438a12d2019-05-24 17:01:38 +02005124 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005125
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005126 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 while ((*pp_stack) > pfunc) {
5128 w = EXT_POP(*pp_stack);
5129 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005133}
5134
Jeremy Hylton52820442001-01-03 23:52:36 +00005135static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005136do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005137{
jdemeyere89de732018-09-19 12:06:20 +02005138 PyObject *result;
5139
scoder4c9ea092020-05-12 16:12:41 +02005140 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005141 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005142 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005144 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005145 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5146 if (nargs > 0 && tstate->use_tracing) {
5147 /* We need to create a temporary bound method as argument
5148 for profiling.
5149
5150 If nargs == 0, then this cannot work because we have no
5151 "self". In any case, the call itself would raise
5152 TypeError (foo needs an argument), so we just skip
5153 profiling. */
5154 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5155 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5156 if (func == NULL) {
5157 return NULL;
5158 }
5159
Victor Stinner4d231bc2019-11-14 13:36:21 +01005160 C_TRACE(result, _PyObject_FastCallDictTstate(
5161 tstate, func,
5162 &_PyTuple_ITEMS(callargs)[1],
5163 nargs - 1,
5164 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005165 Py_DECREF(func);
5166 return result;
5167 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005168 }
jdemeyere89de732018-09-19 12:06:20 +02005169 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005170}
5171
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005172/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005173 nb_index slot defined, and store in *pi.
5174 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005175 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005176 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005177*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005178int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005179_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005180{
Victor Stinner438a12d2019-05-24 17:01:38 +02005181 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005182 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005184 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005186 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 return 0;
5188 }
5189 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005190 _PyErr_SetString(tstate, PyExc_TypeError,
5191 "slice indices must be integers or "
5192 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 return 0;
5194 }
5195 *pi = x;
5196 }
5197 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005198}
5199
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005200int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005201_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005202{
Victor Stinner438a12d2019-05-24 17:01:38 +02005203 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005204 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005205 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005206 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005207 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005208 return 0;
5209 }
5210 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005211 _PyErr_SetString(tstate, PyExc_TypeError,
5212 "slice indices must be integers or "
5213 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005214 return 0;
5215 }
5216 *pi = x;
5217 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005218}
5219
Thomas Wouters52152252000-08-17 22:55:00 +00005220static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005221import_name(PyThreadState *tstate, PyFrameObject *f,
5222 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005223{
5224 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005225 PyObject *import_func, *res;
5226 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005227
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005228 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005229 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005230 if (!_PyErr_Occurred(tstate)) {
5231 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005232 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005233 return NULL;
5234 }
5235
5236 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005237 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005238 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005239 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005240 return NULL;
5241 }
5242 res = PyImport_ImportModuleLevelObject(
5243 name,
5244 f->f_globals,
5245 f->f_locals == NULL ? Py_None : f->f_locals,
5246 fromlist,
5247 ilevel);
5248 return res;
5249 }
5250
5251 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005252
5253 stack[0] = name;
5254 stack[1] = f->f_globals;
5255 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5256 stack[3] = fromlist;
5257 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005258 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005259 Py_DECREF(import_func);
5260 return res;
5261}
5262
5263static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005264import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005267 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005268
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005269 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005270 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005271 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005272 /* Issue #17636: in case this failed because of a circular relative
5273 import, try to fallback on reading the module directly from
5274 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005275 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005276 if (pkgname == NULL) {
5277 goto error;
5278 }
Oren Milman6db70332017-09-19 14:23:01 +03005279 if (!PyUnicode_Check(pkgname)) {
5280 Py_CLEAR(pkgname);
5281 goto error;
5282 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005283 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005284 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005285 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005286 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005287 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005288 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005289 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005290 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005291 goto error;
5292 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005293 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005294 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005295 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005296 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005297 if (pkgname == NULL) {
5298 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5299 if (pkgname_or_unknown == NULL) {
5300 Py_XDECREF(pkgpath);
5301 return NULL;
5302 }
5303 } else {
5304 pkgname_or_unknown = pkgname;
5305 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005306
5307 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005308 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005309 errmsg = PyUnicode_FromFormat(
5310 "cannot import name %R from %R (unknown location)",
5311 name, pkgname_or_unknown
5312 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005313 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005314 PyErr_SetImportError(errmsg, pkgname, NULL);
5315 }
5316 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005317 _Py_IDENTIFIER(__spec__);
5318 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005319 const char *fmt =
5320 _PyModuleSpec_IsInitializing(spec) ?
5321 "cannot import name %R from partially initialized module %R "
5322 "(most likely due to a circular import) (%S)" :
5323 "cannot import name %R from %R (%S)";
5324 Py_XDECREF(spec);
5325
5326 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005327 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005328 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005329 }
5330
Xiang Zhang4830f582017-03-21 11:13:42 +08005331 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005332 Py_XDECREF(pkgname_or_unknown);
5333 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005334 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005335}
Guido van Rossumac7be682001-01-17 15:42:30 +00005336
Thomas Wouters52152252000-08-17 22:55:00 +00005337static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005338import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005339{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005340 _Py_IDENTIFIER(__all__);
5341 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005342 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 int skip_leading_underscores = 0;
5344 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005345
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005346 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5347 return -1; /* Unexpected error */
5348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005349 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005350 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5351 return -1;
5352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005355 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 return -1;
5357 }
5358 all = PyMapping_Keys(dict);
5359 Py_DECREF(dict);
5360 if (all == NULL)
5361 return -1;
5362 skip_leading_underscores = 1;
5363 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 for (pos = 0, err = 0; ; pos++) {
5366 name = PySequence_GetItem(all, pos);
5367 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005368 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005370 }
5371 else {
5372 _PyErr_Clear(tstate);
5373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 break;
5375 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005376 if (!PyUnicode_Check(name)) {
5377 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5378 if (modname == NULL) {
5379 Py_DECREF(name);
5380 err = -1;
5381 break;
5382 }
5383 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005384 _PyErr_Format(tstate, PyExc_TypeError,
5385 "module __name__ must be a string, not %.100s",
5386 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005387 }
5388 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005389 _PyErr_Format(tstate, PyExc_TypeError,
5390 "%s in %U.%s must be str, not %.100s",
5391 skip_leading_underscores ? "Key" : "Item",
5392 modname,
5393 skip_leading_underscores ? "__dict__" : "__all__",
5394 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005395 }
5396 Py_DECREF(modname);
5397 Py_DECREF(name);
5398 err = -1;
5399 break;
5400 }
5401 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005402 if (PyUnicode_READY(name) == -1) {
5403 Py_DECREF(name);
5404 err = -1;
5405 break;
5406 }
5407 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5408 Py_DECREF(name);
5409 continue;
5410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 }
5412 value = PyObject_GetAttr(v, name);
5413 if (value == NULL)
5414 err = -1;
5415 else if (PyDict_CheckExact(locals))
5416 err = PyDict_SetItem(locals, name, value);
5417 else
5418 err = PyObject_SetItem(locals, name, value);
5419 Py_DECREF(name);
5420 Py_XDECREF(value);
5421 if (err != 0)
5422 break;
5423 }
5424 Py_DECREF(all);
5425 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005426}
5427
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005428static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005429check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005430{
Victor Stinnera102ed72020-02-07 02:24:48 +01005431 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005432 /* check_args_iterable() may be called with a live exception:
5433 * clear it to prevent calling _PyObject_FunctionStr() with an
5434 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005435 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005436 PyObject *funcstr = _PyObject_FunctionStr(func);
5437 if (funcstr != NULL) {
5438 _PyErr_Format(tstate, PyExc_TypeError,
5439 "%U argument after * must be an iterable, not %.200s",
5440 funcstr, Py_TYPE(args)->tp_name);
5441 Py_DECREF(funcstr);
5442 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005443 return -1;
5444 }
5445 return 0;
5446}
5447
5448static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005449format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005450{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005451 /* _PyDict_MergeEx raises attribute
5452 * error (percolated from an attempt
5453 * to get 'keys' attribute) instead of
5454 * a type error if its second argument
5455 * is not a mapping.
5456 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005457 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005458 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005459 PyObject *funcstr = _PyObject_FunctionStr(func);
5460 if (funcstr != NULL) {
5461 _PyErr_Format(
5462 tstate, PyExc_TypeError,
5463 "%U argument after ** must be a mapping, not %.200s",
5464 funcstr, Py_TYPE(kwargs)->tp_name);
5465 Py_DECREF(funcstr);
5466 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005467 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005468 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005469 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005470 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005471 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005472 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005473 PyObject *funcstr = _PyObject_FunctionStr(func);
5474 if (funcstr != NULL) {
5475 PyObject *key = PyTuple_GET_ITEM(val, 0);
5476 _PyErr_Format(
5477 tstate, PyExc_TypeError,
5478 "%U got multiple values for keyword argument '%S'",
5479 funcstr, key);
5480 Py_DECREF(funcstr);
5481 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005482 Py_XDECREF(exc);
5483 Py_XDECREF(val);
5484 Py_XDECREF(tb);
5485 }
5486 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005487 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005488 }
5489 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005490}
5491
Guido van Rossumac7be682001-01-17 15:42:30 +00005492static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005493format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5494 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005498 if (!obj)
5499 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005500
Serhiy Storchaka06515832016-11-20 09:13:07 +02005501 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005502 if (!obj_str)
5503 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005504
Victor Stinner438a12d2019-05-24 17:01:38 +02005505 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005506}
Guido van Rossum950361c1997-01-24 13:49:28 +00005507
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005508static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005509format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005510{
5511 PyObject *name;
5512 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005513 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005514 return;
5515 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5516 name = PyTuple_GET_ITEM(co->co_cellvars,
5517 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005518 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005519 PyExc_UnboundLocalError,
5520 UNBOUNDLOCAL_ERROR_MSG,
5521 name);
5522 } else {
5523 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5524 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005525 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005526 UNBOUNDFREE_ERROR_MSG, name);
5527 }
5528}
5529
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005530static void
Mark Shannonfee55262019-11-21 09:11:43 +00005531format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005532{
5533 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5534 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005535 _PyErr_Format(tstate, PyExc_TypeError,
5536 "'async with' received an object from __aenter__ "
5537 "that does not implement __await__: %.100s",
5538 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005539 }
Mark Shannonfee55262019-11-21 09:11:43 +00005540 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005541 _PyErr_Format(tstate, PyExc_TypeError,
5542 "'async with' received an object from __aexit__ "
5543 "that does not implement __await__: %.100s",
5544 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005545 }
5546 }
5547}
5548
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005549static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005550unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005551 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005552{
5553 PyObject *res;
5554 if (Py_REFCNT(v) == 2) {
5555 /* In the common case, there are 2 references to the value
5556 * stored in 'variable' when the += is performed: one on the
5557 * value stack (in 'v') and one still stored in the
5558 * 'variable'. We try to delete the variable now to reduce
5559 * the refcnt to 1.
5560 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005561 int opcode, oparg;
5562 NEXTOPARG();
5563 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005564 case STORE_FAST:
5565 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005566 PyObject **fastlocals = f->f_localsplus;
5567 if (GETLOCAL(oparg) == v)
5568 SETLOCAL(oparg, NULL);
5569 break;
5570 }
5571 case STORE_DEREF:
5572 {
5573 PyObject **freevars = (f->f_localsplus +
5574 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005575 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005576 if (PyCell_GET(c) == v) {
5577 PyCell_SET(c, NULL);
5578 Py_DECREF(v);
5579 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005580 break;
5581 }
5582 case STORE_NAME:
5583 {
5584 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005585 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005586 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005587 if (locals && PyDict_CheckExact(locals)) {
5588 PyObject *w = PyDict_GetItemWithError(locals, name);
5589 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005590 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005591 {
5592 Py_DECREF(v);
5593 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005594 }
5595 }
5596 break;
5597 }
5598 }
5599 }
5600 res = v;
5601 PyUnicode_Append(&res, w);
5602 return res;
5603}
5604
Guido van Rossum950361c1997-01-24 13:49:28 +00005605#ifdef DYNAMIC_EXECUTION_PROFILE
5606
Skip Montanarof118cb12001-10-15 20:51:38 +00005607static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005608getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005610 int i;
5611 PyObject *l = PyList_New(256);
5612 if (l == NULL) return NULL;
5613 for (i = 0; i < 256; i++) {
5614 PyObject *x = PyLong_FromLong(a[i]);
5615 if (x == NULL) {
5616 Py_DECREF(l);
5617 return NULL;
5618 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005619 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005620 }
5621 for (i = 0; i < 256; i++)
5622 a[i] = 0;
5623 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005624}
5625
5626PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005627_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005628{
5629#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005630 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005631#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 int i;
5633 PyObject *l = PyList_New(257);
5634 if (l == NULL) return NULL;
5635 for (i = 0; i < 257; i++) {
5636 PyObject *x = getarray(dxpairs[i]);
5637 if (x == NULL) {
5638 Py_DECREF(l);
5639 return NULL;
5640 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005641 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005642 }
5643 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005644#endif
5645}
5646
5647#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005648
5649Py_ssize_t
5650_PyEval_RequestCodeExtraIndex(freefunc free)
5651{
Victor Stinner81a7be32020-04-14 15:14:01 +02005652 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005653 Py_ssize_t new_index;
5654
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005655 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005656 return -1;
5657 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005658 new_index = interp->co_extra_user_count++;
5659 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005660 return new_index;
5661}
Łukasz Langaa785c872016-09-09 17:37:37 -07005662
5663static void
5664dtrace_function_entry(PyFrameObject *f)
5665{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005666 const char *filename;
5667 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005668 int lineno;
5669
Victor Stinner6d86a232020-04-29 00:56:58 +02005670 PyCodeObject *code = f->f_code;
5671 filename = PyUnicode_AsUTF8(code->co_filename);
5672 funcname = PyUnicode_AsUTF8(code->co_name);
5673 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005674
Andy Lestere6be9b52020-02-11 20:28:35 -06005675 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005676}
5677
5678static void
5679dtrace_function_return(PyFrameObject *f)
5680{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005681 const char *filename;
5682 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005683 int lineno;
5684
Victor Stinner6d86a232020-04-29 00:56:58 +02005685 PyCodeObject *code = f->f_code;
5686 filename = PyUnicode_AsUTF8(code->co_filename);
5687 funcname = PyUnicode_AsUTF8(code->co_name);
5688 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005689
Andy Lestere6be9b52020-02-11 20:28:35 -06005690 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005691}
5692
5693/* DTrace equivalent of maybe_call_line_trace. */
5694static void
5695maybe_dtrace_line(PyFrameObject *frame,
5696 int *instr_lb, int *instr_ub, int *instr_prev)
5697{
5698 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005699 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005700
5701 /* If the last instruction executed isn't in the current
5702 instruction window, reset the window.
5703 */
5704 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5705 PyAddrPair bounds;
5706 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5707 &bounds);
5708 *instr_lb = bounds.ap_lower;
5709 *instr_ub = bounds.ap_upper;
5710 }
5711 /* If the last instruction falls at the start of a line or if
5712 it represents a jump backwards, update the frame's line
5713 number and call the trace function. */
5714 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5715 frame->f_lineno = line;
5716 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5717 if (!co_filename)
5718 co_filename = "?";
5719 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5720 if (!co_name)
5721 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005722 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005723 }
5724 *instr_prev = frame->f_lasti;
5725}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005726
5727
5728/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5729 for the limited API. */
5730
5731#undef Py_EnterRecursiveCall
5732
5733int Py_EnterRecursiveCall(const char *where)
5734{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005735 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005736}
5737
5738#undef Py_LeaveRecursiveCall
5739
5740void Py_LeaveRecursiveCall(void)
5741{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005742 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005743}