blob: 01dd361e5035f4b7710ab1494c73c06eb393e799 [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 Stinner4d231bc2019-11-14 13:36:21 +010014#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020015#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090016#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010017#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010018#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020019#include "pycore_pyerrors.h"
20#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner1c1e68c2020-03-27 15:11:45 +010023#include "pycore_sysmodule.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010024#include "pycore_tupleobject.h"
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
436 * which are not running in the child process, and clear internal locks
437 * which might be held by those threads.
438 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000439
440void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200441_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000442{
Victor Stinner7be4e352020-05-05 20:27:47 +0200443 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200444 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner7be4e352020-05-05 20:27:47 +0200445
446#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
447 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
448#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100449 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200450#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100451 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200453 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100454 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100455
456 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700457
Victor Stinner50e6e992020-03-19 02:41:21 +0100458 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900459 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700460 Py_FatalError("Can't initialize threads for pending calls");
461 }
Jesse Nollera8513972008-07-17 16:49:17 +0000462
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200463 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100464 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000465}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900466#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000467
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000468/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600469 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000470
471void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100472_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000473{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200474 assert(is_tstate_valid(tstate));
475 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000476}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000477
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000478PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000479PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000480{
Victor Stinner09532fe2019-05-10 23:39:09 +0200481 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200482#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
483 PyThreadState *old_tstate = _PyThreadState_GET();
484 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
485#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200486 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200487#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200488 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100489
Victor Stinner0b1e3302020-05-05 16:14:31 +0200490 struct _ceval_runtime_state *ceval = &runtime->ceval;
491 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200492#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
493 assert(gil_created(&ceval2->gil));
494#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200495 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200496#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200497 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000499}
500
501void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000502PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000503{
Victor Stinner3026cad2020-06-01 16:02:40 +0200504 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100505
Victor Stinner85f5a692020-03-09 22:12:04 +0100506 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100507
Victor Stinner85f5a692020-03-09 22:12:04 +0100508 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
509 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000510}
511
512
Guido van Rossuma9672091994-09-14 13:31:22 +0000513/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
514 signal handlers or Mac I/O completion routines) can schedule calls
515 to a function to be called synchronously.
516 The synchronous function is called with one void* argument.
517 It should return 0 for success or -1 for failure -- failure should
518 be accompanied by an exception.
519
520 If registry succeeds, the registry function returns 0; if it fails
521 (e.g. due to too many pending calls) it returns -1 (without setting
522 an exception condition).
523
524 Note that because registry may occur from within signal handlers,
525 or other asynchronous events, calling malloc() is unsafe!
526
Guido van Rossuma9672091994-09-14 13:31:22 +0000527 Any thread can schedule pending calls, but only the main thread
528 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000529 There is no facility to schedule calls to a particular thread, but
530 that should be easy to change, should that ever be required. In
531 that case, the static variables here should go into the python
532 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000533*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000534
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200535void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200536_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200537{
538 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100539 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200540 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200541 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200542}
543
Eric Snow5be45a62019-03-08 22:47:07 -0700544/* Push one item onto the queue while holding the lock. */
545static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200546_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600547 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700548{
Eric Snow842a2f02019-03-15 15:47:51 -0600549 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700550 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600551 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700552 return -1; /* Queue full */
553 }
Eric Snow842a2f02019-03-15 15:47:51 -0600554 pending->calls[i].func = func;
555 pending->calls[i].arg = arg;
556 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700557 return 0;
558}
559
560/* Pop one item off the queue while holding the lock. */
561static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200562_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600563 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700564{
Eric Snow842a2f02019-03-15 15:47:51 -0600565 int i = pending->first;
566 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700567 return; /* Queue empty */
568 }
569
Eric Snow842a2f02019-03-15 15:47:51 -0600570 *func = pending->calls[i].func;
571 *arg = pending->calls[i].arg;
572 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700573}
574
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200575/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000576 scheduling to be made from any thread, and even from an executing
577 callback.
578 */
579
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000580int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200581_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200582 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000583{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200584 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600585
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200586 /* Ensure that _PyEval_InitPendingCalls() was called
587 and that _PyEval_FiniPendingCalls() is not called yet. */
588 assert(pending->lock != NULL);
589
Eric Snow842a2f02019-03-15 15:47:51 -0600590 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200591 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600592 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700593
Victor Stinnere225beb2019-06-03 18:14:24 +0200594 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200595 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597}
598
Victor Stinner09532fe2019-05-10 23:39:09 +0200599int
600Py_AddPendingCall(int (*func)(void *), void *arg)
601{
Victor Stinner50e6e992020-03-19 02:41:21 +0100602 /* Best-effort to support subinterpreters and calls with the GIL released.
603
604 First attempt _PyThreadState_GET() since it supports subinterpreters.
605
606 If the GIL is released, _PyThreadState_GET() returns NULL . In this
607 case, use PyGILState_GetThisThreadState() which works even if the GIL
608 is released.
609
610 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
611 see bpo-10915 and bpo-15751.
612
Victor Stinner8849e592020-03-18 19:28:53 +0100613 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100614 PyThreadState *tstate = _PyThreadState_GET();
615 if (tstate == NULL) {
616 tstate = PyGILState_GetThisThreadState();
617 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200618
619 PyInterpreterState *interp;
620 if (tstate != NULL) {
621 interp = tstate->interp;
622 }
623 else {
624 /* Last resort: use the main interpreter */
625 interp = _PyRuntime.interpreters.main;
626 }
627 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200628}
629
Eric Snowfdf282d2019-01-11 14:26:55 -0700630static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100631handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700632{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200633 assert(is_tstate_valid(tstate));
634 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700635 return 0;
636 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700637
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200638 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100639 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
640 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200641 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700642 return -1;
643 }
644 return 0;
645}
646
647static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100648make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000649{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200650 assert(is_tstate_valid(tstate));
651
Victor Stinnerd8316882020-03-20 14:50:35 +0100652 /* only execute pending calls on main thread */
653 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200654 return 0;
655 }
656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100658 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700659 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700661 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200662 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100663
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200664 /* unsignal before starting to call callbacks, so that any callback
665 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200666 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700667 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100670 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700671 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700672 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 void *arg = NULL;
674
675 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600676 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200677 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600678 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700679
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100680 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700681 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100682 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700683 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700684 res = func(arg);
685 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200686 goto error;
687 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200689
Charles-François Natalif23339a2011-07-23 18:15:43 +0200690 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700691 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200692
693error:
694 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200695 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700696 return res;
697}
698
Eric Snow842a2f02019-03-15 15:47:51 -0600699void
Victor Stinner2b1df452020-01-13 18:46:59 +0100700_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600701{
Eric Snow842a2f02019-03-15 15:47:51 -0600702 assert(PyGILState_Check());
703
Victor Stinner50e6e992020-03-19 02:41:21 +0100704 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200705
Eric Snow842a2f02019-03-15 15:47:51 -0600706 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
707 return;
708 }
709
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100710 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200711 PyObject *exc, *val, *tb;
712 _PyErr_Fetch(tstate, &exc, &val, &tb);
713 PyErr_BadInternalCall();
714 _PyErr_ChainExceptions(exc, val, tb);
715 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600716 }
717}
718
Eric Snowfdf282d2019-01-11 14:26:55 -0700719/* Py_MakePendingCalls() is a simple wrapper for the sake
720 of backward-compatibility. */
721int
722Py_MakePendingCalls(void)
723{
724 assert(PyGILState_Check());
725
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100726 PyThreadState *tstate = _PyThreadState_GET();
727
Eric Snowfdf282d2019-01-11 14:26:55 -0700728 /* Python signal handler doesn't really queue a callback: it only signals
729 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100730 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700731 if (res != 0) {
732 return res;
733 }
734
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100735 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600736 if (res != 0) {
737 return res;
738 }
739
740 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000741}
742
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000743/* The interpreter's recursion limit */
744
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000745#ifndef Py_DEFAULT_RECURSION_LIMIT
746#define Py_DEFAULT_RECURSION_LIMIT 1000
747#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600748
Eric Snow05351c12017-09-05 21:43:08 -0700749int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000750
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600751void
Victor Stinnerdab84232020-03-17 18:56:44 +0100752_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600753{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600754 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinner7be4e352020-05-05 20:27:47 +0200755#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100756 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200757#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100758}
759
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200760int
Victor Stinnerdab84232020-03-17 18:56:44 +0100761_PyEval_InitState(struct _ceval_state *ceval)
762{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200763 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
764
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200765 struct _pending_calls *pending = &ceval->pending;
766 assert(pending->lock == NULL);
767
768 pending->lock = PyThread_allocate_lock();
769 if (pending->lock == NULL) {
770 return -1;
771 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200772
773#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
774 _gil_initialize(&ceval->gil);
775#endif
776
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200777 return 0;
778}
779
780void
781_PyEval_FiniState(struct _ceval_state *ceval)
782{
783 struct _pending_calls *pending = &ceval->pending;
784 if (pending->lock != NULL) {
785 PyThread_free_lock(pending->lock);
786 pending->lock = NULL;
787 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600788}
789
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000790int
791Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000792{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200793 PyThreadState *tstate = _PyThreadState_GET();
794 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000795}
796
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000797void
798Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000799{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200800 PyThreadState *tstate = _PyThreadState_GET();
801 tstate->interp->ceval.recursion_limit = new_limit;
802 if (_Py_IsMainInterpreter(tstate)) {
803 _Py_CheckRecursionLimit = new_limit;
804 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000805}
806
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100807/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000808 if the recursion_depth reaches _Py_CheckRecursionLimit.
809 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
810 to guarantee that _Py_CheckRecursiveCall() is regularly called.
811 Without USE_STACKCHECK, there is no need for this. */
812int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100813_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000814{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200815 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000816
817#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700818 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (PyOS_CheckStack()) {
820 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200821 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 return -1;
823 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200824 if (_Py_IsMainInterpreter(tstate)) {
825 /* Needed for ABI backwards-compatibility (see bpo-31857) */
826 _Py_CheckRecursionLimit = recursion_limit;
827 }
pdox18967932017-10-25 23:03:01 -0700828#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (tstate->recursion_critical)
830 /* Somebody asked that we don't check for recursion. */
831 return 0;
832 if (tstate->overflowed) {
833 if (tstate->recursion_depth > recursion_limit + 50) {
834 /* Overflowing while handling an overflow. Give up. */
835 Py_FatalError("Cannot recover from stack overflow.");
836 }
837 return 0;
838 }
839 if (tstate->recursion_depth > recursion_limit) {
840 --tstate->recursion_depth;
841 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200842 _PyErr_Format(tstate, PyExc_RecursionError,
843 "maximum recursion depth exceeded%s",
844 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 return -1;
846 }
847 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000848}
849
Victor Stinner09532fe2019-05-10 23:39:09 +0200850static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200851static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000852
Victor Stinnere225beb2019-06-03 18:14:24 +0200853#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855
Guido van Rossumb209a111997-04-29 18:18:01 +0000856PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000857PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 return PyEval_EvalCodeEx(co,
860 globals, locals,
861 (PyObject **)NULL, 0,
862 (PyObject **)NULL, 0,
863 (PyObject **)NULL, 0,
864 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000865}
866
867
868/* Interpreter main loop */
869
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000870PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100871PyEval_EvalFrame(PyFrameObject *f)
872{
Victor Stinner0b72b232020-03-12 23:18:39 +0100873 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100874 PyThreadState *tstate = _PyThreadState_GET();
875 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000876}
877
878PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000880{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100881 PyThreadState *tstate = _PyThreadState_GET();
882 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700883}
884
Victor Stinnerda2914d2020-03-20 09:29:08 +0100885
886/* Handle signals, pending calls, GIL drop request
887 and asynchronous exception */
888static int
889eval_frame_handle_pending(PyThreadState *tstate)
890{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100891 _PyRuntimeState * const runtime = &_PyRuntime;
892 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200893
894 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200895 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100896 if (handle_signals(tstate) != 0) {
897 return -1;
898 }
899 }
900
901 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200902 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100903 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
904 if (make_pending_calls(tstate) != 0) {
905 return -1;
906 }
907 }
908
909 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200910 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100911 /* Give another thread a chance */
912 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
913 Py_FatalError("tstate mix-up");
914 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200915 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100916
917 /* Other threads may run now */
918
919 take_gil(tstate);
920
Victor Stinnere838a932020-05-05 19:56:48 +0200921#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
922 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
923#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100924 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
925 Py_FatalError("orphan tstate");
926 }
Victor Stinnere838a932020-05-05 19:56:48 +0200927#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100928 }
929
930 /* Check for asynchronous exception. */
931 if (tstate->async_exc != NULL) {
932 PyObject *exc = tstate->async_exc;
933 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200934 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100935 _PyErr_SetNone(tstate, exc);
936 Py_DECREF(exc);
937 return -1;
938 }
939
940 return 0;
941}
942
Victor Stinnerc6944e72016-11-11 02:13:35 +0100943PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100944_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700945{
Victor Stinner3026cad2020-06-01 16:02:40 +0200946 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100947
Guido van Rossum950361c1997-01-24 13:49:28 +0000948#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000950#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200951 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300952 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200953 int opcode; /* Current opcode */
954 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200955 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100957 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100958 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 is true when the line being executed has changed. The
966 initial values are such as to make this false the first
967 time it is tested. */
968 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000969
Serhiy Storchakaab874002016-09-11 13:48:15 +0300970 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject *names;
972 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900973 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000974
Brett Cannon368b4b72012-04-02 12:17:59 -0400975#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200976 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400977#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200978
Antoine Pitroub52ec782009-01-25 16:34:23 +0000979/* Computed GOTOs, or
980 the-optimization-commonly-but-improperly-known-as-"threaded code"
981 using gcc's labels-as-values extension
982 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
983
984 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000986 combined with a lookup table of jump addresses. However, since the
987 indirect jump instruction is shared by all opcodes, the CPU will have a
988 hard time making the right prediction for where to jump next (actually,
989 it will be always wrong except in the uncommon case of a sequence of
990 several identical opcodes).
991
992 "Threaded code" in contrast, uses an explicit jump table and an explicit
993 indirect jump instruction at the end of each opcode. Since the jump
994 instruction is at a different address for each opcode, the CPU will make a
995 separate prediction for each of these instructions, which is equivalent to
996 predicting the second opcode of each opcode pair. These predictions have
997 a much better chance to turn out valid, especially in small bytecode loops.
998
999 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001001 and potentially many more instructions (depending on the pipeline width).
1002 A correctly predicted branch, however, is nearly free.
1003
1004 At the time of this writing, the "threaded code" version is up to 15-20%
1005 faster than the normal "switch" version, depending on the compiler and the
1006 CPU architecture.
1007
1008 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1009 because it would render the measurements invalid.
1010
1011
1012 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1013 indirect jumps by sharing them between all opcodes. Such optimizations
1014 can be disabled on gcc by using the -fno-gcse flag (or possibly
1015 -fno-crossjumping).
1016*/
1017
Antoine Pitrou042b1282010-08-13 21:15:58 +00001018#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001020#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021#endif
1022
Antoine Pitrou042b1282010-08-13 21:15:58 +00001023#ifdef HAVE_COMPUTED_GOTOS
1024 #ifndef USE_COMPUTED_GOTOS
1025 #define USE_COMPUTED_GOTOS 1
1026 #endif
1027#else
1028 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1029 #error "Computed gotos are not supported on this compiler."
1030 #endif
1031 #undef USE_COMPUTED_GOTOS
1032 #define USE_COMPUTED_GOTOS 0
1033#endif
1034
1035#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001036/* Import the static jump table */
1037#include "opcode_targets.h"
1038
Antoine Pitroub52ec782009-01-25 16:34:23 +00001039#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001040 op: \
1041 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001042
Antoine Pitroub52ec782009-01-25 16:34:23 +00001043#ifdef LLTRACE
1044#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001046 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001048 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001049 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 } \
1051 goto fast_next_opcode; \
1052 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001053#else
1054#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001056 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001058 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001059 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 } \
1061 goto fast_next_opcode; \
1062 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001063#endif
1064
Victor Stinner09532fe2019-05-10 23:39:09 +02001065#define DISPATCH() \
1066 { \
1067 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1068 FAST_DISPATCH(); \
1069 } \
1070 continue; \
1071 }
1072
Antoine Pitroub52ec782009-01-25 16:34:23 +00001073#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001074#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001075#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001076#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001077#endif
1078
1079
Neal Norwitza81d2202002-07-14 00:27:26 +00001080/* Tuple access macros */
1081
1082#ifndef Py_DEBUG
1083#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1084#else
1085#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1086#endif
1087
Guido van Rossum374a9221991-04-04 10:40:29 +00001088/* Code access macros */
1089
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001090/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001091#define INSTR_OFFSET() \
1092 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001093#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001094 _Py_CODEUNIT word = *next_instr; \
1095 opcode = _Py_OPCODE(word); \
1096 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001097 next_instr++; \
1098 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001099#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1100#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001101
Raymond Hettingerf606f872003-03-16 03:11:04 +00001102/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 Some opcodes tend to come in pairs thus making it possible to
1104 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001105 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 Verifying the prediction costs a single high-speed test of a register
1108 variable against a constant. If the pairing was good, then the
1109 processor's own internal branch predication has a high likelihood of
1110 success, resulting in a nearly zero-overhead transition to the
1111 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001112 including its unpredictable switch-case branch. Combined with the
1113 processor's internal branch prediction, a successful PREDICT has the
1114 effect of making the two opcodes run as if they were a single new opcode
1115 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001116
Georg Brandl86b2fb92008-07-16 03:43:04 +00001117 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 predictions turned-on and interpret the results as if some opcodes
1119 had been combined or turn-off predictions so that the opcode frequency
1120 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001121
1122 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 the CPU to record separate branch prediction information for each
1124 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001125
Raymond Hettingerf606f872003-03-16 03:11:04 +00001126*/
1127
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001128#define PREDICT_ID(op) PRED_##op
1129
Antoine Pitrou042b1282010-08-13 21:15:58 +00001130#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001131#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001132#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001133#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001134 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001135 _Py_CODEUNIT word = *next_instr; \
1136 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001137 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001138 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001139 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001140 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001141 } \
1142 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001143#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001144#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001145
Raymond Hettingerf606f872003-03-16 03:11:04 +00001146
Guido van Rossum374a9221991-04-04 10:40:29 +00001147/* Stack manipulation macros */
1148
Martin v. Löwis18e16552006-02-15 17:27:45 +00001149/* The stack can grow at most MAXINT deep, as co_nlocals and
1150 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001151#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1152#define EMPTY() (STACK_LEVEL() == 0)
1153#define TOP() (stack_pointer[-1])
1154#define SECOND() (stack_pointer[-2])
1155#define THIRD() (stack_pointer[-3])
1156#define FOURTH() (stack_pointer[-4])
1157#define PEEK(n) (stack_pointer[-(n)])
1158#define SET_TOP(v) (stack_pointer[-1] = (v))
1159#define SET_SECOND(v) (stack_pointer[-2] = (v))
1160#define SET_THIRD(v) (stack_pointer[-3] = (v))
1161#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1162#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1163#define BASIC_STACKADJ(n) (stack_pointer += n)
1164#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1165#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001166
Guido van Rossum96a42c81992-01-12 02:29:51 +00001167#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001169 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001170 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001171#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001172 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001173#define STACK_GROW(n) do { \
1174 assert(n >= 0); \
1175 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001176 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001177 assert(STACK_LEVEL() <= co->co_stacksize); \
1178 } while (0)
1179#define STACK_SHRINK(n) do { \
1180 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001181 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001182 (void)(BASIC_STACKADJ(-n)); \
1183 assert(STACK_LEVEL() <= co->co_stacksize); \
1184 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001185#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001186 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001187 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001188#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001189#define PUSH(v) BASIC_PUSH(v)
1190#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001191#define STACK_GROW(n) BASIC_STACKADJ(n)
1192#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001193#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001194#endif
1195
Guido van Rossum681d79a1995-07-18 14:51:37 +00001196/* Local variable macros */
1197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001199
1200/* The SETLOCAL() macro must not DECREF the local variable in-place and
1201 then store the new value; it must copy the old value to a temporary
1202 value, then store the new value, and then DECREF the temporary value.
1203 This is because it is possible that during the DECREF the frame is
1204 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1205 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001207 GETLOCAL(i) = value; \
1208 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001209
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001210
1211#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 while (STACK_LEVEL() > (b)->b_level) { \
1213 PyObject *v = POP(); \
1214 Py_XDECREF(v); \
1215 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001216
1217#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001218 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001220 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1222 while (STACK_LEVEL() > (b)->b_level + 3) { \
1223 value = POP(); \
1224 Py_XDECREF(value); \
1225 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001226 exc_info = tstate->exc_info; \
1227 type = exc_info->exc_type; \
1228 value = exc_info->exc_value; \
1229 traceback = exc_info->exc_traceback; \
1230 exc_info->exc_type = POP(); \
1231 exc_info->exc_value = POP(); \
1232 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_XDECREF(type); \
1234 Py_XDECREF(value); \
1235 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001236 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001237
Inada Naoki91234a12019-06-03 21:30:58 +09001238 /* macros for opcode cache */
1239#define OPCACHE_CHECK() \
1240 do { \
1241 co_opcache = NULL; \
1242 if (co->co_opcache != NULL) { \
1243 unsigned char co_opt_offset = \
1244 co->co_opcache_map[next_instr - first_instr]; \
1245 if (co_opt_offset > 0) { \
1246 assert(co_opt_offset <= co->co_opcache_size); \
1247 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1248 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001249 } \
1250 } \
1251 } while (0)
1252
1253#if OPCACHE_STATS
1254
1255#define OPCACHE_STAT_GLOBAL_HIT() \
1256 do { \
1257 if (co->co_opcache != NULL) opcache_global_hits++; \
1258 } while (0)
1259
1260#define OPCACHE_STAT_GLOBAL_MISS() \
1261 do { \
1262 if (co->co_opcache != NULL) opcache_global_misses++; \
1263 } while (0)
1264
1265#define OPCACHE_STAT_GLOBAL_OPT() \
1266 do { \
1267 if (co->co_opcache != NULL) opcache_global_opts++; \
1268 } while (0)
1269
1270#else /* OPCACHE_STATS */
1271
1272#define OPCACHE_STAT_GLOBAL_HIT()
1273#define OPCACHE_STAT_GLOBAL_MISS()
1274#define OPCACHE_STAT_GLOBAL_OPT()
1275
1276#endif
1277
Guido van Rossuma027efa1997-05-05 20:56:21 +00001278/* Start of code */
1279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001281 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001283 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (tstate->use_tracing) {
1288 if (tstate->c_tracefunc != NULL) {
1289 /* tstate->c_tracefunc, if defined, is a
1290 function that will be called on *every* entry
1291 to a code block. Its return value, if not
1292 None, is a function that will be called at
1293 the start of each executed line of code.
1294 (Actually, the function must return itself
1295 in order to continue tracing.) The trace
1296 functions are called with three arguments:
1297 a pointer to the current frame, a string
1298 indicating why the function is called, and
1299 an argument which depends on the situation.
1300 The global trace function is also called
1301 whenever an exception is detected. */
1302 if (call_trace_protected(tstate->c_tracefunc,
1303 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001304 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* Trace function raised an error */
1306 goto exit_eval_frame;
1307 }
1308 }
1309 if (tstate->c_profilefunc != NULL) {
1310 /* Similar for c_profilefunc, except it needn't
1311 return itself and isn't called for "line" events */
1312 if (call_trace_protected(tstate->c_profilefunc,
1313 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001314 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /* Profile function raised an error */
1316 goto exit_eval_frame;
1317 }
1318 }
1319 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001320
Łukasz Langaa785c872016-09-09 17:37:37 -07001321 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1322 dtrace_function_entry(f);
1323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 co = f->f_code;
1325 names = co->co_names;
1326 consts = co->co_consts;
1327 fastlocals = f->f_localsplus;
1328 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001329 assert(PyBytes_Check(co->co_code));
1330 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001331 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1332 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1333 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001334 /*
1335 f->f_lasti refers to the index of the last instruction,
1336 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001337
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001338 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001339 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 When the PREDICT() macros are enabled, some opcode pairs follow in
1342 direct succession without updating f->f_lasti. A successful
1343 prediction effectively links the two codes together as if they
1344 were a single new opcode; accordingly,f->f_lasti will point to
1345 the first code in the pair (for instance, GET_ITER followed by
1346 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001347 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001349 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001350 next_instr = first_instr;
1351 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001352 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1353 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 stack_pointer = f->f_stacktop;
1356 assert(stack_pointer != NULL);
1357 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001358 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001359
Inada Naoki91234a12019-06-03 21:30:58 +09001360 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1361 co->co_opcache_flag++;
1362 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1363 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001364 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001365 }
1366#if OPCACHE_STATS
1367 opcache_code_objects_extra_mem +=
1368 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1369 sizeof(_PyOpcache) * co->co_opcache_size;
1370 opcache_code_objects++;
1371#endif
1372 }
1373 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001374
Tim Peters5ca576e2001-06-18 22:08:13 +00001375#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001376 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001377#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 if (throwflag) /* support for generator.throw() */
1380 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001381
Victor Stinnerace47d72013-07-18 01:41:08 +02001382#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001383 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001384 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001385 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001386 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001387#endif
1388
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001389main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1392 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001393 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* Do periodic things. Doing this every time through
1396 the loop would add too much overhead, so we do it
1397 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001398 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 event needs attention (e.g. a signal handler or
1400 async I/O handler); see Py_AddPendingCall() and
1401 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Eric Snow7bda9de2019-03-08 17:25:54 -07001403 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001404 opcode = _Py_OPCODE(*next_instr);
1405 if (opcode == SETUP_FINALLY ||
1406 opcode == SETUP_WITH ||
1407 opcode == BEFORE_ASYNC_WITH ||
1408 opcode == YIELD_FROM) {
1409 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001410 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001411 - If we're about to enter the 'with:'. It will prevent
1412 emitting a resource warning in the common idiom
1413 'with open(path) as file:'.
1414 - If we're about to enter the 'async with:'.
1415 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001416 *very* useful, but might help in some cases and it's
1417 traditional)
1418 - If we're resuming a chain of nested 'yield from' or
1419 'await' calls, then each frame is parked with YIELD_FROM
1420 as its next opcode. If the user hit control-C we want to
1421 wait until we've reached the innermost frame before
1422 running the signal handler and raising KeyboardInterrupt
1423 (see bpo-30039).
1424 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 goto fast_next_opcode;
1426 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001427
Victor Stinnerda2914d2020-03-20 09:29:08 +01001428 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 }
1431 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 fast_next_opcode:
1434 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Łukasz Langaa785c872016-09-09 17:37:37 -07001436 if (PyDTrace_LINE_ENABLED())
1437 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001440
Victor Stinnerdab84232020-03-17 18:56:44 +01001441 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001442 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001443 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 /* see maybe_call_line_trace
1445 for expository comments */
1446 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 err = maybe_call_line_trace(tstate->c_tracefunc,
1449 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001450 tstate, f,
1451 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 /* Reload possibly changed frame fields */
1453 JUMPTO(f->f_lasti);
1454 if (f->f_stacktop != NULL) {
1455 stack_pointer = f->f_stacktop;
1456 f->f_stacktop = NULL;
1457 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001464
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001465 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001466 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001467#ifdef DYNAMIC_EXECUTION_PROFILE
1468#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 dxpairs[lastopcode][opcode]++;
1470 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001471#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001473#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001474
Guido van Rossum96a42c81992-01-12 02:29:51 +00001475#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (lltrace) {
1479 if (HAS_ARG(opcode)) {
1480 printf("%d: %d, %d\n",
1481 f->f_lasti, opcode, oparg);
1482 }
1483 else {
1484 printf("%d: %d\n",
1485 f->f_lasti, opcode);
1486 }
1487 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001488#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001493 It is essential that any operation that fails must goto error
1494 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *value = GETLOCAL(oparg);
1502 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001503 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 UNBOUNDLOCAL_ERROR_MSG,
1505 PyTuple_GetItem(co->co_varnames, oparg));
1506 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 Py_INCREF(value);
1509 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 }
1512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(LOAD_CONST): {
1514 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 PyObject *value = GETITEM(consts, oparg);
1516 Py_INCREF(value);
1517 PUSH(value);
1518 FAST_DISPATCH();
1519 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(STORE_FAST): {
1522 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *value = POP();
1524 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *value = POP();
1530 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Benjamin Petersonddd19492018-09-16 22:38:02 -07001534 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 PyObject *top = TOP();
1536 PyObject *second = SECOND();
1537 SET_TOP(second);
1538 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *top = TOP();
1544 PyObject *second = SECOND();
1545 PyObject *third = THIRD();
1546 SET_TOP(second);
1547 SET_SECOND(third);
1548 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001551
Benjamin Petersonddd19492018-09-16 22:38:02 -07001552 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001553 PyObject *top = TOP();
1554 PyObject *second = SECOND();
1555 PyObject *third = THIRD();
1556 PyObject *fourth = FOURTH();
1557 SET_TOP(second);
1558 SET_SECOND(third);
1559 SET_THIRD(fourth);
1560 SET_FOURTH(top);
1561 FAST_DISPATCH();
1562 }
1563
Benjamin Petersonddd19492018-09-16 22:38:02 -07001564 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 PyObject *top = TOP();
1566 Py_INCREF(top);
1567 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Benjamin Petersonddd19492018-09-16 22:38:02 -07001571 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001574 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001576 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 SET_TOP(top);
1578 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001579 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *value = TOP();
1584 PyObject *res = PyNumber_Positive(value);
1585 Py_DECREF(value);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *value = TOP();
1594 PyObject *res = PyNumber_Negative(value);
1595 Py_DECREF(value);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *value = TOP();
1604 int err = PyObject_IsTrue(value);
1605 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (err == 0) {
1607 Py_INCREF(Py_True);
1608 SET_TOP(Py_True);
1609 DISPATCH();
1610 }
1611 else if (err > 0) {
1612 Py_INCREF(Py_False);
1613 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 DISPATCH();
1615 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001616 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 goto error;
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *value = TOP();
1622 PyObject *res = PyNumber_Invert(value);
1623 Py_DECREF(value);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Benjamin Petersonddd19492018-09-16 22:38:02 -07001630 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 PyObject *exp = POP();
1632 PyObject *base = TOP();
1633 PyObject *res = PyNumber_Power(base, exp, Py_None);
1634 Py_DECREF(base);
1635 Py_DECREF(exp);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Benjamin Petersonddd19492018-09-16 22:38:02 -07001642 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Multiply(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Petersonddd19492018-09-16 22:38:02 -07001654 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_MatrixMultiply(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *divisor = POP();
1668 PyObject *dividend = TOP();
1669 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1670 Py_DECREF(dividend);
1671 Py_DECREF(divisor);
1672 SET_TOP(quotient);
1673 if (quotient == NULL)
1674 goto error;
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Petersonddd19492018-09-16 22:38:02 -07001678 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 PyObject *divisor = POP();
1680 PyObject *dividend = TOP();
1681 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1682 Py_DECREF(dividend);
1683 Py_DECREF(divisor);
1684 SET_TOP(quotient);
1685 if (quotient == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001689
Benjamin Petersonddd19492018-09-16 22:38:02 -07001690 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 PyObject *divisor = POP();
1692 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001693 PyObject *res;
1694 if (PyUnicode_CheckExact(dividend) && (
1695 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1696 // fast path; string formatting, but not if the RHS is a str subclass
1697 // (see issue28598)
1698 res = PyUnicode_Format(dividend, divisor);
1699 } else {
1700 res = PyNumber_Remainder(dividend, divisor);
1701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 Py_DECREF(divisor);
1703 Py_DECREF(dividend);
1704 SET_TOP(res);
1705 if (res == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *right = POP();
1712 PyObject *left = TOP();
1713 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001714 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1715 CPython using bytecode, it is simply worthless.
1716 See http://bugs.python.org/issue21955 and
1717 http://bugs.python.org/issue10044 for the discussion. In short,
1718 no patch shown any impact on a realistic benchmark, only a minor
1719 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 if (PyUnicode_CheckExact(left) &&
1721 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001722 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001723 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001724 }
1725 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 sum = PyNumber_Add(left, right);
1727 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001728 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 Py_DECREF(right);
1730 SET_TOP(sum);
1731 if (sum == NULL)
1732 goto error;
1733 DISPATCH();
1734 }
1735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *right = POP();
1738 PyObject *left = TOP();
1739 PyObject *diff = PyNumber_Subtract(left, right);
1740 Py_DECREF(right);
1741 Py_DECREF(left);
1742 SET_TOP(diff);
1743 if (diff == NULL)
1744 goto error;
1745 DISPATCH();
1746 }
1747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 PyObject *sub = POP();
1750 PyObject *container = TOP();
1751 PyObject *res = PyObject_GetItem(container, sub);
1752 Py_DECREF(container);
1753 Py_DECREF(sub);
1754 SET_TOP(res);
1755 if (res == NULL)
1756 goto error;
1757 DISPATCH();
1758 }
1759
Benjamin Petersonddd19492018-09-16 22:38:02 -07001760 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 PyObject *right = POP();
1762 PyObject *left = TOP();
1763 PyObject *res = PyNumber_Lshift(left, right);
1764 Py_DECREF(left);
1765 Py_DECREF(right);
1766 SET_TOP(res);
1767 if (res == NULL)
1768 goto error;
1769 DISPATCH();
1770 }
1771
Benjamin Petersonddd19492018-09-16 22:38:02 -07001772 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 PyObject *right = POP();
1774 PyObject *left = TOP();
1775 PyObject *res = PyNumber_Rshift(left, right);
1776 Py_DECREF(left);
1777 Py_DECREF(right);
1778 SET_TOP(res);
1779 if (res == NULL)
1780 goto error;
1781 DISPATCH();
1782 }
1783
Benjamin Petersonddd19492018-09-16 22:38:02 -07001784 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *right = POP();
1786 PyObject *left = TOP();
1787 PyObject *res = PyNumber_And(left, right);
1788 Py_DECREF(left);
1789 Py_DECREF(right);
1790 SET_TOP(res);
1791 if (res == NULL)
1792 goto error;
1793 DISPATCH();
1794 }
1795
Benjamin Petersonddd19492018-09-16 22:38:02 -07001796 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 PyObject *right = POP();
1798 PyObject *left = TOP();
1799 PyObject *res = PyNumber_Xor(left, right);
1800 Py_DECREF(left);
1801 Py_DECREF(right);
1802 SET_TOP(res);
1803 if (res == NULL)
1804 goto error;
1805 DISPATCH();
1806 }
1807
Benjamin Petersonddd19492018-09-16 22:38:02 -07001808 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 PyObject *right = POP();
1810 PyObject *left = TOP();
1811 PyObject *res = PyNumber_Or(left, right);
1812 Py_DECREF(left);
1813 Py_DECREF(right);
1814 SET_TOP(res);
1815 if (res == NULL)
1816 goto error;
1817 DISPATCH();
1818 }
1819
Benjamin Petersonddd19492018-09-16 22:38:02 -07001820 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 PyObject *v = POP();
1822 PyObject *list = PEEK(oparg);
1823 int err;
1824 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 if (err != 0)
1827 goto error;
1828 PREDICT(JUMP_ABSOLUTE);
1829 DISPATCH();
1830 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001831
Benjamin Petersonddd19492018-09-16 22:38:02 -07001832 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001834 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 int err;
1836 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 if (err != 0)
1839 goto error;
1840 PREDICT(JUMP_ABSOLUTE);
1841 DISPATCH();
1842 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Benjamin Petersonddd19492018-09-16 22:38:02 -07001844 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 PyObject *exp = POP();
1846 PyObject *base = TOP();
1847 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1848 Py_DECREF(base);
1849 Py_DECREF(exp);
1850 SET_TOP(res);
1851 if (res == NULL)
1852 goto error;
1853 DISPATCH();
1854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 PyObject *right = POP();
1858 PyObject *left = TOP();
1859 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1860 Py_DECREF(left);
1861 Py_DECREF(right);
1862 SET_TOP(res);
1863 if (res == NULL)
1864 goto error;
1865 DISPATCH();
1866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Benjamin Petersonddd19492018-09-16 22:38:02 -07001868 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001869 PyObject *right = POP();
1870 PyObject *left = TOP();
1871 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1872 Py_DECREF(left);
1873 Py_DECREF(right);
1874 SET_TOP(res);
1875 if (res == NULL)
1876 goto error;
1877 DISPATCH();
1878 }
1879
Benjamin Petersonddd19492018-09-16 22:38:02 -07001880 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 PyObject *divisor = POP();
1882 PyObject *dividend = TOP();
1883 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1884 Py_DECREF(dividend);
1885 Py_DECREF(divisor);
1886 SET_TOP(quotient);
1887 if (quotient == NULL)
1888 goto error;
1889 DISPATCH();
1890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *divisor = POP();
1894 PyObject *dividend = TOP();
1895 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1896 Py_DECREF(dividend);
1897 Py_DECREF(divisor);
1898 SET_TOP(quotient);
1899 if (quotient == NULL)
1900 goto error;
1901 DISPATCH();
1902 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001903
Benjamin Petersonddd19492018-09-16 22:38:02 -07001904 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 PyObject *right = POP();
1906 PyObject *left = TOP();
1907 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1908 Py_DECREF(left);
1909 Py_DECREF(right);
1910 SET_TOP(mod);
1911 if (mod == NULL)
1912 goto error;
1913 DISPATCH();
1914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 PyObject *right = POP();
1918 PyObject *left = TOP();
1919 PyObject *sum;
1920 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001921 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001922 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001923 }
1924 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 sum = PyNumber_InPlaceAdd(left, right);
1926 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 Py_DECREF(right);
1929 SET_TOP(sum);
1930 if (sum == NULL)
1931 goto error;
1932 DISPATCH();
1933 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Benjamin Petersonddd19492018-09-16 22:38:02 -07001935 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 PyObject *right = POP();
1937 PyObject *left = TOP();
1938 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1939 Py_DECREF(left);
1940 Py_DECREF(right);
1941 SET_TOP(diff);
1942 if (diff == NULL)
1943 goto error;
1944 DISPATCH();
1945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 PyObject *right = POP();
1949 PyObject *left = TOP();
1950 PyObject *res = PyNumber_InPlaceLshift(left, right);
1951 Py_DECREF(left);
1952 Py_DECREF(right);
1953 SET_TOP(res);
1954 if (res == NULL)
1955 goto error;
1956 DISPATCH();
1957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Benjamin Petersonddd19492018-09-16 22:38:02 -07001959 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PyObject *right = POP();
1961 PyObject *left = TOP();
1962 PyObject *res = PyNumber_InPlaceRshift(left, right);
1963 Py_DECREF(left);
1964 Py_DECREF(right);
1965 SET_TOP(res);
1966 if (res == NULL)
1967 goto error;
1968 DISPATCH();
1969 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001970
Benjamin Petersonddd19492018-09-16 22:38:02 -07001971 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 PyObject *right = POP();
1973 PyObject *left = TOP();
1974 PyObject *res = PyNumber_InPlaceAnd(left, right);
1975 Py_DECREF(left);
1976 Py_DECREF(right);
1977 SET_TOP(res);
1978 if (res == NULL)
1979 goto error;
1980 DISPATCH();
1981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Benjamin Petersonddd19492018-09-16 22:38:02 -07001983 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 PyObject *right = POP();
1985 PyObject *left = TOP();
1986 PyObject *res = PyNumber_InPlaceXor(left, right);
1987 Py_DECREF(left);
1988 Py_DECREF(right);
1989 SET_TOP(res);
1990 if (res == NULL)
1991 goto error;
1992 DISPATCH();
1993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Benjamin Petersonddd19492018-09-16 22:38:02 -07001995 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 PyObject *right = POP();
1997 PyObject *left = TOP();
1998 PyObject *res = PyNumber_InPlaceOr(left, right);
1999 Py_DECREF(left);
2000 Py_DECREF(right);
2001 SET_TOP(res);
2002 if (res == NULL)
2003 goto error;
2004 DISPATCH();
2005 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002006
Benjamin Petersonddd19492018-09-16 22:38:02 -07002007 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 PyObject *sub = TOP();
2009 PyObject *container = SECOND();
2010 PyObject *v = THIRD();
2011 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002012 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002013 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 Py_DECREF(container);
2017 Py_DECREF(sub);
2018 if (err != 0)
2019 goto error;
2020 DISPATCH();
2021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Benjamin Petersonddd19492018-09-16 22:38:02 -07002023 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 PyObject *sub = TOP();
2025 PyObject *container = SECOND();
2026 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002027 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002028 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 err = PyObject_DelItem(container, sub);
2030 Py_DECREF(container);
2031 Py_DECREF(sub);
2032 if (err != 0)
2033 goto error;
2034 DISPATCH();
2035 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002036
Benjamin Petersonddd19492018-09-16 22:38:02 -07002037 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002038 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002040 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002041 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002043 _PyErr_SetString(tstate, PyExc_RuntimeError,
2044 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 Py_DECREF(value);
2046 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002048 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 Py_DECREF(value);
2050 if (res == NULL)
2051 goto error;
2052 Py_DECREF(res);
2053 DISPATCH();
2054 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002055
Benjamin Petersonddd19492018-09-16 22:38:02 -07002056 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 switch (oparg) {
2059 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002061 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002064 /* fall through */
2065 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002066 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002067 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 break;
2070 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002071 _PyErr_SetString(tstate, PyExc_SystemError,
2072 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 break;
2074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 goto error;
2076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Benjamin Petersonddd19492018-09-16 22:38:02 -07002078 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002080 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002081 assert(EMPTY());
2082 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002084
Benjamin Petersonddd19492018-09-16 22:38:02 -07002085 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002086 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002087 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002088 PyObject *obj = TOP();
2089 PyTypeObject *type = Py_TYPE(obj);
2090
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002091 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002092 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002093 }
Yury Selivanov75445082015-05-11 22:57:16 -04002094
2095 if (getter != NULL) {
2096 iter = (*getter)(obj);
2097 Py_DECREF(obj);
2098 if (iter == NULL) {
2099 SET_TOP(NULL);
2100 goto error;
2101 }
2102 }
2103 else {
2104 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002105 _PyErr_Format(tstate, PyExc_TypeError,
2106 "'async for' requires an object with "
2107 "__aiter__ method, got %.100s",
2108 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002109 Py_DECREF(obj);
2110 goto error;
2111 }
2112
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002113 if (Py_TYPE(iter)->tp_as_async == NULL ||
2114 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002115
Yury Selivanov398ff912017-03-02 22:20:00 -05002116 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002117 _PyErr_Format(tstate, PyExc_TypeError,
2118 "'async for' received an object from __aiter__ "
2119 "that does not implement __anext__: %.100s",
2120 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002121 Py_DECREF(iter);
2122 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002123 }
2124
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002125 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002126 DISPATCH();
2127 }
2128
Benjamin Petersonddd19492018-09-16 22:38:02 -07002129 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002130 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002131 PyObject *next_iter = NULL;
2132 PyObject *awaitable = NULL;
2133 PyObject *aiter = TOP();
2134 PyTypeObject *type = Py_TYPE(aiter);
2135
Yury Selivanoveb636452016-09-08 22:01:51 -07002136 if (PyAsyncGen_CheckExact(aiter)) {
2137 awaitable = type->tp_as_async->am_anext(aiter);
2138 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002139 goto error;
2140 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002141 } else {
2142 if (type->tp_as_async != NULL){
2143 getter = type->tp_as_async->am_anext;
2144 }
Yury Selivanov75445082015-05-11 22:57:16 -04002145
Yury Selivanoveb636452016-09-08 22:01:51 -07002146 if (getter != NULL) {
2147 next_iter = (*getter)(aiter);
2148 if (next_iter == NULL) {
2149 goto error;
2150 }
2151 }
2152 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002153 _PyErr_Format(tstate, PyExc_TypeError,
2154 "'async for' requires an iterator with "
2155 "__anext__ method, got %.100s",
2156 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002157 goto error;
2158 }
Yury Selivanov75445082015-05-11 22:57:16 -04002159
Yury Selivanoveb636452016-09-08 22:01:51 -07002160 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2161 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002162 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002163 PyExc_TypeError,
2164 "'async for' received an invalid object "
2165 "from __anext__: %.100s",
2166 Py_TYPE(next_iter)->tp_name);
2167
2168 Py_DECREF(next_iter);
2169 goto error;
2170 } else {
2171 Py_DECREF(next_iter);
2172 }
2173 }
Yury Selivanov75445082015-05-11 22:57:16 -04002174
2175 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002176 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002177 DISPATCH();
2178 }
2179
Benjamin Petersonddd19492018-09-16 22:38:02 -07002180 case TARGET(GET_AWAITABLE): {
2181 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002182 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002183 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002184
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002185 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002186 int opcode_at_minus_3 = 0;
2187 if ((next_instr - first_instr) > 2) {
2188 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2189 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002190 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002191 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002192 _Py_OPCODE(next_instr[-2]));
2193 }
2194
Yury Selivanov75445082015-05-11 22:57:16 -04002195 Py_DECREF(iterable);
2196
Yury Selivanovc724bae2016-03-02 11:30:46 -05002197 if (iter != NULL && PyCoro_CheckExact(iter)) {
2198 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2199 if (yf != NULL) {
2200 /* `iter` is a coroutine object that is being
2201 awaited, `yf` is a pointer to the current awaitable
2202 being awaited on. */
2203 Py_DECREF(yf);
2204 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002205 _PyErr_SetString(tstate, PyExc_RuntimeError,
2206 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002207 /* The code below jumps to `error` if `iter` is NULL. */
2208 }
2209 }
2210
Yury Selivanov75445082015-05-11 22:57:16 -04002211 SET_TOP(iter); /* Even if it's NULL */
2212
2213 if (iter == NULL) {
2214 goto error;
2215 }
2216
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002217 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002218 DISPATCH();
2219 }
2220
Benjamin Petersonddd19492018-09-16 22:38:02 -07002221 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002223 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002225 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2226 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002227 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002228 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002230 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002231 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002232 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 Py_DECREF(v);
2235 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002236 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002237 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002238 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002239 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002240 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 if (err < 0)
2242 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002243 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 SET_TOP(val);
2245 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002246 }
Martin Panter95f53c12016-07-18 08:23:26 +00002247 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002248 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002249 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002250 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002251 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002252 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002254
Benjamin Petersonddd19492018-09-16 22:38:02 -07002255 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002257
2258 if (co->co_flags & CO_ASYNC_GENERATOR) {
2259 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2260 Py_DECREF(retval);
2261 if (w == NULL) {
2262 retval = NULL;
2263 goto error;
2264 }
2265 retval = w;
2266 }
2267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002269 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002271
Benjamin Petersonddd19492018-09-16 22:38:02 -07002272 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002273 PyObject *type, *value, *traceback;
2274 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 PyTryBlock *b = PyFrame_BlockPop(f);
2276 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002277 _PyErr_SetString(tstate, PyExc_SystemError,
2278 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002281 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2282 STACK_LEVEL() <= (b)->b_level + 4);
2283 exc_info = tstate->exc_info;
2284 type = exc_info->exc_type;
2285 value = exc_info->exc_value;
2286 traceback = exc_info->exc_traceback;
2287 exc_info->exc_type = POP();
2288 exc_info->exc_value = POP();
2289 exc_info->exc_traceback = POP();
2290 Py_XDECREF(type);
2291 Py_XDECREF(value);
2292 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002295
Benjamin Petersonddd19492018-09-16 22:38:02 -07002296 case TARGET(POP_BLOCK): {
2297 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002298 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Mark Shannonfee55262019-11-21 09:11:43 +00002302 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002303 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002304 PyObject *val = POP();
2305 PyObject *tb = POP();
2306 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002307 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002308 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002312 PyObject *exc = POP();
2313 assert(PyExceptionClass_Check(exc));
2314 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2315 PyTryBlock *b = PyFrame_BlockPop(f);
2316 assert(b->b_type == EXCEPT_HANDLER);
2317 Py_DECREF(exc);
2318 UNWIND_EXCEPT_HANDLER(b);
2319 Py_DECREF(POP());
2320 JUMPBY(oparg);
2321 FAST_DISPATCH();
2322 }
2323 else {
2324 PyObject *val = POP();
2325 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002326 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002327 goto exception_unwind;
2328 }
2329 }
2330
Zackery Spytzce6a0702019-08-25 03:44:09 -06002331 case TARGET(LOAD_ASSERTION_ERROR): {
2332 PyObject *value = PyExc_AssertionError;
2333 Py_INCREF(value);
2334 PUSH(value);
2335 FAST_DISPATCH();
2336 }
2337
Benjamin Petersonddd19492018-09-16 22:38:02 -07002338 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002339 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002340
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002342 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002343 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002345 if (!_PyErr_Occurred(tstate)) {
2346 _PyErr_SetString(tstate, PyExc_NameError,
2347 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002348 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002350 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002352 }
2353 else {
2354 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2355 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002356 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2358 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002359 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2360 _PyErr_SetString(tstate, PyExc_NameError,
2361 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002366 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002368
Benjamin Petersonddd19492018-09-16 22:38:02 -07002369 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 PyObject *name = GETITEM(names, oparg);
2371 PyObject *v = POP();
2372 PyObject *ns = f->f_locals;
2373 int err;
2374 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 _PyErr_Format(tstate, PyExc_SystemError,
2376 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 if (PyDict_CheckExact(ns))
2381 err = PyDict_SetItem(ns, name, v);
2382 else
2383 err = PyObject_SetItem(ns, name, v);
2384 Py_DECREF(v);
2385 if (err != 0)
2386 goto error;
2387 DISPATCH();
2388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Benjamin Petersonddd19492018-09-16 22:38:02 -07002390 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 PyObject *name = GETITEM(names, oparg);
2392 PyObject *ns = f->f_locals;
2393 int err;
2394 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002395 _PyErr_Format(tstate, PyExc_SystemError,
2396 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 err = PyObject_DelItem(ns, name);
2400 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 NAME_ERROR_MSG,
2403 name);
2404 goto error;
2405 }
2406 DISPATCH();
2407 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002408
Benjamin Petersonddd19492018-09-16 22:38:02 -07002409 case TARGET(UNPACK_SEQUENCE): {
2410 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 PyObject *seq = POP(), *item, **items;
2412 if (PyTuple_CheckExact(seq) &&
2413 PyTuple_GET_SIZE(seq) == oparg) {
2414 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 item = items[oparg];
2417 Py_INCREF(item);
2418 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 } else if (PyList_CheckExact(seq) &&
2421 PyList_GET_SIZE(seq) == oparg) {
2422 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 item = items[oparg];
2425 Py_INCREF(item);
2426 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002428 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002430 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 } else {
2432 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 Py_DECREF(seq);
2434 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002437 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002439
Benjamin Petersonddd19492018-09-16 22:38:02 -07002440 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2442 PyObject *seq = POP();
2443
Victor Stinner438a12d2019-05-24 17:01:38 +02002444 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 stack_pointer + totalargs)) {
2446 stack_pointer += totalargs;
2447 } else {
2448 Py_DECREF(seq);
2449 goto error;
2450 }
2451 Py_DECREF(seq);
2452 DISPATCH();
2453 }
2454
Benjamin Petersonddd19492018-09-16 22:38:02 -07002455 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 PyObject *name = GETITEM(names, oparg);
2457 PyObject *owner = TOP();
2458 PyObject *v = SECOND();
2459 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002460 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 Py_DECREF(owner);
2464 if (err != 0)
2465 goto error;
2466 DISPATCH();
2467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Benjamin Petersonddd19492018-09-16 22:38:02 -07002469 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 PyObject *name = GETITEM(names, oparg);
2471 PyObject *owner = POP();
2472 int err;
2473 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2474 Py_DECREF(owner);
2475 if (err != 0)
2476 goto error;
2477 DISPATCH();
2478 }
2479
Benjamin Petersonddd19492018-09-16 22:38:02 -07002480 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 PyObject *name = GETITEM(names, oparg);
2482 PyObject *v = POP();
2483 int err;
2484 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 if (err != 0)
2487 goto error;
2488 DISPATCH();
2489 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Benjamin Petersonddd19492018-09-16 22:38:02 -07002491 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 PyObject *name = GETITEM(names, oparg);
2493 int err;
2494 err = PyDict_DelItem(f->f_globals, name);
2495 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002496 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2497 format_exc_check_arg(tstate, PyExc_NameError,
2498 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002499 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002501 }
2502 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002504
Benjamin Petersonddd19492018-09-16 22:38:02 -07002505 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 PyObject *name = GETITEM(names, oparg);
2507 PyObject *locals = f->f_locals;
2508 PyObject *v;
2509 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002510 _PyErr_Format(tstate, PyExc_SystemError,
2511 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002515 v = PyDict_GetItemWithError(locals, name);
2516 if (v != NULL) {
2517 Py_INCREF(v);
2518 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002519 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002520 goto error;
2521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 }
2523 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002525 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002526 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002527 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002528 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 }
2530 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002532 v = PyDict_GetItemWithError(f->f_globals, name);
2533 if (v != NULL) {
2534 Py_INCREF(v);
2535 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002536 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002537 goto error;
2538 }
2539 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002540 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002541 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002543 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002544 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002545 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002547 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002549 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002551 }
2552 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 v = PyObject_GetItem(f->f_builtins, name);
2554 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002555 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002556 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002557 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002559 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002561 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002562 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002570 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002572 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002573 && PyDict_CheckExact(f->f_builtins))
2574 {
Inada Naoki91234a12019-06-03 21:30:58 +09002575 OPCACHE_CHECK();
2576 if (co_opcache != NULL && co_opcache->optimized > 0) {
2577 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2578
2579 if (lg->globals_ver ==
2580 ((PyDictObject *)f->f_globals)->ma_version_tag
2581 && lg->builtins_ver ==
2582 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2583 {
2584 PyObject *ptr = lg->ptr;
2585 OPCACHE_STAT_GLOBAL_HIT();
2586 assert(ptr != NULL);
2587 Py_INCREF(ptr);
2588 PUSH(ptr);
2589 DISPATCH();
2590 }
2591 }
2592
2593 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002594 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002595 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 name);
2597 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002598 if (!_PyErr_OCCURRED()) {
2599 /* _PyDict_LoadGlobal() returns NULL without raising
2600 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002601 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002602 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002603 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 }
Inada Naoki91234a12019-06-03 21:30:58 +09002606
2607 if (co_opcache != NULL) {
2608 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2609
2610 if (co_opcache->optimized == 0) {
2611 /* Wasn't optimized before. */
2612 OPCACHE_STAT_GLOBAL_OPT();
2613 } else {
2614 OPCACHE_STAT_GLOBAL_MISS();
2615 }
2616
2617 co_opcache->optimized = 1;
2618 lg->globals_ver =
2619 ((PyDictObject *)f->f_globals)->ma_version_tag;
2620 lg->builtins_ver =
2621 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2622 lg->ptr = v; /* borrowed */
2623 }
2624
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002627 else {
2628 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002629
2630 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002631 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 v = PyObject_GetItem(f->f_globals, name);
2633 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002634 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002635 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002636 }
2637 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002638
Victor Stinnerb4efc962015-11-20 09:24:02 +01002639 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 v = PyObject_GetItem(f->f_builtins, name);
2641 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002642 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002643 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002644 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002645 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002646 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002648 }
2649 }
2650 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002654
Benjamin Petersonddd19492018-09-16 22:38:02 -07002655 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 PyObject *v = GETLOCAL(oparg);
2657 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 SETLOCAL(oparg, NULL);
2659 DISPATCH();
2660 }
2661 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002662 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 UNBOUNDLOCAL_ERROR_MSG,
2664 PyTuple_GetItem(co->co_varnames, oparg)
2665 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 goto error;
2667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002668
Benjamin Petersonddd19492018-09-16 22:38:02 -07002669 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002671 PyObject *oldobj = PyCell_GET(cell);
2672 if (oldobj != NULL) {
2673 PyCell_SET(cell, NULL);
2674 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002675 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002676 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002677 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 goto error;
2679 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002680
Benjamin Petersonddd19492018-09-16 22:38:02 -07002681 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 PyObject *cell = freevars[oparg];
2683 Py_INCREF(cell);
2684 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002687
Benjamin Petersonddd19492018-09-16 22:38:02 -07002688 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002689 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002690 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002691 assert(locals);
2692 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2693 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2694 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2695 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2696 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002697 value = PyDict_GetItemWithError(locals, name);
2698 if (value != NULL) {
2699 Py_INCREF(value);
2700 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002701 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002702 goto error;
2703 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002704 }
2705 else {
2706 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002707 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002708 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002709 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002710 }
2711 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002712 }
2713 }
2714 if (!value) {
2715 PyObject *cell = freevars[oparg];
2716 value = PyCell_GET(cell);
2717 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002718 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002719 goto error;
2720 }
2721 Py_INCREF(value);
2722 }
2723 PUSH(value);
2724 DISPATCH();
2725 }
2726
Benjamin Petersonddd19492018-09-16 22:38:02 -07002727 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 PyObject *cell = freevars[oparg];
2729 PyObject *value = PyCell_GET(cell);
2730 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002731 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 Py_INCREF(value);
2735 PUSH(value);
2736 DISPATCH();
2737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Benjamin Petersonddd19492018-09-16 22:38:02 -07002739 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 PyObject *v = POP();
2741 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002742 PyObject *oldobj = PyCell_GET(cell);
2743 PyCell_SET(cell, v);
2744 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 DISPATCH();
2746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002747
Benjamin Petersonddd19492018-09-16 22:38:02 -07002748 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002749 PyObject *str;
2750 PyObject *empty = PyUnicode_New(0, 0);
2751 if (empty == NULL) {
2752 goto error;
2753 }
2754 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2755 Py_DECREF(empty);
2756 if (str == NULL)
2757 goto error;
2758 while (--oparg >= 0) {
2759 PyObject *item = POP();
2760 Py_DECREF(item);
2761 }
2762 PUSH(str);
2763 DISPATCH();
2764 }
2765
Benjamin Petersonddd19492018-09-16 22:38:02 -07002766 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 PyObject *tup = PyTuple_New(oparg);
2768 if (tup == NULL)
2769 goto error;
2770 while (--oparg >= 0) {
2771 PyObject *item = POP();
2772 PyTuple_SET_ITEM(tup, oparg, item);
2773 }
2774 PUSH(tup);
2775 DISPATCH();
2776 }
2777
Benjamin Petersonddd19492018-09-16 22:38:02 -07002778 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 PyObject *list = PyList_New(oparg);
2780 if (list == NULL)
2781 goto error;
2782 while (--oparg >= 0) {
2783 PyObject *item = POP();
2784 PyList_SET_ITEM(list, oparg, item);
2785 }
2786 PUSH(list);
2787 DISPATCH();
2788 }
2789
Mark Shannon13bc1392020-01-23 09:25:17 +00002790 case TARGET(LIST_TO_TUPLE): {
2791 PyObject *list = POP();
2792 PyObject *tuple = PyList_AsTuple(list);
2793 Py_DECREF(list);
2794 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002796 }
2797 PUSH(tuple);
2798 DISPATCH();
2799 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800
Mark Shannon13bc1392020-01-23 09:25:17 +00002801 case TARGET(LIST_EXTEND): {
2802 PyObject *iterable = POP();
2803 PyObject *list = PEEK(oparg);
2804 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2805 if (none_val == NULL) {
2806 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002807 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002808 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002809 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002810 _PyErr_Format(tstate, PyExc_TypeError,
2811 "Value after * must be an iterable, not %.200s",
2812 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002814 Py_DECREF(iterable);
2815 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002816 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002817 Py_DECREF(none_val);
2818 Py_DECREF(iterable);
2819 DISPATCH();
2820 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821
Mark Shannon13bc1392020-01-23 09:25:17 +00002822 case TARGET(SET_UPDATE): {
2823 PyObject *iterable = POP();
2824 PyObject *set = PEEK(oparg);
2825 int err = _PySet_Update(set, iterable);
2826 Py_DECREF(iterable);
2827 if (err < 0) {
2828 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002829 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002830 DISPATCH();
2831 }
2832
Benjamin Petersonddd19492018-09-16 22:38:02 -07002833 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 PyObject *set = PySet_New(NULL);
2835 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002836 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 if (set == NULL)
2838 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002839 for (i = oparg; i > 0; i--) {
2840 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 if (err == 0)
2842 err = PySet_Add(set, item);
2843 Py_DECREF(item);
2844 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002845 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 if (err != 0) {
2847 Py_DECREF(set);
2848 goto error;
2849 }
2850 PUSH(set);
2851 DISPATCH();
2852 }
2853
Benjamin Petersonddd19492018-09-16 22:38:02 -07002854 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002855 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2857 if (map == NULL)
2858 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002859 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002860 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002861 PyObject *key = PEEK(2*i);
2862 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002864 if (err != 0) {
2865 Py_DECREF(map);
2866 goto error;
2867 }
2868 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002869
2870 while (oparg--) {
2871 Py_DECREF(POP());
2872 Py_DECREF(POP());
2873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 PUSH(map);
2875 DISPATCH();
2876 }
2877
Benjamin Petersonddd19492018-09-16 22:38:02 -07002878 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002879 _Py_IDENTIFIER(__annotations__);
2880 int err;
2881 PyObject *ann_dict;
2882 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002883 _PyErr_Format(tstate, PyExc_SystemError,
2884 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002885 goto error;
2886 }
2887 /* check if __annotations__ in locals()... */
2888 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002889 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002890 &PyId___annotations__);
2891 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002892 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002893 goto error;
2894 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002895 /* ...if not, create a new one */
2896 ann_dict = PyDict_New();
2897 if (ann_dict == NULL) {
2898 goto error;
2899 }
2900 err = _PyDict_SetItemId(f->f_locals,
2901 &PyId___annotations__, ann_dict);
2902 Py_DECREF(ann_dict);
2903 if (err != 0) {
2904 goto error;
2905 }
2906 }
2907 }
2908 else {
2909 /* do the same if locals() is not a dict */
2910 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2911 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002912 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002913 }
2914 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2915 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002916 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002917 goto error;
2918 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002919 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002920 ann_dict = PyDict_New();
2921 if (ann_dict == NULL) {
2922 goto error;
2923 }
2924 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2925 Py_DECREF(ann_dict);
2926 if (err != 0) {
2927 goto error;
2928 }
2929 }
2930 else {
2931 Py_DECREF(ann_dict);
2932 }
2933 }
2934 DISPATCH();
2935 }
2936
Benjamin Petersonddd19492018-09-16 22:38:02 -07002937 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002938 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002939 PyObject *map;
2940 PyObject *keys = TOP();
2941 if (!PyTuple_CheckExact(keys) ||
2942 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002943 _PyErr_SetString(tstate, PyExc_SystemError,
2944 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002945 goto error;
2946 }
2947 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2948 if (map == NULL) {
2949 goto error;
2950 }
2951 for (i = oparg; i > 0; i--) {
2952 int err;
2953 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2954 PyObject *value = PEEK(i + 1);
2955 err = PyDict_SetItem(map, key, value);
2956 if (err != 0) {
2957 Py_DECREF(map);
2958 goto error;
2959 }
2960 }
2961
2962 Py_DECREF(POP());
2963 while (oparg--) {
2964 Py_DECREF(POP());
2965 }
2966 PUSH(map);
2967 DISPATCH();
2968 }
2969
Mark Shannon8a4cd702020-01-27 09:57:45 +00002970 case TARGET(DICT_UPDATE): {
2971 PyObject *update = POP();
2972 PyObject *dict = PEEK(oparg);
2973 if (PyDict_Update(dict, update) < 0) {
2974 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2975 _PyErr_Format(tstate, PyExc_TypeError,
2976 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002977 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002978 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002979 Py_DECREF(update);
2980 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002981 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002982 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002983 DISPATCH();
2984 }
2985
Mark Shannon8a4cd702020-01-27 09:57:45 +00002986 case TARGET(DICT_MERGE): {
2987 PyObject *update = POP();
2988 PyObject *dict = PEEK(oparg);
2989
2990 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2991 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2992 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002993 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002994 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002995 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002996 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002997 DISPATCH();
2998 }
2999
Benjamin Petersonddd19492018-09-16 22:38:02 -07003000 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003001 PyObject *value = TOP();
3002 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 PyObject *map;
3004 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003005 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003006 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003008 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 Py_DECREF(value);
3010 Py_DECREF(key);
3011 if (err != 0)
3012 goto error;
3013 PREDICT(JUMP_ABSOLUTE);
3014 DISPATCH();
3015 }
3016
Benjamin Petersonddd19492018-09-16 22:38:02 -07003017 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 PyObject *name = GETITEM(names, oparg);
3019 PyObject *owner = TOP();
3020 PyObject *res = PyObject_GetAttr(owner, name);
3021 Py_DECREF(owner);
3022 SET_TOP(res);
3023 if (res == NULL)
3024 goto error;
3025 DISPATCH();
3026 }
3027
Benjamin Petersonddd19492018-09-16 22:38:02 -07003028 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003029 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PyObject *right = POP();
3031 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003032 PyObject *res = PyObject_RichCompare(left, right, oparg);
3033 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 Py_DECREF(left);
3035 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 if (res == NULL)
3037 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 PREDICT(POP_JUMP_IF_FALSE);
3039 PREDICT(POP_JUMP_IF_TRUE);
3040 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003041 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003042
Mark Shannon9af0e472020-01-14 10:12:45 +00003043 case TARGET(IS_OP): {
3044 PyObject *right = POP();
3045 PyObject *left = TOP();
3046 int res = (left == right)^oparg;
3047 PyObject *b = res ? Py_True : Py_False;
3048 Py_INCREF(b);
3049 SET_TOP(b);
3050 Py_DECREF(left);
3051 Py_DECREF(right);
3052 PREDICT(POP_JUMP_IF_FALSE);
3053 PREDICT(POP_JUMP_IF_TRUE);
3054 FAST_DISPATCH();
3055 }
3056
3057 case TARGET(CONTAINS_OP): {
3058 PyObject *right = POP();
3059 PyObject *left = POP();
3060 int res = PySequence_Contains(right, left);
3061 Py_DECREF(left);
3062 Py_DECREF(right);
3063 if (res < 0) {
3064 goto error;
3065 }
3066 PyObject *b = (res^oparg) ? Py_True : Py_False;
3067 Py_INCREF(b);
3068 PUSH(b);
3069 PREDICT(POP_JUMP_IF_FALSE);
3070 PREDICT(POP_JUMP_IF_TRUE);
3071 FAST_DISPATCH();
3072 }
3073
3074#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3075 "BaseException is not allowed"
3076
3077 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3078 PyObject *right = POP();
3079 PyObject *left = POP();
3080 if (PyTuple_Check(right)) {
3081 Py_ssize_t i, length;
3082 length = PyTuple_GET_SIZE(right);
3083 for (i = 0; i < length; i++) {
3084 PyObject *exc = PyTuple_GET_ITEM(right, i);
3085 if (!PyExceptionClass_Check(exc)) {
3086 _PyErr_SetString(tstate, PyExc_TypeError,
3087 CANNOT_CATCH_MSG);
3088 Py_DECREF(left);
3089 Py_DECREF(right);
3090 goto error;
3091 }
3092 }
3093 }
3094 else {
3095 if (!PyExceptionClass_Check(right)) {
3096 _PyErr_SetString(tstate, PyExc_TypeError,
3097 CANNOT_CATCH_MSG);
3098 Py_DECREF(left);
3099 Py_DECREF(right);
3100 goto error;
3101 }
3102 }
3103 int res = PyErr_GivenExceptionMatches(left, right);
3104 Py_DECREF(left);
3105 Py_DECREF(right);
3106 if (res > 0) {
3107 /* Exception matches -- Do nothing */;
3108 }
3109 else if (res == 0) {
3110 JUMPTO(oparg);
3111 }
3112 else {
3113 goto error;
3114 }
3115 DISPATCH();
3116 }
3117
Benjamin Petersonddd19492018-09-16 22:38:02 -07003118 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003120 PyObject *fromlist = POP();
3121 PyObject *level = TOP();
3122 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003123 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003124 Py_DECREF(level);
3125 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 SET_TOP(res);
3127 if (res == NULL)
3128 goto error;
3129 DISPATCH();
3130 }
3131
Benjamin Petersonddd19492018-09-16 22:38:02 -07003132 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 PyObject *from = POP(), *locals;
3134 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003135 if (PyFrame_FastToLocalsWithError(f) < 0) {
3136 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003137 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003138 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003139
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 locals = f->f_locals;
3141 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003142 _PyErr_SetString(tstate, PyExc_SystemError,
3143 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003144 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003147 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 Py_DECREF(from);
3150 if (err != 0)
3151 goto error;
3152 DISPATCH();
3153 }
Guido van Rossum25831651993-05-19 14:50:45 +00003154
Benjamin Petersonddd19492018-09-16 22:38:02 -07003155 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 PyObject *name = GETITEM(names, oparg);
3157 PyObject *from = TOP();
3158 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003159 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 PUSH(res);
3161 if (res == NULL)
3162 goto error;
3163 DISPATCH();
3164 }
Thomas Wouters52152252000-08-17 22:55:00 +00003165
Benjamin Petersonddd19492018-09-16 22:38:02 -07003166 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 JUMPBY(oparg);
3168 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003170
Benjamin Petersonddd19492018-09-16 22:38:02 -07003171 case TARGET(POP_JUMP_IF_FALSE): {
3172 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 PyObject *cond = POP();
3174 int err;
3175 if (cond == Py_True) {
3176 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 FAST_DISPATCH();
3178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 if (cond == Py_False) {
3180 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 JUMPTO(oparg);
3182 FAST_DISPATCH();
3183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 err = PyObject_IsTrue(cond);
3185 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003187 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 else if (err == 0)
3189 JUMPTO(oparg);
3190 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003194
Benjamin Petersonddd19492018-09-16 22:38:02 -07003195 case TARGET(POP_JUMP_IF_TRUE): {
3196 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 PyObject *cond = POP();
3198 int err;
3199 if (cond == Py_False) {
3200 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 FAST_DISPATCH();
3202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 if (cond == Py_True) {
3204 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 JUMPTO(oparg);
3206 FAST_DISPATCH();
3207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 err = PyObject_IsTrue(cond);
3209 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 JUMPTO(oparg);
3212 }
3213 else if (err == 0)
3214 ;
3215 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003219
Benjamin Petersonddd19492018-09-16 22:38:02 -07003220 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 PyObject *cond = TOP();
3222 int err;
3223 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003224 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 FAST_DISPATCH();
3227 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 JUMPTO(oparg);
3230 FAST_DISPATCH();
3231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003234 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 }
3237 else if (err == 0)
3238 JUMPTO(oparg);
3239 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003243
Benjamin Petersonddd19492018-09-16 22:38:02 -07003244 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 PyObject *cond = TOP();
3246 int err;
3247 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003248 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 FAST_DISPATCH();
3251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 JUMPTO(oparg);
3254 FAST_DISPATCH();
3255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 JUMPTO(oparg);
3259 }
3260 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003261 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
3264 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003268
Benjamin Petersonddd19492018-09-16 22:38:02 -07003269 case TARGET(JUMP_ABSOLUTE): {
3270 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003272#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 /* Enabling this path speeds-up all while and for-loops by bypassing
3274 the per-loop checks for signals. By default, this should be turned-off
3275 because it prevents detection of a control-break in tight loops like
3276 "while 1: pass". Compile with this option turned-on when you need
3277 the speed-up and do not need break checking inside tight loops (ones
3278 that contain only instructions ending with FAST_DISPATCH).
3279 */
3280 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003281#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003283#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003285
Benjamin Petersonddd19492018-09-16 22:38:02 -07003286 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003289 PyObject *iter = PyObject_GetIter(iterable);
3290 Py_DECREF(iterable);
3291 SET_TOP(iter);
3292 if (iter == NULL)
3293 goto error;
3294 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003295 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003296 DISPATCH();
3297 }
3298
Benjamin Petersonddd19492018-09-16 22:38:02 -07003299 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003300 /* before: [obj]; after [getiter(obj)] */
3301 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003302 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003303 if (PyCoro_CheckExact(iterable)) {
3304 /* `iterable` is a coroutine */
3305 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3306 /* and it is used in a 'yield from' expression of a
3307 regular generator. */
3308 Py_DECREF(iterable);
3309 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003310 _PyErr_SetString(tstate, PyExc_TypeError,
3311 "cannot 'yield from' a coroutine object "
3312 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003313 goto error;
3314 }
3315 }
3316 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003317 /* `iterable` is not a generator. */
3318 iter = PyObject_GetIter(iterable);
3319 Py_DECREF(iterable);
3320 SET_TOP(iter);
3321 if (iter == NULL)
3322 goto error;
3323 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003324 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 DISPATCH();
3326 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003327
Benjamin Petersonddd19492018-09-16 22:38:02 -07003328 case TARGET(FOR_ITER): {
3329 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003332 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 if (next != NULL) {
3334 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 PREDICT(STORE_FAST);
3336 PREDICT(UNPACK_SEQUENCE);
3337 DISPATCH();
3338 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003339 if (_PyErr_Occurred(tstate)) {
3340 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003342 }
3343 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003344 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003345 }
3346 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 }
3348 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003349 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003352 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003355
Benjamin Petersonddd19492018-09-16 22:38:02 -07003356 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003357 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 STACK_LEVEL());
3359 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003361
Benjamin Petersonddd19492018-09-16 22:38:02 -07003362 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003363 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003364 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003365 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003366 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003367 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003368 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003369 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003370 }
3371 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3372 if (exit == NULL) {
3373 Py_DECREF(enter);
3374 goto error;
3375 }
Yury Selivanov75445082015-05-11 22:57:16 -04003376 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003377 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003378 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003379 Py_DECREF(enter);
3380 if (res == NULL)
3381 goto error;
3382 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003383 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003384 DISPATCH();
3385 }
3386
Benjamin Petersonddd19492018-09-16 22:38:02 -07003387 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003388 PyObject *res = POP();
3389 /* Setup the finally block before pushing the result
3390 of __aenter__ on the stack. */
3391 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3392 STACK_LEVEL());
3393 PUSH(res);
3394 DISPATCH();
3395 }
3396
Benjamin Petersonddd19492018-09-16 22:38:02 -07003397 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003398 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003399 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003401 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003403 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003404 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003405 }
3406 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003407 if (exit == NULL) {
3408 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003410 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003413 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 Py_DECREF(enter);
3415 if (res == NULL)
3416 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 /* Setup the finally block before pushing the result
3418 of __enter__ on the stack. */
3419 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3420 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 DISPATCH();
3424 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003425
Mark Shannonfee55262019-11-21 09:11:43 +00003426 case TARGET(WITH_EXCEPT_START): {
3427 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003429 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3430 - SEVENTH: the context.__exit__ bound method
3431 We call SEVENTH(TOP, SECOND, THIRD).
3432 Then we push again the TOP exception and the __exit__
3433 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003436 PyObject *exc, *val, *tb, *res;
3437
Victor Stinner842cfff2016-12-01 14:45:31 +01003438 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003439 val = SECOND();
3440 tb = THIRD();
3441 assert(exc != Py_None);
3442 assert(!PyLong_Check(exc));
3443 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003444 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003445 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003446 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447 if (res == NULL)
3448 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003449
Yury Selivanov75445082015-05-11 22:57:16 -04003450 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003453
Benjamin Petersonddd19492018-09-16 22:38:02 -07003454 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003455 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003456 PyObject *name = GETITEM(names, oparg);
3457 PyObject *obj = TOP();
3458 PyObject *meth = NULL;
3459
3460 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3461
Yury Selivanovf2392132016-12-13 19:03:51 -05003462 if (meth == NULL) {
3463 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003464 goto error;
3465 }
3466
3467 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003468 /* We can bypass temporary bound method object.
3469 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003470
INADA Naoki015bce62017-01-16 17:23:30 +09003471 meth | self | arg1 | ... | argN
3472 */
3473 SET_TOP(meth);
3474 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003475 }
3476 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003477 /* meth is not an unbound method (but a regular attr, or
3478 something was returned by a descriptor protocol). Set
3479 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003480 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003481
3482 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003483 */
INADA Naoki015bce62017-01-16 17:23:30 +09003484 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003485 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003486 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003487 }
3488 DISPATCH();
3489 }
3490
Benjamin Petersonddd19492018-09-16 22:38:02 -07003491 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003492 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003493 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003494
3495 sp = stack_pointer;
3496
INADA Naoki015bce62017-01-16 17:23:30 +09003497 meth = PEEK(oparg + 2);
3498 if (meth == NULL) {
3499 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3500 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003501
3502 Stack layout:
3503
INADA Naoki015bce62017-01-16 17:23:30 +09003504 ... | NULL | callable | arg1 | ... | argN
3505 ^- TOP()
3506 ^- (-oparg)
3507 ^- (-oparg-1)
3508 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003509
Ville Skyttä49b27342017-08-03 09:00:59 +03003510 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003511 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003512 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003513 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003514 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003515 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003516 }
3517 else {
3518 /* This is a method call. Stack layout:
3519
INADA Naoki015bce62017-01-16 17:23:30 +09003520 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003521 ^- TOP()
3522 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003523 ^- (-oparg-1)
3524 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003525
INADA Naoki015bce62017-01-16 17:23:30 +09003526 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003527 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003528 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003529 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003530 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003531 stack_pointer = sp;
3532 }
3533
3534 PUSH(res);
3535 if (res == NULL)
3536 goto error;
3537 DISPATCH();
3538 }
3539
Benjamin Petersonddd19492018-09-16 22:38:02 -07003540 case TARGET(CALL_FUNCTION): {
3541 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003542 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003544 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003547 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003548 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003549 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003550 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003552
Benjamin Petersonddd19492018-09-16 22:38:02 -07003553 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003554 PyObject **sp, *res, *names;
3555
3556 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003557 assert(PyTuple_Check(names));
3558 assert(PyTuple_GET_SIZE(names) <= oparg);
3559 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003561 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003564 Py_DECREF(names);
3565
3566 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003568 }
3569 DISPATCH();
3570 }
3571
Benjamin Petersonddd19492018-09-16 22:38:02 -07003572 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003573 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003574 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003575 if (oparg & 0x01) {
3576 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003577 if (!PyDict_CheckExact(kwargs)) {
3578 PyObject *d = PyDict_New();
3579 if (d == NULL)
3580 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003581 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003582 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003583 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003584 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003585 goto error;
3586 }
3587 Py_DECREF(kwargs);
3588 kwargs = d;
3589 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003590 assert(PyDict_CheckExact(kwargs));
3591 }
3592 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003593 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003594 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003595 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003596 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003597 goto error;
3598 }
3599 Py_SETREF(callargs, PySequence_Tuple(callargs));
3600 if (callargs == NULL) {
3601 goto error;
3602 }
3603 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003604 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003605
Victor Stinner09532fe2019-05-10 23:39:09 +02003606 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003607 Py_DECREF(func);
3608 Py_DECREF(callargs);
3609 Py_XDECREF(kwargs);
3610
3611 SET_TOP(result);
3612 if (result == NULL) {
3613 goto error;
3614 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003615 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003617
Benjamin Petersonddd19492018-09-16 22:38:02 -07003618 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003619 PyObject *qualname = POP();
3620 PyObject *codeobj = POP();
3621 PyFunctionObject *func = (PyFunctionObject *)
3622 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003623
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003624 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003625 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003626 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003627 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003629
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003630 if (oparg & 0x08) {
3631 assert(PyTuple_CheckExact(TOP()));
3632 func ->func_closure = POP();
3633 }
3634 if (oparg & 0x04) {
3635 assert(PyDict_CheckExact(TOP()));
3636 func->func_annotations = POP();
3637 }
3638 if (oparg & 0x02) {
3639 assert(PyDict_CheckExact(TOP()));
3640 func->func_kwdefaults = POP();
3641 }
3642 if (oparg & 0x01) {
3643 assert(PyTuple_CheckExact(TOP()));
3644 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003646
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003647 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003648 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003650
Benjamin Petersonddd19492018-09-16 22:38:02 -07003651 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003652 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003654 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003656 step = NULL;
3657 stop = POP();
3658 start = TOP();
3659 slice = PySlice_New(start, stop, step);
3660 Py_DECREF(start);
3661 Py_DECREF(stop);
3662 Py_XDECREF(step);
3663 SET_TOP(slice);
3664 if (slice == NULL)
3665 goto error;
3666 DISPATCH();
3667 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003668
Benjamin Petersonddd19492018-09-16 22:38:02 -07003669 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003670 /* Handles f-string value formatting. */
3671 PyObject *result;
3672 PyObject *fmt_spec;
3673 PyObject *value;
3674 PyObject *(*conv_fn)(PyObject *);
3675 int which_conversion = oparg & FVC_MASK;
3676 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3677
3678 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003679 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003680
3681 /* See if any conversion is specified. */
3682 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003683 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003684 case FVC_STR: conv_fn = PyObject_Str; break;
3685 case FVC_REPR: conv_fn = PyObject_Repr; break;
3686 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003687 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003688 _PyErr_Format(tstate, PyExc_SystemError,
3689 "unexpected conversion flag %d",
3690 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003691 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003692 }
3693
3694 /* If there's a conversion function, call it and replace
3695 value with that result. Otherwise, just use value,
3696 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003697 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003698 result = conv_fn(value);
3699 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003700 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003701 Py_XDECREF(fmt_spec);
3702 goto error;
3703 }
3704 value = result;
3705 }
3706
3707 /* If value is a unicode object, and there's no fmt_spec,
3708 then we know the result of format(value) is value
3709 itself. In that case, skip calling format(). I plan to
3710 move this optimization in to PyObject_Format()
3711 itself. */
3712 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3713 /* Do nothing, just transfer ownership to result. */
3714 result = value;
3715 } else {
3716 /* Actually call format(). */
3717 result = PyObject_Format(value, fmt_spec);
3718 Py_DECREF(value);
3719 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003720 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003721 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003722 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003723 }
3724
Eric V. Smith135d5f42016-02-05 18:23:08 -05003725 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003726 DISPATCH();
3727 }
3728
Benjamin Petersonddd19492018-09-16 22:38:02 -07003729 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003730 int oldoparg = oparg;
3731 NEXTOPARG();
3732 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003734 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003735
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003736
Antoine Pitrou042b1282010-08-13 21:15:58 +00003737#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003739#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 default:
3741 fprintf(stderr,
3742 "XXX lineno: %d, opcode: %d\n",
3743 PyFrame_GetLineNumber(f),
3744 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003745 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003746 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003749
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003750 /* This should never be reached. Every opcode should end with DISPATCH()
3751 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003752 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003753
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003754error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003755 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003756#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003757 if (!_PyErr_Occurred(tstate)) {
3758 _PyErr_SetString(tstate, PyExc_SystemError,
3759 "error return without exception set");
3760 }
Victor Stinner365b6932013-07-12 00:11:58 +02003761#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003762 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003763#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003764
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003765 /* Log traceback info. */
3766 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003767
Benjamin Peterson51f46162013-01-23 08:38:47 -05003768 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003769 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3770 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003771
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003772exception_unwind:
3773 /* Unwind stacks if an exception occurred */
3774 while (f->f_iblock > 0) {
3775 /* Pop the current block. */
3776 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 if (b->b_type == EXCEPT_HANDLER) {
3779 UNWIND_EXCEPT_HANDLER(b);
3780 continue;
3781 }
3782 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003783 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 PyObject *exc, *val, *tb;
3785 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003786 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 /* Beware, this invalidates all b->b_* fields */
3788 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003789 PUSH(exc_info->exc_traceback);
3790 PUSH(exc_info->exc_value);
3791 if (exc_info->exc_type != NULL) {
3792 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 }
3794 else {
3795 Py_INCREF(Py_None);
3796 PUSH(Py_None);
3797 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003798 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 /* Make the raw exception data
3800 available to the handler,
3801 so a program can emulate the
3802 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003803 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003804 if (tb != NULL)
3805 PyException_SetTraceback(val, tb);
3806 else
3807 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003809 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003811 exc_info->exc_value = val;
3812 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 if (tb == NULL)
3814 tb = Py_None;
3815 Py_INCREF(tb);
3816 PUSH(tb);
3817 PUSH(val);
3818 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003820 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003821 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3822 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3823 /* Make sure that we trace line after exception if we are in a new execution
3824 * window or we don't need a line update and we are not in the first instruction
3825 * of the line. */
3826 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3827 instr_prev = INT_MAX;
3828 }
Mark Shannonfee55262019-11-21 09:11:43 +00003829 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003830 /* Resume normal execution */
3831 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 }
3833 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003834
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003835 /* End the loop as we still have an error */
3836 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003838
Pablo Galindof00828a2019-05-09 16:52:02 +01003839 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003840 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 /* Pop remaining stack entries. */
3843 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003844 PyObject *o = POP();
3845 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003847
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003848exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003850 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003851 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3852 tstate, f, PyTrace_RETURN, retval)) {
3853 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 }
3855 }
3856 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003857 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3858 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003859 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 }
3861 }
3862 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003865exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003866 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3867 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003868 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003869 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003871
Victor Stinner0b72b232020-03-12 23:18:39 +01003872 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003873}
3874
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003876format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003877 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003878{
3879 int err;
3880 Py_ssize_t len = PyList_GET_SIZE(names);
3881 PyObject *name_str, *comma, *tail, *tmp;
3882
3883 assert(PyList_CheckExact(names));
3884 assert(len >= 1);
3885 /* Deal with the joys of natural language. */
3886 switch (len) {
3887 case 1:
3888 name_str = PyList_GET_ITEM(names, 0);
3889 Py_INCREF(name_str);
3890 break;
3891 case 2:
3892 name_str = PyUnicode_FromFormat("%U and %U",
3893 PyList_GET_ITEM(names, len - 2),
3894 PyList_GET_ITEM(names, len - 1));
3895 break;
3896 default:
3897 tail = PyUnicode_FromFormat(", %U, and %U",
3898 PyList_GET_ITEM(names, len - 2),
3899 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003900 if (tail == NULL)
3901 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003902 /* Chop off the last two objects in the list. This shouldn't actually
3903 fail, but we can't be too careful. */
3904 err = PyList_SetSlice(names, len - 2, len, NULL);
3905 if (err == -1) {
3906 Py_DECREF(tail);
3907 return;
3908 }
3909 /* Stitch everything up into a nice comma-separated list. */
3910 comma = PyUnicode_FromString(", ");
3911 if (comma == NULL) {
3912 Py_DECREF(tail);
3913 return;
3914 }
3915 tmp = PyUnicode_Join(comma, names);
3916 Py_DECREF(comma);
3917 if (tmp == NULL) {
3918 Py_DECREF(tail);
3919 return;
3920 }
3921 name_str = PyUnicode_Concat(tmp, tail);
3922 Py_DECREF(tmp);
3923 Py_DECREF(tail);
3924 break;
3925 }
3926 if (name_str == NULL)
3927 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003928 _PyErr_Format(tstate, PyExc_TypeError,
3929 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003930 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003931 len,
3932 kind,
3933 len == 1 ? "" : "s",
3934 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003935 Py_DECREF(name_str);
3936}
3937
3938static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003939missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3940 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003941 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003942{
Victor Stinner74319ae2016-08-25 00:04:09 +02003943 Py_ssize_t i, j = 0;
3944 Py_ssize_t start, end;
3945 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003946 const char *kind = positional ? "positional" : "keyword-only";
3947 PyObject *missing_names;
3948
3949 /* Compute the names of the arguments that are missing. */
3950 missing_names = PyList_New(missing);
3951 if (missing_names == NULL)
3952 return;
3953 if (positional) {
3954 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003955 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 }
3957 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003958 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003959 end = start + co->co_kwonlyargcount;
3960 }
3961 for (i = start; i < end; i++) {
3962 if (GETLOCAL(i) == NULL) {
3963 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3964 PyObject *name = PyObject_Repr(raw);
3965 if (name == NULL) {
3966 Py_DECREF(missing_names);
3967 return;
3968 }
3969 PyList_SET_ITEM(missing_names, j++, name);
3970 }
3971 }
3972 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003973 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003974 Py_DECREF(missing_names);
3975}
3976
3977static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003978too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3979 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003980 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981{
3982 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003983 Py_ssize_t kwonly_given = 0;
3984 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003985 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003986 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003987
Benjamin Petersone109c702011-06-24 09:37:26 -05003988 assert((co->co_flags & CO_VARARGS) == 0);
3989 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003990 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003991 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003993 }
3994 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003995 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003996 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003998 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 }
4000 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004001 plural = (co_argcount != 1);
4002 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004003 }
4004 if (sig == NULL)
4005 return;
4006 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004007 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4008 kwonly_sig = PyUnicode_FromFormat(format,
4009 given != 1 ? "s" : "",
4010 kwonly_given,
4011 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 if (kwonly_sig == NULL) {
4013 Py_DECREF(sig);
4014 return;
4015 }
4016 }
4017 else {
4018 /* This will not fail. */
4019 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004020 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004021 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004022 _PyErr_Format(tstate, PyExc_TypeError,
4023 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004024 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004025 sig,
4026 plural ? "s" : "",
4027 given,
4028 kwonly_sig,
4029 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 Py_DECREF(sig);
4031 Py_DECREF(kwonly_sig);
4032}
4033
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004034static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004035positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004036 Py_ssize_t kwcount, PyObject* const* kwnames,
4037 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004038{
4039 int posonly_conflicts = 0;
4040 PyObject* posonly_names = PyList_New(0);
4041
4042 for(int k=0; k < co->co_posonlyargcount; k++){
4043 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4044
4045 for (int k2=0; k2<kwcount; k2++){
4046 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4047 PyObject* kwname = kwnames[k2];
4048 if (kwname == posonly_name){
4049 if(PyList_Append(posonly_names, kwname) != 0) {
4050 goto fail;
4051 }
4052 posonly_conflicts++;
4053 continue;
4054 }
4055
4056 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4057
4058 if ( cmp > 0) {
4059 if(PyList_Append(posonly_names, kwname) != 0) {
4060 goto fail;
4061 }
4062 posonly_conflicts++;
4063 } else if (cmp < 0) {
4064 goto fail;
4065 }
4066
4067 }
4068 }
4069 if (posonly_conflicts) {
4070 PyObject* comma = PyUnicode_FromString(", ");
4071 if (comma == NULL) {
4072 goto fail;
4073 }
4074 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4075 Py_DECREF(comma);
4076 if (error_names == NULL) {
4077 goto fail;
4078 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004079 _PyErr_Format(tstate, PyExc_TypeError,
4080 "%U() got some positional-only arguments passed"
4081 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004082 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004083 Py_DECREF(error_names);
4084 goto fail;
4085 }
4086
4087 Py_DECREF(posonly_names);
4088 return 0;
4089
4090fail:
4091 Py_XDECREF(posonly_names);
4092 return 1;
4093
4094}
4095
Guido van Rossumc2e20742006-02-27 22:32:47 +00004096/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004097 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004098 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004099
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004100PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004101_PyEval_EvalCode(PyThreadState *tstate,
4102 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004103 PyObject *const *args, Py_ssize_t argcount,
4104 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004105 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004106 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004107 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004108 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004109{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004110 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004111
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004112 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004113 PyFrameObject *f;
4114 PyObject *retval = NULL;
4115 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004117 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004118 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004119 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004122 _PyErr_SetString(tstate, PyExc_SystemError,
4123 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 return NULL;
4125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Victor Stinnerc7020012016-08-16 23:40:29 +02004127 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004128 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004129 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 fastlocals = f->f_localsplus;
4133 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004134
Victor Stinnerc7020012016-08-16 23:40:29 +02004135 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 if (co->co_flags & CO_VARKEYWORDS) {
4137 kwdict = PyDict_New();
4138 if (kwdict == NULL)
4139 goto fail;
4140 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004141 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004143 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004144 SETLOCAL(i, kwdict);
4145 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004146 else {
4147 kwdict = NULL;
4148 }
4149
Pablo Galindocd74e662019-06-01 18:08:04 +01004150 /* Copy all positional arguments into local variables */
4151 if (argcount > co->co_argcount) {
4152 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004153 }
4154 else {
4155 n = argcount;
4156 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004157 for (j = 0; j < n; j++) {
4158 x = args[j];
4159 Py_INCREF(x);
4160 SETLOCAL(j, x);
4161 }
4162
Victor Stinnerc7020012016-08-16 23:40:29 +02004163 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004165 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004166 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004167 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004168 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004170 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004171
Serhiy Storchakab7281052016-09-12 00:52:40 +03004172 /* Handle keyword arguments passed as two strided arrays */
4173 kwcount *= kwstep;
4174 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004176 PyObject *keyword = kwnames[i];
4177 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004178 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004179
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004181 _PyErr_Format(tstate, PyExc_TypeError,
4182 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004183 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004184 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004186
Benjamin Petersonb204a422011-06-05 22:04:07 -05004187 /* Speed hack: do raw pointer compares. As names are
4188 normally interned this should almost always hit. */
4189 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004190 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004191 PyObject *name = co_varnames[j];
4192 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004193 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004194 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004196
Benjamin Petersonb204a422011-06-05 22:04:07 -05004197 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004198 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004199 PyObject *name = co_varnames[j];
4200 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4201 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004202 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004203 }
4204 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004206 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004207 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004208
Victor Stinner231d1f32017-01-11 02:12:06 +01004209 assert(j >= total_args);
4210 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004211
Victor Stinner438a12d2019-05-24 17:01:38 +02004212 if (co->co_posonlyargcount
4213 && positional_only_passed_as_keyword(tstate, co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004214 kwcount, kwnames, qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004215 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004216 goto fail;
4217 }
4218
Victor Stinner438a12d2019-05-24 17:01:38 +02004219 _PyErr_Format(tstate, PyExc_TypeError,
4220 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004221 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004222 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004224
Christian Heimes0bd447f2013-07-20 14:48:10 +02004225 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4226 goto fail;
4227 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004228 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004229
Benjamin Petersonb204a422011-06-05 22:04:07 -05004230 kw_found:
4231 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004232 _PyErr_Format(tstate, PyExc_TypeError,
4233 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004234 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004235 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004237 Py_INCREF(value);
4238 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004240
4241 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004242 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004243 too_many_positional(tstate, co, argcount, defcount, fastlocals, qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 goto fail;
4245 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004246
4247 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004248 if (argcount < co->co_argcount) {
4249 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004250 Py_ssize_t missing = 0;
4251 for (i = argcount; i < m; i++) {
4252 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004253 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004254 }
4255 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004256 if (missing) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004257 missing_arguments(tstate, co, missing, defcount, fastlocals, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004258 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004259 }
4260 if (n > m)
4261 i = n - m;
4262 else
4263 i = 0;
4264 for (; i < defcount; i++) {
4265 if (GETLOCAL(m+i) == NULL) {
4266 PyObject *def = defs[i];
4267 Py_INCREF(def);
4268 SETLOCAL(m+i, def);
4269 }
4270 }
4271 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004272
4273 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004274 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004275 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004276 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004277 PyObject *name;
4278 if (GETLOCAL(i) != NULL)
4279 continue;
4280 name = PyTuple_GET_ITEM(co->co_varnames, i);
4281 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004282 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004283 if (def) {
4284 Py_INCREF(def);
4285 SETLOCAL(i, def);
4286 continue;
4287 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004288 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004289 goto fail;
4290 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004291 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004292 missing++;
4293 }
4294 if (missing) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004295 missing_arguments(tstate, co, missing, -1, fastlocals, qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004296 goto fail;
4297 }
4298 }
4299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004301 vars into frame. */
4302 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004304 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004305 /* Possibly account for the cell variable being an argument. */
4306 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004307 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004308 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004309 /* Clear the local copy. */
4310 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004311 }
4312 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004313 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004314 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004315 if (c == NULL)
4316 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004317 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004319
4320 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004321 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4322 PyObject *o = PyTuple_GET_ITEM(closure, i);
4323 Py_INCREF(o);
4324 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004326
Yury Selivanoveb636452016-09-08 22:01:51 -07004327 /* Handle generator/coroutine/asynchronous generator */
4328 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004329 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004330 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 /* Don't need to keep the reference to f_back, it will be set
4333 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004334 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 /* Create a new generator that owns the ready to run frame
4337 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004338 if (is_coro) {
4339 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004340 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4341 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004342 } else {
4343 gen = PyGen_NewWithQualName(f, name, qualname);
4344 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004345 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004346 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004347 }
INADA Naoki9c157762016-12-26 18:52:46 +09004348
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004349 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004350
Yury Selivanov75445082015-05-11 22:57:16 -04004351 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004353
Victor Stinnerb9e68122019-11-14 12:20:46 +01004354 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004355
Thomas Woutersce272b62007-09-19 21:19:28 +00004356fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 /* decref'ing the frame can cause __del__ methods to get invoked,
4359 which can call back into Python. While we're done with the
4360 current Python frame (f), the associated C stack is still in use,
4361 so recursion_depth must be boosted for the duration.
4362 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004363 if (Py_REFCNT(f) > 1) {
4364 Py_DECREF(f);
4365 _PyObject_GC_TRACK(f);
4366 }
4367 else {
4368 ++tstate->recursion_depth;
4369 Py_DECREF(f);
4370 --tstate->recursion_depth;
4371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004373}
4374
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004375
4376PyObject *
4377_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4378 PyObject *const *args, Py_ssize_t argcount,
4379 PyObject *const *kwnames, PyObject *const *kwargs,
4380 Py_ssize_t kwcount, int kwstep,
4381 PyObject *const *defs, Py_ssize_t defcount,
4382 PyObject *kwdefs, PyObject *closure,
4383 PyObject *name, PyObject *qualname)
4384{
4385 PyThreadState *tstate = _PyThreadState_GET();
4386 return _PyEval_EvalCode(tstate, _co, globals, locals,
4387 args, argcount,
4388 kwnames, kwargs,
4389 kwcount, kwstep,
4390 defs, defcount,
4391 kwdefs, closure,
4392 name, qualname);
4393}
4394
Victor Stinner40ee3012014-06-16 15:59:28 +02004395PyObject *
4396PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004397 PyObject *const *args, int argcount,
4398 PyObject *const *kws, int kwcount,
4399 PyObject *const *defs, int defcount,
4400 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004401{
4402 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004403 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004404 kws, kws != NULL ? kws + 1 : NULL,
4405 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004406 defs, defcount,
4407 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004408 NULL, NULL);
4409}
Tim Peters5ca576e2001-06-18 22:08:13 +00004410
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004411static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004412special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004415 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004416 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004417 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 return NULL;
4419 }
4420 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004421}
4422
4423
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004424/* Logic for the raise statement (too complicated for inlining).
4425 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004426static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004427do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 if (exc == NULL) {
4432 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004433 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004435 type = exc_info->exc_type;
4436 value = exc_info->exc_value;
4437 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004438 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004439 _PyErr_SetString(tstate, PyExc_RuntimeError,
4440 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004441 return 0;
4442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 Py_XINCREF(type);
4444 Py_XINCREF(value);
4445 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004446 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004447 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 /* We support the following forms of raise:
4451 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004452 raise <instance>
4453 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (PyExceptionClass_Check(exc)) {
4456 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004457 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (value == NULL)
4459 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004460 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004461 _PyErr_Format(tstate, PyExc_TypeError,
4462 "calling %R should have returned an instance of "
4463 "BaseException, not %R",
4464 type, Py_TYPE(value));
4465 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 }
4468 else if (PyExceptionInstance_Check(exc)) {
4469 value = exc;
4470 type = PyExceptionInstance_Class(exc);
4471 Py_INCREF(type);
4472 }
4473 else {
4474 /* Not something you can raise. You get an exception
4475 anyway, just not what you specified :-) */
4476 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004477 _PyErr_SetString(tstate, PyExc_TypeError,
4478 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 goto raise_error;
4480 }
Collin Winter828f04a2007-08-31 00:04:24 +00004481
Serhiy Storchakac0191582016-09-27 11:37:10 +03004482 assert(type != NULL);
4483 assert(value != NULL);
4484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 if (cause) {
4486 PyObject *fixed_cause;
4487 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004488 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (fixed_cause == NULL)
4490 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004491 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004493 else if (PyExceptionInstance_Check(cause)) {
4494 fixed_cause = cause;
4495 }
4496 else if (cause == Py_None) {
4497 Py_DECREF(cause);
4498 fixed_cause = NULL;
4499 }
4500 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_SetString(tstate, PyExc_TypeError,
4502 "exception causes must derive from "
4503 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 goto raise_error;
4505 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004506 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 }
Collin Winter828f04a2007-08-31 00:04:24 +00004508
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004510 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004511 Py_DECREF(value);
4512 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004513 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004514
4515raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 Py_XDECREF(value);
4517 Py_XDECREF(type);
4518 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004519 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004520}
4521
Tim Petersd6d010b2001-06-21 02:49:55 +00004522/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004523 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004524
Guido van Rossum0368b722007-05-11 16:50:42 +00004525 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4526 with a variable target.
4527*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004528
Barry Warsawe42b18f1997-08-25 22:13:04 +00004529static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004530unpack_iterable(PyThreadState *tstate, PyObject *v,
4531 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 int i = 0, j = 0;
4534 Py_ssize_t ll = 0;
4535 PyObject *it; /* iter(v) */
4536 PyObject *w;
4537 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004542 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004544 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004545 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004546 _PyErr_Format(tstate, PyExc_TypeError,
4547 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004548 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004549 }
4550 return 0;
4551 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 for (; i < argcnt; i++) {
4554 w = PyIter_Next(it);
4555 if (w == NULL) {
4556 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004557 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004558 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004559 _PyErr_Format(tstate, PyExc_ValueError,
4560 "not enough values to unpack "
4561 "(expected %d, got %d)",
4562 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004563 }
4564 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004565 _PyErr_Format(tstate, PyExc_ValueError,
4566 "not enough values to unpack "
4567 "(expected at least %d, got %d)",
4568 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 }
4571 goto Error;
4572 }
4573 *--sp = w;
4574 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 if (argcntafter == -1) {
4577 /* We better have exhausted the iterator now. */
4578 w = PyIter_Next(it);
4579 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004580 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 goto Error;
4582 Py_DECREF(it);
4583 return 1;
4584 }
4585 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004586 _PyErr_Format(tstate, PyExc_ValueError,
4587 "too many values to unpack (expected %d)",
4588 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 goto Error;
4590 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 l = PySequence_List(it);
4593 if (l == NULL)
4594 goto Error;
4595 *--sp = l;
4596 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 ll = PyList_GET_SIZE(l);
4599 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004600 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004601 "not enough values to unpack (expected at least %d, got %zd)",
4602 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 goto Error;
4604 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 /* Pop the "after-variable" args off the list. */
4607 for (j = argcntafter; j > 0; j--, i++) {
4608 *--sp = PyList_GET_ITEM(l, ll - j);
4609 }
4610 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004611 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 Py_DECREF(it);
4613 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004614
Tim Petersd6d010b2001-06-21 02:49:55 +00004615Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 for (; i > 0; i--, sp++)
4617 Py_DECREF(*sp);
4618 Py_XDECREF(it);
4619 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004620}
4621
4622
Guido van Rossum96a42c81992-01-12 02:29:51 +00004623#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004624static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004625prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004628 if (PyObject_Print(v, stdout, 0) != 0) {
4629 /* Don't know what else to do */
4630 _PyErr_Clear(tstate);
4631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 printf("\n");
4633 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004634}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004635#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004636
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004637static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004638call_exc_trace(Py_tracefunc func, PyObject *self,
4639 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004640{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004641 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004643 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 if (value == NULL) {
4645 value = Py_None;
4646 Py_INCREF(value);
4647 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004648 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004649 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 arg = PyTuple_Pack(3, type, value, traceback);
4651 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004652 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 return;
4654 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004655 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004657 if (err == 0) {
4658 _PyErr_Restore(tstate, type, value, orig_traceback);
4659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 else {
4661 Py_XDECREF(type);
4662 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004663 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004665}
4666
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004667static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004668call_trace_protected(Py_tracefunc func, PyObject *obj,
4669 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyObject *type, *value, *traceback;
4673 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004674 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004675 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 if (err == 0)
4677 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004678 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 return 0;
4680 }
4681 else {
4682 Py_XDECREF(type);
4683 Py_XDECREF(value);
4684 Py_XDECREF(traceback);
4685 return -1;
4686 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004687}
4688
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004689static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004690call_trace(Py_tracefunc func, PyObject *obj,
4691 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 int result;
4695 if (tstate->tracing)
4696 return 0;
4697 tstate->tracing++;
4698 tstate->use_tracing = 0;
4699 result = func(obj, frame, what, arg);
4700 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4701 || (tstate->c_profilefunc != NULL));
4702 tstate->tracing--;
4703 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004704}
4705
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004706PyObject *
4707_PyEval_CallTracing(PyObject *func, PyObject *args)
4708{
Victor Stinner50b48572018-11-01 01:51:40 +01004709 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 int save_tracing = tstate->tracing;
4711 int save_use_tracing = tstate->use_tracing;
4712 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 tstate->tracing = 0;
4715 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4716 || (tstate->c_profilefunc != NULL));
4717 result = PyObject_Call(func, args, NULL);
4718 tstate->tracing = save_tracing;
4719 tstate->use_tracing = save_use_tracing;
4720 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004721}
4722
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004723/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004724static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004725maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004726 PyThreadState *tstate, PyFrameObject *frame,
4727 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 int result = 0;
4730 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 /* If the last instruction executed isn't in the current
4733 instruction window, reset the window.
4734 */
4735 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4736 PyAddrPair bounds;
4737 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4738 &bounds);
4739 *instr_lb = bounds.ap_lower;
4740 *instr_ub = bounds.ap_upper;
4741 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004742 /* If the last instruction falls at the start of a line or if it
4743 represents a jump backwards, update the frame's line number and
4744 then call the trace function if we're tracing source lines.
4745 */
4746 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004748 if (frame->f_trace_lines) {
4749 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4750 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 }
George King20faa682017-10-18 17:44:22 -07004752 /* Always emit an opcode event if we're tracing all opcodes. */
4753 if (frame->f_trace_opcodes) {
4754 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 *instr_prev = frame->f_lasti;
4757 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004758}
4759
Victor Stinner309d7cc2020-03-13 16:39:12 +01004760int
4761_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4762{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004763 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004764 /* The caller must hold the GIL */
4765 assert(PyGILState_Check());
4766
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004767 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004768 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004769 PyThreadState *current_tstate = _PyThreadState_GET();
4770 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004771 return -1;
4772 }
4773
4774 PyObject *profileobj = tstate->c_profileobj;
4775
4776 tstate->c_profilefunc = NULL;
4777 tstate->c_profileobj = NULL;
4778 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4779 tstate->use_tracing = tstate->c_tracefunc != NULL;
4780 Py_XDECREF(profileobj);
4781
4782 Py_XINCREF(arg);
4783 tstate->c_profileobj = arg;
4784 tstate->c_profilefunc = func;
4785
4786 /* Flag that tracing or profiling is turned on */
4787 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4788 return 0;
4789}
4790
Fred Drake5755ce62001-06-27 19:19:46 +00004791void
4792PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004793{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004794 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004795 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004796 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004797 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4798 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004799}
4800
4801int
4802_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4803{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004804 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004805 /* The caller must hold the GIL */
4806 assert(PyGILState_Check());
4807
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004808 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004809 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004810 PyThreadState *current_tstate = _PyThreadState_GET();
4811 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004812 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004813 }
4814
Victor Stinnerda2914d2020-03-20 09:29:08 +01004815 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004816 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004817 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004818
4819 tstate->c_tracefunc = NULL;
4820 tstate->c_traceobj = NULL;
4821 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4822 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4823 Py_XDECREF(traceobj);
4824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004826 tstate->c_traceobj = arg;
4827 tstate->c_tracefunc = func;
4828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004830 tstate->use_tracing = ((func != NULL)
4831 || (tstate->c_profilefunc != NULL));
4832
4833 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004834}
4835
4836void
4837PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4838{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004839 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004840 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004841 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004842 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4843 }
Fred Draked0838392001-06-16 21:02:31 +00004844}
4845
Victor Stinner309d7cc2020-03-13 16:39:12 +01004846
Yury Selivanov75445082015-05-11 22:57:16 -04004847void
Victor Stinner838f2642019-06-13 22:41:23 +02004848_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004849{
4850 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004851 tstate->coroutine_origin_tracking_depth = new_depth;
4852}
4853
4854int
4855_PyEval_GetCoroutineOriginTrackingDepth(void)
4856{
Victor Stinner50b48572018-11-01 01:51:40 +01004857 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004858 return tstate->coroutine_origin_tracking_depth;
4859}
4860
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004861int
Yury Selivanoveb636452016-09-08 22:01:51 -07004862_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4863{
Victor Stinner50b48572018-11-01 01:51:40 +01004864 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004865
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004866 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004867 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004868 }
4869
Yury Selivanoveb636452016-09-08 22:01:51 -07004870 Py_XINCREF(firstiter);
4871 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004872 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004873}
4874
4875PyObject *
4876_PyEval_GetAsyncGenFirstiter(void)
4877{
Victor Stinner50b48572018-11-01 01:51:40 +01004878 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004879 return tstate->async_gen_firstiter;
4880}
4881
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004882int
Yury Selivanoveb636452016-09-08 22:01:51 -07004883_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4884{
Victor Stinner50b48572018-11-01 01:51:40 +01004885 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004886
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004887 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004888 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004889 }
4890
Yury Selivanoveb636452016-09-08 22:01:51 -07004891 Py_XINCREF(finalizer);
4892 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004893 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004894}
4895
4896PyObject *
4897_PyEval_GetAsyncGenFinalizer(void)
4898{
Victor Stinner50b48572018-11-01 01:51:40 +01004899 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004900 return tstate->async_gen_finalizer;
4901}
4902
Victor Stinner438a12d2019-05-24 17:01:38 +02004903PyFrameObject *
4904PyEval_GetFrame(void)
4905{
4906 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004907 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004908}
4909
Guido van Rossumb209a111997-04-29 18:18:01 +00004910PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004911PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004912{
Victor Stinner438a12d2019-05-24 17:01:38 +02004913 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004914 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004916 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 else
4918 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004919}
4920
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004921/* Convenience function to get a builtin from its name */
4922PyObject *
4923_PyEval_GetBuiltinId(_Py_Identifier *name)
4924{
Victor Stinner438a12d2019-05-24 17:01:38 +02004925 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004926 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4927 if (attr) {
4928 Py_INCREF(attr);
4929 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004930 else if (!_PyErr_Occurred(tstate)) {
4931 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004932 }
4933 return attr;
4934}
4935
Guido van Rossumb209a111997-04-29 18:18:01 +00004936PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004937PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004938{
Victor Stinner438a12d2019-05-24 17:01:38 +02004939 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004940 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004941 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004942 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004944 }
4945
Victor Stinner438a12d2019-05-24 17:01:38 +02004946 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004947 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004948 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004949
4950 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004952}
4953
Guido van Rossumb209a111997-04-29 18:18:01 +00004954PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004955PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004956{
Victor Stinner438a12d2019-05-24 17:01:38 +02004957 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004958 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004959 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004962
4963 assert(current_frame->f_globals != NULL);
4964 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004965}
4966
Guido van Rossum6135a871995-01-09 17:53:26 +00004967int
Tim Peters5ba58662001-07-16 02:29:45 +00004968PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004969{
Victor Stinner438a12d2019-05-24 17:01:38 +02004970 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004971 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 if (current_frame != NULL) {
4975 const int codeflags = current_frame->f_code->co_flags;
4976 const int compilerflags = codeflags & PyCF_MASK;
4977 if (compilerflags) {
4978 result = 1;
4979 cf->cf_flags |= compilerflags;
4980 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004981#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 if (codeflags & CO_GENERATOR_ALLOWED) {
4983 result = 1;
4984 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4985 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004986#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 }
4988 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004989}
4990
Guido van Rossum3f5da241990-12-20 15:06:42 +00004991
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004992const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004993PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 if (PyMethod_Check(func))
4996 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4997 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004998 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 else if (PyCFunction_Check(func))
5000 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5001 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005002 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005003}
5004
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005005const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005006PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (PyMethod_Check(func))
5009 return "()";
5010 else if (PyFunction_Check(func))
5011 return "()";
5012 else if (PyCFunction_Check(func))
5013 return "()";
5014 else
5015 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005016}
5017
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005018#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005019if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005020 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5021 tstate, tstate->frame, \
5022 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 x = NULL; \
5024 } \
5025 else { \
5026 x = call; \
5027 if (tstate->c_profilefunc != NULL) { \
5028 if (x == NULL) { \
5029 call_trace_protected(tstate->c_profilefunc, \
5030 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005031 tstate, tstate->frame, \
5032 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 /* XXX should pass (type, value, tb) */ \
5034 } else { \
5035 if (call_trace(tstate->c_profilefunc, \
5036 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005037 tstate, tstate->frame, \
5038 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 Py_DECREF(x); \
5040 x = NULL; \
5041 } \
5042 } \
5043 } \
5044 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005045} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 x = call; \
5047 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005048
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005049
5050static PyObject *
5051trace_call_function(PyThreadState *tstate,
5052 PyObject *func,
5053 PyObject **args, Py_ssize_t nargs,
5054 PyObject *kwnames)
5055{
5056 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005057 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005058 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005059 return x;
5060 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005061 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005062 /* We need to create a temporary bound method as argument
5063 for profiling.
5064
5065 If nargs == 0, then this cannot work because we have no
5066 "self". In any case, the call itself would raise
5067 TypeError (foo needs an argument), so we just skip
5068 profiling. */
5069 PyObject *self = args[0];
5070 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5071 if (func == NULL) {
5072 return NULL;
5073 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005074 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005075 args+1, nargs-1,
5076 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005077 Py_DECREF(func);
5078 return x;
5079 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005080 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005081}
5082
Victor Stinner415c5102017-01-11 00:54:57 +01005083/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5084 to reduce the stack consumption. */
5085Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005086call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005087{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005088 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 PyObject *func = *pfunc;
5090 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005091 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5092 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005093 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005094
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005095 if (tstate->use_tracing) {
5096 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005097 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005098 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005099 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005101
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005103
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005104 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 while ((*pp_stack) > pfunc) {
5106 w = EXT_POP(*pp_stack);
5107 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005111}
5112
Jeremy Hylton52820442001-01-03 23:52:36 +00005113static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005114do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005115{
jdemeyere89de732018-09-19 12:06:20 +02005116 PyObject *result;
5117
scoder4c9ea092020-05-12 16:12:41 +02005118 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005119 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005120 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005122 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005123 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5124 if (nargs > 0 && tstate->use_tracing) {
5125 /* We need to create a temporary bound method as argument
5126 for profiling.
5127
5128 If nargs == 0, then this cannot work because we have no
5129 "self". In any case, the call itself would raise
5130 TypeError (foo needs an argument), so we just skip
5131 profiling. */
5132 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5133 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5134 if (func == NULL) {
5135 return NULL;
5136 }
5137
Victor Stinner4d231bc2019-11-14 13:36:21 +01005138 C_TRACE(result, _PyObject_FastCallDictTstate(
5139 tstate, func,
5140 &_PyTuple_ITEMS(callargs)[1],
5141 nargs - 1,
5142 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005143 Py_DECREF(func);
5144 return result;
5145 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005146 }
jdemeyere89de732018-09-19 12:06:20 +02005147 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005148}
5149
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005150/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005151 nb_index slot defined, and store in *pi.
5152 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005153 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005154 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005155*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005156int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005157_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005158{
Victor Stinner438a12d2019-05-24 17:01:38 +02005159 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005160 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005162 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005164 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 return 0;
5166 }
5167 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005168 _PyErr_SetString(tstate, PyExc_TypeError,
5169 "slice indices must be integers or "
5170 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 return 0;
5172 }
5173 *pi = x;
5174 }
5175 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005176}
5177
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005178int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005179_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005180{
Victor Stinner438a12d2019-05-24 17:01:38 +02005181 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005182 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005183 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005184 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005185 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005186 return 0;
5187 }
5188 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005189 _PyErr_SetString(tstate, PyExc_TypeError,
5190 "slice indices must be integers or "
5191 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005192 return 0;
5193 }
5194 *pi = x;
5195 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005196}
5197
Thomas Wouters52152252000-08-17 22:55:00 +00005198static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005199import_name(PyThreadState *tstate, PyFrameObject *f,
5200 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005201{
5202 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005203 PyObject *import_func, *res;
5204 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005205
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005206 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005207 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005208 if (!_PyErr_Occurred(tstate)) {
5209 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005210 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005211 return NULL;
5212 }
5213
5214 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005215 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005216 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005217 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005218 return NULL;
5219 }
5220 res = PyImport_ImportModuleLevelObject(
5221 name,
5222 f->f_globals,
5223 f->f_locals == NULL ? Py_None : f->f_locals,
5224 fromlist,
5225 ilevel);
5226 return res;
5227 }
5228
5229 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005230
5231 stack[0] = name;
5232 stack[1] = f->f_globals;
5233 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5234 stack[3] = fromlist;
5235 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005236 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005237 Py_DECREF(import_func);
5238 return res;
5239}
5240
5241static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005242import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005245 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005246
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005247 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005248 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005249 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005250 /* Issue #17636: in case this failed because of a circular relative
5251 import, try to fallback on reading the module directly from
5252 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005253 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005254 if (pkgname == NULL) {
5255 goto error;
5256 }
Oren Milman6db70332017-09-19 14:23:01 +03005257 if (!PyUnicode_Check(pkgname)) {
5258 Py_CLEAR(pkgname);
5259 goto error;
5260 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005261 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005262 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005263 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005264 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005265 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005266 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005267 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005268 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005269 goto error;
5270 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005271 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005273 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005274 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005275 if (pkgname == NULL) {
5276 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5277 if (pkgname_or_unknown == NULL) {
5278 Py_XDECREF(pkgpath);
5279 return NULL;
5280 }
5281 } else {
5282 pkgname_or_unknown = pkgname;
5283 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005284
5285 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005286 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005287 errmsg = PyUnicode_FromFormat(
5288 "cannot import name %R from %R (unknown location)",
5289 name, pkgname_or_unknown
5290 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005291 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005292 PyErr_SetImportError(errmsg, pkgname, NULL);
5293 }
5294 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005295 _Py_IDENTIFIER(__spec__);
5296 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005297 const char *fmt =
5298 _PyModuleSpec_IsInitializing(spec) ?
5299 "cannot import name %R from partially initialized module %R "
5300 "(most likely due to a circular import) (%S)" :
5301 "cannot import name %R from %R (%S)";
5302 Py_XDECREF(spec);
5303
5304 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005305 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005306 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005307 }
5308
Xiang Zhang4830f582017-03-21 11:13:42 +08005309 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005310 Py_XDECREF(pkgname_or_unknown);
5311 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005312 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005313}
Guido van Rossumac7be682001-01-17 15:42:30 +00005314
Thomas Wouters52152252000-08-17 22:55:00 +00005315static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005316import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005317{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005318 _Py_IDENTIFIER(__all__);
5319 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005320 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 int skip_leading_underscores = 0;
5322 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005323
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005324 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5325 return -1; /* Unexpected error */
5326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005328 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5329 return -1;
5330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005332 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005333 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 return -1;
5335 }
5336 all = PyMapping_Keys(dict);
5337 Py_DECREF(dict);
5338 if (all == NULL)
5339 return -1;
5340 skip_leading_underscores = 1;
5341 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 for (pos = 0, err = 0; ; pos++) {
5344 name = PySequence_GetItem(all, pos);
5345 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005346 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005348 }
5349 else {
5350 _PyErr_Clear(tstate);
5351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005352 break;
5353 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005354 if (!PyUnicode_Check(name)) {
5355 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5356 if (modname == NULL) {
5357 Py_DECREF(name);
5358 err = -1;
5359 break;
5360 }
5361 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005362 _PyErr_Format(tstate, PyExc_TypeError,
5363 "module __name__ must be a string, not %.100s",
5364 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005365 }
5366 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005367 _PyErr_Format(tstate, PyExc_TypeError,
5368 "%s in %U.%s must be str, not %.100s",
5369 skip_leading_underscores ? "Key" : "Item",
5370 modname,
5371 skip_leading_underscores ? "__dict__" : "__all__",
5372 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005373 }
5374 Py_DECREF(modname);
5375 Py_DECREF(name);
5376 err = -1;
5377 break;
5378 }
5379 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005380 if (PyUnicode_READY(name) == -1) {
5381 Py_DECREF(name);
5382 err = -1;
5383 break;
5384 }
5385 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5386 Py_DECREF(name);
5387 continue;
5388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 }
5390 value = PyObject_GetAttr(v, name);
5391 if (value == NULL)
5392 err = -1;
5393 else if (PyDict_CheckExact(locals))
5394 err = PyDict_SetItem(locals, name, value);
5395 else
5396 err = PyObject_SetItem(locals, name, value);
5397 Py_DECREF(name);
5398 Py_XDECREF(value);
5399 if (err != 0)
5400 break;
5401 }
5402 Py_DECREF(all);
5403 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005404}
5405
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005406static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005407check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005408{
Victor Stinnera102ed72020-02-07 02:24:48 +01005409 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005410 /* check_args_iterable() may be called with a live exception:
5411 * clear it to prevent calling _PyObject_FunctionStr() with an
5412 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005413 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005414 PyObject *funcstr = _PyObject_FunctionStr(func);
5415 if (funcstr != NULL) {
5416 _PyErr_Format(tstate, PyExc_TypeError,
5417 "%U argument after * must be an iterable, not %.200s",
5418 funcstr, Py_TYPE(args)->tp_name);
5419 Py_DECREF(funcstr);
5420 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005421 return -1;
5422 }
5423 return 0;
5424}
5425
5426static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005427format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005428{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005429 /* _PyDict_MergeEx raises attribute
5430 * error (percolated from an attempt
5431 * to get 'keys' attribute) instead of
5432 * a type error if its second argument
5433 * is not a mapping.
5434 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005435 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005436 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005437 PyObject *funcstr = _PyObject_FunctionStr(func);
5438 if (funcstr != NULL) {
5439 _PyErr_Format(
5440 tstate, PyExc_TypeError,
5441 "%U argument after ** must be a mapping, not %.200s",
5442 funcstr, Py_TYPE(kwargs)->tp_name);
5443 Py_DECREF(funcstr);
5444 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005445 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005446 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005447 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005449 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005450 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005451 PyObject *funcstr = _PyObject_FunctionStr(func);
5452 if (funcstr != NULL) {
5453 PyObject *key = PyTuple_GET_ITEM(val, 0);
5454 _PyErr_Format(
5455 tstate, PyExc_TypeError,
5456 "%U got multiple values for keyword argument '%S'",
5457 funcstr, key);
5458 Py_DECREF(funcstr);
5459 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005460 Py_XDECREF(exc);
5461 Py_XDECREF(val);
5462 Py_XDECREF(tb);
5463 }
5464 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005465 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005466 }
5467 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005468}
5469
Guido van Rossumac7be682001-01-17 15:42:30 +00005470static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005471format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5472 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 if (!obj)
5477 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005478
Serhiy Storchaka06515832016-11-20 09:13:07 +02005479 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 if (!obj_str)
5481 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005482
Victor Stinner438a12d2019-05-24 17:01:38 +02005483 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005484}
Guido van Rossum950361c1997-01-24 13:49:28 +00005485
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005486static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005487format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005488{
5489 PyObject *name;
5490 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005491 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005492 return;
5493 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5494 name = PyTuple_GET_ITEM(co->co_cellvars,
5495 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005496 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005497 PyExc_UnboundLocalError,
5498 UNBOUNDLOCAL_ERROR_MSG,
5499 name);
5500 } else {
5501 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5502 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005503 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005504 UNBOUNDFREE_ERROR_MSG, name);
5505 }
5506}
5507
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005508static void
Mark Shannonfee55262019-11-21 09:11:43 +00005509format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005510{
5511 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5512 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005513 _PyErr_Format(tstate, PyExc_TypeError,
5514 "'async with' received an object from __aenter__ "
5515 "that does not implement __await__: %.100s",
5516 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005517 }
Mark Shannonfee55262019-11-21 09:11:43 +00005518 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005519 _PyErr_Format(tstate, PyExc_TypeError,
5520 "'async with' received an object from __aexit__ "
5521 "that does not implement __await__: %.100s",
5522 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005523 }
5524 }
5525}
5526
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005527static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005528unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005529 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005530{
5531 PyObject *res;
5532 if (Py_REFCNT(v) == 2) {
5533 /* In the common case, there are 2 references to the value
5534 * stored in 'variable' when the += is performed: one on the
5535 * value stack (in 'v') and one still stored in the
5536 * 'variable'. We try to delete the variable now to reduce
5537 * the refcnt to 1.
5538 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005539 int opcode, oparg;
5540 NEXTOPARG();
5541 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005542 case STORE_FAST:
5543 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005544 PyObject **fastlocals = f->f_localsplus;
5545 if (GETLOCAL(oparg) == v)
5546 SETLOCAL(oparg, NULL);
5547 break;
5548 }
5549 case STORE_DEREF:
5550 {
5551 PyObject **freevars = (f->f_localsplus +
5552 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005553 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005554 if (PyCell_GET(c) == v) {
5555 PyCell_SET(c, NULL);
5556 Py_DECREF(v);
5557 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005558 break;
5559 }
5560 case STORE_NAME:
5561 {
5562 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005563 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005564 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005565 if (locals && PyDict_CheckExact(locals)) {
5566 PyObject *w = PyDict_GetItemWithError(locals, name);
5567 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005568 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005569 {
5570 Py_DECREF(v);
5571 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005572 }
5573 }
5574 break;
5575 }
5576 }
5577 }
5578 res = v;
5579 PyUnicode_Append(&res, w);
5580 return res;
5581}
5582
Guido van Rossum950361c1997-01-24 13:49:28 +00005583#ifdef DYNAMIC_EXECUTION_PROFILE
5584
Skip Montanarof118cb12001-10-15 20:51:38 +00005585static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005586getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005588 int i;
5589 PyObject *l = PyList_New(256);
5590 if (l == NULL) return NULL;
5591 for (i = 0; i < 256; i++) {
5592 PyObject *x = PyLong_FromLong(a[i]);
5593 if (x == NULL) {
5594 Py_DECREF(l);
5595 return NULL;
5596 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005597 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005598 }
5599 for (i = 0; i < 256; i++)
5600 a[i] = 0;
5601 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005602}
5603
5604PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005605_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005606{
5607#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005609#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005610 int i;
5611 PyObject *l = PyList_New(257);
5612 if (l == NULL) return NULL;
5613 for (i = 0; i < 257; i++) {
5614 PyObject *x = getarray(dxpairs[i]);
5615 if (x == NULL) {
5616 Py_DECREF(l);
5617 return NULL;
5618 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005619 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005620 }
5621 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005622#endif
5623}
5624
5625#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005626
5627Py_ssize_t
5628_PyEval_RequestCodeExtraIndex(freefunc free)
5629{
Victor Stinner81a7be32020-04-14 15:14:01 +02005630 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005631 Py_ssize_t new_index;
5632
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005633 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005634 return -1;
5635 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005636 new_index = interp->co_extra_user_count++;
5637 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005638 return new_index;
5639}
Łukasz Langaa785c872016-09-09 17:37:37 -07005640
5641static void
5642dtrace_function_entry(PyFrameObject *f)
5643{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005644 const char *filename;
5645 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005646 int lineno;
5647
Victor Stinner6d86a232020-04-29 00:56:58 +02005648 PyCodeObject *code = f->f_code;
5649 filename = PyUnicode_AsUTF8(code->co_filename);
5650 funcname = PyUnicode_AsUTF8(code->co_name);
5651 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005652
Andy Lestere6be9b52020-02-11 20:28:35 -06005653 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005654}
5655
5656static void
5657dtrace_function_return(PyFrameObject *f)
5658{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005659 const char *filename;
5660 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005661 int lineno;
5662
Victor Stinner6d86a232020-04-29 00:56:58 +02005663 PyCodeObject *code = f->f_code;
5664 filename = PyUnicode_AsUTF8(code->co_filename);
5665 funcname = PyUnicode_AsUTF8(code->co_name);
5666 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005667
Andy Lestere6be9b52020-02-11 20:28:35 -06005668 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005669}
5670
5671/* DTrace equivalent of maybe_call_line_trace. */
5672static void
5673maybe_dtrace_line(PyFrameObject *frame,
5674 int *instr_lb, int *instr_ub, int *instr_prev)
5675{
5676 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005677 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005678
5679 /* If the last instruction executed isn't in the current
5680 instruction window, reset the window.
5681 */
5682 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5683 PyAddrPair bounds;
5684 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5685 &bounds);
5686 *instr_lb = bounds.ap_lower;
5687 *instr_ub = bounds.ap_upper;
5688 }
5689 /* If the last instruction falls at the start of a line or if
5690 it represents a jump backwards, update the frame's line
5691 number and call the trace function. */
5692 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5693 frame->f_lineno = line;
5694 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5695 if (!co_filename)
5696 co_filename = "?";
5697 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5698 if (!co_name)
5699 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005700 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005701 }
5702 *instr_prev = frame->f_lasti;
5703}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005704
5705
5706/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5707 for the limited API. */
5708
5709#undef Py_EnterRecursiveCall
5710
5711int Py_EnterRecursiveCall(const char *where)
5712{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005713 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005714}
5715
5716#undef Py_LeaveRecursiveCall
5717
5718void Py_LeaveRecursiveCall(void)
5719{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005720 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005721}