blob: 0386929a5b2b376ce1c08c4a9d91d0bdbcb5fbdf [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 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 stack_pointer = f->f_stacktop;
1353 assert(stack_pointer != NULL);
1354 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001355 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001356
Inada Naoki91234a12019-06-03 21:30:58 +09001357 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1358 co->co_opcache_flag++;
1359 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1360 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001361 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001362 }
1363#if OPCACHE_STATS
1364 opcache_code_objects_extra_mem +=
1365 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1366 sizeof(_PyOpcache) * co->co_opcache_size;
1367 opcache_code_objects++;
1368#endif
1369 }
1370 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001371
Tim Peters5ca576e2001-06-18 22:08:13 +00001372#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001373 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001374#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 if (throwflag) /* support for generator.throw() */
1377 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001378
Victor Stinnerace47d72013-07-18 01:41:08 +02001379#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001380 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001381 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001382 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001383 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001384#endif
1385
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001386main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1389 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001390 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* Do periodic things. Doing this every time through
1393 the loop would add too much overhead, so we do it
1394 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001395 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 event needs attention (e.g. a signal handler or
1397 async I/O handler); see Py_AddPendingCall() and
1398 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Eric Snow7bda9de2019-03-08 17:25:54 -07001400 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001401 opcode = _Py_OPCODE(*next_instr);
1402 if (opcode == SETUP_FINALLY ||
1403 opcode == SETUP_WITH ||
1404 opcode == BEFORE_ASYNC_WITH ||
1405 opcode == YIELD_FROM) {
1406 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001407 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001408 - If we're about to enter the 'with:'. It will prevent
1409 emitting a resource warning in the common idiom
1410 'with open(path) as file:'.
1411 - If we're about to enter the 'async with:'.
1412 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001413 *very* useful, but might help in some cases and it's
1414 traditional)
1415 - If we're resuming a chain of nested 'yield from' or
1416 'await' calls, then each frame is parked with YIELD_FROM
1417 as its next opcode. If the user hit control-C we want to
1418 wait until we've reached the innermost frame before
1419 running the signal handler and raising KeyboardInterrupt
1420 (see bpo-30039).
1421 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 goto fast_next_opcode;
1423 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001424
Victor Stinnerda2914d2020-03-20 09:29:08 +01001425 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 }
1428 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 fast_next_opcode:
1431 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Łukasz Langaa785c872016-09-09 17:37:37 -07001433 if (PyDTrace_LINE_ENABLED())
1434 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001437
Victor Stinnerdab84232020-03-17 18:56:44 +01001438 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001439 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001440 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* see maybe_call_line_trace
1442 for expository comments */
1443 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 err = maybe_call_line_trace(tstate->c_tracefunc,
1446 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001447 tstate, f,
1448 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 /* Reload possibly changed frame fields */
1450 JUMPTO(f->f_lasti);
1451 if (f->f_stacktop != NULL) {
1452 stack_pointer = f->f_stacktop;
1453 f->f_stacktop = NULL;
1454 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001461
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001462 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001463 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001464#ifdef DYNAMIC_EXECUTION_PROFILE
1465#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 dxpairs[lastopcode][opcode]++;
1467 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001468#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001470#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001471
Guido van Rossum96a42c81992-01-12 02:29:51 +00001472#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if (lltrace) {
1476 if (HAS_ARG(opcode)) {
1477 printf("%d: %d, %d\n",
1478 f->f_lasti, opcode, oparg);
1479 }
1480 else {
1481 printf("%d: %d\n",
1482 f->f_lasti, opcode);
1483 }
1484 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001485#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001490 It is essential that any operation that fails must goto error
1491 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Benjamin Petersonddd19492018-09-16 22:38:02 -07001493 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 PyObject *value = GETLOCAL(oparg);
1499 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001500 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 UNBOUNDLOCAL_ERROR_MSG,
1502 PyTuple_GetItem(co->co_varnames, oparg));
1503 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 Py_INCREF(value);
1506 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 }
1509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(LOAD_CONST): {
1511 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 PyObject *value = GETITEM(consts, oparg);
1513 Py_INCREF(value);
1514 PUSH(value);
1515 FAST_DISPATCH();
1516 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(STORE_FAST): {
1519 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001520 PyObject *value = POP();
1521 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *value = POP();
1527 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Benjamin Petersonddd19492018-09-16 22:38:02 -07001531 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 PyObject *top = TOP();
1533 PyObject *second = SECOND();
1534 SET_TOP(second);
1535 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 PyObject *top = TOP();
1541 PyObject *second = SECOND();
1542 PyObject *third = THIRD();
1543 SET_TOP(second);
1544 SET_SECOND(third);
1545 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001550 PyObject *top = TOP();
1551 PyObject *second = SECOND();
1552 PyObject *third = THIRD();
1553 PyObject *fourth = FOURTH();
1554 SET_TOP(second);
1555 SET_SECOND(third);
1556 SET_THIRD(fourth);
1557 SET_FOURTH(top);
1558 FAST_DISPATCH();
1559 }
1560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *top = TOP();
1563 Py_INCREF(top);
1564 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001571 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001573 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 SET_TOP(top);
1575 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001576 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001578
Benjamin Petersonddd19492018-09-16 22:38:02 -07001579 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 PyObject *value = TOP();
1581 PyObject *res = PyNumber_Positive(value);
1582 Py_DECREF(value);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Benjamin Petersonddd19492018-09-16 22:38:02 -07001589 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 PyObject *value = TOP();
1591 PyObject *res = PyNumber_Negative(value);
1592 Py_DECREF(value);
1593 SET_TOP(res);
1594 if (res == NULL)
1595 goto error;
1596 DISPATCH();
1597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Benjamin Petersonddd19492018-09-16 22:38:02 -07001599 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001600 PyObject *value = TOP();
1601 int err = PyObject_IsTrue(value);
1602 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (err == 0) {
1604 Py_INCREF(Py_True);
1605 SET_TOP(Py_True);
1606 DISPATCH();
1607 }
1608 else if (err > 0) {
1609 Py_INCREF(Py_False);
1610 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 DISPATCH();
1612 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001613 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 goto error;
1615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Benjamin Petersonddd19492018-09-16 22:38:02 -07001617 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 PyObject *value = TOP();
1619 PyObject *res = PyNumber_Invert(value);
1620 Py_DECREF(value);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 PyObject *exp = POP();
1629 PyObject *base = TOP();
1630 PyObject *res = PyNumber_Power(base, exp, Py_None);
1631 Py_DECREF(base);
1632 Py_DECREF(exp);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Benjamin Petersonddd19492018-09-16 22:38:02 -07001639 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_Multiply(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001652 PyObject *right = POP();
1653 PyObject *left = TOP();
1654 PyObject *res = PyNumber_MatrixMultiply(left, right);
1655 Py_DECREF(left);
1656 Py_DECREF(right);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
1662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *divisor = POP();
1665 PyObject *dividend = TOP();
1666 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1667 Py_DECREF(dividend);
1668 Py_DECREF(divisor);
1669 SET_TOP(quotient);
1670 if (quotient == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *divisor = POP();
1677 PyObject *dividend = TOP();
1678 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1679 Py_DECREF(dividend);
1680 Py_DECREF(divisor);
1681 SET_TOP(quotient);
1682 if (quotient == NULL)
1683 goto error;
1684 DISPATCH();
1685 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001686
Benjamin Petersonddd19492018-09-16 22:38:02 -07001687 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 PyObject *divisor = POP();
1689 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001690 PyObject *res;
1691 if (PyUnicode_CheckExact(dividend) && (
1692 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1693 // fast path; string formatting, but not if the RHS is a str subclass
1694 // (see issue28598)
1695 res = PyUnicode_Format(dividend, divisor);
1696 } else {
1697 res = PyNumber_Remainder(dividend, divisor);
1698 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 Py_DECREF(divisor);
1700 Py_DECREF(dividend);
1701 SET_TOP(res);
1702 if (res == NULL)
1703 goto error;
1704 DISPATCH();
1705 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Benjamin Petersonddd19492018-09-16 22:38:02 -07001707 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 PyObject *right = POP();
1709 PyObject *left = TOP();
1710 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001711 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1712 CPython using bytecode, it is simply worthless.
1713 See http://bugs.python.org/issue21955 and
1714 http://bugs.python.org/issue10044 for the discussion. In short,
1715 no patch shown any impact on a realistic benchmark, only a minor
1716 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 if (PyUnicode_CheckExact(left) &&
1718 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001719 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001720 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001721 }
1722 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 sum = PyNumber_Add(left, right);
1724 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001725 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 Py_DECREF(right);
1727 SET_TOP(sum);
1728 if (sum == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
1732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *right = POP();
1735 PyObject *left = TOP();
1736 PyObject *diff = PyNumber_Subtract(left, right);
1737 Py_DECREF(right);
1738 Py_DECREF(left);
1739 SET_TOP(diff);
1740 if (diff == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
1744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 PyObject *sub = POP();
1747 PyObject *container = TOP();
1748 PyObject *res = PyObject_GetItem(container, sub);
1749 Py_DECREF(container);
1750 Py_DECREF(sub);
1751 SET_TOP(res);
1752 if (res == NULL)
1753 goto error;
1754 DISPATCH();
1755 }
1756
Benjamin Petersonddd19492018-09-16 22:38:02 -07001757 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001758 PyObject *right = POP();
1759 PyObject *left = TOP();
1760 PyObject *res = PyNumber_Lshift(left, right);
1761 Py_DECREF(left);
1762 Py_DECREF(right);
1763 SET_TOP(res);
1764 if (res == NULL)
1765 goto error;
1766 DISPATCH();
1767 }
1768
Benjamin Petersonddd19492018-09-16 22:38:02 -07001769 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 PyObject *right = POP();
1771 PyObject *left = TOP();
1772 PyObject *res = PyNumber_Rshift(left, right);
1773 Py_DECREF(left);
1774 Py_DECREF(right);
1775 SET_TOP(res);
1776 if (res == NULL)
1777 goto error;
1778 DISPATCH();
1779 }
1780
Benjamin Petersonddd19492018-09-16 22:38:02 -07001781 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 PyObject *right = POP();
1783 PyObject *left = TOP();
1784 PyObject *res = PyNumber_And(left, right);
1785 Py_DECREF(left);
1786 Py_DECREF(right);
1787 SET_TOP(res);
1788 if (res == NULL)
1789 goto error;
1790 DISPATCH();
1791 }
1792
Benjamin Petersonddd19492018-09-16 22:38:02 -07001793 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 PyObject *right = POP();
1795 PyObject *left = TOP();
1796 PyObject *res = PyNumber_Xor(left, right);
1797 Py_DECREF(left);
1798 Py_DECREF(right);
1799 SET_TOP(res);
1800 if (res == NULL)
1801 goto error;
1802 DISPATCH();
1803 }
1804
Benjamin Petersonddd19492018-09-16 22:38:02 -07001805 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *res = PyNumber_Or(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(res);
1812 if (res == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
1816
Benjamin Petersonddd19492018-09-16 22:38:02 -07001817 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *v = POP();
1819 PyObject *list = PEEK(oparg);
1820 int err;
1821 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001823 if (err != 0)
1824 goto error;
1825 PREDICT(JUMP_ABSOLUTE);
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Petersonddd19492018-09-16 22:38:02 -07001829 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001831 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 int err;
1833 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 if (err != 0)
1836 goto error;
1837 PREDICT(JUMP_ABSOLUTE);
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *exp = POP();
1843 PyObject *base = TOP();
1844 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1845 Py_DECREF(base);
1846 Py_DECREF(exp);
1847 SET_TOP(res);
1848 if (res == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *right = POP();
1855 PyObject *left = TOP();
1856 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1857 Py_DECREF(left);
1858 Py_DECREF(right);
1859 SET_TOP(res);
1860 if (res == NULL)
1861 goto error;
1862 DISPATCH();
1863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001866 PyObject *right = POP();
1867 PyObject *left = TOP();
1868 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1869 Py_DECREF(left);
1870 Py_DECREF(right);
1871 SET_TOP(res);
1872 if (res == NULL)
1873 goto error;
1874 DISPATCH();
1875 }
1876
Benjamin Petersonddd19492018-09-16 22:38:02 -07001877 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *divisor = POP();
1879 PyObject *dividend = TOP();
1880 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1881 Py_DECREF(dividend);
1882 Py_DECREF(divisor);
1883 SET_TOP(quotient);
1884 if (quotient == NULL)
1885 goto error;
1886 DISPATCH();
1887 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Benjamin Petersonddd19492018-09-16 22:38:02 -07001889 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *divisor = POP();
1891 PyObject *dividend = TOP();
1892 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1893 Py_DECREF(dividend);
1894 Py_DECREF(divisor);
1895 SET_TOP(quotient);
1896 if (quotient == NULL)
1897 goto error;
1898 DISPATCH();
1899 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001900
Benjamin Petersonddd19492018-09-16 22:38:02 -07001901 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 PyObject *right = POP();
1903 PyObject *left = TOP();
1904 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1905 Py_DECREF(left);
1906 Py_DECREF(right);
1907 SET_TOP(mod);
1908 if (mod == NULL)
1909 goto error;
1910 DISPATCH();
1911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 PyObject *right = POP();
1915 PyObject *left = TOP();
1916 PyObject *sum;
1917 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001918 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001919 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001920 }
1921 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 sum = PyNumber_InPlaceAdd(left, right);
1923 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001924 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 Py_DECREF(right);
1926 SET_TOP(sum);
1927 if (sum == NULL)
1928 goto error;
1929 DISPATCH();
1930 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Benjamin Petersonddd19492018-09-16 22:38:02 -07001932 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 PyObject *right = POP();
1934 PyObject *left = TOP();
1935 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1936 Py_DECREF(left);
1937 Py_DECREF(right);
1938 SET_TOP(diff);
1939 if (diff == NULL)
1940 goto error;
1941 DISPATCH();
1942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Benjamin Petersonddd19492018-09-16 22:38:02 -07001944 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 PyObject *right = POP();
1946 PyObject *left = TOP();
1947 PyObject *res = PyNumber_InPlaceLshift(left, right);
1948 Py_DECREF(left);
1949 Py_DECREF(right);
1950 SET_TOP(res);
1951 if (res == NULL)
1952 goto error;
1953 DISPATCH();
1954 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Benjamin Petersonddd19492018-09-16 22:38:02 -07001956 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 PyObject *right = POP();
1958 PyObject *left = TOP();
1959 PyObject *res = PyNumber_InPlaceRshift(left, right);
1960 Py_DECREF(left);
1961 Py_DECREF(right);
1962 SET_TOP(res);
1963 if (res == NULL)
1964 goto error;
1965 DISPATCH();
1966 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 PyObject *right = POP();
1970 PyObject *left = TOP();
1971 PyObject *res = PyNumber_InPlaceAnd(left, right);
1972 Py_DECREF(left);
1973 Py_DECREF(right);
1974 SET_TOP(res);
1975 if (res == NULL)
1976 goto error;
1977 DISPATCH();
1978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Benjamin Petersonddd19492018-09-16 22:38:02 -07001980 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 PyObject *right = POP();
1982 PyObject *left = TOP();
1983 PyObject *res = PyNumber_InPlaceXor(left, right);
1984 Py_DECREF(left);
1985 Py_DECREF(right);
1986 SET_TOP(res);
1987 if (res == NULL)
1988 goto error;
1989 DISPATCH();
1990 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Benjamin Petersonddd19492018-09-16 22:38:02 -07001992 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *right = POP();
1994 PyObject *left = TOP();
1995 PyObject *res = PyNumber_InPlaceOr(left, right);
1996 Py_DECREF(left);
1997 Py_DECREF(right);
1998 SET_TOP(res);
1999 if (res == NULL)
2000 goto error;
2001 DISPATCH();
2002 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002003
Benjamin Petersonddd19492018-09-16 22:38:02 -07002004 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 PyObject *sub = TOP();
2006 PyObject *container = SECOND();
2007 PyObject *v = THIRD();
2008 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002009 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002010 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 Py_DECREF(container);
2014 Py_DECREF(sub);
2015 if (err != 0)
2016 goto error;
2017 DISPATCH();
2018 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002019
Benjamin Petersonddd19492018-09-16 22:38:02 -07002020 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 PyObject *sub = TOP();
2022 PyObject *container = SECOND();
2023 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002024 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002025 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 err = PyObject_DelItem(container, sub);
2027 Py_DECREF(container);
2028 Py_DECREF(sub);
2029 if (err != 0)
2030 goto error;
2031 DISPATCH();
2032 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002033
Benjamin Petersonddd19492018-09-16 22:38:02 -07002034 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002035 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002037 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002038 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002040 _PyErr_SetString(tstate, PyExc_RuntimeError,
2041 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 Py_DECREF(value);
2043 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002045 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 Py_DECREF(value);
2047 if (res == NULL)
2048 goto error;
2049 Py_DECREF(res);
2050 DISPATCH();
2051 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002052
Benjamin Petersonddd19492018-09-16 22:38:02 -07002053 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 switch (oparg) {
2056 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002058 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002061 /* fall through */
2062 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002063 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002064 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 break;
2067 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002068 _PyErr_SetString(tstate, PyExc_SystemError,
2069 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 break;
2071 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 goto error;
2073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Benjamin Petersonddd19492018-09-16 22:38:02 -07002075 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002077 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002078 assert(EMPTY());
2079 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002081
Benjamin Petersonddd19492018-09-16 22:38:02 -07002082 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002083 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002084 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002085 PyObject *obj = TOP();
2086 PyTypeObject *type = Py_TYPE(obj);
2087
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002088 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002089 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002090 }
Yury Selivanov75445082015-05-11 22:57:16 -04002091
2092 if (getter != NULL) {
2093 iter = (*getter)(obj);
2094 Py_DECREF(obj);
2095 if (iter == NULL) {
2096 SET_TOP(NULL);
2097 goto error;
2098 }
2099 }
2100 else {
2101 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002102 _PyErr_Format(tstate, PyExc_TypeError,
2103 "'async for' requires an object with "
2104 "__aiter__ method, got %.100s",
2105 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002106 Py_DECREF(obj);
2107 goto error;
2108 }
2109
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002110 if (Py_TYPE(iter)->tp_as_async == NULL ||
2111 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002112
Yury Selivanov398ff912017-03-02 22:20:00 -05002113 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002114 _PyErr_Format(tstate, PyExc_TypeError,
2115 "'async for' received an object from __aiter__ "
2116 "that does not implement __anext__: %.100s",
2117 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002118 Py_DECREF(iter);
2119 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002120 }
2121
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002122 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002123 DISPATCH();
2124 }
2125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002127 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002128 PyObject *next_iter = NULL;
2129 PyObject *awaitable = NULL;
2130 PyObject *aiter = TOP();
2131 PyTypeObject *type = Py_TYPE(aiter);
2132
Yury Selivanoveb636452016-09-08 22:01:51 -07002133 if (PyAsyncGen_CheckExact(aiter)) {
2134 awaitable = type->tp_as_async->am_anext(aiter);
2135 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002136 goto error;
2137 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002138 } else {
2139 if (type->tp_as_async != NULL){
2140 getter = type->tp_as_async->am_anext;
2141 }
Yury Selivanov75445082015-05-11 22:57:16 -04002142
Yury Selivanoveb636452016-09-08 22:01:51 -07002143 if (getter != NULL) {
2144 next_iter = (*getter)(aiter);
2145 if (next_iter == NULL) {
2146 goto error;
2147 }
2148 }
2149 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002150 _PyErr_Format(tstate, PyExc_TypeError,
2151 "'async for' requires an iterator with "
2152 "__anext__ method, got %.100s",
2153 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002154 goto error;
2155 }
Yury Selivanov75445082015-05-11 22:57:16 -04002156
Yury Selivanoveb636452016-09-08 22:01:51 -07002157 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2158 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002159 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002160 PyExc_TypeError,
2161 "'async for' received an invalid object "
2162 "from __anext__: %.100s",
2163 Py_TYPE(next_iter)->tp_name);
2164
2165 Py_DECREF(next_iter);
2166 goto error;
2167 } else {
2168 Py_DECREF(next_iter);
2169 }
2170 }
Yury Selivanov75445082015-05-11 22:57:16 -04002171
2172 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002173 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002174 DISPATCH();
2175 }
2176
Benjamin Petersonddd19492018-09-16 22:38:02 -07002177 case TARGET(GET_AWAITABLE): {
2178 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002179 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002180 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002181
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002182 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002183 int opcode_at_minus_3 = 0;
2184 if ((next_instr - first_instr) > 2) {
2185 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2186 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002187 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002188 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002189 _Py_OPCODE(next_instr[-2]));
2190 }
2191
Yury Selivanov75445082015-05-11 22:57:16 -04002192 Py_DECREF(iterable);
2193
Yury Selivanovc724bae2016-03-02 11:30:46 -05002194 if (iter != NULL && PyCoro_CheckExact(iter)) {
2195 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2196 if (yf != NULL) {
2197 /* `iter` is a coroutine object that is being
2198 awaited, `yf` is a pointer to the current awaitable
2199 being awaited on. */
2200 Py_DECREF(yf);
2201 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002202 _PyErr_SetString(tstate, PyExc_RuntimeError,
2203 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002204 /* The code below jumps to `error` if `iter` is NULL. */
2205 }
2206 }
2207
Yury Selivanov75445082015-05-11 22:57:16 -04002208 SET_TOP(iter); /* Even if it's NULL */
2209
2210 if (iter == NULL) {
2211 goto error;
2212 }
2213
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002214 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002215 DISPATCH();
2216 }
2217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002220 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002222 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2223 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002224 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002225 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002227 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002228 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002229 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 Py_DECREF(v);
2232 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002233 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002234 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002235 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002236 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002237 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 if (err < 0)
2239 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002240 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 SET_TOP(val);
2242 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002243 }
Martin Panter95f53c12016-07-18 08:23:26 +00002244 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002245 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002246 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002247 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002248 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002249 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002254
2255 if (co->co_flags & CO_ASYNC_GENERATOR) {
2256 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2257 Py_DECREF(retval);
2258 if (w == NULL) {
2259 retval = NULL;
2260 goto error;
2261 }
2262 retval = w;
2263 }
2264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002266 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002268
Benjamin Petersonddd19492018-09-16 22:38:02 -07002269 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002270 PyObject *type, *value, *traceback;
2271 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 PyTryBlock *b = PyFrame_BlockPop(f);
2273 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002274 _PyErr_SetString(tstate, PyExc_SystemError,
2275 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002278 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2279 STACK_LEVEL() <= (b)->b_level + 4);
2280 exc_info = tstate->exc_info;
2281 type = exc_info->exc_type;
2282 value = exc_info->exc_value;
2283 traceback = exc_info->exc_traceback;
2284 exc_info->exc_type = POP();
2285 exc_info->exc_value = POP();
2286 exc_info->exc_traceback = POP();
2287 Py_XDECREF(type);
2288 Py_XDECREF(value);
2289 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002292
Benjamin Petersonddd19492018-09-16 22:38:02 -07002293 case TARGET(POP_BLOCK): {
2294 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002295 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Mark Shannonfee55262019-11-21 09:11:43 +00002299 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002300 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002301 PyObject *val = POP();
2302 PyObject *tb = POP();
2303 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002304 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002305 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Benjamin Petersonddd19492018-09-16 22:38:02 -07002308 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002309 PyObject *exc = POP();
2310 assert(PyExceptionClass_Check(exc));
2311 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2312 PyTryBlock *b = PyFrame_BlockPop(f);
2313 assert(b->b_type == EXCEPT_HANDLER);
2314 Py_DECREF(exc);
2315 UNWIND_EXCEPT_HANDLER(b);
2316 Py_DECREF(POP());
2317 JUMPBY(oparg);
2318 FAST_DISPATCH();
2319 }
2320 else {
2321 PyObject *val = POP();
2322 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002323 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002324 goto exception_unwind;
2325 }
2326 }
2327
Zackery Spytzce6a0702019-08-25 03:44:09 -06002328 case TARGET(LOAD_ASSERTION_ERROR): {
2329 PyObject *value = PyExc_AssertionError;
2330 Py_INCREF(value);
2331 PUSH(value);
2332 FAST_DISPATCH();
2333 }
2334
Benjamin Petersonddd19492018-09-16 22:38:02 -07002335 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002336 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002337
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002339 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002340 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 if (!_PyErr_Occurred(tstate)) {
2343 _PyErr_SetString(tstate, PyExc_NameError,
2344 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002345 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002349 }
2350 else {
2351 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2352 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002353 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2355 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002356 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2357 _PyErr_SetString(tstate, PyExc_NameError,
2358 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002363 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Benjamin Petersonddd19492018-09-16 22:38:02 -07002366 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 PyObject *name = GETITEM(names, oparg);
2368 PyObject *v = POP();
2369 PyObject *ns = f->f_locals;
2370 int err;
2371 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002372 _PyErr_Format(tstate, PyExc_SystemError,
2373 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 if (PyDict_CheckExact(ns))
2378 err = PyDict_SetItem(ns, name, v);
2379 else
2380 err = PyObject_SetItem(ns, name, v);
2381 Py_DECREF(v);
2382 if (err != 0)
2383 goto error;
2384 DISPATCH();
2385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Benjamin Petersonddd19492018-09-16 22:38:02 -07002387 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 PyObject *name = GETITEM(names, oparg);
2389 PyObject *ns = f->f_locals;
2390 int err;
2391 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002392 _PyErr_Format(tstate, PyExc_SystemError,
2393 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 err = PyObject_DelItem(ns, name);
2397 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002398 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 NAME_ERROR_MSG,
2400 name);
2401 goto error;
2402 }
2403 DISPATCH();
2404 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(UNPACK_SEQUENCE): {
2407 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 PyObject *seq = POP(), *item, **items;
2409 if (PyTuple_CheckExact(seq) &&
2410 PyTuple_GET_SIZE(seq) == oparg) {
2411 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 item = items[oparg];
2414 Py_INCREF(item);
2415 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 } else if (PyList_CheckExact(seq) &&
2418 PyList_GET_SIZE(seq) == oparg) {
2419 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 item = items[oparg];
2422 Py_INCREF(item);
2423 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002425 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002427 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 } else {
2429 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 Py_DECREF(seq);
2431 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002434 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2439 PyObject *seq = POP();
2440
Victor Stinner438a12d2019-05-24 17:01:38 +02002441 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 stack_pointer + totalargs)) {
2443 stack_pointer += totalargs;
2444 } else {
2445 Py_DECREF(seq);
2446 goto error;
2447 }
2448 Py_DECREF(seq);
2449 DISPATCH();
2450 }
2451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *name = GETITEM(names, oparg);
2454 PyObject *owner = TOP();
2455 PyObject *v = SECOND();
2456 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002457 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 Py_DECREF(owner);
2461 if (err != 0)
2462 goto error;
2463 DISPATCH();
2464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Benjamin Petersonddd19492018-09-16 22:38:02 -07002466 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 PyObject *name = GETITEM(names, oparg);
2468 PyObject *owner = POP();
2469 int err;
2470 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2471 Py_DECREF(owner);
2472 if (err != 0)
2473 goto error;
2474 DISPATCH();
2475 }
2476
Benjamin Petersonddd19492018-09-16 22:38:02 -07002477 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 PyObject *name = GETITEM(names, oparg);
2479 PyObject *v = POP();
2480 int err;
2481 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 if (err != 0)
2484 goto error;
2485 DISPATCH();
2486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002487
Benjamin Petersonddd19492018-09-16 22:38:02 -07002488 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 PyObject *name = GETITEM(names, oparg);
2490 int err;
2491 err = PyDict_DelItem(f->f_globals, name);
2492 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2494 format_exc_check_arg(tstate, PyExc_NameError,
2495 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002496 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002498 }
2499 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002501
Benjamin Petersonddd19492018-09-16 22:38:02 -07002502 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 PyObject *name = GETITEM(names, oparg);
2504 PyObject *locals = f->f_locals;
2505 PyObject *v;
2506 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002507 _PyErr_Format(tstate, PyExc_SystemError,
2508 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002512 v = PyDict_GetItemWithError(locals, name);
2513 if (v != NULL) {
2514 Py_INCREF(v);
2515 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002516 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002517 goto error;
2518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 }
2520 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002522 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002523 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002524 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002525 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 }
2527 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002529 v = PyDict_GetItemWithError(f->f_globals, name);
2530 if (v != NULL) {
2531 Py_INCREF(v);
2532 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002533 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002534 goto error;
2535 }
2536 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002537 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002538 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002541 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002544 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002546 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002548 }
2549 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 v = PyObject_GetItem(f->f_builtins, name);
2551 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002552 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002553 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002554 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002558 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002559 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002565
Benjamin Petersonddd19492018-09-16 22:38:02 -07002566 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002567 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002569 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002570 && PyDict_CheckExact(f->f_builtins))
2571 {
Inada Naoki91234a12019-06-03 21:30:58 +09002572 OPCACHE_CHECK();
2573 if (co_opcache != NULL && co_opcache->optimized > 0) {
2574 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2575
2576 if (lg->globals_ver ==
2577 ((PyDictObject *)f->f_globals)->ma_version_tag
2578 && lg->builtins_ver ==
2579 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2580 {
2581 PyObject *ptr = lg->ptr;
2582 OPCACHE_STAT_GLOBAL_HIT();
2583 assert(ptr != NULL);
2584 Py_INCREF(ptr);
2585 PUSH(ptr);
2586 DISPATCH();
2587 }
2588 }
2589
2590 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002592 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 name);
2594 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002595 if (!_PyErr_OCCURRED()) {
2596 /* _PyDict_LoadGlobal() returns NULL without raising
2597 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002598 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002599 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002600 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 }
Inada Naoki91234a12019-06-03 21:30:58 +09002603
2604 if (co_opcache != NULL) {
2605 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2606
2607 if (co_opcache->optimized == 0) {
2608 /* Wasn't optimized before. */
2609 OPCACHE_STAT_GLOBAL_OPT();
2610 } else {
2611 OPCACHE_STAT_GLOBAL_MISS();
2612 }
2613
2614 co_opcache->optimized = 1;
2615 lg->globals_ver =
2616 ((PyDictObject *)f->f_globals)->ma_version_tag;
2617 lg->builtins_ver =
2618 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2619 lg->ptr = v; /* borrowed */
2620 }
2621
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002624 else {
2625 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002626
2627 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002628 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002629 v = PyObject_GetItem(f->f_globals, name);
2630 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002631 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002632 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002633 }
2634 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002635
Victor Stinnerb4efc962015-11-20 09:24:02 +01002636 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 v = PyObject_GetItem(f->f_builtins, name);
2638 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002639 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002640 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002641 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002642 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002643 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002645 }
2646 }
2647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002651
Benjamin Petersonddd19492018-09-16 22:38:02 -07002652 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 PyObject *v = GETLOCAL(oparg);
2654 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 SETLOCAL(oparg, NULL);
2656 DISPATCH();
2657 }
2658 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002659 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 UNBOUNDLOCAL_ERROR_MSG,
2661 PyTuple_GetItem(co->co_varnames, oparg)
2662 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 goto error;
2664 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002665
Benjamin Petersonddd19492018-09-16 22:38:02 -07002666 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002668 PyObject *oldobj = PyCell_GET(cell);
2669 if (oldobj != NULL) {
2670 PyCell_SET(cell, NULL);
2671 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002672 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002673 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002674 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 goto error;
2676 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002677
Benjamin Petersonddd19492018-09-16 22:38:02 -07002678 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 PyObject *cell = freevars[oparg];
2680 Py_INCREF(cell);
2681 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002684
Benjamin Petersonddd19492018-09-16 22:38:02 -07002685 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002686 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002687 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002688 assert(locals);
2689 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2690 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2691 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2692 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2693 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002694 value = PyDict_GetItemWithError(locals, name);
2695 if (value != NULL) {
2696 Py_INCREF(value);
2697 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002698 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002699 goto error;
2700 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002701 }
2702 else {
2703 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002704 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002705 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002706 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002707 }
2708 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002709 }
2710 }
2711 if (!value) {
2712 PyObject *cell = freevars[oparg];
2713 value = PyCell_GET(cell);
2714 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002715 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002716 goto error;
2717 }
2718 Py_INCREF(value);
2719 }
2720 PUSH(value);
2721 DISPATCH();
2722 }
2723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 PyObject *cell = freevars[oparg];
2726 PyObject *value = PyCell_GET(cell);
2727 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002728 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 Py_INCREF(value);
2732 PUSH(value);
2733 DISPATCH();
2734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002735
Benjamin Petersonddd19492018-09-16 22:38:02 -07002736 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 PyObject *v = POP();
2738 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002739 PyObject *oldobj = PyCell_GET(cell);
2740 PyCell_SET(cell, v);
2741 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 DISPATCH();
2743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002744
Benjamin Petersonddd19492018-09-16 22:38:02 -07002745 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002746 PyObject *str;
2747 PyObject *empty = PyUnicode_New(0, 0);
2748 if (empty == NULL) {
2749 goto error;
2750 }
2751 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2752 Py_DECREF(empty);
2753 if (str == NULL)
2754 goto error;
2755 while (--oparg >= 0) {
2756 PyObject *item = POP();
2757 Py_DECREF(item);
2758 }
2759 PUSH(str);
2760 DISPATCH();
2761 }
2762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 PyObject *tup = PyTuple_New(oparg);
2765 if (tup == NULL)
2766 goto error;
2767 while (--oparg >= 0) {
2768 PyObject *item = POP();
2769 PyTuple_SET_ITEM(tup, oparg, item);
2770 }
2771 PUSH(tup);
2772 DISPATCH();
2773 }
2774
Benjamin Petersonddd19492018-09-16 22:38:02 -07002775 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 PyObject *list = PyList_New(oparg);
2777 if (list == NULL)
2778 goto error;
2779 while (--oparg >= 0) {
2780 PyObject *item = POP();
2781 PyList_SET_ITEM(list, oparg, item);
2782 }
2783 PUSH(list);
2784 DISPATCH();
2785 }
2786
Mark Shannon13bc1392020-01-23 09:25:17 +00002787 case TARGET(LIST_TO_TUPLE): {
2788 PyObject *list = POP();
2789 PyObject *tuple = PyList_AsTuple(list);
2790 Py_DECREF(list);
2791 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002792 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002793 }
2794 PUSH(tuple);
2795 DISPATCH();
2796 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002797
Mark Shannon13bc1392020-01-23 09:25:17 +00002798 case TARGET(LIST_EXTEND): {
2799 PyObject *iterable = POP();
2800 PyObject *list = PEEK(oparg);
2801 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2802 if (none_val == NULL) {
2803 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002804 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002805 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002806 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002807 _PyErr_Format(tstate, PyExc_TypeError,
2808 "Value after * must be an iterable, not %.200s",
2809 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002811 Py_DECREF(iterable);
2812 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002814 Py_DECREF(none_val);
2815 Py_DECREF(iterable);
2816 DISPATCH();
2817 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002818
Mark Shannon13bc1392020-01-23 09:25:17 +00002819 case TARGET(SET_UPDATE): {
2820 PyObject *iterable = POP();
2821 PyObject *set = PEEK(oparg);
2822 int err = _PySet_Update(set, iterable);
2823 Py_DECREF(iterable);
2824 if (err < 0) {
2825 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002826 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002827 DISPATCH();
2828 }
2829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *set = PySet_New(NULL);
2832 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002833 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 if (set == NULL)
2835 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002836 for (i = oparg; i > 0; i--) {
2837 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 if (err == 0)
2839 err = PySet_Add(set, item);
2840 Py_DECREF(item);
2841 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002842 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 if (err != 0) {
2844 Py_DECREF(set);
2845 goto error;
2846 }
2847 PUSH(set);
2848 DISPATCH();
2849 }
2850
Benjamin Petersonddd19492018-09-16 22:38:02 -07002851 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002852 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2854 if (map == NULL)
2855 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002856 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002857 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002858 PyObject *key = PEEK(2*i);
2859 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002860 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002861 if (err != 0) {
2862 Py_DECREF(map);
2863 goto error;
2864 }
2865 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002866
2867 while (oparg--) {
2868 Py_DECREF(POP());
2869 Py_DECREF(POP());
2870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 PUSH(map);
2872 DISPATCH();
2873 }
2874
Benjamin Petersonddd19492018-09-16 22:38:02 -07002875 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002876 _Py_IDENTIFIER(__annotations__);
2877 int err;
2878 PyObject *ann_dict;
2879 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002880 _PyErr_Format(tstate, PyExc_SystemError,
2881 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002882 goto error;
2883 }
2884 /* check if __annotations__ in locals()... */
2885 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002886 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002887 &PyId___annotations__);
2888 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002889 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002890 goto error;
2891 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002892 /* ...if not, create a new one */
2893 ann_dict = PyDict_New();
2894 if (ann_dict == NULL) {
2895 goto error;
2896 }
2897 err = _PyDict_SetItemId(f->f_locals,
2898 &PyId___annotations__, ann_dict);
2899 Py_DECREF(ann_dict);
2900 if (err != 0) {
2901 goto error;
2902 }
2903 }
2904 }
2905 else {
2906 /* do the same if locals() is not a dict */
2907 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2908 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002909 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002910 }
2911 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2912 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002913 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002914 goto error;
2915 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002916 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002917 ann_dict = PyDict_New();
2918 if (ann_dict == NULL) {
2919 goto error;
2920 }
2921 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2922 Py_DECREF(ann_dict);
2923 if (err != 0) {
2924 goto error;
2925 }
2926 }
2927 else {
2928 Py_DECREF(ann_dict);
2929 }
2930 }
2931 DISPATCH();
2932 }
2933
Benjamin Petersonddd19492018-09-16 22:38:02 -07002934 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002935 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002936 PyObject *map;
2937 PyObject *keys = TOP();
2938 if (!PyTuple_CheckExact(keys) ||
2939 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002940 _PyErr_SetString(tstate, PyExc_SystemError,
2941 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002942 goto error;
2943 }
2944 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2945 if (map == NULL) {
2946 goto error;
2947 }
2948 for (i = oparg; i > 0; i--) {
2949 int err;
2950 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2951 PyObject *value = PEEK(i + 1);
2952 err = PyDict_SetItem(map, key, value);
2953 if (err != 0) {
2954 Py_DECREF(map);
2955 goto error;
2956 }
2957 }
2958
2959 Py_DECREF(POP());
2960 while (oparg--) {
2961 Py_DECREF(POP());
2962 }
2963 PUSH(map);
2964 DISPATCH();
2965 }
2966
Mark Shannon8a4cd702020-01-27 09:57:45 +00002967 case TARGET(DICT_UPDATE): {
2968 PyObject *update = POP();
2969 PyObject *dict = PEEK(oparg);
2970 if (PyDict_Update(dict, update) < 0) {
2971 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2972 _PyErr_Format(tstate, PyExc_TypeError,
2973 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002974 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002975 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002976 Py_DECREF(update);
2977 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002978 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002979 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002980 DISPATCH();
2981 }
2982
Mark Shannon8a4cd702020-01-27 09:57:45 +00002983 case TARGET(DICT_MERGE): {
2984 PyObject *update = POP();
2985 PyObject *dict = PEEK(oparg);
2986
2987 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2988 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2989 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002990 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002991 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002992 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002993 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002994 DISPATCH();
2995 }
2996
Benjamin Petersonddd19492018-09-16 22:38:02 -07002997 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002998 PyObject *value = TOP();
2999 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 PyObject *map;
3001 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003002 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003003 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003005 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 Py_DECREF(value);
3007 Py_DECREF(key);
3008 if (err != 0)
3009 goto error;
3010 PREDICT(JUMP_ABSOLUTE);
3011 DISPATCH();
3012 }
3013
Benjamin Petersonddd19492018-09-16 22:38:02 -07003014 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 PyObject *name = GETITEM(names, oparg);
3016 PyObject *owner = TOP();
3017 PyObject *res = PyObject_GetAttr(owner, name);
3018 Py_DECREF(owner);
3019 SET_TOP(res);
3020 if (res == NULL)
3021 goto error;
3022 DISPATCH();
3023 }
3024
Benjamin Petersonddd19492018-09-16 22:38:02 -07003025 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003026 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 PyObject *right = POP();
3028 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003029 PyObject *res = PyObject_RichCompare(left, right, oparg);
3030 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 Py_DECREF(left);
3032 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 if (res == NULL)
3034 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 PREDICT(POP_JUMP_IF_FALSE);
3036 PREDICT(POP_JUMP_IF_TRUE);
3037 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003039
Mark Shannon9af0e472020-01-14 10:12:45 +00003040 case TARGET(IS_OP): {
3041 PyObject *right = POP();
3042 PyObject *left = TOP();
3043 int res = (left == right)^oparg;
3044 PyObject *b = res ? Py_True : Py_False;
3045 Py_INCREF(b);
3046 SET_TOP(b);
3047 Py_DECREF(left);
3048 Py_DECREF(right);
3049 PREDICT(POP_JUMP_IF_FALSE);
3050 PREDICT(POP_JUMP_IF_TRUE);
3051 FAST_DISPATCH();
3052 }
3053
3054 case TARGET(CONTAINS_OP): {
3055 PyObject *right = POP();
3056 PyObject *left = POP();
3057 int res = PySequence_Contains(right, left);
3058 Py_DECREF(left);
3059 Py_DECREF(right);
3060 if (res < 0) {
3061 goto error;
3062 }
3063 PyObject *b = (res^oparg) ? Py_True : Py_False;
3064 Py_INCREF(b);
3065 PUSH(b);
3066 PREDICT(POP_JUMP_IF_FALSE);
3067 PREDICT(POP_JUMP_IF_TRUE);
3068 FAST_DISPATCH();
3069 }
3070
3071#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3072 "BaseException is not allowed"
3073
3074 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3075 PyObject *right = POP();
3076 PyObject *left = POP();
3077 if (PyTuple_Check(right)) {
3078 Py_ssize_t i, length;
3079 length = PyTuple_GET_SIZE(right);
3080 for (i = 0; i < length; i++) {
3081 PyObject *exc = PyTuple_GET_ITEM(right, i);
3082 if (!PyExceptionClass_Check(exc)) {
3083 _PyErr_SetString(tstate, PyExc_TypeError,
3084 CANNOT_CATCH_MSG);
3085 Py_DECREF(left);
3086 Py_DECREF(right);
3087 goto error;
3088 }
3089 }
3090 }
3091 else {
3092 if (!PyExceptionClass_Check(right)) {
3093 _PyErr_SetString(tstate, PyExc_TypeError,
3094 CANNOT_CATCH_MSG);
3095 Py_DECREF(left);
3096 Py_DECREF(right);
3097 goto error;
3098 }
3099 }
3100 int res = PyErr_GivenExceptionMatches(left, right);
3101 Py_DECREF(left);
3102 Py_DECREF(right);
3103 if (res > 0) {
3104 /* Exception matches -- Do nothing */;
3105 }
3106 else if (res == 0) {
3107 JUMPTO(oparg);
3108 }
3109 else {
3110 goto error;
3111 }
3112 DISPATCH();
3113 }
3114
Benjamin Petersonddd19492018-09-16 22:38:02 -07003115 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003117 PyObject *fromlist = POP();
3118 PyObject *level = TOP();
3119 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003120 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003121 Py_DECREF(level);
3122 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 SET_TOP(res);
3124 if (res == NULL)
3125 goto error;
3126 DISPATCH();
3127 }
3128
Benjamin Petersonddd19492018-09-16 22:38:02 -07003129 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 PyObject *from = POP(), *locals;
3131 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003132 if (PyFrame_FastToLocalsWithError(f) < 0) {
3133 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003134 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003135 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003136
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 locals = f->f_locals;
3138 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003139 _PyErr_SetString(tstate, PyExc_SystemError,
3140 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003141 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003144 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 Py_DECREF(from);
3147 if (err != 0)
3148 goto error;
3149 DISPATCH();
3150 }
Guido van Rossum25831651993-05-19 14:50:45 +00003151
Benjamin Petersonddd19492018-09-16 22:38:02 -07003152 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 PyObject *name = GETITEM(names, oparg);
3154 PyObject *from = TOP();
3155 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003156 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PUSH(res);
3158 if (res == NULL)
3159 goto error;
3160 DISPATCH();
3161 }
Thomas Wouters52152252000-08-17 22:55:00 +00003162
Benjamin Petersonddd19492018-09-16 22:38:02 -07003163 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 JUMPBY(oparg);
3165 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003167
Benjamin Petersonddd19492018-09-16 22:38:02 -07003168 case TARGET(POP_JUMP_IF_FALSE): {
3169 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 PyObject *cond = POP();
3171 int err;
3172 if (cond == Py_True) {
3173 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 FAST_DISPATCH();
3175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 if (cond == Py_False) {
3177 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 JUMPTO(oparg);
3179 FAST_DISPATCH();
3180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 err = PyObject_IsTrue(cond);
3182 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003184 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 else if (err == 0)
3186 JUMPTO(oparg);
3187 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003191
Benjamin Petersonddd19492018-09-16 22:38:02 -07003192 case TARGET(POP_JUMP_IF_TRUE): {
3193 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 PyObject *cond = POP();
3195 int err;
3196 if (cond == Py_False) {
3197 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 FAST_DISPATCH();
3199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 if (cond == Py_True) {
3201 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 JUMPTO(oparg);
3203 FAST_DISPATCH();
3204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 err = PyObject_IsTrue(cond);
3206 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 JUMPTO(oparg);
3209 }
3210 else if (err == 0)
3211 ;
3212 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003216
Benjamin Petersonddd19492018-09-16 22:38:02 -07003217 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 PyObject *cond = TOP();
3219 int err;
3220 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003221 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 FAST_DISPATCH();
3224 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 JUMPTO(oparg);
3227 FAST_DISPATCH();
3228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003231 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 }
3234 else if (err == 0)
3235 JUMPTO(oparg);
3236 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003240
Benjamin Petersonddd19492018-09-16 22:38:02 -07003241 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 PyObject *cond = TOP();
3243 int err;
3244 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003245 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 FAST_DISPATCH();
3248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 JUMPTO(oparg);
3251 FAST_DISPATCH();
3252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 JUMPTO(oparg);
3256 }
3257 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003258 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 }
3261 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003265
Benjamin Petersonddd19492018-09-16 22:38:02 -07003266 case TARGET(JUMP_ABSOLUTE): {
3267 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003269#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 /* Enabling this path speeds-up all while and for-loops by bypassing
3271 the per-loop checks for signals. By default, this should be turned-off
3272 because it prevents detection of a control-break in tight loops like
3273 "while 1: pass". Compile with this option turned-on when you need
3274 the speed-up and do not need break checking inside tight loops (ones
3275 that contain only instructions ending with FAST_DISPATCH).
3276 */
3277 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003278#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003280#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003282
Benjamin Petersonddd19492018-09-16 22:38:02 -07003283 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003286 PyObject *iter = PyObject_GetIter(iterable);
3287 Py_DECREF(iterable);
3288 SET_TOP(iter);
3289 if (iter == NULL)
3290 goto error;
3291 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003292 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003293 DISPATCH();
3294 }
3295
Benjamin Petersonddd19492018-09-16 22:38:02 -07003296 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003297 /* before: [obj]; after [getiter(obj)] */
3298 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003299 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003300 if (PyCoro_CheckExact(iterable)) {
3301 /* `iterable` is a coroutine */
3302 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3303 /* and it is used in a 'yield from' expression of a
3304 regular generator. */
3305 Py_DECREF(iterable);
3306 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003307 _PyErr_SetString(tstate, PyExc_TypeError,
3308 "cannot 'yield from' a coroutine object "
3309 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003310 goto error;
3311 }
3312 }
3313 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003314 /* `iterable` is not a generator. */
3315 iter = PyObject_GetIter(iterable);
3316 Py_DECREF(iterable);
3317 SET_TOP(iter);
3318 if (iter == NULL)
3319 goto error;
3320 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003321 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 DISPATCH();
3323 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003324
Benjamin Petersonddd19492018-09-16 22:38:02 -07003325 case TARGET(FOR_ITER): {
3326 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003329 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 if (next != NULL) {
3331 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 PREDICT(STORE_FAST);
3333 PREDICT(UNPACK_SEQUENCE);
3334 DISPATCH();
3335 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003336 if (_PyErr_Occurred(tstate)) {
3337 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003339 }
3340 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003341 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003342 }
3343 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 }
3345 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003346 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003349 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003352
Benjamin Petersonddd19492018-09-16 22:38:02 -07003353 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003354 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 STACK_LEVEL());
3356 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003358
Benjamin Petersonddd19492018-09-16 22:38:02 -07003359 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003360 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003361 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003362 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003363 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003364 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003365 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003366 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003367 }
3368 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3369 if (exit == NULL) {
3370 Py_DECREF(enter);
3371 goto error;
3372 }
Yury Selivanov75445082015-05-11 22:57:16 -04003373 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003374 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003375 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003376 Py_DECREF(enter);
3377 if (res == NULL)
3378 goto error;
3379 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003380 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003381 DISPATCH();
3382 }
3383
Benjamin Petersonddd19492018-09-16 22:38:02 -07003384 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003385 PyObject *res = POP();
3386 /* Setup the finally block before pushing the result
3387 of __aenter__ on the stack. */
3388 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3389 STACK_LEVEL());
3390 PUSH(res);
3391 DISPATCH();
3392 }
3393
Benjamin Petersonddd19492018-09-16 22:38:02 -07003394 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003395 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003396 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003398 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003400 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003401 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003402 }
3403 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003404 if (exit == NULL) {
3405 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003407 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003410 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 Py_DECREF(enter);
3412 if (res == NULL)
3413 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 /* Setup the finally block before pushing the result
3415 of __enter__ on the stack. */
3416 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3417 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003418
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 DISPATCH();
3421 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003422
Mark Shannonfee55262019-11-21 09:11:43 +00003423 case TARGET(WITH_EXCEPT_START): {
3424 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003426 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3427 - SEVENTH: the context.__exit__ bound method
3428 We call SEVENTH(TOP, SECOND, THIRD).
3429 Then we push again the TOP exception and the __exit__
3430 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003433 PyObject *exc, *val, *tb, *res;
3434
Victor Stinner842cfff2016-12-01 14:45:31 +01003435 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003436 val = SECOND();
3437 tb = THIRD();
3438 assert(exc != Py_None);
3439 assert(!PyLong_Check(exc));
3440 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003441 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003442 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003443 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003444 if (res == NULL)
3445 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003446
Yury Selivanov75445082015-05-11 22:57:16 -04003447 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003450
Benjamin Petersonddd19492018-09-16 22:38:02 -07003451 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003452 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003453 PyObject *name = GETITEM(names, oparg);
3454 PyObject *obj = TOP();
3455 PyObject *meth = NULL;
3456
3457 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3458
Yury Selivanovf2392132016-12-13 19:03:51 -05003459 if (meth == NULL) {
3460 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003461 goto error;
3462 }
3463
3464 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003465 /* We can bypass temporary bound method object.
3466 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003467
INADA Naoki015bce62017-01-16 17:23:30 +09003468 meth | self | arg1 | ... | argN
3469 */
3470 SET_TOP(meth);
3471 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003472 }
3473 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003474 /* meth is not an unbound method (but a regular attr, or
3475 something was returned by a descriptor protocol). Set
3476 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003477 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003478
3479 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003480 */
INADA Naoki015bce62017-01-16 17:23:30 +09003481 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003482 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003483 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 }
3485 DISPATCH();
3486 }
3487
Benjamin Petersonddd19492018-09-16 22:38:02 -07003488 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003489 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003490 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003491
3492 sp = stack_pointer;
3493
INADA Naoki015bce62017-01-16 17:23:30 +09003494 meth = PEEK(oparg + 2);
3495 if (meth == NULL) {
3496 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3497 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003498
3499 Stack layout:
3500
INADA Naoki015bce62017-01-16 17:23:30 +09003501 ... | NULL | callable | arg1 | ... | argN
3502 ^- TOP()
3503 ^- (-oparg)
3504 ^- (-oparg-1)
3505 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003506
Ville Skyttä49b27342017-08-03 09:00:59 +03003507 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003508 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003509 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003510 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003511 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003512 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003513 }
3514 else {
3515 /* This is a method call. Stack layout:
3516
INADA Naoki015bce62017-01-16 17:23:30 +09003517 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003518 ^- TOP()
3519 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003520 ^- (-oparg-1)
3521 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003522
INADA Naoki015bce62017-01-16 17:23:30 +09003523 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003524 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003525 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003526 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003527 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003528 stack_pointer = sp;
3529 }
3530
3531 PUSH(res);
3532 if (res == NULL)
3533 goto error;
3534 DISPATCH();
3535 }
3536
Benjamin Petersonddd19492018-09-16 22:38:02 -07003537 case TARGET(CALL_FUNCTION): {
3538 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003541 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003544 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003545 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003546 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003549
Benjamin Petersonddd19492018-09-16 22:38:02 -07003550 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003551 PyObject **sp, *res, *names;
3552
3553 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003554 assert(PyTuple_Check(names));
3555 assert(PyTuple_GET_SIZE(names) <= oparg);
3556 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003558 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003560 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003561 Py_DECREF(names);
3562
3563 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003565 }
3566 DISPATCH();
3567 }
3568
Benjamin Petersonddd19492018-09-16 22:38:02 -07003569 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003570 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003571 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003572 if (oparg & 0x01) {
3573 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003574 if (!PyDict_CheckExact(kwargs)) {
3575 PyObject *d = PyDict_New();
3576 if (d == NULL)
3577 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003578 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003579 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003580 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003581 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003582 goto error;
3583 }
3584 Py_DECREF(kwargs);
3585 kwargs = d;
3586 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003587 assert(PyDict_CheckExact(kwargs));
3588 }
3589 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003590 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003591 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003592 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003593 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003594 goto error;
3595 }
3596 Py_SETREF(callargs, PySequence_Tuple(callargs));
3597 if (callargs == NULL) {
3598 goto error;
3599 }
3600 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003601 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003602
Victor Stinner09532fe2019-05-10 23:39:09 +02003603 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003604 Py_DECREF(func);
3605 Py_DECREF(callargs);
3606 Py_XDECREF(kwargs);
3607
3608 SET_TOP(result);
3609 if (result == NULL) {
3610 goto error;
3611 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003612 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003614
Benjamin Petersonddd19492018-09-16 22:38:02 -07003615 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003616 PyObject *qualname = POP();
3617 PyObject *codeobj = POP();
3618 PyFunctionObject *func = (PyFunctionObject *)
3619 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003620
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003621 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003622 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003623 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003624 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003626
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003627 if (oparg & 0x08) {
3628 assert(PyTuple_CheckExact(TOP()));
3629 func ->func_closure = POP();
3630 }
3631 if (oparg & 0x04) {
3632 assert(PyDict_CheckExact(TOP()));
3633 func->func_annotations = POP();
3634 }
3635 if (oparg & 0x02) {
3636 assert(PyDict_CheckExact(TOP()));
3637 func->func_kwdefaults = POP();
3638 }
3639 if (oparg & 0x01) {
3640 assert(PyTuple_CheckExact(TOP()));
3641 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003643
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003644 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003645 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003647
Benjamin Petersonddd19492018-09-16 22:38:02 -07003648 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003649 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003651 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003653 step = NULL;
3654 stop = POP();
3655 start = TOP();
3656 slice = PySlice_New(start, stop, step);
3657 Py_DECREF(start);
3658 Py_DECREF(stop);
3659 Py_XDECREF(step);
3660 SET_TOP(slice);
3661 if (slice == NULL)
3662 goto error;
3663 DISPATCH();
3664 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003665
Benjamin Petersonddd19492018-09-16 22:38:02 -07003666 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003667 /* Handles f-string value formatting. */
3668 PyObject *result;
3669 PyObject *fmt_spec;
3670 PyObject *value;
3671 PyObject *(*conv_fn)(PyObject *);
3672 int which_conversion = oparg & FVC_MASK;
3673 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3674
3675 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003676 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003677
3678 /* See if any conversion is specified. */
3679 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003680 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003681 case FVC_STR: conv_fn = PyObject_Str; break;
3682 case FVC_REPR: conv_fn = PyObject_Repr; break;
3683 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003684 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003685 _PyErr_Format(tstate, PyExc_SystemError,
3686 "unexpected conversion flag %d",
3687 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003688 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003689 }
3690
3691 /* If there's a conversion function, call it and replace
3692 value with that result. Otherwise, just use value,
3693 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003694 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003695 result = conv_fn(value);
3696 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003697 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003698 Py_XDECREF(fmt_spec);
3699 goto error;
3700 }
3701 value = result;
3702 }
3703
3704 /* If value is a unicode object, and there's no fmt_spec,
3705 then we know the result of format(value) is value
3706 itself. In that case, skip calling format(). I plan to
3707 move this optimization in to PyObject_Format()
3708 itself. */
3709 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3710 /* Do nothing, just transfer ownership to result. */
3711 result = value;
3712 } else {
3713 /* Actually call format(). */
3714 result = PyObject_Format(value, fmt_spec);
3715 Py_DECREF(value);
3716 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003717 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003718 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003719 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003720 }
3721
Eric V. Smith135d5f42016-02-05 18:23:08 -05003722 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003723 DISPATCH();
3724 }
3725
Benjamin Petersonddd19492018-09-16 22:38:02 -07003726 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003727 int oldoparg = oparg;
3728 NEXTOPARG();
3729 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003731 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003732
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003733
Antoine Pitrou042b1282010-08-13 21:15:58 +00003734#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003736#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 default:
3738 fprintf(stderr,
3739 "XXX lineno: %d, opcode: %d\n",
3740 PyFrame_GetLineNumber(f),
3741 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003742 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003743 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003746
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 /* This should never be reached. Every opcode should end with DISPATCH()
3748 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003749 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003750
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003751error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003752 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003753#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003754 if (!_PyErr_Occurred(tstate)) {
3755 _PyErr_SetString(tstate, PyExc_SystemError,
3756 "error return without exception set");
3757 }
Victor Stinner365b6932013-07-12 00:11:58 +02003758#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003759 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003760#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003761
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003762 /* Log traceback info. */
3763 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003764
Benjamin Peterson51f46162013-01-23 08:38:47 -05003765 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003766 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3767 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003768
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003769exception_unwind:
3770 /* Unwind stacks if an exception occurred */
3771 while (f->f_iblock > 0) {
3772 /* Pop the current block. */
3773 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 if (b->b_type == EXCEPT_HANDLER) {
3776 UNWIND_EXCEPT_HANDLER(b);
3777 continue;
3778 }
3779 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003780 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 PyObject *exc, *val, *tb;
3782 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003783 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 /* Beware, this invalidates all b->b_* fields */
3785 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003786 PUSH(exc_info->exc_traceback);
3787 PUSH(exc_info->exc_value);
3788 if (exc_info->exc_type != NULL) {
3789 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
3791 else {
3792 Py_INCREF(Py_None);
3793 PUSH(Py_None);
3794 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003795 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 /* Make the raw exception data
3797 available to the handler,
3798 so a program can emulate the
3799 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003800 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003801 if (tb != NULL)
3802 PyException_SetTraceback(val, tb);
3803 else
3804 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003806 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003808 exc_info->exc_value = val;
3809 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 if (tb == NULL)
3811 tb = Py_None;
3812 Py_INCREF(tb);
3813 PUSH(tb);
3814 PUSH(val);
3815 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003817 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003818 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3819 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3820 /* Make sure that we trace line after exception if we are in a new execution
3821 * window or we don't need a line update and we are not in the first instruction
3822 * of the line. */
3823 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3824 instr_prev = INT_MAX;
3825 }
Mark Shannonfee55262019-11-21 09:11:43 +00003826 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003827 /* Resume normal execution */
3828 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 }
3830 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003831
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003832 /* End the loop as we still have an error */
3833 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003835
Pablo Galindof00828a2019-05-09 16:52:02 +01003836 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003837 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 /* Pop remaining stack entries. */
3840 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003841 PyObject *o = POP();
3842 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003844
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003845exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003847 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003848 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3849 tstate, f, PyTrace_RETURN, retval)) {
3850 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 }
3852 }
3853 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003854 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3855 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003856 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 }
3858 }
3859 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003862exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003863 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3864 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003865 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003866 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003868
Victor Stinner0b72b232020-03-12 23:18:39 +01003869 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003870}
3871
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003873format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003874 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003875{
3876 int err;
3877 Py_ssize_t len = PyList_GET_SIZE(names);
3878 PyObject *name_str, *comma, *tail, *tmp;
3879
3880 assert(PyList_CheckExact(names));
3881 assert(len >= 1);
3882 /* Deal with the joys of natural language. */
3883 switch (len) {
3884 case 1:
3885 name_str = PyList_GET_ITEM(names, 0);
3886 Py_INCREF(name_str);
3887 break;
3888 case 2:
3889 name_str = PyUnicode_FromFormat("%U and %U",
3890 PyList_GET_ITEM(names, len - 2),
3891 PyList_GET_ITEM(names, len - 1));
3892 break;
3893 default:
3894 tail = PyUnicode_FromFormat(", %U, and %U",
3895 PyList_GET_ITEM(names, len - 2),
3896 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003897 if (tail == NULL)
3898 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003899 /* Chop off the last two objects in the list. This shouldn't actually
3900 fail, but we can't be too careful. */
3901 err = PyList_SetSlice(names, len - 2, len, NULL);
3902 if (err == -1) {
3903 Py_DECREF(tail);
3904 return;
3905 }
3906 /* Stitch everything up into a nice comma-separated list. */
3907 comma = PyUnicode_FromString(", ");
3908 if (comma == NULL) {
3909 Py_DECREF(tail);
3910 return;
3911 }
3912 tmp = PyUnicode_Join(comma, names);
3913 Py_DECREF(comma);
3914 if (tmp == NULL) {
3915 Py_DECREF(tail);
3916 return;
3917 }
3918 name_str = PyUnicode_Concat(tmp, tail);
3919 Py_DECREF(tmp);
3920 Py_DECREF(tail);
3921 break;
3922 }
3923 if (name_str == NULL)
3924 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003925 _PyErr_Format(tstate, PyExc_TypeError,
3926 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003927 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003928 len,
3929 kind,
3930 len == 1 ? "" : "s",
3931 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003932 Py_DECREF(name_str);
3933}
3934
3935static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003936missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3937 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003938 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003939{
Victor Stinner74319ae2016-08-25 00:04:09 +02003940 Py_ssize_t i, j = 0;
3941 Py_ssize_t start, end;
3942 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003943 const char *kind = positional ? "positional" : "keyword-only";
3944 PyObject *missing_names;
3945
3946 /* Compute the names of the arguments that are missing. */
3947 missing_names = PyList_New(missing);
3948 if (missing_names == NULL)
3949 return;
3950 if (positional) {
3951 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003952 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003953 }
3954 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003955 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 end = start + co->co_kwonlyargcount;
3957 }
3958 for (i = start; i < end; i++) {
3959 if (GETLOCAL(i) == NULL) {
3960 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3961 PyObject *name = PyObject_Repr(raw);
3962 if (name == NULL) {
3963 Py_DECREF(missing_names);
3964 return;
3965 }
3966 PyList_SET_ITEM(missing_names, j++, name);
3967 }
3968 }
3969 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003970 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003971 Py_DECREF(missing_names);
3972}
3973
3974static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003975too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3976 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003977 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978{
3979 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003980 Py_ssize_t kwonly_given = 0;
3981 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003982 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003983 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984
Benjamin Petersone109c702011-06-24 09:37:26 -05003985 assert((co->co_flags & CO_VARARGS) == 0);
3986 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003987 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003988 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003989 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003990 }
3991 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003992 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003993 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003994 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003995 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 }
3997 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003998 plural = (co_argcount != 1);
3999 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 }
4001 if (sig == NULL)
4002 return;
4003 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004004 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4005 kwonly_sig = PyUnicode_FromFormat(format,
4006 given != 1 ? "s" : "",
4007 kwonly_given,
4008 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 if (kwonly_sig == NULL) {
4010 Py_DECREF(sig);
4011 return;
4012 }
4013 }
4014 else {
4015 /* This will not fail. */
4016 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004017 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004019 _PyErr_Format(tstate, PyExc_TypeError,
4020 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004021 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004022 sig,
4023 plural ? "s" : "",
4024 given,
4025 kwonly_sig,
4026 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 Py_DECREF(sig);
4028 Py_DECREF(kwonly_sig);
4029}
4030
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004031static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004032positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004033 Py_ssize_t kwcount, PyObject* const* kwnames,
4034 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004035{
4036 int posonly_conflicts = 0;
4037 PyObject* posonly_names = PyList_New(0);
4038
4039 for(int k=0; k < co->co_posonlyargcount; k++){
4040 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4041
4042 for (int k2=0; k2<kwcount; k2++){
4043 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4044 PyObject* kwname = kwnames[k2];
4045 if (kwname == posonly_name){
4046 if(PyList_Append(posonly_names, kwname) != 0) {
4047 goto fail;
4048 }
4049 posonly_conflicts++;
4050 continue;
4051 }
4052
4053 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4054
4055 if ( cmp > 0) {
4056 if(PyList_Append(posonly_names, kwname) != 0) {
4057 goto fail;
4058 }
4059 posonly_conflicts++;
4060 } else if (cmp < 0) {
4061 goto fail;
4062 }
4063
4064 }
4065 }
4066 if (posonly_conflicts) {
4067 PyObject* comma = PyUnicode_FromString(", ");
4068 if (comma == NULL) {
4069 goto fail;
4070 }
4071 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4072 Py_DECREF(comma);
4073 if (error_names == NULL) {
4074 goto fail;
4075 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004076 _PyErr_Format(tstate, PyExc_TypeError,
4077 "%U() got some positional-only arguments passed"
4078 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004079 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004080 Py_DECREF(error_names);
4081 goto fail;
4082 }
4083
4084 Py_DECREF(posonly_names);
4085 return 0;
4086
4087fail:
4088 Py_XDECREF(posonly_names);
4089 return 1;
4090
4091}
4092
Guido van Rossumc2e20742006-02-27 22:32:47 +00004093/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004094 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004095 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004096
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004097PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004098_PyEval_EvalCode(PyThreadState *tstate,
4099 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004100 PyObject *const *args, Py_ssize_t argcount,
4101 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004102 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004103 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004104 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004105 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004106{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004107 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004108
Victor Stinner232dda62020-06-04 15:19:02 +02004109 PyCodeObject *co = (PyCodeObject*)_co;
4110
4111 if (!name) {
4112 name = co->co_name;
4113 }
4114 assert(name != NULL);
4115 assert(PyUnicode_Check(name));
4116
4117 if (!qualname) {
4118 qualname = name;
4119 }
4120 assert(qualname != NULL);
4121 assert(PyUnicode_Check(qualname));
4122
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004123 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004124 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004127 _PyErr_SetString(tstate, PyExc_SystemError,
4128 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 return NULL;
4130 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004131
Victor Stinnerc7020012016-08-16 23:40:29 +02004132 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004133 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004134 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004136 }
Victor Stinner232dda62020-06-04 15:19:02 +02004137 PyObject **fastlocals = f->f_localsplus;
4138 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004139
Victor Stinnerc7020012016-08-16 23:40:29 +02004140 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004141 PyObject *kwdict;
4142 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 if (co->co_flags & CO_VARKEYWORDS) {
4144 kwdict = PyDict_New();
4145 if (kwdict == NULL)
4146 goto fail;
4147 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004148 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004149 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004150 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004151 SETLOCAL(i, kwdict);
4152 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004153 else {
4154 kwdict = NULL;
4155 }
4156
Pablo Galindocd74e662019-06-01 18:08:04 +01004157 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004158 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004159 if (argcount > co->co_argcount) {
4160 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004161 }
4162 else {
4163 n = argcount;
4164 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004165 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004166 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004167 Py_INCREF(x);
4168 SETLOCAL(j, x);
4169 }
4170
Victor Stinnerc7020012016-08-16 23:40:29 +02004171 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004172 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004173 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004174 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004176 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004177 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004179
Serhiy Storchakab7281052016-09-12 00:52:40 +03004180 /* Handle keyword arguments passed as two strided arrays */
4181 kwcount *= kwstep;
4182 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004183 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004184 PyObject *keyword = kwnames[i];
4185 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004186 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004187
Benjamin Petersonb204a422011-06-05 22:04:07 -05004188 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004189 _PyErr_Format(tstate, PyExc_TypeError,
4190 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004191 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004192 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004194
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 /* Speed hack: do raw pointer compares. As names are
4196 normally interned this should almost always hit. */
4197 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004198 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004199 PyObject *varname = co_varnames[j];
4200 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004202 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004204
Benjamin Petersonb204a422011-06-05 22:04:07 -05004205 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004206 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004207 PyObject *varname = co_varnames[j];
4208 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004209 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004210 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004211 }
4212 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004214 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004215 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004216
Victor Stinner231d1f32017-01-11 02:12:06 +01004217 assert(j >= total_args);
4218 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004219
Victor Stinner438a12d2019-05-24 17:01:38 +02004220 if (co->co_posonlyargcount
4221 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004222 kwcount, kwnames,
4223 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004224 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004225 goto fail;
4226 }
4227
Victor Stinner438a12d2019-05-24 17:01:38 +02004228 _PyErr_Format(tstate, PyExc_TypeError,
4229 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004230 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004231 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004233
Christian Heimes0bd447f2013-07-20 14:48:10 +02004234 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4235 goto fail;
4236 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004237 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004238
Benjamin Petersonb204a422011-06-05 22:04:07 -05004239 kw_found:
4240 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004241 _PyErr_Format(tstate, PyExc_TypeError,
4242 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004243 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004244 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004246 Py_INCREF(value);
4247 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004249
4250 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004251 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004252 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4253 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 goto fail;
4255 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004256
4257 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004258 if (argcount < co->co_argcount) {
4259 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004260 Py_ssize_t missing = 0;
4261 for (i = argcount; i < m; i++) {
4262 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004263 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004264 }
4265 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004266 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004267 missing_arguments(tstate, co, missing, defcount, fastlocals,
4268 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004269 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004270 }
4271 if (n > m)
4272 i = n - m;
4273 else
4274 i = 0;
4275 for (; i < defcount; i++) {
4276 if (GETLOCAL(m+i) == NULL) {
4277 PyObject *def = defs[i];
4278 Py_INCREF(def);
4279 SETLOCAL(m+i, def);
4280 }
4281 }
4282 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004283
4284 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004285 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004286 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004287 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004288 if (GETLOCAL(i) != NULL)
4289 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004290 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004291 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004292 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004293 if (def) {
4294 Py_INCREF(def);
4295 SETLOCAL(i, def);
4296 continue;
4297 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004298 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004299 goto fail;
4300 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004301 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004302 missing++;
4303 }
4304 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004305 missing_arguments(tstate, co, missing, -1, fastlocals,
4306 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004307 goto fail;
4308 }
4309 }
4310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004312 vars into frame. */
4313 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004315 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004316 /* Possibly account for the cell variable being an argument. */
4317 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004318 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004319 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004320 /* Clear the local copy. */
4321 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004322 }
4323 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004324 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004325 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004326 if (c == NULL)
4327 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004328 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004330
4331 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004332 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4333 PyObject *o = PyTuple_GET_ITEM(closure, i);
4334 Py_INCREF(o);
4335 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004337
Yury Selivanoveb636452016-09-08 22:01:51 -07004338 /* Handle generator/coroutine/asynchronous generator */
4339 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004340 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004341 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 /* Don't need to keep the reference to f_back, it will be set
4344 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004345 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 /* Create a new generator that owns the ready to run frame
4348 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004349 if (is_coro) {
4350 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004351 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4352 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004353 } else {
4354 gen = PyGen_NewWithQualName(f, name, qualname);
4355 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004356 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004357 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004358 }
INADA Naoki9c157762016-12-26 18:52:46 +09004359
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004360 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004361
Yury Selivanov75445082015-05-11 22:57:16 -04004362 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004364
Victor Stinnerb9e68122019-11-14 12:20:46 +01004365 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004366
Thomas Woutersce272b62007-09-19 21:19:28 +00004367fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 /* decref'ing the frame can cause __del__ methods to get invoked,
4370 which can call back into Python. While we're done with the
4371 current Python frame (f), the associated C stack is still in use,
4372 so recursion_depth must be boosted for the duration.
4373 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004374 if (Py_REFCNT(f) > 1) {
4375 Py_DECREF(f);
4376 _PyObject_GC_TRACK(f);
4377 }
4378 else {
4379 ++tstate->recursion_depth;
4380 Py_DECREF(f);
4381 --tstate->recursion_depth;
4382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004384}
4385
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004386
4387PyObject *
4388_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4389 PyObject *const *args, Py_ssize_t argcount,
4390 PyObject *const *kwnames, PyObject *const *kwargs,
4391 Py_ssize_t kwcount, int kwstep,
4392 PyObject *const *defs, Py_ssize_t defcount,
4393 PyObject *kwdefs, PyObject *closure,
4394 PyObject *name, PyObject *qualname)
4395{
4396 PyThreadState *tstate = _PyThreadState_GET();
4397 return _PyEval_EvalCode(tstate, _co, globals, locals,
4398 args, argcount,
4399 kwnames, kwargs,
4400 kwcount, kwstep,
4401 defs, defcount,
4402 kwdefs, closure,
4403 name, qualname);
4404}
4405
Victor Stinner40ee3012014-06-16 15:59:28 +02004406PyObject *
4407PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004408 PyObject *const *args, int argcount,
4409 PyObject *const *kws, int kwcount,
4410 PyObject *const *defs, int defcount,
4411 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004412{
4413 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004414 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004415 kws, kws != NULL ? kws + 1 : NULL,
4416 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004417 defs, defcount,
4418 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004419 NULL, NULL);
4420}
Tim Peters5ca576e2001-06-18 22:08:13 +00004421
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004422static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004423special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004426 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004427 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004428 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 return NULL;
4430 }
4431 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004432}
4433
4434
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004435/* Logic for the raise statement (too complicated for inlining).
4436 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004437static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004438do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 if (exc == NULL) {
4443 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004444 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004446 type = exc_info->exc_type;
4447 value = exc_info->exc_value;
4448 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004449 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004450 _PyErr_SetString(tstate, PyExc_RuntimeError,
4451 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004452 return 0;
4453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 Py_XINCREF(type);
4455 Py_XINCREF(value);
4456 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004457 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004458 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 /* We support the following forms of raise:
4462 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004463 raise <instance>
4464 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 if (PyExceptionClass_Check(exc)) {
4467 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004468 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 if (value == NULL)
4470 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004471 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004472 _PyErr_Format(tstate, PyExc_TypeError,
4473 "calling %R should have returned an instance of "
4474 "BaseException, not %R",
4475 type, Py_TYPE(value));
4476 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004477 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 }
4479 else if (PyExceptionInstance_Check(exc)) {
4480 value = exc;
4481 type = PyExceptionInstance_Class(exc);
4482 Py_INCREF(type);
4483 }
4484 else {
4485 /* Not something you can raise. You get an exception
4486 anyway, just not what you specified :-) */
4487 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 _PyErr_SetString(tstate, PyExc_TypeError,
4489 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 goto raise_error;
4491 }
Collin Winter828f04a2007-08-31 00:04:24 +00004492
Serhiy Storchakac0191582016-09-27 11:37:10 +03004493 assert(type != NULL);
4494 assert(value != NULL);
4495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 if (cause) {
4497 PyObject *fixed_cause;
4498 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004499 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (fixed_cause == NULL)
4501 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004502 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004504 else if (PyExceptionInstance_Check(cause)) {
4505 fixed_cause = cause;
4506 }
4507 else if (cause == Py_None) {
4508 Py_DECREF(cause);
4509 fixed_cause = NULL;
4510 }
4511 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004512 _PyErr_SetString(tstate, PyExc_TypeError,
4513 "exception causes must derive from "
4514 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 goto raise_error;
4516 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004517 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 }
Collin Winter828f04a2007-08-31 00:04:24 +00004519
Victor Stinner438a12d2019-05-24 17:01:38 +02004520 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004521 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004522 Py_DECREF(value);
4523 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004524 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004525
4526raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 Py_XDECREF(value);
4528 Py_XDECREF(type);
4529 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004530 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004531}
4532
Tim Petersd6d010b2001-06-21 02:49:55 +00004533/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004534 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004535
Guido van Rossum0368b722007-05-11 16:50:42 +00004536 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4537 with a variable target.
4538*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004539
Barry Warsawe42b18f1997-08-25 22:13:04 +00004540static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004541unpack_iterable(PyThreadState *tstate, PyObject *v,
4542 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 int i = 0, j = 0;
4545 Py_ssize_t ll = 0;
4546 PyObject *it; /* iter(v) */
4547 PyObject *w;
4548 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004553 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004554 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004555 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004556 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004557 _PyErr_Format(tstate, PyExc_TypeError,
4558 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004559 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004560 }
4561 return 0;
4562 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 for (; i < argcnt; i++) {
4565 w = PyIter_Next(it);
4566 if (w == NULL) {
4567 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004568 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004569 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004570 _PyErr_Format(tstate, PyExc_ValueError,
4571 "not enough values to unpack "
4572 "(expected %d, got %d)",
4573 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004574 }
4575 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004576 _PyErr_Format(tstate, PyExc_ValueError,
4577 "not enough values to unpack "
4578 "(expected at least %d, got %d)",
4579 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004580 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 }
4582 goto Error;
4583 }
4584 *--sp = w;
4585 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 if (argcntafter == -1) {
4588 /* We better have exhausted the iterator now. */
4589 w = PyIter_Next(it);
4590 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004591 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 goto Error;
4593 Py_DECREF(it);
4594 return 1;
4595 }
4596 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004597 _PyErr_Format(tstate, PyExc_ValueError,
4598 "too many values to unpack (expected %d)",
4599 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 goto Error;
4601 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 l = PySequence_List(it);
4604 if (l == NULL)
4605 goto Error;
4606 *--sp = l;
4607 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 ll = PyList_GET_SIZE(l);
4610 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004611 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004612 "not enough values to unpack (expected at least %d, got %zd)",
4613 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 goto Error;
4615 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 /* Pop the "after-variable" args off the list. */
4618 for (j = argcntafter; j > 0; j--, i++) {
4619 *--sp = PyList_GET_ITEM(l, ll - j);
4620 }
4621 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004622 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 Py_DECREF(it);
4624 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004625
Tim Petersd6d010b2001-06-21 02:49:55 +00004626Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 for (; i > 0; i--, sp++)
4628 Py_DECREF(*sp);
4629 Py_XDECREF(it);
4630 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004631}
4632
4633
Guido van Rossum96a42c81992-01-12 02:29:51 +00004634#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004635static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004636prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004639 if (PyObject_Print(v, stdout, 0) != 0) {
4640 /* Don't know what else to do */
4641 _PyErr_Clear(tstate);
4642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 printf("\n");
4644 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004645}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004646#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004647
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004648static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004649call_exc_trace(Py_tracefunc func, PyObject *self,
4650 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004651{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004652 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004654 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 if (value == NULL) {
4656 value = Py_None;
4657 Py_INCREF(value);
4658 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004659 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004660 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 arg = PyTuple_Pack(3, type, value, traceback);
4662 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004663 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 return;
4665 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004666 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004668 if (err == 0) {
4669 _PyErr_Restore(tstate, type, value, orig_traceback);
4670 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 else {
4672 Py_XDECREF(type);
4673 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004674 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004676}
4677
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004678static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004679call_trace_protected(Py_tracefunc func, PyObject *obj,
4680 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 PyObject *type, *value, *traceback;
4684 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004685 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004686 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 if (err == 0)
4688 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004689 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 return 0;
4691 }
4692 else {
4693 Py_XDECREF(type);
4694 Py_XDECREF(value);
4695 Py_XDECREF(traceback);
4696 return -1;
4697 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004698}
4699
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004700static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004701call_trace(Py_tracefunc func, PyObject *obj,
4702 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 int result;
4706 if (tstate->tracing)
4707 return 0;
4708 tstate->tracing++;
4709 tstate->use_tracing = 0;
4710 result = func(obj, frame, what, arg);
4711 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4712 || (tstate->c_profilefunc != NULL));
4713 tstate->tracing--;
4714 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004715}
4716
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004717PyObject *
4718_PyEval_CallTracing(PyObject *func, PyObject *args)
4719{
Victor Stinner50b48572018-11-01 01:51:40 +01004720 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 int save_tracing = tstate->tracing;
4722 int save_use_tracing = tstate->use_tracing;
4723 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 tstate->tracing = 0;
4726 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4727 || (tstate->c_profilefunc != NULL));
4728 result = PyObject_Call(func, args, NULL);
4729 tstate->tracing = save_tracing;
4730 tstate->use_tracing = save_use_tracing;
4731 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004732}
4733
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004734/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004735static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004736maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004737 PyThreadState *tstate, PyFrameObject *frame,
4738 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 int result = 0;
4741 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 /* If the last instruction executed isn't in the current
4744 instruction window, reset the window.
4745 */
4746 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4747 PyAddrPair bounds;
4748 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4749 &bounds);
4750 *instr_lb = bounds.ap_lower;
4751 *instr_ub = bounds.ap_upper;
4752 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004753 /* If the last instruction falls at the start of a line or if it
4754 represents a jump backwards, update the frame's line number and
4755 then call the trace function if we're tracing source lines.
4756 */
4757 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004759 if (frame->f_trace_lines) {
4760 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 }
George King20faa682017-10-18 17:44:22 -07004763 /* Always emit an opcode event if we're tracing all opcodes. */
4764 if (frame->f_trace_opcodes) {
4765 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 *instr_prev = frame->f_lasti;
4768 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004769}
4770
Victor Stinner309d7cc2020-03-13 16:39:12 +01004771int
4772_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4773{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004774 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004775 /* The caller must hold the GIL */
4776 assert(PyGILState_Check());
4777
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004778 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004779 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004780 PyThreadState *current_tstate = _PyThreadState_GET();
4781 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782 return -1;
4783 }
4784
4785 PyObject *profileobj = tstate->c_profileobj;
4786
4787 tstate->c_profilefunc = NULL;
4788 tstate->c_profileobj = NULL;
4789 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4790 tstate->use_tracing = tstate->c_tracefunc != NULL;
4791 Py_XDECREF(profileobj);
4792
4793 Py_XINCREF(arg);
4794 tstate->c_profileobj = arg;
4795 tstate->c_profilefunc = func;
4796
4797 /* Flag that tracing or profiling is turned on */
4798 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4799 return 0;
4800}
4801
Fred Drake5755ce62001-06-27 19:19:46 +00004802void
4803PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004804{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004805 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004806 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004807 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004808 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4809 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004810}
4811
4812int
4813_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4814{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004815 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004816 /* The caller must hold the GIL */
4817 assert(PyGILState_Check());
4818
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004819 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004820 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004821 PyThreadState *current_tstate = _PyThreadState_GET();
4822 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004823 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004824 }
4825
Victor Stinnerda2914d2020-03-20 09:29:08 +01004826 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004827 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004828 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004829
4830 tstate->c_tracefunc = NULL;
4831 tstate->c_traceobj = NULL;
4832 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4833 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4834 Py_XDECREF(traceobj);
4835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004837 tstate->c_traceobj = arg;
4838 tstate->c_tracefunc = func;
4839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004841 tstate->use_tracing = ((func != NULL)
4842 || (tstate->c_profilefunc != NULL));
4843
4844 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004845}
4846
4847void
4848PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4849{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004850 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004851 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004852 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004853 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4854 }
Fred Draked0838392001-06-16 21:02:31 +00004855}
4856
Victor Stinner309d7cc2020-03-13 16:39:12 +01004857
Yury Selivanov75445082015-05-11 22:57:16 -04004858void
Victor Stinner838f2642019-06-13 22:41:23 +02004859_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004860{
4861 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004862 tstate->coroutine_origin_tracking_depth = new_depth;
4863}
4864
4865int
4866_PyEval_GetCoroutineOriginTrackingDepth(void)
4867{
Victor Stinner50b48572018-11-01 01:51:40 +01004868 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004869 return tstate->coroutine_origin_tracking_depth;
4870}
4871
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004872int
Yury Selivanoveb636452016-09-08 22:01:51 -07004873_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4874{
Victor Stinner50b48572018-11-01 01:51:40 +01004875 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004876
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004877 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004878 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004879 }
4880
Yury Selivanoveb636452016-09-08 22:01:51 -07004881 Py_XINCREF(firstiter);
4882 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004883 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004884}
4885
4886PyObject *
4887_PyEval_GetAsyncGenFirstiter(void)
4888{
Victor Stinner50b48572018-11-01 01:51:40 +01004889 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004890 return tstate->async_gen_firstiter;
4891}
4892
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004893int
Yury Selivanoveb636452016-09-08 22:01:51 -07004894_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4895{
Victor Stinner50b48572018-11-01 01:51:40 +01004896 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004897
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004898 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004899 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004900 }
4901
Yury Selivanoveb636452016-09-08 22:01:51 -07004902 Py_XINCREF(finalizer);
4903 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004904 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004905}
4906
4907PyObject *
4908_PyEval_GetAsyncGenFinalizer(void)
4909{
Victor Stinner50b48572018-11-01 01:51:40 +01004910 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004911 return tstate->async_gen_finalizer;
4912}
4913
Victor Stinner438a12d2019-05-24 17:01:38 +02004914PyFrameObject *
4915PyEval_GetFrame(void)
4916{
4917 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004918 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004919}
4920
Guido van Rossumb209a111997-04-29 18:18:01 +00004921PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004922PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004923{
Victor Stinner438a12d2019-05-24 17:01:38 +02004924 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004925 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004927 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928 else
4929 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004930}
4931
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004932/* Convenience function to get a builtin from its name */
4933PyObject *
4934_PyEval_GetBuiltinId(_Py_Identifier *name)
4935{
Victor Stinner438a12d2019-05-24 17:01:38 +02004936 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004937 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4938 if (attr) {
4939 Py_INCREF(attr);
4940 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004941 else if (!_PyErr_Occurred(tstate)) {
4942 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004943 }
4944 return attr;
4945}
4946
Guido van Rossumb209a111997-04-29 18:18:01 +00004947PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004948PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004949{
Victor Stinner438a12d2019-05-24 17:01:38 +02004950 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004951 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004952 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004953 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004955 }
4956
Victor Stinner438a12d2019-05-24 17:01:38 +02004957 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004958 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004959 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004960
4961 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004963}
4964
Guido van Rossumb209a111997-04-29 18:18:01 +00004965PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004966PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004967{
Victor Stinner438a12d2019-05-24 17:01:38 +02004968 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004969 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004970 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004972 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004973
4974 assert(current_frame->f_globals != NULL);
4975 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004976}
4977
Guido van Rossum6135a871995-01-09 17:53:26 +00004978int
Tim Peters5ba58662001-07-16 02:29:45 +00004979PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004980{
Victor Stinner438a12d2019-05-24 17:01:38 +02004981 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004982 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 if (current_frame != NULL) {
4986 const int codeflags = current_frame->f_code->co_flags;
4987 const int compilerflags = codeflags & PyCF_MASK;
4988 if (compilerflags) {
4989 result = 1;
4990 cf->cf_flags |= compilerflags;
4991 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004992#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 if (codeflags & CO_GENERATOR_ALLOWED) {
4994 result = 1;
4995 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4996 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004997#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 }
4999 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005000}
5001
Guido van Rossum3f5da241990-12-20 15:06:42 +00005002
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005003const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005004PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 if (PyMethod_Check(func))
5007 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5008 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005009 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 else if (PyCFunction_Check(func))
5011 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5012 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005013 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005014}
5015
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005016const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005017PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 if (PyMethod_Check(func))
5020 return "()";
5021 else if (PyFunction_Check(func))
5022 return "()";
5023 else if (PyCFunction_Check(func))
5024 return "()";
5025 else
5026 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005027}
5028
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005029#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005030if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005031 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5032 tstate, tstate->frame, \
5033 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 x = NULL; \
5035 } \
5036 else { \
5037 x = call; \
5038 if (tstate->c_profilefunc != NULL) { \
5039 if (x == NULL) { \
5040 call_trace_protected(tstate->c_profilefunc, \
5041 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005042 tstate, tstate->frame, \
5043 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 /* XXX should pass (type, value, tb) */ \
5045 } else { \
5046 if (call_trace(tstate->c_profilefunc, \
5047 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005048 tstate, tstate->frame, \
5049 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 Py_DECREF(x); \
5051 x = NULL; \
5052 } \
5053 } \
5054 } \
5055 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005056} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 x = call; \
5058 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005059
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005060
5061static PyObject *
5062trace_call_function(PyThreadState *tstate,
5063 PyObject *func,
5064 PyObject **args, Py_ssize_t nargs,
5065 PyObject *kwnames)
5066{
5067 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005068 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005069 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005070 return x;
5071 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005072 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005073 /* We need to create a temporary bound method as argument
5074 for profiling.
5075
5076 If nargs == 0, then this cannot work because we have no
5077 "self". In any case, the call itself would raise
5078 TypeError (foo needs an argument), so we just skip
5079 profiling. */
5080 PyObject *self = args[0];
5081 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5082 if (func == NULL) {
5083 return NULL;
5084 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005085 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005086 args+1, nargs-1,
5087 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005088 Py_DECREF(func);
5089 return x;
5090 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005091 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005092}
5093
Victor Stinner415c5102017-01-11 00:54:57 +01005094/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5095 to reduce the stack consumption. */
5096Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005097call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005098{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005099 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 PyObject *func = *pfunc;
5101 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005102 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5103 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005104 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005105
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005106 if (tstate->use_tracing) {
5107 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005108 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005109 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005110 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005112
Victor Stinner438a12d2019-05-24 17:01:38 +02005113 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005114
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005115 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 while ((*pp_stack) > pfunc) {
5117 w = EXT_POP(*pp_stack);
5118 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005122}
5123
Jeremy Hylton52820442001-01-03 23:52:36 +00005124static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005125do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005126{
jdemeyere89de732018-09-19 12:06:20 +02005127 PyObject *result;
5128
scoder4c9ea092020-05-12 16:12:41 +02005129 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005130 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005131 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005133 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005134 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5135 if (nargs > 0 && tstate->use_tracing) {
5136 /* We need to create a temporary bound method as argument
5137 for profiling.
5138
5139 If nargs == 0, then this cannot work because we have no
5140 "self". In any case, the call itself would raise
5141 TypeError (foo needs an argument), so we just skip
5142 profiling. */
5143 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5144 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5145 if (func == NULL) {
5146 return NULL;
5147 }
5148
Victor Stinner4d231bc2019-11-14 13:36:21 +01005149 C_TRACE(result, _PyObject_FastCallDictTstate(
5150 tstate, func,
5151 &_PyTuple_ITEMS(callargs)[1],
5152 nargs - 1,
5153 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005154 Py_DECREF(func);
5155 return result;
5156 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005157 }
jdemeyere89de732018-09-19 12:06:20 +02005158 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005159}
5160
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005161/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005162 nb_index slot defined, and store in *pi.
5163 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005164 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005165 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005166*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005167int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005168_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005169{
Victor Stinner438a12d2019-05-24 17:01:38 +02005170 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005171 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005173 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005175 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005176 return 0;
5177 }
5178 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005179 _PyErr_SetString(tstate, PyExc_TypeError,
5180 "slice indices must be integers or "
5181 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005182 return 0;
5183 }
5184 *pi = x;
5185 }
5186 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005187}
5188
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005189int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005190_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005191{
Victor Stinner438a12d2019-05-24 17:01:38 +02005192 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005193 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005194 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005195 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005196 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005197 return 0;
5198 }
5199 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005200 _PyErr_SetString(tstate, PyExc_TypeError,
5201 "slice indices must be integers or "
5202 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005203 return 0;
5204 }
5205 *pi = x;
5206 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005207}
5208
Thomas Wouters52152252000-08-17 22:55:00 +00005209static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005210import_name(PyThreadState *tstate, PyFrameObject *f,
5211 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005212{
5213 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005214 PyObject *import_func, *res;
5215 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005216
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005217 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005218 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005219 if (!_PyErr_Occurred(tstate)) {
5220 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005221 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005222 return NULL;
5223 }
5224
5225 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005226 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005227 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005228 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005229 return NULL;
5230 }
5231 res = PyImport_ImportModuleLevelObject(
5232 name,
5233 f->f_globals,
5234 f->f_locals == NULL ? Py_None : f->f_locals,
5235 fromlist,
5236 ilevel);
5237 return res;
5238 }
5239
5240 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005241
5242 stack[0] = name;
5243 stack[1] = f->f_globals;
5244 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5245 stack[3] = fromlist;
5246 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005247 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005248 Py_DECREF(import_func);
5249 return res;
5250}
5251
5252static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005253import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005256 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005257
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005258 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005259 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005260 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005261 /* Issue #17636: in case this failed because of a circular relative
5262 import, try to fallback on reading the module directly from
5263 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005264 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005265 if (pkgname == NULL) {
5266 goto error;
5267 }
Oren Milman6db70332017-09-19 14:23:01 +03005268 if (!PyUnicode_Check(pkgname)) {
5269 Py_CLEAR(pkgname);
5270 goto error;
5271 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005272 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005273 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005274 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005275 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005276 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005277 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005278 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005279 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005280 goto error;
5281 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005282 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005284 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005285 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005286 if (pkgname == NULL) {
5287 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5288 if (pkgname_or_unknown == NULL) {
5289 Py_XDECREF(pkgpath);
5290 return NULL;
5291 }
5292 } else {
5293 pkgname_or_unknown = pkgname;
5294 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005295
5296 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005298 errmsg = PyUnicode_FromFormat(
5299 "cannot import name %R from %R (unknown location)",
5300 name, pkgname_or_unknown
5301 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005302 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005303 PyErr_SetImportError(errmsg, pkgname, NULL);
5304 }
5305 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005306 _Py_IDENTIFIER(__spec__);
5307 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005308 const char *fmt =
5309 _PyModuleSpec_IsInitializing(spec) ?
5310 "cannot import name %R from partially initialized module %R "
5311 "(most likely due to a circular import) (%S)" :
5312 "cannot import name %R from %R (%S)";
5313 Py_XDECREF(spec);
5314
5315 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005316 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005317 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005318 }
5319
Xiang Zhang4830f582017-03-21 11:13:42 +08005320 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005321 Py_XDECREF(pkgname_or_unknown);
5322 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005323 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005324}
Guido van Rossumac7be682001-01-17 15:42:30 +00005325
Thomas Wouters52152252000-08-17 22:55:00 +00005326static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005327import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005328{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005329 _Py_IDENTIFIER(__all__);
5330 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005331 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 int skip_leading_underscores = 0;
5333 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005334
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005335 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5336 return -1; /* Unexpected error */
5337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005339 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5340 return -1;
5341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005343 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005344 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 return -1;
5346 }
5347 all = PyMapping_Keys(dict);
5348 Py_DECREF(dict);
5349 if (all == NULL)
5350 return -1;
5351 skip_leading_underscores = 1;
5352 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 for (pos = 0, err = 0; ; pos++) {
5355 name = PySequence_GetItem(all, pos);
5356 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005357 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005359 }
5360 else {
5361 _PyErr_Clear(tstate);
5362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 break;
5364 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005365 if (!PyUnicode_Check(name)) {
5366 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5367 if (modname == NULL) {
5368 Py_DECREF(name);
5369 err = -1;
5370 break;
5371 }
5372 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005373 _PyErr_Format(tstate, PyExc_TypeError,
5374 "module __name__ must be a string, not %.100s",
5375 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005376 }
5377 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 _PyErr_Format(tstate, PyExc_TypeError,
5379 "%s in %U.%s must be str, not %.100s",
5380 skip_leading_underscores ? "Key" : "Item",
5381 modname,
5382 skip_leading_underscores ? "__dict__" : "__all__",
5383 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005384 }
5385 Py_DECREF(modname);
5386 Py_DECREF(name);
5387 err = -1;
5388 break;
5389 }
5390 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005391 if (PyUnicode_READY(name) == -1) {
5392 Py_DECREF(name);
5393 err = -1;
5394 break;
5395 }
5396 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5397 Py_DECREF(name);
5398 continue;
5399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 }
5401 value = PyObject_GetAttr(v, name);
5402 if (value == NULL)
5403 err = -1;
5404 else if (PyDict_CheckExact(locals))
5405 err = PyDict_SetItem(locals, name, value);
5406 else
5407 err = PyObject_SetItem(locals, name, value);
5408 Py_DECREF(name);
5409 Py_XDECREF(value);
5410 if (err != 0)
5411 break;
5412 }
5413 Py_DECREF(all);
5414 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005415}
5416
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005417static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005418check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005419{
Victor Stinnera102ed72020-02-07 02:24:48 +01005420 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005421 /* check_args_iterable() may be called with a live exception:
5422 * clear it to prevent calling _PyObject_FunctionStr() with an
5423 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005424 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005425 PyObject *funcstr = _PyObject_FunctionStr(func);
5426 if (funcstr != NULL) {
5427 _PyErr_Format(tstate, PyExc_TypeError,
5428 "%U argument after * must be an iterable, not %.200s",
5429 funcstr, Py_TYPE(args)->tp_name);
5430 Py_DECREF(funcstr);
5431 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005432 return -1;
5433 }
5434 return 0;
5435}
5436
5437static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005438format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005439{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005440 /* _PyDict_MergeEx raises attribute
5441 * error (percolated from an attempt
5442 * to get 'keys' attribute) instead of
5443 * a type error if its second argument
5444 * is not a mapping.
5445 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005446 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005447 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005448 PyObject *funcstr = _PyObject_FunctionStr(func);
5449 if (funcstr != NULL) {
5450 _PyErr_Format(
5451 tstate, PyExc_TypeError,
5452 "%U argument after ** must be a mapping, not %.200s",
5453 funcstr, Py_TYPE(kwargs)->tp_name);
5454 Py_DECREF(funcstr);
5455 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005456 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005457 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005458 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005459 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005460 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005461 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005462 PyObject *funcstr = _PyObject_FunctionStr(func);
5463 if (funcstr != NULL) {
5464 PyObject *key = PyTuple_GET_ITEM(val, 0);
5465 _PyErr_Format(
5466 tstate, PyExc_TypeError,
5467 "%U got multiple values for keyword argument '%S'",
5468 funcstr, key);
5469 Py_DECREF(funcstr);
5470 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005471 Py_XDECREF(exc);
5472 Py_XDECREF(val);
5473 Py_XDECREF(tb);
5474 }
5475 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005476 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005477 }
5478 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005479}
5480
Guido van Rossumac7be682001-01-17 15:42:30 +00005481static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005482format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5483 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 if (!obj)
5488 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005489
Serhiy Storchaka06515832016-11-20 09:13:07 +02005490 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005491 if (!obj_str)
5492 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005493
Victor Stinner438a12d2019-05-24 17:01:38 +02005494 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005495}
Guido van Rossum950361c1997-01-24 13:49:28 +00005496
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005497static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005498format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005499{
5500 PyObject *name;
5501 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005502 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005503 return;
5504 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5505 name = PyTuple_GET_ITEM(co->co_cellvars,
5506 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005507 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005508 PyExc_UnboundLocalError,
5509 UNBOUNDLOCAL_ERROR_MSG,
5510 name);
5511 } else {
5512 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5513 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005514 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005515 UNBOUNDFREE_ERROR_MSG, name);
5516 }
5517}
5518
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005519static void
Mark Shannonfee55262019-11-21 09:11:43 +00005520format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005521{
5522 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5523 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005524 _PyErr_Format(tstate, PyExc_TypeError,
5525 "'async with' received an object from __aenter__ "
5526 "that does not implement __await__: %.100s",
5527 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005528 }
Mark Shannonfee55262019-11-21 09:11:43 +00005529 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005530 _PyErr_Format(tstate, PyExc_TypeError,
5531 "'async with' received an object from __aexit__ "
5532 "that does not implement __await__: %.100s",
5533 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005534 }
5535 }
5536}
5537
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005538static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005539unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005540 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005541{
5542 PyObject *res;
5543 if (Py_REFCNT(v) == 2) {
5544 /* In the common case, there are 2 references to the value
5545 * stored in 'variable' when the += is performed: one on the
5546 * value stack (in 'v') and one still stored in the
5547 * 'variable'. We try to delete the variable now to reduce
5548 * the refcnt to 1.
5549 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005550 int opcode, oparg;
5551 NEXTOPARG();
5552 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005553 case STORE_FAST:
5554 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005555 PyObject **fastlocals = f->f_localsplus;
5556 if (GETLOCAL(oparg) == v)
5557 SETLOCAL(oparg, NULL);
5558 break;
5559 }
5560 case STORE_DEREF:
5561 {
5562 PyObject **freevars = (f->f_localsplus +
5563 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005564 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005565 if (PyCell_GET(c) == v) {
5566 PyCell_SET(c, NULL);
5567 Py_DECREF(v);
5568 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005569 break;
5570 }
5571 case STORE_NAME:
5572 {
5573 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005574 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005575 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005576 if (locals && PyDict_CheckExact(locals)) {
5577 PyObject *w = PyDict_GetItemWithError(locals, name);
5578 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005579 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005580 {
5581 Py_DECREF(v);
5582 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005583 }
5584 }
5585 break;
5586 }
5587 }
5588 }
5589 res = v;
5590 PyUnicode_Append(&res, w);
5591 return res;
5592}
5593
Guido van Rossum950361c1997-01-24 13:49:28 +00005594#ifdef DYNAMIC_EXECUTION_PROFILE
5595
Skip Montanarof118cb12001-10-15 20:51:38 +00005596static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005597getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 int i;
5600 PyObject *l = PyList_New(256);
5601 if (l == NULL) return NULL;
5602 for (i = 0; i < 256; i++) {
5603 PyObject *x = PyLong_FromLong(a[i]);
5604 if (x == NULL) {
5605 Py_DECREF(l);
5606 return NULL;
5607 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005608 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 }
5610 for (i = 0; i < 256; i++)
5611 a[i] = 0;
5612 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005613}
5614
5615PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005616_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005617{
5618#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005619 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005620#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005621 int i;
5622 PyObject *l = PyList_New(257);
5623 if (l == NULL) return NULL;
5624 for (i = 0; i < 257; i++) {
5625 PyObject *x = getarray(dxpairs[i]);
5626 if (x == NULL) {
5627 Py_DECREF(l);
5628 return NULL;
5629 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005630 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005631 }
5632 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005633#endif
5634}
5635
5636#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005637
5638Py_ssize_t
5639_PyEval_RequestCodeExtraIndex(freefunc free)
5640{
Victor Stinner81a7be32020-04-14 15:14:01 +02005641 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005642 Py_ssize_t new_index;
5643
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005644 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005645 return -1;
5646 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005647 new_index = interp->co_extra_user_count++;
5648 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005649 return new_index;
5650}
Łukasz Langaa785c872016-09-09 17:37:37 -07005651
5652static void
5653dtrace_function_entry(PyFrameObject *f)
5654{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005655 const char *filename;
5656 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005657 int lineno;
5658
Victor Stinner6d86a232020-04-29 00:56:58 +02005659 PyCodeObject *code = f->f_code;
5660 filename = PyUnicode_AsUTF8(code->co_filename);
5661 funcname = PyUnicode_AsUTF8(code->co_name);
5662 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005663
Andy Lestere6be9b52020-02-11 20:28:35 -06005664 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005665}
5666
5667static void
5668dtrace_function_return(PyFrameObject *f)
5669{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005670 const char *filename;
5671 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005672 int lineno;
5673
Victor Stinner6d86a232020-04-29 00:56:58 +02005674 PyCodeObject *code = f->f_code;
5675 filename = PyUnicode_AsUTF8(code->co_filename);
5676 funcname = PyUnicode_AsUTF8(code->co_name);
5677 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005678
Andy Lestere6be9b52020-02-11 20:28:35 -06005679 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005680}
5681
5682/* DTrace equivalent of maybe_call_line_trace. */
5683static void
5684maybe_dtrace_line(PyFrameObject *frame,
5685 int *instr_lb, int *instr_ub, int *instr_prev)
5686{
5687 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005688 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005689
5690 /* If the last instruction executed isn't in the current
5691 instruction window, reset the window.
5692 */
5693 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5694 PyAddrPair bounds;
5695 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5696 &bounds);
5697 *instr_lb = bounds.ap_lower;
5698 *instr_ub = bounds.ap_upper;
5699 }
5700 /* If the last instruction falls at the start of a line or if
5701 it represents a jump backwards, update the frame's line
5702 number and call the trace function. */
5703 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5704 frame->f_lineno = line;
5705 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5706 if (!co_filename)
5707 co_filename = "?";
5708 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5709 if (!co_name)
5710 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005711 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005712 }
5713 *instr_prev = frame->f_lasti;
5714}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005715
5716
5717/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5718 for the limited API. */
5719
5720#undef Py_EnterRecursiveCall
5721
5722int Py_EnterRecursiveCall(const char *where)
5723{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005724 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005725}
5726
5727#undef Py_LeaveRecursiveCall
5728
5729void Py_LeaveRecursiveCall(void)
5730{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005731 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005732}