blob: 5edcfe354054a62240cd64c064d0d39e3351125c [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
Victor Stinner26881c82020-06-02 15:51:37 +0200436 which are not running in the child process, and clear internal locks
437 which might be held by those threads. */
438PyStatus
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200439_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000440{
Victor Stinner7be4e352020-05-05 20:27:47 +0200441 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200442 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner7be4e352020-05-05 20:27:47 +0200443
444#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
445 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
446#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100447 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200448#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100449 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200450 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200451 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100452 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100453
454 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700455
Victor Stinner50e6e992020-03-19 02:41:21 +0100456 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900457 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200458 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700459 }
Jesse Nollera8513972008-07-17 16:49:17 +0000460
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200461 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100462 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200463 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000464}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900465#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000466
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000467/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600468 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000469
470void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100471_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000472{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200473 assert(is_tstate_valid(tstate));
474 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000475}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000476
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000477PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000478PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000479{
Victor Stinner09532fe2019-05-10 23:39:09 +0200480 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200481#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
482 PyThreadState *old_tstate = _PyThreadState_GET();
483 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
484#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200485 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200486#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200487 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100488
Victor Stinner0b1e3302020-05-05 16:14:31 +0200489 struct _ceval_runtime_state *ceval = &runtime->ceval;
490 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200491#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
492 assert(gil_created(&ceval2->gil));
493#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200494 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200495#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200496 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000498}
499
500void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000502{
Victor Stinner3026cad2020-06-01 16:02:40 +0200503 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100504
Victor Stinner85f5a692020-03-09 22:12:04 +0100505 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100506
Victor Stinner85f5a692020-03-09 22:12:04 +0100507 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
508 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509}
510
511
Guido van Rossuma9672091994-09-14 13:31:22 +0000512/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
513 signal handlers or Mac I/O completion routines) can schedule calls
514 to a function to be called synchronously.
515 The synchronous function is called with one void* argument.
516 It should return 0 for success or -1 for failure -- failure should
517 be accompanied by an exception.
518
519 If registry succeeds, the registry function returns 0; if it fails
520 (e.g. due to too many pending calls) it returns -1 (without setting
521 an exception condition).
522
523 Note that because registry may occur from within signal handlers,
524 or other asynchronous events, calling malloc() is unsafe!
525
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 Any thread can schedule pending calls, but only the main thread
527 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000528 There is no facility to schedule calls to a particular thread, but
529 that should be easy to change, should that ever be required. In
530 that case, the static variables here should go into the python
531 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000533
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200534void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200535_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200536{
537 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100538 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200539 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200540 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200541}
542
Eric Snow5be45a62019-03-08 22:47:07 -0700543/* Push one item onto the queue while holding the lock. */
544static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200545_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600546 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700547{
Eric Snow842a2f02019-03-15 15:47:51 -0600548 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700549 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600550 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700551 return -1; /* Queue full */
552 }
Eric Snow842a2f02019-03-15 15:47:51 -0600553 pending->calls[i].func = func;
554 pending->calls[i].arg = arg;
555 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700556 return 0;
557}
558
559/* Pop one item off the queue while holding the lock. */
560static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200561_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600562 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700563{
Eric Snow842a2f02019-03-15 15:47:51 -0600564 int i = pending->first;
565 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700566 return; /* Queue empty */
567 }
568
Eric Snow842a2f02019-03-15 15:47:51 -0600569 *func = pending->calls[i].func;
570 *arg = pending->calls[i].arg;
571 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700572}
573
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200574/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000575 scheduling to be made from any thread, and even from an executing
576 callback.
577 */
578
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000579int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200580_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200581 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000582{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200583 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600584
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200585 /* Ensure that _PyEval_InitPendingCalls() was called
586 and that _PyEval_FiniPendingCalls() is not called yet. */
587 assert(pending->lock != NULL);
588
Eric Snow842a2f02019-03-15 15:47:51 -0600589 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200590 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600591 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700592
Victor Stinnere225beb2019-06-03 18:14:24 +0200593 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200594 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000596}
597
Victor Stinner09532fe2019-05-10 23:39:09 +0200598int
599Py_AddPendingCall(int (*func)(void *), void *arg)
600{
Victor Stinner50e6e992020-03-19 02:41:21 +0100601 /* Best-effort to support subinterpreters and calls with the GIL released.
602
603 First attempt _PyThreadState_GET() since it supports subinterpreters.
604
605 If the GIL is released, _PyThreadState_GET() returns NULL . In this
606 case, use PyGILState_GetThisThreadState() which works even if the GIL
607 is released.
608
609 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
610 see bpo-10915 and bpo-15751.
611
Victor Stinner8849e592020-03-18 19:28:53 +0100612 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100613 PyThreadState *tstate = _PyThreadState_GET();
614 if (tstate == NULL) {
615 tstate = PyGILState_GetThisThreadState();
616 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200617
618 PyInterpreterState *interp;
619 if (tstate != NULL) {
620 interp = tstate->interp;
621 }
622 else {
623 /* Last resort: use the main interpreter */
624 interp = _PyRuntime.interpreters.main;
625 }
626 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200627}
628
Eric Snowfdf282d2019-01-11 14:26:55 -0700629static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100630handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700631{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200632 assert(is_tstate_valid(tstate));
633 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700634 return 0;
635 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700636
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200637 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100638 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
639 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200640 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700641 return -1;
642 }
643 return 0;
644}
645
646static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100647make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000648{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200649 assert(is_tstate_valid(tstate));
650
Victor Stinnerd8316882020-03-20 14:50:35 +0100651 /* only execute pending calls on main thread */
652 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200653 return 0;
654 }
655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100657 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700658 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700660 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200661 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100662
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200663 /* unsignal before starting to call callbacks, so that any callback
664 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200665 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700666 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100669 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700670 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700671 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 void *arg = NULL;
673
674 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600675 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200676 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600677 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700678
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100679 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700680 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100681 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700682 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 res = func(arg);
684 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200685 goto error;
686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200688
Charles-François Natalif23339a2011-07-23 18:15:43 +0200689 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700690 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200691
692error:
693 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200694 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700695 return res;
696}
697
Eric Snow842a2f02019-03-15 15:47:51 -0600698void
Victor Stinner2b1df452020-01-13 18:46:59 +0100699_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600700{
Eric Snow842a2f02019-03-15 15:47:51 -0600701 assert(PyGILState_Check());
702
Victor Stinner50e6e992020-03-19 02:41:21 +0100703 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200704
Eric Snow842a2f02019-03-15 15:47:51 -0600705 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
706 return;
707 }
708
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100709 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200710 PyObject *exc, *val, *tb;
711 _PyErr_Fetch(tstate, &exc, &val, &tb);
712 PyErr_BadInternalCall();
713 _PyErr_ChainExceptions(exc, val, tb);
714 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600715 }
716}
717
Eric Snowfdf282d2019-01-11 14:26:55 -0700718/* Py_MakePendingCalls() is a simple wrapper for the sake
719 of backward-compatibility. */
720int
721Py_MakePendingCalls(void)
722{
723 assert(PyGILState_Check());
724
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100725 PyThreadState *tstate = _PyThreadState_GET();
726
Eric Snowfdf282d2019-01-11 14:26:55 -0700727 /* Python signal handler doesn't really queue a callback: it only signals
728 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100729 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700730 if (res != 0) {
731 return res;
732 }
733
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100734 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600735 if (res != 0) {
736 return res;
737 }
738
739 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000740}
741
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000742/* The interpreter's recursion limit */
743
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000744#ifndef Py_DEFAULT_RECURSION_LIMIT
745#define Py_DEFAULT_RECURSION_LIMIT 1000
746#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600747
Eric Snow05351c12017-09-05 21:43:08 -0700748int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000749
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600750void
Victor Stinnerdab84232020-03-17 18:56:44 +0100751_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600752{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600753 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinner7be4e352020-05-05 20:27:47 +0200754#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100755 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200756#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100757}
758
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200759int
Victor Stinnerdab84232020-03-17 18:56:44 +0100760_PyEval_InitState(struct _ceval_state *ceval)
761{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200762 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
763
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200764 struct _pending_calls *pending = &ceval->pending;
765 assert(pending->lock == NULL);
766
767 pending->lock = PyThread_allocate_lock();
768 if (pending->lock == NULL) {
769 return -1;
770 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200771
772#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
773 _gil_initialize(&ceval->gil);
774#endif
775
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200776 return 0;
777}
778
779void
780_PyEval_FiniState(struct _ceval_state *ceval)
781{
782 struct _pending_calls *pending = &ceval->pending;
783 if (pending->lock != NULL) {
784 PyThread_free_lock(pending->lock);
785 pending->lock = NULL;
786 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600787}
788
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000789int
790Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000791{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200792 PyThreadState *tstate = _PyThreadState_GET();
793 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000794}
795
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000796void
797Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000798{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200799 PyThreadState *tstate = _PyThreadState_GET();
800 tstate->interp->ceval.recursion_limit = new_limit;
801 if (_Py_IsMainInterpreter(tstate)) {
802 _Py_CheckRecursionLimit = new_limit;
803 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000804}
805
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100806/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000807 if the recursion_depth reaches _Py_CheckRecursionLimit.
808 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
809 to guarantee that _Py_CheckRecursiveCall() is regularly called.
810 Without USE_STACKCHECK, there is no need for this. */
811int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100812_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000813{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200814 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000815
816#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700817 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (PyOS_CheckStack()) {
819 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200820 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 return -1;
822 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200823 if (_Py_IsMainInterpreter(tstate)) {
824 /* Needed for ABI backwards-compatibility (see bpo-31857) */
825 _Py_CheckRecursionLimit = recursion_limit;
826 }
pdox18967932017-10-25 23:03:01 -0700827#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 if (tstate->recursion_critical)
829 /* Somebody asked that we don't check for recursion. */
830 return 0;
831 if (tstate->overflowed) {
832 if (tstate->recursion_depth > recursion_limit + 50) {
833 /* Overflowing while handling an overflow. Give up. */
834 Py_FatalError("Cannot recover from stack overflow.");
835 }
836 return 0;
837 }
838 if (tstate->recursion_depth > recursion_limit) {
839 --tstate->recursion_depth;
840 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200841 _PyErr_Format(tstate, PyExc_RecursionError,
842 "maximum recursion depth exceeded%s",
843 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 return -1;
845 }
846 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000847}
848
Victor Stinner09532fe2019-05-10 23:39:09 +0200849static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200850static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000851
Victor Stinnere225beb2019-06-03 18:14:24 +0200852#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000853
Guido van Rossum374a9221991-04-04 10:40:29 +0000854
Guido van Rossumb209a111997-04-29 18:18:01 +0000855PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000856PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 return PyEval_EvalCodeEx(co,
859 globals, locals,
860 (PyObject **)NULL, 0,
861 (PyObject **)NULL, 0,
862 (PyObject **)NULL, 0,
863 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000864}
865
866
867/* Interpreter main loop */
868
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000869PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100870PyEval_EvalFrame(PyFrameObject *f)
871{
Victor Stinner0b72b232020-03-12 23:18:39 +0100872 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100873 PyThreadState *tstate = _PyThreadState_GET();
874 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000875}
876
877PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000879{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100880 PyThreadState *tstate = _PyThreadState_GET();
881 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700882}
883
Victor Stinnerda2914d2020-03-20 09:29:08 +0100884
885/* Handle signals, pending calls, GIL drop request
886 and asynchronous exception */
887static int
888eval_frame_handle_pending(PyThreadState *tstate)
889{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100890 _PyRuntimeState * const runtime = &_PyRuntime;
891 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200892
893 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200894 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100895 if (handle_signals(tstate) != 0) {
896 return -1;
897 }
898 }
899
900 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200901 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100902 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
903 if (make_pending_calls(tstate) != 0) {
904 return -1;
905 }
906 }
907
908 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200909 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100910 /* Give another thread a chance */
911 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
912 Py_FatalError("tstate mix-up");
913 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200914 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100915
916 /* Other threads may run now */
917
918 take_gil(tstate);
919
Victor Stinnere838a932020-05-05 19:56:48 +0200920#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
921 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
922#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100923 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
924 Py_FatalError("orphan tstate");
925 }
Victor Stinnere838a932020-05-05 19:56:48 +0200926#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100927 }
928
929 /* Check for asynchronous exception. */
930 if (tstate->async_exc != NULL) {
931 PyObject *exc = tstate->async_exc;
932 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200933 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100934 _PyErr_SetNone(tstate, exc);
935 Py_DECREF(exc);
936 return -1;
937 }
938
939 return 0;
940}
941
Victor Stinnerc6944e72016-11-11 02:13:35 +0100942PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100943_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700944{
Victor Stinner3026cad2020-06-01 16:02:40 +0200945 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100946
Guido van Rossum950361c1997-01-24 13:49:28 +0000947#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000949#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200950 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300951 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200952 int opcode; /* Current opcode */
953 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200954 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100956 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100957 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 is true when the line being executed has changed. The
965 initial values are such as to make this false the first
966 time it is tested. */
967 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000968
Serhiy Storchakaab874002016-09-11 13:48:15 +0300969 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject *names;
971 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900972 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000973
Brett Cannon368b4b72012-04-02 12:17:59 -0400974#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200975 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400976#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200977
Antoine Pitroub52ec782009-01-25 16:34:23 +0000978/* Computed GOTOs, or
979 the-optimization-commonly-but-improperly-known-as-"threaded code"
980 using gcc's labels-as-values extension
981 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
982
983 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000985 combined with a lookup table of jump addresses. However, since the
986 indirect jump instruction is shared by all opcodes, the CPU will have a
987 hard time making the right prediction for where to jump next (actually,
988 it will be always wrong except in the uncommon case of a sequence of
989 several identical opcodes).
990
991 "Threaded code" in contrast, uses an explicit jump table and an explicit
992 indirect jump instruction at the end of each opcode. Since the jump
993 instruction is at a different address for each opcode, the CPU will make a
994 separate prediction for each of these instructions, which is equivalent to
995 predicting the second opcode of each opcode pair. These predictions have
996 a much better chance to turn out valid, especially in small bytecode loops.
997
998 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001000 and potentially many more instructions (depending on the pipeline width).
1001 A correctly predicted branch, however, is nearly free.
1002
1003 At the time of this writing, the "threaded code" version is up to 15-20%
1004 faster than the normal "switch" version, depending on the compiler and the
1005 CPU architecture.
1006
1007 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1008 because it would render the measurements invalid.
1009
1010
1011 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1012 indirect jumps by sharing them between all opcodes. Such optimizations
1013 can be disabled on gcc by using the -fno-gcse flag (or possibly
1014 -fno-crossjumping).
1015*/
1016
Antoine Pitrou042b1282010-08-13 21:15:58 +00001017#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001018#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001019#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020#endif
1021
Antoine Pitrou042b1282010-08-13 21:15:58 +00001022#ifdef HAVE_COMPUTED_GOTOS
1023 #ifndef USE_COMPUTED_GOTOS
1024 #define USE_COMPUTED_GOTOS 1
1025 #endif
1026#else
1027 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1028 #error "Computed gotos are not supported on this compiler."
1029 #endif
1030 #undef USE_COMPUTED_GOTOS
1031 #define USE_COMPUTED_GOTOS 0
1032#endif
1033
1034#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001035/* Import the static jump table */
1036#include "opcode_targets.h"
1037
Antoine Pitroub52ec782009-01-25 16:34:23 +00001038#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001039 op: \
1040 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001041
Antoine Pitroub52ec782009-01-25 16:34:23 +00001042#ifdef LLTRACE
1043#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001045 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001047 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001048 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 } \
1050 goto fast_next_opcode; \
1051 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001052#else
1053#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001055 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001057 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001058 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 } \
1060 goto fast_next_opcode; \
1061 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001062#endif
1063
Victor Stinner09532fe2019-05-10 23:39:09 +02001064#define DISPATCH() \
1065 { \
1066 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1067 FAST_DISPATCH(); \
1068 } \
1069 continue; \
1070 }
1071
Antoine Pitroub52ec782009-01-25 16:34:23 +00001072#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001073#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001074#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001075#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001076#endif
1077
1078
Neal Norwitza81d2202002-07-14 00:27:26 +00001079/* Tuple access macros */
1080
1081#ifndef Py_DEBUG
1082#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1083#else
1084#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1085#endif
1086
Guido van Rossum374a9221991-04-04 10:40:29 +00001087/* Code access macros */
1088
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001089/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001090#define INSTR_OFFSET() \
1091 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001092#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001093 _Py_CODEUNIT word = *next_instr; \
1094 opcode = _Py_OPCODE(word); \
1095 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001096 next_instr++; \
1097 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001098#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1099#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001100
Raymond Hettingerf606f872003-03-16 03:11:04 +00001101/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 Some opcodes tend to come in pairs thus making it possible to
1103 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001104 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 Verifying the prediction costs a single high-speed test of a register
1107 variable against a constant. If the pairing was good, then the
1108 processor's own internal branch predication has a high likelihood of
1109 success, resulting in a nearly zero-overhead transition to the
1110 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001111 including its unpredictable switch-case branch. Combined with the
1112 processor's internal branch prediction, a successful PREDICT has the
1113 effect of making the two opcodes run as if they were a single new opcode
1114 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001115
Georg Brandl86b2fb92008-07-16 03:43:04 +00001116 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 predictions turned-on and interpret the results as if some opcodes
1118 had been combined or turn-off predictions so that the opcode frequency
1119 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001120
1121 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 the CPU to record separate branch prediction information for each
1123 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001124
Raymond Hettingerf606f872003-03-16 03:11:04 +00001125*/
1126
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001127#define PREDICT_ID(op) PRED_##op
1128
Antoine Pitrou042b1282010-08-13 21:15:58 +00001129#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001130#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001131#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001132#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001133 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001134 _Py_CODEUNIT word = *next_instr; \
1135 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001136 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001137 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001138 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001139 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001140 } \
1141 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001142#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001143#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001144
Raymond Hettingerf606f872003-03-16 03:11:04 +00001145
Guido van Rossum374a9221991-04-04 10:40:29 +00001146/* Stack manipulation macros */
1147
Martin v. Löwis18e16552006-02-15 17:27:45 +00001148/* The stack can grow at most MAXINT deep, as co_nlocals and
1149 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001150#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1151#define EMPTY() (STACK_LEVEL() == 0)
1152#define TOP() (stack_pointer[-1])
1153#define SECOND() (stack_pointer[-2])
1154#define THIRD() (stack_pointer[-3])
1155#define FOURTH() (stack_pointer[-4])
1156#define PEEK(n) (stack_pointer[-(n)])
1157#define SET_TOP(v) (stack_pointer[-1] = (v))
1158#define SET_SECOND(v) (stack_pointer[-2] = (v))
1159#define SET_THIRD(v) (stack_pointer[-3] = (v))
1160#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1161#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1162#define BASIC_STACKADJ(n) (stack_pointer += n)
1163#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1164#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001165
Guido van Rossum96a42c81992-01-12 02:29:51 +00001166#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001168 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001169 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001170#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001171 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001172#define STACK_GROW(n) do { \
1173 assert(n >= 0); \
1174 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001175 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001176 assert(STACK_LEVEL() <= co->co_stacksize); \
1177 } while (0)
1178#define STACK_SHRINK(n) do { \
1179 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001180 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001181 (void)(BASIC_STACKADJ(-n)); \
1182 assert(STACK_LEVEL() <= co->co_stacksize); \
1183 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001184#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001185 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001186 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001187#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001188#define PUSH(v) BASIC_PUSH(v)
1189#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001190#define STACK_GROW(n) BASIC_STACKADJ(n)
1191#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001192#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001193#endif
1194
Guido van Rossum681d79a1995-07-18 14:51:37 +00001195/* Local variable macros */
1196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001198
1199/* The SETLOCAL() macro must not DECREF the local variable in-place and
1200 then store the new value; it must copy the old value to a temporary
1201 value, then store the new value, and then DECREF the temporary value.
1202 This is because it is possible that during the DECREF the frame is
1203 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1204 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001206 GETLOCAL(i) = value; \
1207 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001208
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001209
1210#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 while (STACK_LEVEL() > (b)->b_level) { \
1212 PyObject *v = POP(); \
1213 Py_XDECREF(v); \
1214 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001215
1216#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001217 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001219 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1221 while (STACK_LEVEL() > (b)->b_level + 3) { \
1222 value = POP(); \
1223 Py_XDECREF(value); \
1224 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001225 exc_info = tstate->exc_info; \
1226 type = exc_info->exc_type; \
1227 value = exc_info->exc_value; \
1228 traceback = exc_info->exc_traceback; \
1229 exc_info->exc_type = POP(); \
1230 exc_info->exc_value = POP(); \
1231 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_XDECREF(type); \
1233 Py_XDECREF(value); \
1234 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001235 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001236
Inada Naoki91234a12019-06-03 21:30:58 +09001237 /* macros for opcode cache */
1238#define OPCACHE_CHECK() \
1239 do { \
1240 co_opcache = NULL; \
1241 if (co->co_opcache != NULL) { \
1242 unsigned char co_opt_offset = \
1243 co->co_opcache_map[next_instr - first_instr]; \
1244 if (co_opt_offset > 0) { \
1245 assert(co_opt_offset <= co->co_opcache_size); \
1246 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1247 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001248 } \
1249 } \
1250 } while (0)
1251
1252#if OPCACHE_STATS
1253
1254#define OPCACHE_STAT_GLOBAL_HIT() \
1255 do { \
1256 if (co->co_opcache != NULL) opcache_global_hits++; \
1257 } while (0)
1258
1259#define OPCACHE_STAT_GLOBAL_MISS() \
1260 do { \
1261 if (co->co_opcache != NULL) opcache_global_misses++; \
1262 } while (0)
1263
1264#define OPCACHE_STAT_GLOBAL_OPT() \
1265 do { \
1266 if (co->co_opcache != NULL) opcache_global_opts++; \
1267 } while (0)
1268
1269#else /* OPCACHE_STATS */
1270
1271#define OPCACHE_STAT_GLOBAL_HIT()
1272#define OPCACHE_STAT_GLOBAL_MISS()
1273#define OPCACHE_STAT_GLOBAL_OPT()
1274
1275#endif
1276
Guido van Rossuma027efa1997-05-05 20:56:21 +00001277/* Start of code */
1278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001280 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001282 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 if (tstate->use_tracing) {
1287 if (tstate->c_tracefunc != NULL) {
1288 /* tstate->c_tracefunc, if defined, is a
1289 function that will be called on *every* entry
1290 to a code block. Its return value, if not
1291 None, is a function that will be called at
1292 the start of each executed line of code.
1293 (Actually, the function must return itself
1294 in order to continue tracing.) The trace
1295 functions are called with three arguments:
1296 a pointer to the current frame, a string
1297 indicating why the function is called, and
1298 an argument which depends on the situation.
1299 The global trace function is also called
1300 whenever an exception is detected. */
1301 if (call_trace_protected(tstate->c_tracefunc,
1302 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001303 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 /* Trace function raised an error */
1305 goto exit_eval_frame;
1306 }
1307 }
1308 if (tstate->c_profilefunc != NULL) {
1309 /* Similar for c_profilefunc, except it needn't
1310 return itself and isn't called for "line" events */
1311 if (call_trace_protected(tstate->c_profilefunc,
1312 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001313 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* Profile function raised an error */
1315 goto exit_eval_frame;
1316 }
1317 }
1318 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001319
Łukasz Langaa785c872016-09-09 17:37:37 -07001320 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1321 dtrace_function_entry(f);
1322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 co = f->f_code;
1324 names = co->co_names;
1325 consts = co->co_consts;
1326 fastlocals = f->f_localsplus;
1327 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001328 assert(PyBytes_Check(co->co_code));
1329 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001330 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1331 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1332 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001333 /*
1334 f->f_lasti refers to the index of the last instruction,
1335 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001336
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001337 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001338 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 When the PREDICT() macros are enabled, some opcode pairs follow in
1341 direct succession without updating f->f_lasti. A successful
1342 prediction effectively links the two codes together as if they
1343 were a single new opcode; accordingly,f->f_lasti will point to
1344 the first code in the pair (for instance, GET_ITER followed by
1345 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001346 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001348 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001349 next_instr = first_instr;
1350 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001351 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1352 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 stack_pointer = f->f_stacktop;
1355 assert(stack_pointer != NULL);
1356 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001357 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001358
Inada Naoki91234a12019-06-03 21:30:58 +09001359 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1360 co->co_opcache_flag++;
1361 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1362 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001363 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001364 }
1365#if OPCACHE_STATS
1366 opcache_code_objects_extra_mem +=
1367 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1368 sizeof(_PyOpcache) * co->co_opcache_size;
1369 opcache_code_objects++;
1370#endif
1371 }
1372 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001373
Tim Peters5ca576e2001-06-18 22:08:13 +00001374#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001375 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001376#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 if (throwflag) /* support for generator.throw() */
1379 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380
Victor Stinnerace47d72013-07-18 01:41:08 +02001381#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001382 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001383 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001384 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001385 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001386#endif
1387
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001388main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1391 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001392 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* Do periodic things. Doing this every time through
1395 the loop would add too much overhead, so we do it
1396 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001397 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 event needs attention (e.g. a signal handler or
1399 async I/O handler); see Py_AddPendingCall() and
1400 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Eric Snow7bda9de2019-03-08 17:25:54 -07001402 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001403 opcode = _Py_OPCODE(*next_instr);
1404 if (opcode == SETUP_FINALLY ||
1405 opcode == SETUP_WITH ||
1406 opcode == BEFORE_ASYNC_WITH ||
1407 opcode == YIELD_FROM) {
1408 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001409 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001410 - If we're about to enter the 'with:'. It will prevent
1411 emitting a resource warning in the common idiom
1412 'with open(path) as file:'.
1413 - If we're about to enter the 'async with:'.
1414 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001415 *very* useful, but might help in some cases and it's
1416 traditional)
1417 - If we're resuming a chain of nested 'yield from' or
1418 'await' calls, then each frame is parked with YIELD_FROM
1419 as its next opcode. If the user hit control-C we want to
1420 wait until we've reached the innermost frame before
1421 running the signal handler and raising KeyboardInterrupt
1422 (see bpo-30039).
1423 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 goto fast_next_opcode;
1425 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001426
Victor Stinnerda2914d2020-03-20 09:29:08 +01001427 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 }
1430 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 fast_next_opcode:
1433 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Łukasz Langaa785c872016-09-09 17:37:37 -07001435 if (PyDTrace_LINE_ENABLED())
1436 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001439
Victor Stinnerdab84232020-03-17 18:56:44 +01001440 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001441 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 /* see maybe_call_line_trace
1444 for expository comments */
1445 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 err = maybe_call_line_trace(tstate->c_tracefunc,
1448 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001449 tstate, f,
1450 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 /* Reload possibly changed frame fields */
1452 JUMPTO(f->f_lasti);
1453 if (f->f_stacktop != NULL) {
1454 stack_pointer = f->f_stacktop;
1455 f->f_stacktop = NULL;
1456 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001463
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001464 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001465 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001466#ifdef DYNAMIC_EXECUTION_PROFILE
1467#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 dxpairs[lastopcode][opcode]++;
1469 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001470#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001472#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001473
Guido van Rossum96a42c81992-01-12 02:29:51 +00001474#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (lltrace) {
1478 if (HAS_ARG(opcode)) {
1479 printf("%d: %d, %d\n",
1480 f->f_lasti, opcode, oparg);
1481 }
1482 else {
1483 printf("%d: %d\n",
1484 f->f_lasti, opcode);
1485 }
1486 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001487#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001492 It is essential that any operation that fails must goto error
1493 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001498
Benjamin Petersonddd19492018-09-16 22:38:02 -07001499 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 PyObject *value = GETLOCAL(oparg);
1501 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001502 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 UNBOUNDLOCAL_ERROR_MSG,
1504 PyTuple_GetItem(co->co_varnames, oparg));
1505 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 Py_INCREF(value);
1508 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 }
1511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(LOAD_CONST): {
1513 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *value = GETITEM(consts, oparg);
1515 Py_INCREF(value);
1516 PUSH(value);
1517 FAST_DISPATCH();
1518 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001519
Benjamin Petersonddd19492018-09-16 22:38:02 -07001520 case TARGET(STORE_FAST): {
1521 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *value = POP();
1523 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001526
Benjamin Petersonddd19492018-09-16 22:38:02 -07001527 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001528 PyObject *value = POP();
1529 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Petersonddd19492018-09-16 22:38:02 -07001533 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 PyObject *top = TOP();
1535 PyObject *second = SECOND();
1536 SET_TOP(second);
1537 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Benjamin Petersonddd19492018-09-16 22:38:02 -07001541 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001542 PyObject *top = TOP();
1543 PyObject *second = SECOND();
1544 PyObject *third = THIRD();
1545 SET_TOP(second);
1546 SET_SECOND(third);
1547 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Benjamin Petersonddd19492018-09-16 22:38:02 -07001551 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001552 PyObject *top = TOP();
1553 PyObject *second = SECOND();
1554 PyObject *third = THIRD();
1555 PyObject *fourth = FOURTH();
1556 SET_TOP(second);
1557 SET_SECOND(third);
1558 SET_THIRD(fourth);
1559 SET_FOURTH(top);
1560 FAST_DISPATCH();
1561 }
1562
Benjamin Petersonddd19492018-09-16 22:38:02 -07001563 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001564 PyObject *top = TOP();
1565 Py_INCREF(top);
1566 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Benjamin Petersonddd19492018-09-16 22:38:02 -07001570 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001573 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001575 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 SET_TOP(top);
1577 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001578 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001580
Benjamin Petersonddd19492018-09-16 22:38:02 -07001581 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 PyObject *value = TOP();
1583 PyObject *res = PyNumber_Positive(value);
1584 Py_DECREF(value);
1585 SET_TOP(res);
1586 if (res == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Benjamin Petersonddd19492018-09-16 22:38:02 -07001591 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 PyObject *value = TOP();
1593 PyObject *res = PyNumber_Negative(value);
1594 Py_DECREF(value);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Benjamin Petersonddd19492018-09-16 22:38:02 -07001601 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 PyObject *value = TOP();
1603 int err = PyObject_IsTrue(value);
1604 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 if (err == 0) {
1606 Py_INCREF(Py_True);
1607 SET_TOP(Py_True);
1608 DISPATCH();
1609 }
1610 else if (err > 0) {
1611 Py_INCREF(Py_False);
1612 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 DISPATCH();
1614 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001615 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 goto error;
1617 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Benjamin Petersonddd19492018-09-16 22:38:02 -07001619 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 PyObject *value = TOP();
1621 PyObject *res = PyNumber_Invert(value);
1622 Py_DECREF(value);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Benjamin Petersonddd19492018-09-16 22:38:02 -07001629 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 PyObject *exp = POP();
1631 PyObject *base = TOP();
1632 PyObject *res = PyNumber_Power(base, exp, Py_None);
1633 Py_DECREF(base);
1634 Py_DECREF(exp);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Petersonddd19492018-09-16 22:38:02 -07001641 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *res = PyNumber_Multiply(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(res);
1648 if (res == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001654 PyObject *right = POP();
1655 PyObject *left = TOP();
1656 PyObject *res = PyNumber_MatrixMultiply(left, right);
1657 Py_DECREF(left);
1658 Py_DECREF(right);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
1664
Benjamin Petersonddd19492018-09-16 22:38:02 -07001665 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 PyObject *divisor = POP();
1667 PyObject *dividend = TOP();
1668 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1669 Py_DECREF(dividend);
1670 Py_DECREF(divisor);
1671 SET_TOP(quotient);
1672 if (quotient == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Benjamin Petersonddd19492018-09-16 22:38:02 -07001677 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 PyObject *divisor = POP();
1679 PyObject *dividend = TOP();
1680 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1681 Py_DECREF(dividend);
1682 Py_DECREF(divisor);
1683 SET_TOP(quotient);
1684 if (quotient == NULL)
1685 goto error;
1686 DISPATCH();
1687 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001688
Benjamin Petersonddd19492018-09-16 22:38:02 -07001689 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 PyObject *divisor = POP();
1691 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001692 PyObject *res;
1693 if (PyUnicode_CheckExact(dividend) && (
1694 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1695 // fast path; string formatting, but not if the RHS is a str subclass
1696 // (see issue28598)
1697 res = PyUnicode_Format(dividend, divisor);
1698 } else {
1699 res = PyNumber_Remainder(dividend, divisor);
1700 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 Py_DECREF(divisor);
1702 Py_DECREF(dividend);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Benjamin Petersonddd19492018-09-16 22:38:02 -07001709 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 PyObject *right = POP();
1711 PyObject *left = TOP();
1712 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001713 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1714 CPython using bytecode, it is simply worthless.
1715 See http://bugs.python.org/issue21955 and
1716 http://bugs.python.org/issue10044 for the discussion. In short,
1717 no patch shown any impact on a realistic benchmark, only a minor
1718 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 if (PyUnicode_CheckExact(left) &&
1720 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001721 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001722 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001723 }
1724 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 sum = PyNumber_Add(left, right);
1726 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001727 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 Py_DECREF(right);
1729 SET_TOP(sum);
1730 if (sum == NULL)
1731 goto error;
1732 DISPATCH();
1733 }
1734
Benjamin Petersonddd19492018-09-16 22:38:02 -07001735 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 PyObject *right = POP();
1737 PyObject *left = TOP();
1738 PyObject *diff = PyNumber_Subtract(left, right);
1739 Py_DECREF(right);
1740 Py_DECREF(left);
1741 SET_TOP(diff);
1742 if (diff == NULL)
1743 goto error;
1744 DISPATCH();
1745 }
1746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 PyObject *sub = POP();
1749 PyObject *container = TOP();
1750 PyObject *res = PyObject_GetItem(container, sub);
1751 Py_DECREF(container);
1752 Py_DECREF(sub);
1753 SET_TOP(res);
1754 if (res == NULL)
1755 goto error;
1756 DISPATCH();
1757 }
1758
Benjamin Petersonddd19492018-09-16 22:38:02 -07001759 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 PyObject *right = POP();
1761 PyObject *left = TOP();
1762 PyObject *res = PyNumber_Lshift(left, right);
1763 Py_DECREF(left);
1764 Py_DECREF(right);
1765 SET_TOP(res);
1766 if (res == NULL)
1767 goto error;
1768 DISPATCH();
1769 }
1770
Benjamin Petersonddd19492018-09-16 22:38:02 -07001771 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 PyObject *right = POP();
1773 PyObject *left = TOP();
1774 PyObject *res = PyNumber_Rshift(left, right);
1775 Py_DECREF(left);
1776 Py_DECREF(right);
1777 SET_TOP(res);
1778 if (res == NULL)
1779 goto error;
1780 DISPATCH();
1781 }
1782
Benjamin Petersonddd19492018-09-16 22:38:02 -07001783 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 PyObject *right = POP();
1785 PyObject *left = TOP();
1786 PyObject *res = PyNumber_And(left, right);
1787 Py_DECREF(left);
1788 Py_DECREF(right);
1789 SET_TOP(res);
1790 if (res == NULL)
1791 goto error;
1792 DISPATCH();
1793 }
1794
Benjamin Petersonddd19492018-09-16 22:38:02 -07001795 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001796 PyObject *right = POP();
1797 PyObject *left = TOP();
1798 PyObject *res = PyNumber_Xor(left, right);
1799 Py_DECREF(left);
1800 Py_DECREF(right);
1801 SET_TOP(res);
1802 if (res == NULL)
1803 goto error;
1804 DISPATCH();
1805 }
1806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 PyObject *right = POP();
1809 PyObject *left = TOP();
1810 PyObject *res = PyNumber_Or(left, right);
1811 Py_DECREF(left);
1812 Py_DECREF(right);
1813 SET_TOP(res);
1814 if (res == NULL)
1815 goto error;
1816 DISPATCH();
1817 }
1818
Benjamin Petersonddd19492018-09-16 22:38:02 -07001819 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyObject *v = POP();
1821 PyObject *list = PEEK(oparg);
1822 int err;
1823 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 if (err != 0)
1826 goto error;
1827 PREDICT(JUMP_ABSOLUTE);
1828 DISPATCH();
1829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Benjamin Petersonddd19492018-09-16 22:38:02 -07001831 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001833 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 int err;
1835 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 if (err != 0)
1838 goto error;
1839 PREDICT(JUMP_ABSOLUTE);
1840 DISPATCH();
1841 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001842
Benjamin Petersonddd19492018-09-16 22:38:02 -07001843 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 PyObject *exp = POP();
1845 PyObject *base = TOP();
1846 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1847 Py_DECREF(base);
1848 Py_DECREF(exp);
1849 SET_TOP(res);
1850 if (res == NULL)
1851 goto error;
1852 DISPATCH();
1853 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001854
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *right = POP();
1857 PyObject *left = TOP();
1858 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1859 Py_DECREF(left);
1860 Py_DECREF(right);
1861 SET_TOP(res);
1862 if (res == NULL)
1863 goto error;
1864 DISPATCH();
1865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001868 PyObject *right = POP();
1869 PyObject *left = TOP();
1870 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1871 Py_DECREF(left);
1872 Py_DECREF(right);
1873 SET_TOP(res);
1874 if (res == NULL)
1875 goto error;
1876 DISPATCH();
1877 }
1878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *divisor = POP();
1881 PyObject *dividend = TOP();
1882 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1883 Py_DECREF(dividend);
1884 Py_DECREF(divisor);
1885 SET_TOP(quotient);
1886 if (quotient == NULL)
1887 goto error;
1888 DISPATCH();
1889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *divisor = POP();
1893 PyObject *dividend = TOP();
1894 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1895 Py_DECREF(dividend);
1896 Py_DECREF(divisor);
1897 SET_TOP(quotient);
1898 if (quotient == NULL)
1899 goto error;
1900 DISPATCH();
1901 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 PyObject *right = POP();
1905 PyObject *left = TOP();
1906 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1907 Py_DECREF(left);
1908 Py_DECREF(right);
1909 SET_TOP(mod);
1910 if (mod == NULL)
1911 goto error;
1912 DISPATCH();
1913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001914
Benjamin Petersonddd19492018-09-16 22:38:02 -07001915 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 PyObject *right = POP();
1917 PyObject *left = TOP();
1918 PyObject *sum;
1919 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001920 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001921 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001922 }
1923 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 sum = PyNumber_InPlaceAdd(left, right);
1925 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 Py_DECREF(right);
1928 SET_TOP(sum);
1929 if (sum == NULL)
1930 goto error;
1931 DISPATCH();
1932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Benjamin Petersonddd19492018-09-16 22:38:02 -07001934 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 PyObject *right = POP();
1936 PyObject *left = TOP();
1937 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1938 Py_DECREF(left);
1939 Py_DECREF(right);
1940 SET_TOP(diff);
1941 if (diff == NULL)
1942 goto error;
1943 DISPATCH();
1944 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001945
Benjamin Petersonddd19492018-09-16 22:38:02 -07001946 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 PyObject *right = POP();
1948 PyObject *left = TOP();
1949 PyObject *res = PyNumber_InPlaceLshift(left, right);
1950 Py_DECREF(left);
1951 Py_DECREF(right);
1952 SET_TOP(res);
1953 if (res == NULL)
1954 goto error;
1955 DISPATCH();
1956 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001957
Benjamin Petersonddd19492018-09-16 22:38:02 -07001958 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 PyObject *right = POP();
1960 PyObject *left = TOP();
1961 PyObject *res = PyNumber_InPlaceRshift(left, right);
1962 Py_DECREF(left);
1963 Py_DECREF(right);
1964 SET_TOP(res);
1965 if (res == NULL)
1966 goto error;
1967 DISPATCH();
1968 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Benjamin Petersonddd19492018-09-16 22:38:02 -07001970 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 PyObject *right = POP();
1972 PyObject *left = TOP();
1973 PyObject *res = PyNumber_InPlaceAnd(left, right);
1974 Py_DECREF(left);
1975 Py_DECREF(right);
1976 SET_TOP(res);
1977 if (res == NULL)
1978 goto error;
1979 DISPATCH();
1980 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Benjamin Petersonddd19492018-09-16 22:38:02 -07001982 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 PyObject *right = POP();
1984 PyObject *left = TOP();
1985 PyObject *res = PyNumber_InPlaceXor(left, right);
1986 Py_DECREF(left);
1987 Py_DECREF(right);
1988 SET_TOP(res);
1989 if (res == NULL)
1990 goto error;
1991 DISPATCH();
1992 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 PyObject *right = POP();
1996 PyObject *left = TOP();
1997 PyObject *res = PyNumber_InPlaceOr(left, right);
1998 Py_DECREF(left);
1999 Py_DECREF(right);
2000 SET_TOP(res);
2001 if (res == NULL)
2002 goto error;
2003 DISPATCH();
2004 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002005
Benjamin Petersonddd19492018-09-16 22:38:02 -07002006 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 PyObject *sub = TOP();
2008 PyObject *container = SECOND();
2009 PyObject *v = THIRD();
2010 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002011 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002012 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 Py_DECREF(container);
2016 Py_DECREF(sub);
2017 if (err != 0)
2018 goto error;
2019 DISPATCH();
2020 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 PyObject *sub = TOP();
2024 PyObject *container = SECOND();
2025 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002026 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002027 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 err = PyObject_DelItem(container, sub);
2029 Py_DECREF(container);
2030 Py_DECREF(sub);
2031 if (err != 0)
2032 goto error;
2033 DISPATCH();
2034 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002035
Benjamin Petersonddd19492018-09-16 22:38:02 -07002036 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002037 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002039 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002040 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002042 _PyErr_SetString(tstate, PyExc_RuntimeError,
2043 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 Py_DECREF(value);
2045 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002047 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 Py_DECREF(value);
2049 if (res == NULL)
2050 goto error;
2051 Py_DECREF(res);
2052 DISPATCH();
2053 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002054
Benjamin Petersonddd19492018-09-16 22:38:02 -07002055 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 switch (oparg) {
2058 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002060 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002063 /* fall through */
2064 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002065 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002066 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 break;
2069 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002070 _PyErr_SetString(tstate, PyExc_SystemError,
2071 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 break;
2073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 goto error;
2075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Benjamin Petersonddd19492018-09-16 22:38:02 -07002077 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002079 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002080 assert(EMPTY());
2081 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002083
Benjamin Petersonddd19492018-09-16 22:38:02 -07002084 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002085 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002086 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002087 PyObject *obj = TOP();
2088 PyTypeObject *type = Py_TYPE(obj);
2089
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002090 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002091 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002092 }
Yury Selivanov75445082015-05-11 22:57:16 -04002093
2094 if (getter != NULL) {
2095 iter = (*getter)(obj);
2096 Py_DECREF(obj);
2097 if (iter == NULL) {
2098 SET_TOP(NULL);
2099 goto error;
2100 }
2101 }
2102 else {
2103 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002104 _PyErr_Format(tstate, PyExc_TypeError,
2105 "'async for' requires an object with "
2106 "__aiter__ method, got %.100s",
2107 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002108 Py_DECREF(obj);
2109 goto error;
2110 }
2111
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002112 if (Py_TYPE(iter)->tp_as_async == NULL ||
2113 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002114
Yury Selivanov398ff912017-03-02 22:20:00 -05002115 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002116 _PyErr_Format(tstate, PyExc_TypeError,
2117 "'async for' received an object from __aiter__ "
2118 "that does not implement __anext__: %.100s",
2119 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002120 Py_DECREF(iter);
2121 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002122 }
2123
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002124 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002125 DISPATCH();
2126 }
2127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002129 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002130 PyObject *next_iter = NULL;
2131 PyObject *awaitable = NULL;
2132 PyObject *aiter = TOP();
2133 PyTypeObject *type = Py_TYPE(aiter);
2134
Yury Selivanoveb636452016-09-08 22:01:51 -07002135 if (PyAsyncGen_CheckExact(aiter)) {
2136 awaitable = type->tp_as_async->am_anext(aiter);
2137 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002138 goto error;
2139 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002140 } else {
2141 if (type->tp_as_async != NULL){
2142 getter = type->tp_as_async->am_anext;
2143 }
Yury Selivanov75445082015-05-11 22:57:16 -04002144
Yury Selivanoveb636452016-09-08 22:01:51 -07002145 if (getter != NULL) {
2146 next_iter = (*getter)(aiter);
2147 if (next_iter == NULL) {
2148 goto error;
2149 }
2150 }
2151 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002152 _PyErr_Format(tstate, PyExc_TypeError,
2153 "'async for' requires an iterator with "
2154 "__anext__ method, got %.100s",
2155 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002156 goto error;
2157 }
Yury Selivanov75445082015-05-11 22:57:16 -04002158
Yury Selivanoveb636452016-09-08 22:01:51 -07002159 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2160 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002161 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002162 PyExc_TypeError,
2163 "'async for' received an invalid object "
2164 "from __anext__: %.100s",
2165 Py_TYPE(next_iter)->tp_name);
2166
2167 Py_DECREF(next_iter);
2168 goto error;
2169 } else {
2170 Py_DECREF(next_iter);
2171 }
2172 }
Yury Selivanov75445082015-05-11 22:57:16 -04002173
2174 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002175 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002176 DISPATCH();
2177 }
2178
Benjamin Petersonddd19492018-09-16 22:38:02 -07002179 case TARGET(GET_AWAITABLE): {
2180 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002181 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002182 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002183
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002184 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002185 int opcode_at_minus_3 = 0;
2186 if ((next_instr - first_instr) > 2) {
2187 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2188 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002189 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002190 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002191 _Py_OPCODE(next_instr[-2]));
2192 }
2193
Yury Selivanov75445082015-05-11 22:57:16 -04002194 Py_DECREF(iterable);
2195
Yury Selivanovc724bae2016-03-02 11:30:46 -05002196 if (iter != NULL && PyCoro_CheckExact(iter)) {
2197 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2198 if (yf != NULL) {
2199 /* `iter` is a coroutine object that is being
2200 awaited, `yf` is a pointer to the current awaitable
2201 being awaited on. */
2202 Py_DECREF(yf);
2203 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002204 _PyErr_SetString(tstate, PyExc_RuntimeError,
2205 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002206 /* The code below jumps to `error` if `iter` is NULL. */
2207 }
2208 }
2209
Yury Selivanov75445082015-05-11 22:57:16 -04002210 SET_TOP(iter); /* Even if it's NULL */
2211
2212 if (iter == NULL) {
2213 goto error;
2214 }
2215
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002216 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002217 DISPATCH();
2218 }
2219
Benjamin Petersonddd19492018-09-16 22:38:02 -07002220 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002222 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002224 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2225 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002226 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002227 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002229 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002230 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002231 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 Py_DECREF(v);
2234 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002235 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002236 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002237 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002238 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002239 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 if (err < 0)
2241 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002242 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 SET_TOP(val);
2244 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002245 }
Martin Panter95f53c12016-07-18 08:23:26 +00002246 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002247 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002248 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002249 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002250 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002251 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002253
Benjamin Petersonddd19492018-09-16 22:38:02 -07002254 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002256
2257 if (co->co_flags & CO_ASYNC_GENERATOR) {
2258 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2259 Py_DECREF(retval);
2260 if (w == NULL) {
2261 retval = NULL;
2262 goto error;
2263 }
2264 retval = w;
2265 }
2266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002268 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002270
Benjamin Petersonddd19492018-09-16 22:38:02 -07002271 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002272 PyObject *type, *value, *traceback;
2273 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 PyTryBlock *b = PyFrame_BlockPop(f);
2275 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002276 _PyErr_SetString(tstate, PyExc_SystemError,
2277 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002280 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2281 STACK_LEVEL() <= (b)->b_level + 4);
2282 exc_info = tstate->exc_info;
2283 type = exc_info->exc_type;
2284 value = exc_info->exc_value;
2285 traceback = exc_info->exc_traceback;
2286 exc_info->exc_type = POP();
2287 exc_info->exc_value = POP();
2288 exc_info->exc_traceback = POP();
2289 Py_XDECREF(type);
2290 Py_XDECREF(value);
2291 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002294
Benjamin Petersonddd19492018-09-16 22:38:02 -07002295 case TARGET(POP_BLOCK): {
2296 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002297 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Mark Shannonfee55262019-11-21 09:11:43 +00002301 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002302 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002303 PyObject *val = POP();
2304 PyObject *tb = POP();
2305 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002306 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002307 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Benjamin Petersonddd19492018-09-16 22:38:02 -07002310 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002311 PyObject *exc = POP();
2312 assert(PyExceptionClass_Check(exc));
2313 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2314 PyTryBlock *b = PyFrame_BlockPop(f);
2315 assert(b->b_type == EXCEPT_HANDLER);
2316 Py_DECREF(exc);
2317 UNWIND_EXCEPT_HANDLER(b);
2318 Py_DECREF(POP());
2319 JUMPBY(oparg);
2320 FAST_DISPATCH();
2321 }
2322 else {
2323 PyObject *val = POP();
2324 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002325 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002326 goto exception_unwind;
2327 }
2328 }
2329
Zackery Spytzce6a0702019-08-25 03:44:09 -06002330 case TARGET(LOAD_ASSERTION_ERROR): {
2331 PyObject *value = PyExc_AssertionError;
2332 Py_INCREF(value);
2333 PUSH(value);
2334 FAST_DISPATCH();
2335 }
2336
Benjamin Petersonddd19492018-09-16 22:38:02 -07002337 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002338 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002339
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002341 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002342 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002344 if (!_PyErr_Occurred(tstate)) {
2345 _PyErr_SetString(tstate, PyExc_NameError,
2346 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002351 }
2352 else {
2353 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2354 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002355 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2357 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2359 _PyErr_SetString(tstate, PyExc_NameError,
2360 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002365 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002366 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002367
Benjamin Petersonddd19492018-09-16 22:38:02 -07002368 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 PyObject *name = GETITEM(names, oparg);
2370 PyObject *v = POP();
2371 PyObject *ns = f->f_locals;
2372 int err;
2373 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002374 _PyErr_Format(tstate, PyExc_SystemError,
2375 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 if (PyDict_CheckExact(ns))
2380 err = PyDict_SetItem(ns, name, v);
2381 else
2382 err = PyObject_SetItem(ns, name, v);
2383 Py_DECREF(v);
2384 if (err != 0)
2385 goto error;
2386 DISPATCH();
2387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Benjamin Petersonddd19492018-09-16 22:38:02 -07002389 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 PyObject *name = GETITEM(names, oparg);
2391 PyObject *ns = f->f_locals;
2392 int err;
2393 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002394 _PyErr_Format(tstate, PyExc_SystemError,
2395 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 err = PyObject_DelItem(ns, name);
2399 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002400 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 NAME_ERROR_MSG,
2402 name);
2403 goto error;
2404 }
2405 DISPATCH();
2406 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002407
Benjamin Petersonddd19492018-09-16 22:38:02 -07002408 case TARGET(UNPACK_SEQUENCE): {
2409 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 PyObject *seq = POP(), *item, **items;
2411 if (PyTuple_CheckExact(seq) &&
2412 PyTuple_GET_SIZE(seq) == oparg) {
2413 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 item = items[oparg];
2416 Py_INCREF(item);
2417 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 } else if (PyList_CheckExact(seq) &&
2420 PyList_GET_SIZE(seq) == oparg) {
2421 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 item = items[oparg];
2424 Py_INCREF(item);
2425 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002429 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 } else {
2431 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 Py_DECREF(seq);
2433 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002436 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002438
Benjamin Petersonddd19492018-09-16 22:38:02 -07002439 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2441 PyObject *seq = POP();
2442
Victor Stinner438a12d2019-05-24 17:01:38 +02002443 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 stack_pointer + totalargs)) {
2445 stack_pointer += totalargs;
2446 } else {
2447 Py_DECREF(seq);
2448 goto error;
2449 }
2450 Py_DECREF(seq);
2451 DISPATCH();
2452 }
2453
Benjamin Petersonddd19492018-09-16 22:38:02 -07002454 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 PyObject *name = GETITEM(names, oparg);
2456 PyObject *owner = TOP();
2457 PyObject *v = SECOND();
2458 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002459 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 Py_DECREF(owner);
2463 if (err != 0)
2464 goto error;
2465 DISPATCH();
2466 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002467
Benjamin Petersonddd19492018-09-16 22:38:02 -07002468 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 PyObject *name = GETITEM(names, oparg);
2470 PyObject *owner = POP();
2471 int err;
2472 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2473 Py_DECREF(owner);
2474 if (err != 0)
2475 goto error;
2476 DISPATCH();
2477 }
2478
Benjamin Petersonddd19492018-09-16 22:38:02 -07002479 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 PyObject *name = GETITEM(names, oparg);
2481 PyObject *v = POP();
2482 int err;
2483 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 if (err != 0)
2486 goto error;
2487 DISPATCH();
2488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002489
Benjamin Petersonddd19492018-09-16 22:38:02 -07002490 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002491 PyObject *name = GETITEM(names, oparg);
2492 int err;
2493 err = PyDict_DelItem(f->f_globals, name);
2494 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002495 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2496 format_exc_check_arg(tstate, PyExc_NameError,
2497 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002498 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002500 }
2501 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002503
Benjamin Petersonddd19492018-09-16 22:38:02 -07002504 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 PyObject *name = GETITEM(names, oparg);
2506 PyObject *locals = f->f_locals;
2507 PyObject *v;
2508 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002509 _PyErr_Format(tstate, PyExc_SystemError,
2510 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002514 v = PyDict_GetItemWithError(locals, name);
2515 if (v != NULL) {
2516 Py_INCREF(v);
2517 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002518 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002519 goto error;
2520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 }
2522 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002524 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002525 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002526 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002527 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 }
2529 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002531 v = PyDict_GetItemWithError(f->f_globals, name);
2532 if (v != NULL) {
2533 Py_INCREF(v);
2534 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002535 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002536 goto error;
2537 }
2538 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002539 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002540 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002543 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002546 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002548 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002550 }
2551 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 v = PyObject_GetItem(f->f_builtins, name);
2553 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002554 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002555 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002560 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002567
Benjamin Petersonddd19492018-09-16 22:38:02 -07002568 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002569 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002571 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002572 && PyDict_CheckExact(f->f_builtins))
2573 {
Inada Naoki91234a12019-06-03 21:30:58 +09002574 OPCACHE_CHECK();
2575 if (co_opcache != NULL && co_opcache->optimized > 0) {
2576 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2577
2578 if (lg->globals_ver ==
2579 ((PyDictObject *)f->f_globals)->ma_version_tag
2580 && lg->builtins_ver ==
2581 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2582 {
2583 PyObject *ptr = lg->ptr;
2584 OPCACHE_STAT_GLOBAL_HIT();
2585 assert(ptr != NULL);
2586 Py_INCREF(ptr);
2587 PUSH(ptr);
2588 DISPATCH();
2589 }
2590 }
2591
2592 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002594 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 name);
2596 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002597 if (!_PyErr_OCCURRED()) {
2598 /* _PyDict_LoadGlobal() returns NULL without raising
2599 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002600 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002601 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002602 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 }
Inada Naoki91234a12019-06-03 21:30:58 +09002605
2606 if (co_opcache != NULL) {
2607 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2608
2609 if (co_opcache->optimized == 0) {
2610 /* Wasn't optimized before. */
2611 OPCACHE_STAT_GLOBAL_OPT();
2612 } else {
2613 OPCACHE_STAT_GLOBAL_MISS();
2614 }
2615
2616 co_opcache->optimized = 1;
2617 lg->globals_ver =
2618 ((PyDictObject *)f->f_globals)->ma_version_tag;
2619 lg->builtins_ver =
2620 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2621 lg->ptr = v; /* borrowed */
2622 }
2623
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002626 else {
2627 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002628
2629 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002630 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 v = PyObject_GetItem(f->f_globals, name);
2632 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002633 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002634 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002635 }
2636 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002637
Victor Stinnerb4efc962015-11-20 09:24:02 +01002638 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 v = PyObject_GetItem(f->f_builtins, name);
2640 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002641 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002642 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002643 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002644 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002647 }
2648 }
2649 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002653
Benjamin Petersonddd19492018-09-16 22:38:02 -07002654 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 PyObject *v = GETLOCAL(oparg);
2656 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 SETLOCAL(oparg, NULL);
2658 DISPATCH();
2659 }
2660 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002661 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 UNBOUNDLOCAL_ERROR_MSG,
2663 PyTuple_GetItem(co->co_varnames, oparg)
2664 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 goto error;
2666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Benjamin Petersonddd19492018-09-16 22:38:02 -07002668 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002670 PyObject *oldobj = PyCell_GET(cell);
2671 if (oldobj != NULL) {
2672 PyCell_SET(cell, NULL);
2673 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002674 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002675 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002676 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 goto error;
2678 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002679
Benjamin Petersonddd19492018-09-16 22:38:02 -07002680 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 PyObject *cell = freevars[oparg];
2682 Py_INCREF(cell);
2683 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002686
Benjamin Petersonddd19492018-09-16 22:38:02 -07002687 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002688 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002689 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002690 assert(locals);
2691 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2692 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2693 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2694 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2695 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002696 value = PyDict_GetItemWithError(locals, name);
2697 if (value != NULL) {
2698 Py_INCREF(value);
2699 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002700 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002701 goto error;
2702 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002703 }
2704 else {
2705 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002706 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002707 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002708 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002709 }
2710 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002711 }
2712 }
2713 if (!value) {
2714 PyObject *cell = freevars[oparg];
2715 value = PyCell_GET(cell);
2716 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002717 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002718 goto error;
2719 }
2720 Py_INCREF(value);
2721 }
2722 PUSH(value);
2723 DISPATCH();
2724 }
2725
Benjamin Petersonddd19492018-09-16 22:38:02 -07002726 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 PyObject *cell = freevars[oparg];
2728 PyObject *value = PyCell_GET(cell);
2729 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002730 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 Py_INCREF(value);
2734 PUSH(value);
2735 DISPATCH();
2736 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002737
Benjamin Petersonddd19492018-09-16 22:38:02 -07002738 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 PyObject *v = POP();
2740 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002741 PyObject *oldobj = PyCell_GET(cell);
2742 PyCell_SET(cell, v);
2743 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 DISPATCH();
2745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002746
Benjamin Petersonddd19492018-09-16 22:38:02 -07002747 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002748 PyObject *str;
2749 PyObject *empty = PyUnicode_New(0, 0);
2750 if (empty == NULL) {
2751 goto error;
2752 }
2753 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2754 Py_DECREF(empty);
2755 if (str == NULL)
2756 goto error;
2757 while (--oparg >= 0) {
2758 PyObject *item = POP();
2759 Py_DECREF(item);
2760 }
2761 PUSH(str);
2762 DISPATCH();
2763 }
2764
Benjamin Petersonddd19492018-09-16 22:38:02 -07002765 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 PyObject *tup = PyTuple_New(oparg);
2767 if (tup == NULL)
2768 goto error;
2769 while (--oparg >= 0) {
2770 PyObject *item = POP();
2771 PyTuple_SET_ITEM(tup, oparg, item);
2772 }
2773 PUSH(tup);
2774 DISPATCH();
2775 }
2776
Benjamin Petersonddd19492018-09-16 22:38:02 -07002777 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 PyObject *list = PyList_New(oparg);
2779 if (list == NULL)
2780 goto error;
2781 while (--oparg >= 0) {
2782 PyObject *item = POP();
2783 PyList_SET_ITEM(list, oparg, item);
2784 }
2785 PUSH(list);
2786 DISPATCH();
2787 }
2788
Mark Shannon13bc1392020-01-23 09:25:17 +00002789 case TARGET(LIST_TO_TUPLE): {
2790 PyObject *list = POP();
2791 PyObject *tuple = PyList_AsTuple(list);
2792 Py_DECREF(list);
2793 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002794 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002795 }
2796 PUSH(tuple);
2797 DISPATCH();
2798 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799
Mark Shannon13bc1392020-01-23 09:25:17 +00002800 case TARGET(LIST_EXTEND): {
2801 PyObject *iterable = POP();
2802 PyObject *list = PEEK(oparg);
2803 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2804 if (none_val == NULL) {
2805 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002806 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002807 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002808 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002809 _PyErr_Format(tstate, PyExc_TypeError,
2810 "Value after * must be an iterable, not %.200s",
2811 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002813 Py_DECREF(iterable);
2814 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002815 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002816 Py_DECREF(none_val);
2817 Py_DECREF(iterable);
2818 DISPATCH();
2819 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002820
Mark Shannon13bc1392020-01-23 09:25:17 +00002821 case TARGET(SET_UPDATE): {
2822 PyObject *iterable = POP();
2823 PyObject *set = PEEK(oparg);
2824 int err = _PySet_Update(set, iterable);
2825 Py_DECREF(iterable);
2826 if (err < 0) {
2827 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002828 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002829 DISPATCH();
2830 }
2831
Benjamin Petersonddd19492018-09-16 22:38:02 -07002832 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 PyObject *set = PySet_New(NULL);
2834 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002835 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 if (set == NULL)
2837 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002838 for (i = oparg; i > 0; i--) {
2839 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 if (err == 0)
2841 err = PySet_Add(set, item);
2842 Py_DECREF(item);
2843 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002844 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 if (err != 0) {
2846 Py_DECREF(set);
2847 goto error;
2848 }
2849 PUSH(set);
2850 DISPATCH();
2851 }
2852
Benjamin Petersonddd19492018-09-16 22:38:02 -07002853 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002854 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2856 if (map == NULL)
2857 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002858 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002859 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002860 PyObject *key = PEEK(2*i);
2861 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002862 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 if (err != 0) {
2864 Py_DECREF(map);
2865 goto error;
2866 }
2867 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002868
2869 while (oparg--) {
2870 Py_DECREF(POP());
2871 Py_DECREF(POP());
2872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 PUSH(map);
2874 DISPATCH();
2875 }
2876
Benjamin Petersonddd19492018-09-16 22:38:02 -07002877 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002878 _Py_IDENTIFIER(__annotations__);
2879 int err;
2880 PyObject *ann_dict;
2881 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002882 _PyErr_Format(tstate, PyExc_SystemError,
2883 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002884 goto error;
2885 }
2886 /* check if __annotations__ in locals()... */
2887 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002888 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002889 &PyId___annotations__);
2890 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002891 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002892 goto error;
2893 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002894 /* ...if not, create a new one */
2895 ann_dict = PyDict_New();
2896 if (ann_dict == NULL) {
2897 goto error;
2898 }
2899 err = _PyDict_SetItemId(f->f_locals,
2900 &PyId___annotations__, ann_dict);
2901 Py_DECREF(ann_dict);
2902 if (err != 0) {
2903 goto error;
2904 }
2905 }
2906 }
2907 else {
2908 /* do the same if locals() is not a dict */
2909 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2910 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002911 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002912 }
2913 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2914 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002915 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002916 goto error;
2917 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002918 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002919 ann_dict = PyDict_New();
2920 if (ann_dict == NULL) {
2921 goto error;
2922 }
2923 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2924 Py_DECREF(ann_dict);
2925 if (err != 0) {
2926 goto error;
2927 }
2928 }
2929 else {
2930 Py_DECREF(ann_dict);
2931 }
2932 }
2933 DISPATCH();
2934 }
2935
Benjamin Petersonddd19492018-09-16 22:38:02 -07002936 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002937 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002938 PyObject *map;
2939 PyObject *keys = TOP();
2940 if (!PyTuple_CheckExact(keys) ||
2941 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002942 _PyErr_SetString(tstate, PyExc_SystemError,
2943 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002944 goto error;
2945 }
2946 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2947 if (map == NULL) {
2948 goto error;
2949 }
2950 for (i = oparg; i > 0; i--) {
2951 int err;
2952 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2953 PyObject *value = PEEK(i + 1);
2954 err = PyDict_SetItem(map, key, value);
2955 if (err != 0) {
2956 Py_DECREF(map);
2957 goto error;
2958 }
2959 }
2960
2961 Py_DECREF(POP());
2962 while (oparg--) {
2963 Py_DECREF(POP());
2964 }
2965 PUSH(map);
2966 DISPATCH();
2967 }
2968
Mark Shannon8a4cd702020-01-27 09:57:45 +00002969 case TARGET(DICT_UPDATE): {
2970 PyObject *update = POP();
2971 PyObject *dict = PEEK(oparg);
2972 if (PyDict_Update(dict, update) < 0) {
2973 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2974 _PyErr_Format(tstate, PyExc_TypeError,
2975 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002976 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002977 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002978 Py_DECREF(update);
2979 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002980 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002981 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002982 DISPATCH();
2983 }
2984
Mark Shannon8a4cd702020-01-27 09:57:45 +00002985 case TARGET(DICT_MERGE): {
2986 PyObject *update = POP();
2987 PyObject *dict = PEEK(oparg);
2988
2989 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2990 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2991 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002992 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002993 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002994 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002995 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002996 DISPATCH();
2997 }
2998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003000 PyObject *value = TOP();
3001 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 PyObject *map;
3003 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003004 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003005 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003007 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 Py_DECREF(value);
3009 Py_DECREF(key);
3010 if (err != 0)
3011 goto error;
3012 PREDICT(JUMP_ABSOLUTE);
3013 DISPATCH();
3014 }
3015
Benjamin Petersonddd19492018-09-16 22:38:02 -07003016 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 PyObject *name = GETITEM(names, oparg);
3018 PyObject *owner = TOP();
3019 PyObject *res = PyObject_GetAttr(owner, name);
3020 Py_DECREF(owner);
3021 SET_TOP(res);
3022 if (res == NULL)
3023 goto error;
3024 DISPATCH();
3025 }
3026
Benjamin Petersonddd19492018-09-16 22:38:02 -07003027 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003028 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PyObject *right = POP();
3030 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003031 PyObject *res = PyObject_RichCompare(left, right, oparg);
3032 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 Py_DECREF(left);
3034 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 if (res == NULL)
3036 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 PREDICT(POP_JUMP_IF_FALSE);
3038 PREDICT(POP_JUMP_IF_TRUE);
3039 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003040 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003041
Mark Shannon9af0e472020-01-14 10:12:45 +00003042 case TARGET(IS_OP): {
3043 PyObject *right = POP();
3044 PyObject *left = TOP();
3045 int res = (left == right)^oparg;
3046 PyObject *b = res ? Py_True : Py_False;
3047 Py_INCREF(b);
3048 SET_TOP(b);
3049 Py_DECREF(left);
3050 Py_DECREF(right);
3051 PREDICT(POP_JUMP_IF_FALSE);
3052 PREDICT(POP_JUMP_IF_TRUE);
3053 FAST_DISPATCH();
3054 }
3055
3056 case TARGET(CONTAINS_OP): {
3057 PyObject *right = POP();
3058 PyObject *left = POP();
3059 int res = PySequence_Contains(right, left);
3060 Py_DECREF(left);
3061 Py_DECREF(right);
3062 if (res < 0) {
3063 goto error;
3064 }
3065 PyObject *b = (res^oparg) ? Py_True : Py_False;
3066 Py_INCREF(b);
3067 PUSH(b);
3068 PREDICT(POP_JUMP_IF_FALSE);
3069 PREDICT(POP_JUMP_IF_TRUE);
3070 FAST_DISPATCH();
3071 }
3072
3073#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3074 "BaseException is not allowed"
3075
3076 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3077 PyObject *right = POP();
3078 PyObject *left = POP();
3079 if (PyTuple_Check(right)) {
3080 Py_ssize_t i, length;
3081 length = PyTuple_GET_SIZE(right);
3082 for (i = 0; i < length; i++) {
3083 PyObject *exc = PyTuple_GET_ITEM(right, i);
3084 if (!PyExceptionClass_Check(exc)) {
3085 _PyErr_SetString(tstate, PyExc_TypeError,
3086 CANNOT_CATCH_MSG);
3087 Py_DECREF(left);
3088 Py_DECREF(right);
3089 goto error;
3090 }
3091 }
3092 }
3093 else {
3094 if (!PyExceptionClass_Check(right)) {
3095 _PyErr_SetString(tstate, PyExc_TypeError,
3096 CANNOT_CATCH_MSG);
3097 Py_DECREF(left);
3098 Py_DECREF(right);
3099 goto error;
3100 }
3101 }
3102 int res = PyErr_GivenExceptionMatches(left, right);
3103 Py_DECREF(left);
3104 Py_DECREF(right);
3105 if (res > 0) {
3106 /* Exception matches -- Do nothing */;
3107 }
3108 else if (res == 0) {
3109 JUMPTO(oparg);
3110 }
3111 else {
3112 goto error;
3113 }
3114 DISPATCH();
3115 }
3116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003119 PyObject *fromlist = POP();
3120 PyObject *level = TOP();
3121 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003122 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003123 Py_DECREF(level);
3124 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 SET_TOP(res);
3126 if (res == NULL)
3127 goto error;
3128 DISPATCH();
3129 }
3130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 PyObject *from = POP(), *locals;
3133 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003134 if (PyFrame_FastToLocalsWithError(f) < 0) {
3135 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003136 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003137 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003138
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 locals = f->f_locals;
3140 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003141 _PyErr_SetString(tstate, PyExc_SystemError,
3142 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003143 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003146 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 Py_DECREF(from);
3149 if (err != 0)
3150 goto error;
3151 DISPATCH();
3152 }
Guido van Rossum25831651993-05-19 14:50:45 +00003153
Benjamin Petersonddd19492018-09-16 22:38:02 -07003154 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 PyObject *name = GETITEM(names, oparg);
3156 PyObject *from = TOP();
3157 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003158 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PUSH(res);
3160 if (res == NULL)
3161 goto error;
3162 DISPATCH();
3163 }
Thomas Wouters52152252000-08-17 22:55:00 +00003164
Benjamin Petersonddd19492018-09-16 22:38:02 -07003165 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 JUMPBY(oparg);
3167 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003169
Benjamin Petersonddd19492018-09-16 22:38:02 -07003170 case TARGET(POP_JUMP_IF_FALSE): {
3171 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 PyObject *cond = POP();
3173 int err;
3174 if (cond == Py_True) {
3175 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 FAST_DISPATCH();
3177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 if (cond == Py_False) {
3179 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 JUMPTO(oparg);
3181 FAST_DISPATCH();
3182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 err = PyObject_IsTrue(cond);
3184 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003186 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 else if (err == 0)
3188 JUMPTO(oparg);
3189 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003193
Benjamin Petersonddd19492018-09-16 22:38:02 -07003194 case TARGET(POP_JUMP_IF_TRUE): {
3195 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 PyObject *cond = POP();
3197 int err;
3198 if (cond == Py_False) {
3199 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 FAST_DISPATCH();
3201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 if (cond == Py_True) {
3203 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 JUMPTO(oparg);
3205 FAST_DISPATCH();
3206 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 err = PyObject_IsTrue(cond);
3208 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 JUMPTO(oparg);
3211 }
3212 else if (err == 0)
3213 ;
3214 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003218
Benjamin Petersonddd19492018-09-16 22:38:02 -07003219 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 PyObject *cond = TOP();
3221 int err;
3222 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003223 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 FAST_DISPATCH();
3226 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003227 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 JUMPTO(oparg);
3229 FAST_DISPATCH();
3230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003233 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 }
3236 else if (err == 0)
3237 JUMPTO(oparg);
3238 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 PyObject *cond = TOP();
3245 int err;
3246 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003247 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 FAST_DISPATCH();
3250 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 JUMPTO(oparg);
3253 FAST_DISPATCH();
3254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 JUMPTO(oparg);
3258 }
3259 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003260 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 }
3263 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003267
Benjamin Petersonddd19492018-09-16 22:38:02 -07003268 case TARGET(JUMP_ABSOLUTE): {
3269 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003271#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 /* Enabling this path speeds-up all while and for-loops by bypassing
3273 the per-loop checks for signals. By default, this should be turned-off
3274 because it prevents detection of a control-break in tight loops like
3275 "while 1: pass". Compile with this option turned-on when you need
3276 the speed-up and do not need break checking inside tight loops (ones
3277 that contain only instructions ending with FAST_DISPATCH).
3278 */
3279 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003280#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003282#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003284
Benjamin Petersonddd19492018-09-16 22:38:02 -07003285 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003288 PyObject *iter = PyObject_GetIter(iterable);
3289 Py_DECREF(iterable);
3290 SET_TOP(iter);
3291 if (iter == NULL)
3292 goto error;
3293 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003294 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003295 DISPATCH();
3296 }
3297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003299 /* before: [obj]; after [getiter(obj)] */
3300 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003301 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003302 if (PyCoro_CheckExact(iterable)) {
3303 /* `iterable` is a coroutine */
3304 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3305 /* and it is used in a 'yield from' expression of a
3306 regular generator. */
3307 Py_DECREF(iterable);
3308 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003309 _PyErr_SetString(tstate, PyExc_TypeError,
3310 "cannot 'yield from' a coroutine object "
3311 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003312 goto error;
3313 }
3314 }
3315 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003316 /* `iterable` is not a generator. */
3317 iter = PyObject_GetIter(iterable);
3318 Py_DECREF(iterable);
3319 SET_TOP(iter);
3320 if (iter == NULL)
3321 goto error;
3322 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003323 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 DISPATCH();
3325 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003326
Benjamin Petersonddd19492018-09-16 22:38:02 -07003327 case TARGET(FOR_ITER): {
3328 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003331 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 if (next != NULL) {
3333 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 PREDICT(STORE_FAST);
3335 PREDICT(UNPACK_SEQUENCE);
3336 DISPATCH();
3337 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003338 if (_PyErr_Occurred(tstate)) {
3339 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003341 }
3342 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003343 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003344 }
3345 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 }
3347 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003348 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003351 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003354
Benjamin Petersonddd19492018-09-16 22:38:02 -07003355 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003356 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 STACK_LEVEL());
3358 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003360
Benjamin Petersonddd19492018-09-16 22:38:02 -07003361 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003362 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003363 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003364 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003365 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003366 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003367 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003368 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003369 }
3370 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3371 if (exit == NULL) {
3372 Py_DECREF(enter);
3373 goto error;
3374 }
Yury Selivanov75445082015-05-11 22:57:16 -04003375 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003376 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003377 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003378 Py_DECREF(enter);
3379 if (res == NULL)
3380 goto error;
3381 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003382 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003383 DISPATCH();
3384 }
3385
Benjamin Petersonddd19492018-09-16 22:38:02 -07003386 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003387 PyObject *res = POP();
3388 /* Setup the finally block before pushing the result
3389 of __aenter__ on the stack. */
3390 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3391 STACK_LEVEL());
3392 PUSH(res);
3393 DISPATCH();
3394 }
3395
Benjamin Petersonddd19492018-09-16 22:38:02 -07003396 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003397 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003398 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003400 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003402 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003403 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003404 }
3405 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003406 if (exit == NULL) {
3407 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003409 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003412 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 Py_DECREF(enter);
3414 if (res == NULL)
3415 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 /* Setup the finally block before pushing the result
3417 of __enter__ on the stack. */
3418 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3419 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003421 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 DISPATCH();
3423 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003424
Mark Shannonfee55262019-11-21 09:11:43 +00003425 case TARGET(WITH_EXCEPT_START): {
3426 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003428 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3429 - SEVENTH: the context.__exit__ bound method
3430 We call SEVENTH(TOP, SECOND, THIRD).
3431 Then we push again the TOP exception and the __exit__
3432 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003435 PyObject *exc, *val, *tb, *res;
3436
Victor Stinner842cfff2016-12-01 14:45:31 +01003437 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003438 val = SECOND();
3439 tb = THIRD();
3440 assert(exc != Py_None);
3441 assert(!PyLong_Check(exc));
3442 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003443 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003444 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003445 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 if (res == NULL)
3447 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003448
Yury Selivanov75445082015-05-11 22:57:16 -04003449 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003452
Benjamin Petersonddd19492018-09-16 22:38:02 -07003453 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003454 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003455 PyObject *name = GETITEM(names, oparg);
3456 PyObject *obj = TOP();
3457 PyObject *meth = NULL;
3458
3459 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3460
Yury Selivanovf2392132016-12-13 19:03:51 -05003461 if (meth == NULL) {
3462 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003463 goto error;
3464 }
3465
3466 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003467 /* We can bypass temporary bound method object.
3468 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003469
INADA Naoki015bce62017-01-16 17:23:30 +09003470 meth | self | arg1 | ... | argN
3471 */
3472 SET_TOP(meth);
3473 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003474 }
3475 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003476 /* meth is not an unbound method (but a regular attr, or
3477 something was returned by a descriptor protocol). Set
3478 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003479 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003480
3481 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003482 */
INADA Naoki015bce62017-01-16 17:23:30 +09003483 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003485 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 }
3487 DISPATCH();
3488 }
3489
Benjamin Petersonddd19492018-09-16 22:38:02 -07003490 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003491 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003492 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003493
3494 sp = stack_pointer;
3495
INADA Naoki015bce62017-01-16 17:23:30 +09003496 meth = PEEK(oparg + 2);
3497 if (meth == NULL) {
3498 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3499 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003500
3501 Stack layout:
3502
INADA Naoki015bce62017-01-16 17:23:30 +09003503 ... | NULL | callable | arg1 | ... | argN
3504 ^- TOP()
3505 ^- (-oparg)
3506 ^- (-oparg-1)
3507 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003508
Ville Skyttä49b27342017-08-03 09:00:59 +03003509 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003510 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003511 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003512 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003513 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003514 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003515 }
3516 else {
3517 /* This is a method call. Stack layout:
3518
INADA Naoki015bce62017-01-16 17:23:30 +09003519 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003520 ^- TOP()
3521 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003522 ^- (-oparg-1)
3523 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003524
INADA Naoki015bce62017-01-16 17:23:30 +09003525 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003526 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003527 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003528 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003529 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003530 stack_pointer = sp;
3531 }
3532
3533 PUSH(res);
3534 if (res == NULL)
3535 goto error;
3536 DISPATCH();
3537 }
3538
Benjamin Petersonddd19492018-09-16 22:38:02 -07003539 case TARGET(CALL_FUNCTION): {
3540 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003543 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003545 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003546 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003548 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003551
Benjamin Petersonddd19492018-09-16 22:38:02 -07003552 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003553 PyObject **sp, *res, *names;
3554
3555 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003556 assert(PyTuple_Check(names));
3557 assert(PyTuple_GET_SIZE(names) <= oparg);
3558 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003560 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003563 Py_DECREF(names);
3564
3565 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003567 }
3568 DISPATCH();
3569 }
3570
Benjamin Petersonddd19492018-09-16 22:38:02 -07003571 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003572 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003573 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003574 if (oparg & 0x01) {
3575 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003576 if (!PyDict_CheckExact(kwargs)) {
3577 PyObject *d = PyDict_New();
3578 if (d == NULL)
3579 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003580 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003581 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003582 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003583 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003584 goto error;
3585 }
3586 Py_DECREF(kwargs);
3587 kwargs = d;
3588 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003589 assert(PyDict_CheckExact(kwargs));
3590 }
3591 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003592 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003593 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003594 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003595 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003596 goto error;
3597 }
3598 Py_SETREF(callargs, PySequence_Tuple(callargs));
3599 if (callargs == NULL) {
3600 goto error;
3601 }
3602 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003603 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003604
Victor Stinner09532fe2019-05-10 23:39:09 +02003605 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003606 Py_DECREF(func);
3607 Py_DECREF(callargs);
3608 Py_XDECREF(kwargs);
3609
3610 SET_TOP(result);
3611 if (result == NULL) {
3612 goto error;
3613 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003616
Benjamin Petersonddd19492018-09-16 22:38:02 -07003617 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003618 PyObject *qualname = POP();
3619 PyObject *codeobj = POP();
3620 PyFunctionObject *func = (PyFunctionObject *)
3621 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003622
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003623 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003624 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003625 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003626 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003628
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003629 if (oparg & 0x08) {
3630 assert(PyTuple_CheckExact(TOP()));
3631 func ->func_closure = POP();
3632 }
3633 if (oparg & 0x04) {
3634 assert(PyDict_CheckExact(TOP()));
3635 func->func_annotations = POP();
3636 }
3637 if (oparg & 0x02) {
3638 assert(PyDict_CheckExact(TOP()));
3639 func->func_kwdefaults = POP();
3640 }
3641 if (oparg & 0x01) {
3642 assert(PyTuple_CheckExact(TOP()));
3643 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003645
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003646 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003647 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003649
Benjamin Petersonddd19492018-09-16 22:38:02 -07003650 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003651 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003653 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003655 step = NULL;
3656 stop = POP();
3657 start = TOP();
3658 slice = PySlice_New(start, stop, step);
3659 Py_DECREF(start);
3660 Py_DECREF(stop);
3661 Py_XDECREF(step);
3662 SET_TOP(slice);
3663 if (slice == NULL)
3664 goto error;
3665 DISPATCH();
3666 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003667
Benjamin Petersonddd19492018-09-16 22:38:02 -07003668 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003669 /* Handles f-string value formatting. */
3670 PyObject *result;
3671 PyObject *fmt_spec;
3672 PyObject *value;
3673 PyObject *(*conv_fn)(PyObject *);
3674 int which_conversion = oparg & FVC_MASK;
3675 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3676
3677 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003678 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003679
3680 /* See if any conversion is specified. */
3681 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003682 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003683 case FVC_STR: conv_fn = PyObject_Str; break;
3684 case FVC_REPR: conv_fn = PyObject_Repr; break;
3685 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003686 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003687 _PyErr_Format(tstate, PyExc_SystemError,
3688 "unexpected conversion flag %d",
3689 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003690 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003691 }
3692
3693 /* If there's a conversion function, call it and replace
3694 value with that result. Otherwise, just use value,
3695 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003696 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003697 result = conv_fn(value);
3698 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003699 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003700 Py_XDECREF(fmt_spec);
3701 goto error;
3702 }
3703 value = result;
3704 }
3705
3706 /* If value is a unicode object, and there's no fmt_spec,
3707 then we know the result of format(value) is value
3708 itself. In that case, skip calling format(). I plan to
3709 move this optimization in to PyObject_Format()
3710 itself. */
3711 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3712 /* Do nothing, just transfer ownership to result. */
3713 result = value;
3714 } else {
3715 /* Actually call format(). */
3716 result = PyObject_Format(value, fmt_spec);
3717 Py_DECREF(value);
3718 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003719 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003720 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003721 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003722 }
3723
Eric V. Smith135d5f42016-02-05 18:23:08 -05003724 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003725 DISPATCH();
3726 }
3727
Benjamin Petersonddd19492018-09-16 22:38:02 -07003728 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003729 int oldoparg = oparg;
3730 NEXTOPARG();
3731 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003733 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003734
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003735
Antoine Pitrou042b1282010-08-13 21:15:58 +00003736#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003738#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 default:
3740 fprintf(stderr,
3741 "XXX lineno: %d, opcode: %d\n",
3742 PyFrame_GetLineNumber(f),
3743 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003744 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003745 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003748
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003749 /* This should never be reached. Every opcode should end with DISPATCH()
3750 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003751 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003752
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003753error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003754 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003755#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003756 if (!_PyErr_Occurred(tstate)) {
3757 _PyErr_SetString(tstate, PyExc_SystemError,
3758 "error return without exception set");
3759 }
Victor Stinner365b6932013-07-12 00:11:58 +02003760#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003761 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003762#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003764 /* Log traceback info. */
3765 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003766
Benjamin Peterson51f46162013-01-23 08:38:47 -05003767 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003768 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3769 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003770
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003771exception_unwind:
3772 /* Unwind stacks if an exception occurred */
3773 while (f->f_iblock > 0) {
3774 /* Pop the current block. */
3775 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 if (b->b_type == EXCEPT_HANDLER) {
3778 UNWIND_EXCEPT_HANDLER(b);
3779 continue;
3780 }
3781 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003782 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 PyObject *exc, *val, *tb;
3784 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003785 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 /* Beware, this invalidates all b->b_* fields */
3787 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003788 PUSH(exc_info->exc_traceback);
3789 PUSH(exc_info->exc_value);
3790 if (exc_info->exc_type != NULL) {
3791 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 }
3793 else {
3794 Py_INCREF(Py_None);
3795 PUSH(Py_None);
3796 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003797 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 /* Make the raw exception data
3799 available to the handler,
3800 so a program can emulate the
3801 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003802 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003803 if (tb != NULL)
3804 PyException_SetTraceback(val, tb);
3805 else
3806 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003808 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003810 exc_info->exc_value = val;
3811 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 if (tb == NULL)
3813 tb = Py_None;
3814 Py_INCREF(tb);
3815 PUSH(tb);
3816 PUSH(val);
3817 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003819 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003820 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3821 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3822 /* Make sure that we trace line after exception if we are in a new execution
3823 * window or we don't need a line update and we are not in the first instruction
3824 * of the line. */
3825 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3826 instr_prev = INT_MAX;
3827 }
Mark Shannonfee55262019-11-21 09:11:43 +00003828 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003829 /* Resume normal execution */
3830 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 }
3832 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003833
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003834 /* End the loop as we still have an error */
3835 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003837
Pablo Galindof00828a2019-05-09 16:52:02 +01003838 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003839 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 /* Pop remaining stack entries. */
3842 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003843 PyObject *o = POP();
3844 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003846
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003847exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003849 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003850 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3851 tstate, f, PyTrace_RETURN, retval)) {
3852 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 }
3854 }
3855 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003856 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3857 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003858 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 }
3860 }
3861 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003864exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003865 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3866 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003867 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003868 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003870
Victor Stinner0b72b232020-03-12 23:18:39 +01003871 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003872}
3873
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003875format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003876 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003877{
3878 int err;
3879 Py_ssize_t len = PyList_GET_SIZE(names);
3880 PyObject *name_str, *comma, *tail, *tmp;
3881
3882 assert(PyList_CheckExact(names));
3883 assert(len >= 1);
3884 /* Deal with the joys of natural language. */
3885 switch (len) {
3886 case 1:
3887 name_str = PyList_GET_ITEM(names, 0);
3888 Py_INCREF(name_str);
3889 break;
3890 case 2:
3891 name_str = PyUnicode_FromFormat("%U and %U",
3892 PyList_GET_ITEM(names, len - 2),
3893 PyList_GET_ITEM(names, len - 1));
3894 break;
3895 default:
3896 tail = PyUnicode_FromFormat(", %U, and %U",
3897 PyList_GET_ITEM(names, len - 2),
3898 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003899 if (tail == NULL)
3900 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003901 /* Chop off the last two objects in the list. This shouldn't actually
3902 fail, but we can't be too careful. */
3903 err = PyList_SetSlice(names, len - 2, len, NULL);
3904 if (err == -1) {
3905 Py_DECREF(tail);
3906 return;
3907 }
3908 /* Stitch everything up into a nice comma-separated list. */
3909 comma = PyUnicode_FromString(", ");
3910 if (comma == NULL) {
3911 Py_DECREF(tail);
3912 return;
3913 }
3914 tmp = PyUnicode_Join(comma, names);
3915 Py_DECREF(comma);
3916 if (tmp == NULL) {
3917 Py_DECREF(tail);
3918 return;
3919 }
3920 name_str = PyUnicode_Concat(tmp, tail);
3921 Py_DECREF(tmp);
3922 Py_DECREF(tail);
3923 break;
3924 }
3925 if (name_str == NULL)
3926 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003927 _PyErr_Format(tstate, PyExc_TypeError,
3928 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003929 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02003930 len,
3931 kind,
3932 len == 1 ? "" : "s",
3933 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003934 Py_DECREF(name_str);
3935}
3936
3937static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003938missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3939 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003940 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05003941{
Victor Stinner74319ae2016-08-25 00:04:09 +02003942 Py_ssize_t i, j = 0;
3943 Py_ssize_t start, end;
3944 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003945 const char *kind = positional ? "positional" : "keyword-only";
3946 PyObject *missing_names;
3947
3948 /* Compute the names of the arguments that are missing. */
3949 missing_names = PyList_New(missing);
3950 if (missing_names == NULL)
3951 return;
3952 if (positional) {
3953 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003954 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003955 }
3956 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003957 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003958 end = start + co->co_kwonlyargcount;
3959 }
3960 for (i = start; i < end; i++) {
3961 if (GETLOCAL(i) == NULL) {
3962 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3963 PyObject *name = PyObject_Repr(raw);
3964 if (name == NULL) {
3965 Py_DECREF(missing_names);
3966 return;
3967 }
3968 PyList_SET_ITEM(missing_names, j++, name);
3969 }
3970 }
3971 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003972 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05003973 Py_DECREF(missing_names);
3974}
3975
3976static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003977too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3978 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04003979 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980{
3981 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003982 Py_ssize_t kwonly_given = 0;
3983 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003985 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986
Benjamin Petersone109c702011-06-24 09:37:26 -05003987 assert((co->co_flags & CO_VARARGS) == 0);
3988 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003989 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003990 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003992 }
3993 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003994 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003995 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003997 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 }
3999 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004000 plural = (co_argcount != 1);
4001 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 }
4003 if (sig == NULL)
4004 return;
4005 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004006 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4007 kwonly_sig = PyUnicode_FromFormat(format,
4008 given != 1 ? "s" : "",
4009 kwonly_given,
4010 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 if (kwonly_sig == NULL) {
4012 Py_DECREF(sig);
4013 return;
4014 }
4015 }
4016 else {
4017 /* This will not fail. */
4018 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004019 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004021 _PyErr_Format(tstate, PyExc_TypeError,
4022 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004023 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004024 sig,
4025 plural ? "s" : "",
4026 given,
4027 kwonly_sig,
4028 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 Py_DECREF(sig);
4030 Py_DECREF(kwonly_sig);
4031}
4032
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004033static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004034positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004035 Py_ssize_t kwcount, PyObject* const* kwnames,
4036 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004037{
4038 int posonly_conflicts = 0;
4039 PyObject* posonly_names = PyList_New(0);
4040
4041 for(int k=0; k < co->co_posonlyargcount; k++){
4042 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4043
4044 for (int k2=0; k2<kwcount; k2++){
4045 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4046 PyObject* kwname = kwnames[k2];
4047 if (kwname == posonly_name){
4048 if(PyList_Append(posonly_names, kwname) != 0) {
4049 goto fail;
4050 }
4051 posonly_conflicts++;
4052 continue;
4053 }
4054
4055 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4056
4057 if ( cmp > 0) {
4058 if(PyList_Append(posonly_names, kwname) != 0) {
4059 goto fail;
4060 }
4061 posonly_conflicts++;
4062 } else if (cmp < 0) {
4063 goto fail;
4064 }
4065
4066 }
4067 }
4068 if (posonly_conflicts) {
4069 PyObject* comma = PyUnicode_FromString(", ");
4070 if (comma == NULL) {
4071 goto fail;
4072 }
4073 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4074 Py_DECREF(comma);
4075 if (error_names == NULL) {
4076 goto fail;
4077 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004078 _PyErr_Format(tstate, PyExc_TypeError,
4079 "%U() got some positional-only arguments passed"
4080 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004081 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004082 Py_DECREF(error_names);
4083 goto fail;
4084 }
4085
4086 Py_DECREF(posonly_names);
4087 return 0;
4088
4089fail:
4090 Py_XDECREF(posonly_names);
4091 return 1;
4092
4093}
4094
Guido van Rossumc2e20742006-02-27 22:32:47 +00004095/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004096 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004097 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004098
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004099PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004100_PyEval_EvalCode(PyThreadState *tstate,
4101 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004102 PyObject *const *args, Py_ssize_t argcount,
4103 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004104 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004105 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004106 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004107 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004108{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004109 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004110
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004111 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004112 PyFrameObject *f;
4113 PyObject *retval = NULL;
4114 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004116 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004117 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004118 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004121 _PyErr_SetString(tstate, PyExc_SystemError,
4122 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 return NULL;
4124 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004125
Victor Stinnerc7020012016-08-16 23:40:29 +02004126 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004127 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004128 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 fastlocals = f->f_localsplus;
4132 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004133
Victor Stinnerc7020012016-08-16 23:40:29 +02004134 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004135 if (co->co_flags & CO_VARKEYWORDS) {
4136 kwdict = PyDict_New();
4137 if (kwdict == NULL)
4138 goto fail;
4139 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004140 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004141 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004142 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 SETLOCAL(i, kwdict);
4144 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004145 else {
4146 kwdict = NULL;
4147 }
4148
Pablo Galindocd74e662019-06-01 18:08:04 +01004149 /* Copy all positional arguments into local variables */
4150 if (argcount > co->co_argcount) {
4151 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004152 }
4153 else {
4154 n = argcount;
4155 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004156 for (j = 0; j < n; j++) {
4157 x = args[j];
4158 Py_INCREF(x);
4159 SETLOCAL(j, x);
4160 }
4161
Victor Stinnerc7020012016-08-16 23:40:29 +02004162 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004163 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004164 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004165 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004166 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004167 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004168 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004170
Serhiy Storchakab7281052016-09-12 00:52:40 +03004171 /* Handle keyword arguments passed as two strided arrays */
4172 kwcount *= kwstep;
4173 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004174 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004175 PyObject *keyword = kwnames[i];
4176 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004177 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004178
Benjamin Petersonb204a422011-06-05 22:04:07 -05004179 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004180 _PyErr_Format(tstate, PyExc_TypeError,
4181 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004182 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004183 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004185
Benjamin Petersonb204a422011-06-05 22:04:07 -05004186 /* Speed hack: do raw pointer compares. As names are
4187 normally interned this should almost always hit. */
4188 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004189 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004190 PyObject *name = co_varnames[j];
4191 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004192 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004193 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004194 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004195
Benjamin Petersonb204a422011-06-05 22:04:07 -05004196 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004197 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004198 PyObject *name = co_varnames[j];
4199 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4200 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004202 }
4203 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004205 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004206 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004207
Victor Stinner231d1f32017-01-11 02:12:06 +01004208 assert(j >= total_args);
4209 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004210
Victor Stinner438a12d2019-05-24 17:01:38 +02004211 if (co->co_posonlyargcount
4212 && positional_only_passed_as_keyword(tstate, co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004213 kwcount, kwnames, qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004214 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004215 goto fail;
4216 }
4217
Victor Stinner438a12d2019-05-24 17:01:38 +02004218 _PyErr_Format(tstate, PyExc_TypeError,
4219 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004220 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004221 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004223
Christian Heimes0bd447f2013-07-20 14:48:10 +02004224 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4225 goto fail;
4226 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004227 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004228
Benjamin Petersonb204a422011-06-05 22:04:07 -05004229 kw_found:
4230 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004231 _PyErr_Format(tstate, PyExc_TypeError,
4232 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004233 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004234 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004236 Py_INCREF(value);
4237 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004239
4240 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004241 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004242 too_many_positional(tstate, co, argcount, defcount, fastlocals, qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 goto fail;
4244 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004245
4246 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004247 if (argcount < co->co_argcount) {
4248 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004249 Py_ssize_t missing = 0;
4250 for (i = argcount; i < m; i++) {
4251 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004252 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004253 }
4254 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004255 if (missing) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004256 missing_arguments(tstate, co, missing, defcount, fastlocals, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004257 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004258 }
4259 if (n > m)
4260 i = n - m;
4261 else
4262 i = 0;
4263 for (; i < defcount; i++) {
4264 if (GETLOCAL(m+i) == NULL) {
4265 PyObject *def = defs[i];
4266 Py_INCREF(def);
4267 SETLOCAL(m+i, def);
4268 }
4269 }
4270 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004271
4272 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004273 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004274 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004275 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004276 PyObject *name;
4277 if (GETLOCAL(i) != NULL)
4278 continue;
4279 name = PyTuple_GET_ITEM(co->co_varnames, i);
4280 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004281 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004282 if (def) {
4283 Py_INCREF(def);
4284 SETLOCAL(i, def);
4285 continue;
4286 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004287 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004288 goto fail;
4289 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004290 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004291 missing++;
4292 }
4293 if (missing) {
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004294 missing_arguments(tstate, co, missing, -1, fastlocals, qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004295 goto fail;
4296 }
4297 }
4298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004300 vars into frame. */
4301 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004303 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004304 /* Possibly account for the cell variable being an argument. */
4305 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004306 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004307 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004308 /* Clear the local copy. */
4309 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004310 }
4311 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004312 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004313 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004314 if (c == NULL)
4315 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004316 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004318
4319 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004320 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4321 PyObject *o = PyTuple_GET_ITEM(closure, i);
4322 Py_INCREF(o);
4323 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004325
Yury Selivanoveb636452016-09-08 22:01:51 -07004326 /* Handle generator/coroutine/asynchronous generator */
4327 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004328 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004329 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 /* Don't need to keep the reference to f_back, it will be set
4332 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004333 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 /* Create a new generator that owns the ready to run frame
4336 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004337 if (is_coro) {
4338 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004339 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4340 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004341 } else {
4342 gen = PyGen_NewWithQualName(f, name, qualname);
4343 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004344 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004345 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004346 }
INADA Naoki9c157762016-12-26 18:52:46 +09004347
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004348 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004349
Yury Selivanov75445082015-05-11 22:57:16 -04004350 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004352
Victor Stinnerb9e68122019-11-14 12:20:46 +01004353 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004354
Thomas Woutersce272b62007-09-19 21:19:28 +00004355fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 /* decref'ing the frame can cause __del__ methods to get invoked,
4358 which can call back into Python. While we're done with the
4359 current Python frame (f), the associated C stack is still in use,
4360 so recursion_depth must be boosted for the duration.
4361 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004362 if (Py_REFCNT(f) > 1) {
4363 Py_DECREF(f);
4364 _PyObject_GC_TRACK(f);
4365 }
4366 else {
4367 ++tstate->recursion_depth;
4368 Py_DECREF(f);
4369 --tstate->recursion_depth;
4370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004372}
4373
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004374
4375PyObject *
4376_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4377 PyObject *const *args, Py_ssize_t argcount,
4378 PyObject *const *kwnames, PyObject *const *kwargs,
4379 Py_ssize_t kwcount, int kwstep,
4380 PyObject *const *defs, Py_ssize_t defcount,
4381 PyObject *kwdefs, PyObject *closure,
4382 PyObject *name, PyObject *qualname)
4383{
4384 PyThreadState *tstate = _PyThreadState_GET();
4385 return _PyEval_EvalCode(tstate, _co, globals, locals,
4386 args, argcount,
4387 kwnames, kwargs,
4388 kwcount, kwstep,
4389 defs, defcount,
4390 kwdefs, closure,
4391 name, qualname);
4392}
4393
Victor Stinner40ee3012014-06-16 15:59:28 +02004394PyObject *
4395PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004396 PyObject *const *args, int argcount,
4397 PyObject *const *kws, int kwcount,
4398 PyObject *const *defs, int defcount,
4399 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004400{
4401 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004402 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004403 kws, kws != NULL ? kws + 1 : NULL,
4404 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004405 defs, defcount,
4406 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004407 NULL, NULL);
4408}
Tim Peters5ca576e2001-06-18 22:08:13 +00004409
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004410static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004411special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004414 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004415 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004416 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 return NULL;
4418 }
4419 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004420}
4421
4422
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004423/* Logic for the raise statement (too complicated for inlining).
4424 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004425static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004426do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 if (exc == NULL) {
4431 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004432 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004434 type = exc_info->exc_type;
4435 value = exc_info->exc_value;
4436 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004437 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004438 _PyErr_SetString(tstate, PyExc_RuntimeError,
4439 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004440 return 0;
4441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 Py_XINCREF(type);
4443 Py_XINCREF(value);
4444 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004445 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004446 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 /* We support the following forms of raise:
4450 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004451 raise <instance>
4452 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 if (PyExceptionClass_Check(exc)) {
4455 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004456 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 if (value == NULL)
4458 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004459 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004460 _PyErr_Format(tstate, PyExc_TypeError,
4461 "calling %R should have returned an instance of "
4462 "BaseException, not %R",
4463 type, Py_TYPE(value));
4464 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 }
4467 else if (PyExceptionInstance_Check(exc)) {
4468 value = exc;
4469 type = PyExceptionInstance_Class(exc);
4470 Py_INCREF(type);
4471 }
4472 else {
4473 /* Not something you can raise. You get an exception
4474 anyway, just not what you specified :-) */
4475 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 _PyErr_SetString(tstate, PyExc_TypeError,
4477 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 goto raise_error;
4479 }
Collin Winter828f04a2007-08-31 00:04:24 +00004480
Serhiy Storchakac0191582016-09-27 11:37:10 +03004481 assert(type != NULL);
4482 assert(value != NULL);
4483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 if (cause) {
4485 PyObject *fixed_cause;
4486 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004487 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 if (fixed_cause == NULL)
4489 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004490 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004492 else if (PyExceptionInstance_Check(cause)) {
4493 fixed_cause = cause;
4494 }
4495 else if (cause == Py_None) {
4496 Py_DECREF(cause);
4497 fixed_cause = NULL;
4498 }
4499 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004500 _PyErr_SetString(tstate, PyExc_TypeError,
4501 "exception causes must derive from "
4502 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 goto raise_error;
4504 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004505 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 }
Collin Winter828f04a2007-08-31 00:04:24 +00004507
Victor Stinner438a12d2019-05-24 17:01:38 +02004508 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004509 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004510 Py_DECREF(value);
4511 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004512 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004513
4514raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 Py_XDECREF(value);
4516 Py_XDECREF(type);
4517 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004518 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004519}
4520
Tim Petersd6d010b2001-06-21 02:49:55 +00004521/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004522 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004523
Guido van Rossum0368b722007-05-11 16:50:42 +00004524 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4525 with a variable target.
4526*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004527
Barry Warsawe42b18f1997-08-25 22:13:04 +00004528static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004529unpack_iterable(PyThreadState *tstate, PyObject *v,
4530 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 int i = 0, j = 0;
4533 Py_ssize_t ll = 0;
4534 PyObject *it; /* iter(v) */
4535 PyObject *w;
4536 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004541 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004542 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004543 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004544 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004545 _PyErr_Format(tstate, PyExc_TypeError,
4546 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004547 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004548 }
4549 return 0;
4550 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 for (; i < argcnt; i++) {
4553 w = PyIter_Next(it);
4554 if (w == NULL) {
4555 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004556 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004557 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004558 _PyErr_Format(tstate, PyExc_ValueError,
4559 "not enough values to unpack "
4560 "(expected %d, got %d)",
4561 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004562 }
4563 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004564 _PyErr_Format(tstate, PyExc_ValueError,
4565 "not enough values to unpack "
4566 "(expected at least %d, got %d)",
4567 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 }
4570 goto Error;
4571 }
4572 *--sp = w;
4573 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 if (argcntafter == -1) {
4576 /* We better have exhausted the iterator now. */
4577 w = PyIter_Next(it);
4578 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004579 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 goto Error;
4581 Py_DECREF(it);
4582 return 1;
4583 }
4584 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004585 _PyErr_Format(tstate, PyExc_ValueError,
4586 "too many values to unpack (expected %d)",
4587 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 goto Error;
4589 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 l = PySequence_List(it);
4592 if (l == NULL)
4593 goto Error;
4594 *--sp = l;
4595 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 ll = PyList_GET_SIZE(l);
4598 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004599 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004600 "not enough values to unpack (expected at least %d, got %zd)",
4601 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 goto Error;
4603 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 /* Pop the "after-variable" args off the list. */
4606 for (j = argcntafter; j > 0; j--, i++) {
4607 *--sp = PyList_GET_ITEM(l, ll - j);
4608 }
4609 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004610 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 Py_DECREF(it);
4612 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004613
Tim Petersd6d010b2001-06-21 02:49:55 +00004614Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 for (; i > 0; i--, sp++)
4616 Py_DECREF(*sp);
4617 Py_XDECREF(it);
4618 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004619}
4620
4621
Guido van Rossum96a42c81992-01-12 02:29:51 +00004622#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004623static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004624prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004627 if (PyObject_Print(v, stdout, 0) != 0) {
4628 /* Don't know what else to do */
4629 _PyErr_Clear(tstate);
4630 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 printf("\n");
4632 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004633}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004634#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004635
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004636static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004637call_exc_trace(Py_tracefunc func, PyObject *self,
4638 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004639{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004640 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004642 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 if (value == NULL) {
4644 value = Py_None;
4645 Py_INCREF(value);
4646 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004647 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004648 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 arg = PyTuple_Pack(3, type, value, traceback);
4650 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004651 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 return;
4653 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004654 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004656 if (err == 0) {
4657 _PyErr_Restore(tstate, type, value, orig_traceback);
4658 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 else {
4660 Py_XDECREF(type);
4661 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004662 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004664}
4665
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004666static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004667call_trace_protected(Py_tracefunc func, PyObject *obj,
4668 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 PyObject *type, *value, *traceback;
4672 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004673 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004674 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 if (err == 0)
4676 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004677 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 return 0;
4679 }
4680 else {
4681 Py_XDECREF(type);
4682 Py_XDECREF(value);
4683 Py_XDECREF(traceback);
4684 return -1;
4685 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004686}
4687
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004688static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004689call_trace(Py_tracefunc func, PyObject *obj,
4690 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 int result;
4694 if (tstate->tracing)
4695 return 0;
4696 tstate->tracing++;
4697 tstate->use_tracing = 0;
4698 result = func(obj, frame, what, arg);
4699 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4700 || (tstate->c_profilefunc != NULL));
4701 tstate->tracing--;
4702 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004703}
4704
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004705PyObject *
4706_PyEval_CallTracing(PyObject *func, PyObject *args)
4707{
Victor Stinner50b48572018-11-01 01:51:40 +01004708 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 int save_tracing = tstate->tracing;
4710 int save_use_tracing = tstate->use_tracing;
4711 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 tstate->tracing = 0;
4714 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4715 || (tstate->c_profilefunc != NULL));
4716 result = PyObject_Call(func, args, NULL);
4717 tstate->tracing = save_tracing;
4718 tstate->use_tracing = save_use_tracing;
4719 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004720}
4721
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004722/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004723static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004724maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004725 PyThreadState *tstate, PyFrameObject *frame,
4726 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 int result = 0;
4729 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 /* If the last instruction executed isn't in the current
4732 instruction window, reset the window.
4733 */
4734 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4735 PyAddrPair bounds;
4736 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4737 &bounds);
4738 *instr_lb = bounds.ap_lower;
4739 *instr_ub = bounds.ap_upper;
4740 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004741 /* If the last instruction falls at the start of a line or if it
4742 represents a jump backwards, update the frame's line number and
4743 then call the trace function if we're tracing source lines.
4744 */
4745 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004747 if (frame->f_trace_lines) {
4748 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 }
George King20faa682017-10-18 17:44:22 -07004751 /* Always emit an opcode event if we're tracing all opcodes. */
4752 if (frame->f_trace_opcodes) {
4753 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4754 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 *instr_prev = frame->f_lasti;
4756 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004757}
4758
Victor Stinner309d7cc2020-03-13 16:39:12 +01004759int
4760_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4761{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004762 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004763 /* The caller must hold the GIL */
4764 assert(PyGILState_Check());
4765
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004766 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004767 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004768 PyThreadState *current_tstate = _PyThreadState_GET();
4769 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004770 return -1;
4771 }
4772
4773 PyObject *profileobj = tstate->c_profileobj;
4774
4775 tstate->c_profilefunc = NULL;
4776 tstate->c_profileobj = NULL;
4777 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4778 tstate->use_tracing = tstate->c_tracefunc != NULL;
4779 Py_XDECREF(profileobj);
4780
4781 Py_XINCREF(arg);
4782 tstate->c_profileobj = arg;
4783 tstate->c_profilefunc = func;
4784
4785 /* Flag that tracing or profiling is turned on */
4786 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4787 return 0;
4788}
4789
Fred Drake5755ce62001-06-27 19:19:46 +00004790void
4791PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004792{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004793 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004794 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004795 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004796 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4797 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004798}
4799
4800int
4801_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4802{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004803 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004804 /* The caller must hold the GIL */
4805 assert(PyGILState_Check());
4806
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004807 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004808 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004809 PyThreadState *current_tstate = _PyThreadState_GET();
4810 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004811 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004812 }
4813
Victor Stinnerda2914d2020-03-20 09:29:08 +01004814 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004815 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004816 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004817
4818 tstate->c_tracefunc = NULL;
4819 tstate->c_traceobj = NULL;
4820 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4821 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4822 Py_XDECREF(traceobj);
4823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004825 tstate->c_traceobj = arg;
4826 tstate->c_tracefunc = func;
4827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004829 tstate->use_tracing = ((func != NULL)
4830 || (tstate->c_profilefunc != NULL));
4831
4832 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004833}
4834
4835void
4836PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4837{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004838 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004839 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004840 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004841 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4842 }
Fred Draked0838392001-06-16 21:02:31 +00004843}
4844
Victor Stinner309d7cc2020-03-13 16:39:12 +01004845
Yury Selivanov75445082015-05-11 22:57:16 -04004846void
Victor Stinner838f2642019-06-13 22:41:23 +02004847_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004848{
4849 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004850 tstate->coroutine_origin_tracking_depth = new_depth;
4851}
4852
4853int
4854_PyEval_GetCoroutineOriginTrackingDepth(void)
4855{
Victor Stinner50b48572018-11-01 01:51:40 +01004856 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004857 return tstate->coroutine_origin_tracking_depth;
4858}
4859
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004860int
Yury Selivanoveb636452016-09-08 22:01:51 -07004861_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4862{
Victor Stinner50b48572018-11-01 01:51:40 +01004863 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004864
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004865 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004866 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004867 }
4868
Yury Selivanoveb636452016-09-08 22:01:51 -07004869 Py_XINCREF(firstiter);
4870 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004871 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004872}
4873
4874PyObject *
4875_PyEval_GetAsyncGenFirstiter(void)
4876{
Victor Stinner50b48572018-11-01 01:51:40 +01004877 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004878 return tstate->async_gen_firstiter;
4879}
4880
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004881int
Yury Selivanoveb636452016-09-08 22:01:51 -07004882_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4883{
Victor Stinner50b48572018-11-01 01:51:40 +01004884 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004885
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004886 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004887 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004888 }
4889
Yury Selivanoveb636452016-09-08 22:01:51 -07004890 Py_XINCREF(finalizer);
4891 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004892 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004893}
4894
4895PyObject *
4896_PyEval_GetAsyncGenFinalizer(void)
4897{
Victor Stinner50b48572018-11-01 01:51:40 +01004898 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004899 return tstate->async_gen_finalizer;
4900}
4901
Victor Stinner438a12d2019-05-24 17:01:38 +02004902PyFrameObject *
4903PyEval_GetFrame(void)
4904{
4905 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004906 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004907}
4908
Guido van Rossumb209a111997-04-29 18:18:01 +00004909PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004910PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004911{
Victor Stinner438a12d2019-05-24 17:01:38 +02004912 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004913 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 else
4917 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004918}
4919
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004920/* Convenience function to get a builtin from its name */
4921PyObject *
4922_PyEval_GetBuiltinId(_Py_Identifier *name)
4923{
Victor Stinner438a12d2019-05-24 17:01:38 +02004924 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004925 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4926 if (attr) {
4927 Py_INCREF(attr);
4928 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004929 else if (!_PyErr_Occurred(tstate)) {
4930 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004931 }
4932 return attr;
4933}
4934
Guido van Rossumb209a111997-04-29 18:18:01 +00004935PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004936PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004937{
Victor Stinner438a12d2019-05-24 17:01:38 +02004938 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004939 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004940 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004941 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004943 }
4944
Victor Stinner438a12d2019-05-24 17:01:38 +02004945 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004946 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004947 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004948
4949 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004951}
4952
Guido van Rossumb209a111997-04-29 18:18:01 +00004953PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004954PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004955{
Victor Stinner438a12d2019-05-24 17:01:38 +02004956 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004957 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004958 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004960 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004961
4962 assert(current_frame->f_globals != NULL);
4963 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004964}
4965
Guido van Rossum6135a871995-01-09 17:53:26 +00004966int
Tim Peters5ba58662001-07-16 02:29:45 +00004967PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004968{
Victor Stinner438a12d2019-05-24 17:01:38 +02004969 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004970 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 if (current_frame != NULL) {
4974 const int codeflags = current_frame->f_code->co_flags;
4975 const int compilerflags = codeflags & PyCF_MASK;
4976 if (compilerflags) {
4977 result = 1;
4978 cf->cf_flags |= compilerflags;
4979 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004980#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 if (codeflags & CO_GENERATOR_ALLOWED) {
4982 result = 1;
4983 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4984 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004985#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 }
4987 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004988}
4989
Guido van Rossum3f5da241990-12-20 15:06:42 +00004990
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004991const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004992PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 if (PyMethod_Check(func))
4995 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4996 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004997 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 else if (PyCFunction_Check(func))
4999 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5000 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005001 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005002}
5003
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005004const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005005PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 if (PyMethod_Check(func))
5008 return "()";
5009 else if (PyFunction_Check(func))
5010 return "()";
5011 else if (PyCFunction_Check(func))
5012 return "()";
5013 else
5014 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005015}
5016
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005017#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005018if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005019 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5020 tstate, tstate->frame, \
5021 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 x = NULL; \
5023 } \
5024 else { \
5025 x = call; \
5026 if (tstate->c_profilefunc != NULL) { \
5027 if (x == NULL) { \
5028 call_trace_protected(tstate->c_profilefunc, \
5029 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005030 tstate, tstate->frame, \
5031 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 /* XXX should pass (type, value, tb) */ \
5033 } else { \
5034 if (call_trace(tstate->c_profilefunc, \
5035 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005036 tstate, tstate->frame, \
5037 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 Py_DECREF(x); \
5039 x = NULL; \
5040 } \
5041 } \
5042 } \
5043 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005044} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 x = call; \
5046 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005047
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005048
5049static PyObject *
5050trace_call_function(PyThreadState *tstate,
5051 PyObject *func,
5052 PyObject **args, Py_ssize_t nargs,
5053 PyObject *kwnames)
5054{
5055 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005056 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005057 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005058 return x;
5059 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005060 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005061 /* We need to create a temporary bound method as argument
5062 for profiling.
5063
5064 If nargs == 0, then this cannot work because we have no
5065 "self". In any case, the call itself would raise
5066 TypeError (foo needs an argument), so we just skip
5067 profiling. */
5068 PyObject *self = args[0];
5069 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5070 if (func == NULL) {
5071 return NULL;
5072 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005073 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005074 args+1, nargs-1,
5075 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005076 Py_DECREF(func);
5077 return x;
5078 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005079 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005080}
5081
Victor Stinner415c5102017-01-11 00:54:57 +01005082/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5083 to reduce the stack consumption. */
5084Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005085call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005086{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005087 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 PyObject *func = *pfunc;
5089 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005090 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5091 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005092 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005093
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005094 if (tstate->use_tracing) {
5095 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005096 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005097 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005098 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005099 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005100
Victor Stinner438a12d2019-05-24 17:01:38 +02005101 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005102
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005103 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 while ((*pp_stack) > pfunc) {
5105 w = EXT_POP(*pp_stack);
5106 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005110}
5111
Jeremy Hylton52820442001-01-03 23:52:36 +00005112static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005113do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005114{
jdemeyere89de732018-09-19 12:06:20 +02005115 PyObject *result;
5116
scoder4c9ea092020-05-12 16:12:41 +02005117 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005118 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005119 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005121 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005122 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5123 if (nargs > 0 && tstate->use_tracing) {
5124 /* We need to create a temporary bound method as argument
5125 for profiling.
5126
5127 If nargs == 0, then this cannot work because we have no
5128 "self". In any case, the call itself would raise
5129 TypeError (foo needs an argument), so we just skip
5130 profiling. */
5131 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5132 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5133 if (func == NULL) {
5134 return NULL;
5135 }
5136
Victor Stinner4d231bc2019-11-14 13:36:21 +01005137 C_TRACE(result, _PyObject_FastCallDictTstate(
5138 tstate, func,
5139 &_PyTuple_ITEMS(callargs)[1],
5140 nargs - 1,
5141 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005142 Py_DECREF(func);
5143 return result;
5144 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005145 }
jdemeyere89de732018-09-19 12:06:20 +02005146 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005147}
5148
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005149/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005150 nb_index slot defined, and store in *pi.
5151 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005152 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005153 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005154*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005155int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005156_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005157{
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005159 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005161 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005163 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 return 0;
5165 }
5166 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005167 _PyErr_SetString(tstate, PyExc_TypeError,
5168 "slice indices must be integers or "
5169 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005170 return 0;
5171 }
5172 *pi = x;
5173 }
5174 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005175}
5176
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005177int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005178_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005179{
Victor Stinner438a12d2019-05-24 17:01:38 +02005180 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005181 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005182 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005183 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005184 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005185 return 0;
5186 }
5187 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005188 _PyErr_SetString(tstate, PyExc_TypeError,
5189 "slice indices must be integers or "
5190 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005191 return 0;
5192 }
5193 *pi = x;
5194 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005195}
5196
Thomas Wouters52152252000-08-17 22:55:00 +00005197static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005198import_name(PyThreadState *tstate, PyFrameObject *f,
5199 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005200{
5201 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005202 PyObject *import_func, *res;
5203 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005204
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005205 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005206 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005207 if (!_PyErr_Occurred(tstate)) {
5208 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005209 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005210 return NULL;
5211 }
5212
5213 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005214 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005215 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005216 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005217 return NULL;
5218 }
5219 res = PyImport_ImportModuleLevelObject(
5220 name,
5221 f->f_globals,
5222 f->f_locals == NULL ? Py_None : f->f_locals,
5223 fromlist,
5224 ilevel);
5225 return res;
5226 }
5227
5228 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005229
5230 stack[0] = name;
5231 stack[1] = f->f_globals;
5232 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5233 stack[3] = fromlist;
5234 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005235 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005236 Py_DECREF(import_func);
5237 return res;
5238}
5239
5240static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005241import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005244 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005245
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005246 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005247 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005248 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005249 /* Issue #17636: in case this failed because of a circular relative
5250 import, try to fallback on reading the module directly from
5251 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005252 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005253 if (pkgname == NULL) {
5254 goto error;
5255 }
Oren Milman6db70332017-09-19 14:23:01 +03005256 if (!PyUnicode_Check(pkgname)) {
5257 Py_CLEAR(pkgname);
5258 goto error;
5259 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005260 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005261 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005262 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005263 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005264 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005265 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005266 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005267 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005268 goto error;
5269 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005270 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005272 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005273 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005274 if (pkgname == NULL) {
5275 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5276 if (pkgname_or_unknown == NULL) {
5277 Py_XDECREF(pkgpath);
5278 return NULL;
5279 }
5280 } else {
5281 pkgname_or_unknown = pkgname;
5282 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005283
5284 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005285 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005286 errmsg = PyUnicode_FromFormat(
5287 "cannot import name %R from %R (unknown location)",
5288 name, pkgname_or_unknown
5289 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005290 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005291 PyErr_SetImportError(errmsg, pkgname, NULL);
5292 }
5293 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005294 _Py_IDENTIFIER(__spec__);
5295 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005296 const char *fmt =
5297 _PyModuleSpec_IsInitializing(spec) ?
5298 "cannot import name %R from partially initialized module %R "
5299 "(most likely due to a circular import) (%S)" :
5300 "cannot import name %R from %R (%S)";
5301 Py_XDECREF(spec);
5302
5303 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005304 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005305 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005306 }
5307
Xiang Zhang4830f582017-03-21 11:13:42 +08005308 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005309 Py_XDECREF(pkgname_or_unknown);
5310 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005311 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005312}
Guido van Rossumac7be682001-01-17 15:42:30 +00005313
Thomas Wouters52152252000-08-17 22:55:00 +00005314static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005315import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005316{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005317 _Py_IDENTIFIER(__all__);
5318 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005319 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 int skip_leading_underscores = 0;
5321 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005322
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005323 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5324 return -1; /* Unexpected error */
5325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005326 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005327 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5328 return -1;
5329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005331 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005332 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 return -1;
5334 }
5335 all = PyMapping_Keys(dict);
5336 Py_DECREF(dict);
5337 if (all == NULL)
5338 return -1;
5339 skip_leading_underscores = 1;
5340 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 for (pos = 0, err = 0; ; pos++) {
5343 name = PySequence_GetItem(all, pos);
5344 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005345 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005347 }
5348 else {
5349 _PyErr_Clear(tstate);
5350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 break;
5352 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005353 if (!PyUnicode_Check(name)) {
5354 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5355 if (modname == NULL) {
5356 Py_DECREF(name);
5357 err = -1;
5358 break;
5359 }
5360 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005361 _PyErr_Format(tstate, PyExc_TypeError,
5362 "module __name__ must be a string, not %.100s",
5363 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005364 }
5365 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 _PyErr_Format(tstate, PyExc_TypeError,
5367 "%s in %U.%s must be str, not %.100s",
5368 skip_leading_underscores ? "Key" : "Item",
5369 modname,
5370 skip_leading_underscores ? "__dict__" : "__all__",
5371 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005372 }
5373 Py_DECREF(modname);
5374 Py_DECREF(name);
5375 err = -1;
5376 break;
5377 }
5378 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005379 if (PyUnicode_READY(name) == -1) {
5380 Py_DECREF(name);
5381 err = -1;
5382 break;
5383 }
5384 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5385 Py_DECREF(name);
5386 continue;
5387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 }
5389 value = PyObject_GetAttr(v, name);
5390 if (value == NULL)
5391 err = -1;
5392 else if (PyDict_CheckExact(locals))
5393 err = PyDict_SetItem(locals, name, value);
5394 else
5395 err = PyObject_SetItem(locals, name, value);
5396 Py_DECREF(name);
5397 Py_XDECREF(value);
5398 if (err != 0)
5399 break;
5400 }
5401 Py_DECREF(all);
5402 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005403}
5404
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005405static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005406check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005407{
Victor Stinnera102ed72020-02-07 02:24:48 +01005408 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005409 /* check_args_iterable() may be called with a live exception:
5410 * clear it to prevent calling _PyObject_FunctionStr() with an
5411 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005412 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005413 PyObject *funcstr = _PyObject_FunctionStr(func);
5414 if (funcstr != NULL) {
5415 _PyErr_Format(tstate, PyExc_TypeError,
5416 "%U argument after * must be an iterable, not %.200s",
5417 funcstr, Py_TYPE(args)->tp_name);
5418 Py_DECREF(funcstr);
5419 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005420 return -1;
5421 }
5422 return 0;
5423}
5424
5425static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005426format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005427{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005428 /* _PyDict_MergeEx raises attribute
5429 * error (percolated from an attempt
5430 * to get 'keys' attribute) instead of
5431 * a type error if its second argument
5432 * is not a mapping.
5433 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005434 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005435 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005436 PyObject *funcstr = _PyObject_FunctionStr(func);
5437 if (funcstr != NULL) {
5438 _PyErr_Format(
5439 tstate, PyExc_TypeError,
5440 "%U argument after ** must be a mapping, not %.200s",
5441 funcstr, Py_TYPE(kwargs)->tp_name);
5442 Py_DECREF(funcstr);
5443 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005444 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005446 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005447 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005448 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005449 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005450 PyObject *funcstr = _PyObject_FunctionStr(func);
5451 if (funcstr != NULL) {
5452 PyObject *key = PyTuple_GET_ITEM(val, 0);
5453 _PyErr_Format(
5454 tstate, PyExc_TypeError,
5455 "%U got multiple values for keyword argument '%S'",
5456 funcstr, key);
5457 Py_DECREF(funcstr);
5458 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005459 Py_XDECREF(exc);
5460 Py_XDECREF(val);
5461 Py_XDECREF(tb);
5462 }
5463 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005464 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005465 }
5466 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005467}
5468
Guido van Rossumac7be682001-01-17 15:42:30 +00005469static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005470format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5471 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 if (!obj)
5476 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005477
Serhiy Storchaka06515832016-11-20 09:13:07 +02005478 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 if (!obj_str)
5480 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005481
Victor Stinner438a12d2019-05-24 17:01:38 +02005482 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005483}
Guido van Rossum950361c1997-01-24 13:49:28 +00005484
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005485static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005486format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005487{
5488 PyObject *name;
5489 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005490 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005491 return;
5492 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5493 name = PyTuple_GET_ITEM(co->co_cellvars,
5494 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005495 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005496 PyExc_UnboundLocalError,
5497 UNBOUNDLOCAL_ERROR_MSG,
5498 name);
5499 } else {
5500 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5501 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005502 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005503 UNBOUNDFREE_ERROR_MSG, name);
5504 }
5505}
5506
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005507static void
Mark Shannonfee55262019-11-21 09:11:43 +00005508format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005509{
5510 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5511 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005512 _PyErr_Format(tstate, PyExc_TypeError,
5513 "'async with' received an object from __aenter__ "
5514 "that does not implement __await__: %.100s",
5515 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005516 }
Mark Shannonfee55262019-11-21 09:11:43 +00005517 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005518 _PyErr_Format(tstate, PyExc_TypeError,
5519 "'async with' received an object from __aexit__ "
5520 "that does not implement __await__: %.100s",
5521 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005522 }
5523 }
5524}
5525
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005526static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005527unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005528 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005529{
5530 PyObject *res;
5531 if (Py_REFCNT(v) == 2) {
5532 /* In the common case, there are 2 references to the value
5533 * stored in 'variable' when the += is performed: one on the
5534 * value stack (in 'v') and one still stored in the
5535 * 'variable'. We try to delete the variable now to reduce
5536 * the refcnt to 1.
5537 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005538 int opcode, oparg;
5539 NEXTOPARG();
5540 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005541 case STORE_FAST:
5542 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005543 PyObject **fastlocals = f->f_localsplus;
5544 if (GETLOCAL(oparg) == v)
5545 SETLOCAL(oparg, NULL);
5546 break;
5547 }
5548 case STORE_DEREF:
5549 {
5550 PyObject **freevars = (f->f_localsplus +
5551 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005552 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005553 if (PyCell_GET(c) == v) {
5554 PyCell_SET(c, NULL);
5555 Py_DECREF(v);
5556 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005557 break;
5558 }
5559 case STORE_NAME:
5560 {
5561 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005562 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005563 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005564 if (locals && PyDict_CheckExact(locals)) {
5565 PyObject *w = PyDict_GetItemWithError(locals, name);
5566 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005567 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005568 {
5569 Py_DECREF(v);
5570 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005571 }
5572 }
5573 break;
5574 }
5575 }
5576 }
5577 res = v;
5578 PyUnicode_Append(&res, w);
5579 return res;
5580}
5581
Guido van Rossum950361c1997-01-24 13:49:28 +00005582#ifdef DYNAMIC_EXECUTION_PROFILE
5583
Skip Montanarof118cb12001-10-15 20:51:38 +00005584static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005585getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 int i;
5588 PyObject *l = PyList_New(256);
5589 if (l == NULL) return NULL;
5590 for (i = 0; i < 256; i++) {
5591 PyObject *x = PyLong_FromLong(a[i]);
5592 if (x == NULL) {
5593 Py_DECREF(l);
5594 return NULL;
5595 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005596 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005597 }
5598 for (i = 0; i < 256; i++)
5599 a[i] = 0;
5600 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005601}
5602
5603PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005604_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005605{
5606#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005607 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005608#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 int i;
5610 PyObject *l = PyList_New(257);
5611 if (l == NULL) return NULL;
5612 for (i = 0; i < 257; i++) {
5613 PyObject *x = getarray(dxpairs[i]);
5614 if (x == NULL) {
5615 Py_DECREF(l);
5616 return NULL;
5617 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005618 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005619 }
5620 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005621#endif
5622}
5623
5624#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005625
5626Py_ssize_t
5627_PyEval_RequestCodeExtraIndex(freefunc free)
5628{
Victor Stinner81a7be32020-04-14 15:14:01 +02005629 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005630 Py_ssize_t new_index;
5631
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005632 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005633 return -1;
5634 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005635 new_index = interp->co_extra_user_count++;
5636 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005637 return new_index;
5638}
Łukasz Langaa785c872016-09-09 17:37:37 -07005639
5640static void
5641dtrace_function_entry(PyFrameObject *f)
5642{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005643 const char *filename;
5644 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005645 int lineno;
5646
Victor Stinner6d86a232020-04-29 00:56:58 +02005647 PyCodeObject *code = f->f_code;
5648 filename = PyUnicode_AsUTF8(code->co_filename);
5649 funcname = PyUnicode_AsUTF8(code->co_name);
5650 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005651
Andy Lestere6be9b52020-02-11 20:28:35 -06005652 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005653}
5654
5655static void
5656dtrace_function_return(PyFrameObject *f)
5657{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005658 const char *filename;
5659 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005660 int lineno;
5661
Victor Stinner6d86a232020-04-29 00:56:58 +02005662 PyCodeObject *code = f->f_code;
5663 filename = PyUnicode_AsUTF8(code->co_filename);
5664 funcname = PyUnicode_AsUTF8(code->co_name);
5665 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005666
Andy Lestere6be9b52020-02-11 20:28:35 -06005667 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005668}
5669
5670/* DTrace equivalent of maybe_call_line_trace. */
5671static void
5672maybe_dtrace_line(PyFrameObject *frame,
5673 int *instr_lb, int *instr_ub, int *instr_prev)
5674{
5675 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005676 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005677
5678 /* If the last instruction executed isn't in the current
5679 instruction window, reset the window.
5680 */
5681 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5682 PyAddrPair bounds;
5683 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5684 &bounds);
5685 *instr_lb = bounds.ap_lower;
5686 *instr_ub = bounds.ap_upper;
5687 }
5688 /* If the last instruction falls at the start of a line or if
5689 it represents a jump backwards, update the frame's line
5690 number and call the trace function. */
5691 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5692 frame->f_lineno = line;
5693 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5694 if (!co_filename)
5695 co_filename = "?";
5696 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5697 if (!co_name)
5698 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005699 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005700 }
5701 *instr_prev = frame->f_lasti;
5702}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005703
5704
5705/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5706 for the limited API. */
5707
5708#undef Py_EnterRecursiveCall
5709
5710int Py_EnterRecursiveCall(const char *where)
5711{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005712 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005713}
5714
5715#undef Py_LeaveRecursiveCall
5716
5717void Py_LeaveRecursiveCall(void)
5718{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005719 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005720}