blob: e54e344a5fd5145031f369695d6a474f94ecbffa [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 Stinnereb4e2ae2020-03-08 11:57:45 +0100243static void
244ensure_tstate_not_null(const char *func, PyThreadState *tstate)
245{
246 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100247 _Py_FatalErrorFunc(func,
248 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100249 }
250}
251
252
Victor Stinner7be4e352020-05-05 20:27:47 +0200253#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
254int
255_PyEval_ThreadsInitialized(PyInterpreterState *interp)
256{
257 return gil_created(&interp->ceval.gil);
258}
259
260int
261PyEval_ThreadsInitialized(void)
262{
263 // Fatal error if there is no current interpreter
264 PyInterpreterState *interp = PyInterpreterState_Get();
265 return _PyEval_ThreadsInitialized(interp);
266}
267#else
Tim Peters7f468f22004-10-11 02:40:51 +0000268int
Victor Stinner175a7042020-03-10 00:37:48 +0100269_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
270{
271 return gil_created(&runtime->ceval.gil);
272}
273
274int
Tim Peters7f468f22004-10-11 02:40:51 +0000275PyEval_ThreadsInitialized(void)
276{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100277 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100278 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000279}
Victor Stinner7be4e352020-05-05 20:27:47 +0200280#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000281
Victor Stinner111e4ee2020-03-09 21:24:14 +0100282PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200283_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000284{
Victor Stinner7be4e352020-05-05 20:27:47 +0200285#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200286 if (!_Py_IsMainInterpreter(tstate)) {
287 /* Currently, the GIL is shared by all interpreters,
288 and only the main interpreter is responsible to create
289 and destroy it. */
290 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100291 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200292#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100293
Victor Stinner7be4e352020-05-05 20:27:47 +0200294#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
295 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
296#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200297 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200298#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200299 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100300
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200301 PyThread_init_thread();
302 create_gil(gil);
303
304 take_gil(tstate);
305
306 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100307 return _PyStatus_OK();
308}
309
310void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200311_PyEval_FiniGIL(PyThreadState *tstate)
312{
Victor Stinner7be4e352020-05-05 20:27:47 +0200313#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200314 if (!_Py_IsMainInterpreter(tstate)) {
315 /* Currently, the GIL is shared by all interpreters,
316 and only the main interpreter is responsible to create
317 and destroy it. */
318 return;
319 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200320#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200321
Victor Stinner7be4e352020-05-05 20:27:47 +0200322#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
323 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
324#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200325 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200326#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200327 if (!gil_created(gil)) {
328 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
329 yet: do nothing. */
330 return;
331 }
332
333 destroy_gil(gil);
334 assert(!gil_created(gil));
335}
336
337void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100338PyEval_InitThreads(void)
339{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100340 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000342
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000343void
Inada Naoki91234a12019-06-03 21:30:58 +0900344_PyEval_Fini(void)
345{
346#if OPCACHE_STATS
347 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
348 opcache_code_objects);
349
350 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
351 opcache_code_objects_extra_mem);
352
353 fprintf(stderr, "\n");
354
355 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
356 opcache_global_hits,
357 (int) (100.0 * opcache_global_hits /
358 (opcache_global_hits + opcache_global_misses)));
359
360 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
361 opcache_global_misses,
362 (int) (100.0 * opcache_global_misses /
363 (opcache_global_hits + opcache_global_misses)));
364
365 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
366 opcache_global_opts);
367
368 fprintf(stderr, "\n");
369#endif
370}
371
372void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374{
Victor Stinner09532fe2019-05-10 23:39:09 +0200375 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200376 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100377 ensure_tstate_not_null(__func__, tstate);
378
Victor Stinner85f5a692020-03-09 22:12:04 +0100379 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000380}
381
382void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000384{
Victor Stinner09532fe2019-05-10 23:39:09 +0200385 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200386 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100388 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100389 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200390 struct _ceval_runtime_state *ceval = &runtime->ceval;
391 struct _ceval_state *ceval2 = &tstate->interp->ceval;
392 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000393}
394
395void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100396_PyEval_ReleaseLock(PyThreadState *tstate)
397{
398 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200399 struct _ceval_state *ceval2 = &tstate->interp->ceval;
400 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100401}
402
403void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000405{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100406 ensure_tstate_not_null(__func__, tstate);
407
Victor Stinner85f5a692020-03-09 22:12:04 +0100408 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200409
Victor Stinner85f5a692020-03-09 22:12:04 +0100410 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200411#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
412 (void)_PyThreadState_Swap(gilstate, tstate);
413#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100414 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100415 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200416 }
Victor Stinnere838a932020-05-05 19:56:48 +0200417#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000418}
419
420void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000422{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100423 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200424
Victor Stinner01b1cc12019-11-20 02:27:56 +0100425 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200426 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
427 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100428 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200429 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200430 struct _ceval_runtime_state *ceval = &runtime->ceval;
431 struct _ceval_state *ceval2 = &tstate->interp->ceval;
432 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000433}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000434
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900435#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200436/* This function is called from PyOS_AfterFork_Child to destroy all threads
437 * which are not running in the child process, and clear internal locks
438 * which might be held by those threads.
439 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000440
441void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200442_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000443{
Victor Stinner7be4e352020-05-05 20:27:47 +0200444 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
445 ensure_tstate_not_null(__func__, tstate);
446
447#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
448 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
449#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100450 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200451#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100452 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200454 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100455 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100456
457 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700458
Victor Stinner50e6e992020-03-19 02:41:21 +0100459 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900460 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700461 Py_FatalError("Can't initialize threads for pending calls");
462 }
Jesse Nollera8513972008-07-17 16:49:17 +0000463
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200464 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100465 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000466}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900467#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000468
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000469/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600470 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000471
472void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100473_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000474{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200475 assert(is_tstate_valid(tstate));
476 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000477}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000478
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000479PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000480PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000481{
Victor Stinner09532fe2019-05-10 23:39:09 +0200482 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200483#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
484 PyThreadState *old_tstate = _PyThreadState_GET();
485 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
486#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200487 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200488#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100489 ensure_tstate_not_null(__func__, tstate);
490
Victor Stinner0b1e3302020-05-05 16:14:31 +0200491 struct _ceval_runtime_state *ceval = &runtime->ceval;
492 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200493#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
494 assert(gil_created(&ceval2->gil));
495#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200496 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200497#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200498 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000500}
501
502void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000503PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000504{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100505 ensure_tstate_not_null(__func__, tstate);
506
Victor Stinner85f5a692020-03-09 22:12:04 +0100507 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100508
Victor Stinner85f5a692020-03-09 22:12:04 +0100509 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
510 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000511}
512
513
Guido van Rossuma9672091994-09-14 13:31:22 +0000514/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
515 signal handlers or Mac I/O completion routines) can schedule calls
516 to a function to be called synchronously.
517 The synchronous function is called with one void* argument.
518 It should return 0 for success or -1 for failure -- failure should
519 be accompanied by an exception.
520
521 If registry succeeds, the registry function returns 0; if it fails
522 (e.g. due to too many pending calls) it returns -1 (without setting
523 an exception condition).
524
525 Note that because registry may occur from within signal handlers,
526 or other asynchronous events, calling malloc() is unsafe!
527
Guido van Rossuma9672091994-09-14 13:31:22 +0000528 Any thread can schedule pending calls, but only the main thread
529 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000530 There is no facility to schedule calls to a particular thread, but
531 that should be easy to change, should that ever be required. In
532 that case, the static variables here should go into the python
533 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000534*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000535
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200536void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200537_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200538{
539 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100540 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200541 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200542 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200543}
544
Eric Snow5be45a62019-03-08 22:47:07 -0700545/* Push one item onto the queue while holding the lock. */
546static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200547_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600548 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700549{
Eric Snow842a2f02019-03-15 15:47:51 -0600550 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700551 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600552 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700553 return -1; /* Queue full */
554 }
Eric Snow842a2f02019-03-15 15:47:51 -0600555 pending->calls[i].func = func;
556 pending->calls[i].arg = arg;
557 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700558 return 0;
559}
560
561/* Pop one item off the queue while holding the lock. */
562static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200563_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600564 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700565{
Eric Snow842a2f02019-03-15 15:47:51 -0600566 int i = pending->first;
567 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700568 return; /* Queue empty */
569 }
570
Eric Snow842a2f02019-03-15 15:47:51 -0600571 *func = pending->calls[i].func;
572 *arg = pending->calls[i].arg;
573 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700574}
575
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200576/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000577 scheduling to be made from any thread, and even from an executing
578 callback.
579 */
580
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000581int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200582_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200583 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000584{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200585 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600586
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200587 /* Ensure that _PyEval_InitPendingCalls() was called
588 and that _PyEval_FiniPendingCalls() is not called yet. */
589 assert(pending->lock != NULL);
590
Eric Snow842a2f02019-03-15 15:47:51 -0600591 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200592 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600593 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700594
Victor Stinnere225beb2019-06-03 18:14:24 +0200595 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200596 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000598}
599
Victor Stinner09532fe2019-05-10 23:39:09 +0200600int
601Py_AddPendingCall(int (*func)(void *), void *arg)
602{
Victor Stinner50e6e992020-03-19 02:41:21 +0100603 /* Best-effort to support subinterpreters and calls with the GIL released.
604
605 First attempt _PyThreadState_GET() since it supports subinterpreters.
606
607 If the GIL is released, _PyThreadState_GET() returns NULL . In this
608 case, use PyGILState_GetThisThreadState() which works even if the GIL
609 is released.
610
611 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
612 see bpo-10915 and bpo-15751.
613
Victor Stinner8849e592020-03-18 19:28:53 +0100614 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100615 PyThreadState *tstate = _PyThreadState_GET();
616 if (tstate == NULL) {
617 tstate = PyGILState_GetThisThreadState();
618 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200619
620 PyInterpreterState *interp;
621 if (tstate != NULL) {
622 interp = tstate->interp;
623 }
624 else {
625 /* Last resort: use the main interpreter */
626 interp = _PyRuntime.interpreters.main;
627 }
628 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200629}
630
Eric Snowfdf282d2019-01-11 14:26:55 -0700631static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100632handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700633{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200634 assert(is_tstate_valid(tstate));
635 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700636 return 0;
637 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700638
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100640 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
641 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200642 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700643 return -1;
644 }
645 return 0;
646}
647
648static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100649make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000650{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200651 assert(is_tstate_valid(tstate));
652
Victor Stinnerd8316882020-03-20 14:50:35 +0100653 /* only execute pending calls on main thread */
654 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200655 return 0;
656 }
657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100659 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700660 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700662 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200663 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100664
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200665 /* unsignal before starting to call callbacks, so that any callback
666 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200667 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700668 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100671 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700672 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700673 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 void *arg = NULL;
675
676 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600677 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200678 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600679 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700680
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100681 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700682 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100683 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700684 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700685 res = func(arg);
686 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200687 goto error;
688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200690
Charles-François Natalif23339a2011-07-23 18:15:43 +0200691 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700692 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200693
694error:
695 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200696 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700697 return res;
698}
699
Eric Snow842a2f02019-03-15 15:47:51 -0600700void
Victor Stinner2b1df452020-01-13 18:46:59 +0100701_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600702{
Eric Snow842a2f02019-03-15 15:47:51 -0600703 assert(PyGILState_Check());
704
Victor Stinner50e6e992020-03-19 02:41:21 +0100705 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200706
Eric Snow842a2f02019-03-15 15:47:51 -0600707 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
708 return;
709 }
710
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100711 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200712 PyObject *exc, *val, *tb;
713 _PyErr_Fetch(tstate, &exc, &val, &tb);
714 PyErr_BadInternalCall();
715 _PyErr_ChainExceptions(exc, val, tb);
716 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600717 }
718}
719
Eric Snowfdf282d2019-01-11 14:26:55 -0700720/* Py_MakePendingCalls() is a simple wrapper for the sake
721 of backward-compatibility. */
722int
723Py_MakePendingCalls(void)
724{
725 assert(PyGILState_Check());
726
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100727 PyThreadState *tstate = _PyThreadState_GET();
728
Eric Snowfdf282d2019-01-11 14:26:55 -0700729 /* Python signal handler doesn't really queue a callback: it only signals
730 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100731 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700732 if (res != 0) {
733 return res;
734 }
735
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100736 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600737 if (res != 0) {
738 return res;
739 }
740
741 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000742}
743
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000744/* The interpreter's recursion limit */
745
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000746#ifndef Py_DEFAULT_RECURSION_LIMIT
747#define Py_DEFAULT_RECURSION_LIMIT 1000
748#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600749
Eric Snow05351c12017-09-05 21:43:08 -0700750int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000751
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600752void
Victor Stinnerdab84232020-03-17 18:56:44 +0100753_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600754{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600755 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinner7be4e352020-05-05 20:27:47 +0200756#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100757 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200758#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100759}
760
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200761int
Victor Stinnerdab84232020-03-17 18:56:44 +0100762_PyEval_InitState(struct _ceval_state *ceval)
763{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200764 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
765
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200766 struct _pending_calls *pending = &ceval->pending;
767 assert(pending->lock == NULL);
768
769 pending->lock = PyThread_allocate_lock();
770 if (pending->lock == NULL) {
771 return -1;
772 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200773
774#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
775 _gil_initialize(&ceval->gil);
776#endif
777
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200778 return 0;
779}
780
781void
782_PyEval_FiniState(struct _ceval_state *ceval)
783{
784 struct _pending_calls *pending = &ceval->pending;
785 if (pending->lock != NULL) {
786 PyThread_free_lock(pending->lock);
787 pending->lock = NULL;
788 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600789}
790
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000791int
792Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000793{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200794 PyThreadState *tstate = _PyThreadState_GET();
795 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000796}
797
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000798void
799Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000800{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200801 PyThreadState *tstate = _PyThreadState_GET();
802 tstate->interp->ceval.recursion_limit = new_limit;
803 if (_Py_IsMainInterpreter(tstate)) {
804 _Py_CheckRecursionLimit = new_limit;
805 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000806}
807
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100808/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000809 if the recursion_depth reaches _Py_CheckRecursionLimit.
810 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
811 to guarantee that _Py_CheckRecursiveCall() is regularly called.
812 Without USE_STACKCHECK, there is no need for this. */
813int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100814_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000815{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200816 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000817
818#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700819 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (PyOS_CheckStack()) {
821 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200822 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 return -1;
824 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200825 if (_Py_IsMainInterpreter(tstate)) {
826 /* Needed for ABI backwards-compatibility (see bpo-31857) */
827 _Py_CheckRecursionLimit = recursion_limit;
828 }
pdox18967932017-10-25 23:03:01 -0700829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (tstate->recursion_critical)
831 /* Somebody asked that we don't check for recursion. */
832 return 0;
833 if (tstate->overflowed) {
834 if (tstate->recursion_depth > recursion_limit + 50) {
835 /* Overflowing while handling an overflow. Give up. */
836 Py_FatalError("Cannot recover from stack overflow.");
837 }
838 return 0;
839 }
840 if (tstate->recursion_depth > recursion_limit) {
841 --tstate->recursion_depth;
842 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200843 _PyErr_Format(tstate, PyExc_RecursionError,
844 "maximum recursion depth exceeded%s",
845 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 return -1;
847 }
848 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000849}
850
Victor Stinner09532fe2019-05-10 23:39:09 +0200851static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200852static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000853
Victor Stinnere225beb2019-06-03 18:14:24 +0200854#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000855
Guido van Rossum374a9221991-04-04 10:40:29 +0000856
Guido van Rossumb209a111997-04-29 18:18:01 +0000857PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000858PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 return PyEval_EvalCodeEx(co,
861 globals, locals,
862 (PyObject **)NULL, 0,
863 (PyObject **)NULL, 0,
864 (PyObject **)NULL, 0,
865 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000866}
867
868
869/* Interpreter main loop */
870
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000871PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100872PyEval_EvalFrame(PyFrameObject *f)
873{
Victor Stinner0b72b232020-03-12 23:18:39 +0100874 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100875 PyThreadState *tstate = _PyThreadState_GET();
876 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000877}
878
879PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000881{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100882 PyThreadState *tstate = _PyThreadState_GET();
883 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700884}
885
Victor Stinnerda2914d2020-03-20 09:29:08 +0100886
887/* Handle signals, pending calls, GIL drop request
888 and asynchronous exception */
889static int
890eval_frame_handle_pending(PyThreadState *tstate)
891{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100892 _PyRuntimeState * const runtime = &_PyRuntime;
893 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200894
895 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200896 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100897 if (handle_signals(tstate) != 0) {
898 return -1;
899 }
900 }
901
902 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200903 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100904 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
905 if (make_pending_calls(tstate) != 0) {
906 return -1;
907 }
908 }
909
910 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200911 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100912 /* Give another thread a chance */
913 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
914 Py_FatalError("tstate mix-up");
915 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200916 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100917
918 /* Other threads may run now */
919
920 take_gil(tstate);
921
Victor Stinnere838a932020-05-05 19:56:48 +0200922#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
923 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
924#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100925 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
926 Py_FatalError("orphan tstate");
927 }
Victor Stinnere838a932020-05-05 19:56:48 +0200928#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100929 }
930
931 /* Check for asynchronous exception. */
932 if (tstate->async_exc != NULL) {
933 PyObject *exc = tstate->async_exc;
934 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200935 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100936 _PyErr_SetNone(tstate, exc);
937 Py_DECREF(exc);
938 return -1;
939 }
940
941 return 0;
942}
943
Victor Stinnerc6944e72016-11-11 02:13:35 +0100944PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100945_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700946{
Victor Stinner0b72b232020-03-12 23:18:39 +0100947 ensure_tstate_not_null(__func__, tstate);
948
Guido van Rossum950361c1997-01-24 13:49:28 +0000949#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000951#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200952 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300953 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200954 int opcode; /* Current opcode */
955 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200956 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100958 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100959 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 is true when the line being executed has changed. The
967 initial values are such as to make this false the first
968 time it is tested. */
969 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000970
Serhiy Storchakaab874002016-09-11 13:48:15 +0300971 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyObject *names;
973 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900974 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000975
Brett Cannon368b4b72012-04-02 12:17:59 -0400976#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200977 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400978#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200979
Antoine Pitroub52ec782009-01-25 16:34:23 +0000980/* Computed GOTOs, or
981 the-optimization-commonly-but-improperly-known-as-"threaded code"
982 using gcc's labels-as-values extension
983 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
984
985 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000987 combined with a lookup table of jump addresses. However, since the
988 indirect jump instruction is shared by all opcodes, the CPU will have a
989 hard time making the right prediction for where to jump next (actually,
990 it will be always wrong except in the uncommon case of a sequence of
991 several identical opcodes).
992
993 "Threaded code" in contrast, uses an explicit jump table and an explicit
994 indirect jump instruction at the end of each opcode. Since the jump
995 instruction is at a different address for each opcode, the CPU will make a
996 separate prediction for each of these instructions, which is equivalent to
997 predicting the second opcode of each opcode pair. These predictions have
998 a much better chance to turn out valid, especially in small bytecode loops.
999
1000 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001002 and potentially many more instructions (depending on the pipeline width).
1003 A correctly predicted branch, however, is nearly free.
1004
1005 At the time of this writing, the "threaded code" version is up to 15-20%
1006 faster than the normal "switch" version, depending on the compiler and the
1007 CPU architecture.
1008
1009 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1010 because it would render the measurements invalid.
1011
1012
1013 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1014 indirect jumps by sharing them between all opcodes. Such optimizations
1015 can be disabled on gcc by using the -fno-gcse flag (or possibly
1016 -fno-crossjumping).
1017*/
1018
Antoine Pitrou042b1282010-08-13 21:15:58 +00001019#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001021#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001022#endif
1023
Antoine Pitrou042b1282010-08-13 21:15:58 +00001024#ifdef HAVE_COMPUTED_GOTOS
1025 #ifndef USE_COMPUTED_GOTOS
1026 #define USE_COMPUTED_GOTOS 1
1027 #endif
1028#else
1029 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1030 #error "Computed gotos are not supported on this compiler."
1031 #endif
1032 #undef USE_COMPUTED_GOTOS
1033 #define USE_COMPUTED_GOTOS 0
1034#endif
1035
1036#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037/* Import the static jump table */
1038#include "opcode_targets.h"
1039
Antoine Pitroub52ec782009-01-25 16:34:23 +00001040#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001041 op: \
1042 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001043
Antoine Pitroub52ec782009-01-25 16:34:23 +00001044#ifdef LLTRACE
1045#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001047 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001049 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001050 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 } \
1052 goto fast_next_opcode; \
1053 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001054#else
1055#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001057 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001059 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001060 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 } \
1062 goto fast_next_opcode; \
1063 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001064#endif
1065
Victor Stinner09532fe2019-05-10 23:39:09 +02001066#define DISPATCH() \
1067 { \
1068 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1069 FAST_DISPATCH(); \
1070 } \
1071 continue; \
1072 }
1073
Antoine Pitroub52ec782009-01-25 16:34:23 +00001074#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001075#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001076#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001077#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001078#endif
1079
1080
Neal Norwitza81d2202002-07-14 00:27:26 +00001081/* Tuple access macros */
1082
1083#ifndef Py_DEBUG
1084#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1085#else
1086#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1087#endif
1088
Guido van Rossum374a9221991-04-04 10:40:29 +00001089/* Code access macros */
1090
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001091/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001092#define INSTR_OFFSET() \
1093 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001094#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001095 _Py_CODEUNIT word = *next_instr; \
1096 opcode = _Py_OPCODE(word); \
1097 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001098 next_instr++; \
1099 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001100#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1101#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001102
Raymond Hettingerf606f872003-03-16 03:11:04 +00001103/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 Some opcodes tend to come in pairs thus making it possible to
1105 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001106 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 Verifying the prediction costs a single high-speed test of a register
1109 variable against a constant. If the pairing was good, then the
1110 processor's own internal branch predication has a high likelihood of
1111 success, resulting in a nearly zero-overhead transition to the
1112 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001113 including its unpredictable switch-case branch. Combined with the
1114 processor's internal branch prediction, a successful PREDICT has the
1115 effect of making the two opcodes run as if they were a single new opcode
1116 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001117
Georg Brandl86b2fb92008-07-16 03:43:04 +00001118 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 predictions turned-on and interpret the results as if some opcodes
1120 had been combined or turn-off predictions so that the opcode frequency
1121 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001122
1123 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 the CPU to record separate branch prediction information for each
1125 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001126
Raymond Hettingerf606f872003-03-16 03:11:04 +00001127*/
1128
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001129#define PREDICT_ID(op) PRED_##op
1130
Antoine Pitrou042b1282010-08-13 21:15:58 +00001131#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001132#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001133#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001134#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001135 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001136 _Py_CODEUNIT word = *next_instr; \
1137 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001138 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001139 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001140 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001141 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001142 } \
1143 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001144#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001145#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001146
Raymond Hettingerf606f872003-03-16 03:11:04 +00001147
Guido van Rossum374a9221991-04-04 10:40:29 +00001148/* Stack manipulation macros */
1149
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150/* The stack can grow at most MAXINT deep, as co_nlocals and
1151 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001152#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1153#define EMPTY() (STACK_LEVEL() == 0)
1154#define TOP() (stack_pointer[-1])
1155#define SECOND() (stack_pointer[-2])
1156#define THIRD() (stack_pointer[-3])
1157#define FOURTH() (stack_pointer[-4])
1158#define PEEK(n) (stack_pointer[-(n)])
1159#define SET_TOP(v) (stack_pointer[-1] = (v))
1160#define SET_SECOND(v) (stack_pointer[-2] = (v))
1161#define SET_THIRD(v) (stack_pointer[-3] = (v))
1162#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1163#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1164#define BASIC_STACKADJ(n) (stack_pointer += n)
1165#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1166#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001167
Guido van Rossum96a42c81992-01-12 02:29:51 +00001168#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001170 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001171 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001172#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001173 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001174#define STACK_GROW(n) do { \
1175 assert(n >= 0); \
1176 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001177 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001178 assert(STACK_LEVEL() <= co->co_stacksize); \
1179 } while (0)
1180#define STACK_SHRINK(n) do { \
1181 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001182 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001183 (void)(BASIC_STACKADJ(-n)); \
1184 assert(STACK_LEVEL() <= co->co_stacksize); \
1185 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001186#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001187 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001188 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001189#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001190#define PUSH(v) BASIC_PUSH(v)
1191#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001192#define STACK_GROW(n) BASIC_STACKADJ(n)
1193#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001194#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001195#endif
1196
Guido van Rossum681d79a1995-07-18 14:51:37 +00001197/* Local variable macros */
1198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001200
1201/* The SETLOCAL() macro must not DECREF the local variable in-place and
1202 then store the new value; it must copy the old value to a temporary
1203 value, then store the new value, and then DECREF the temporary value.
1204 This is because it is possible that during the DECREF the frame is
1205 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1206 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001208 GETLOCAL(i) = value; \
1209 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001210
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001211
1212#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 while (STACK_LEVEL() > (b)->b_level) { \
1214 PyObject *v = POP(); \
1215 Py_XDECREF(v); \
1216 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001217
1218#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001219 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001221 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1223 while (STACK_LEVEL() > (b)->b_level + 3) { \
1224 value = POP(); \
1225 Py_XDECREF(value); \
1226 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001227 exc_info = tstate->exc_info; \
1228 type = exc_info->exc_type; \
1229 value = exc_info->exc_value; \
1230 traceback = exc_info->exc_traceback; \
1231 exc_info->exc_type = POP(); \
1232 exc_info->exc_value = POP(); \
1233 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 Py_XDECREF(type); \
1235 Py_XDECREF(value); \
1236 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001237 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001238
Inada Naoki91234a12019-06-03 21:30:58 +09001239 /* macros for opcode cache */
1240#define OPCACHE_CHECK() \
1241 do { \
1242 co_opcache = NULL; \
1243 if (co->co_opcache != NULL) { \
1244 unsigned char co_opt_offset = \
1245 co->co_opcache_map[next_instr - first_instr]; \
1246 if (co_opt_offset > 0) { \
1247 assert(co_opt_offset <= co->co_opcache_size); \
1248 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1249 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001250 } \
1251 } \
1252 } while (0)
1253
1254#if OPCACHE_STATS
1255
1256#define OPCACHE_STAT_GLOBAL_HIT() \
1257 do { \
1258 if (co->co_opcache != NULL) opcache_global_hits++; \
1259 } while (0)
1260
1261#define OPCACHE_STAT_GLOBAL_MISS() \
1262 do { \
1263 if (co->co_opcache != NULL) opcache_global_misses++; \
1264 } while (0)
1265
1266#define OPCACHE_STAT_GLOBAL_OPT() \
1267 do { \
1268 if (co->co_opcache != NULL) opcache_global_opts++; \
1269 } while (0)
1270
1271#else /* OPCACHE_STATS */
1272
1273#define OPCACHE_STAT_GLOBAL_HIT()
1274#define OPCACHE_STAT_GLOBAL_MISS()
1275#define OPCACHE_STAT_GLOBAL_OPT()
1276
1277#endif
1278
Guido van Rossuma027efa1997-05-05 20:56:21 +00001279/* Start of code */
1280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001282 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001284 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (tstate->use_tracing) {
1289 if (tstate->c_tracefunc != NULL) {
1290 /* tstate->c_tracefunc, if defined, is a
1291 function that will be called on *every* entry
1292 to a code block. Its return value, if not
1293 None, is a function that will be called at
1294 the start of each executed line of code.
1295 (Actually, the function must return itself
1296 in order to continue tracing.) The trace
1297 functions are called with three arguments:
1298 a pointer to the current frame, a string
1299 indicating why the function is called, and
1300 an argument which depends on the situation.
1301 The global trace function is also called
1302 whenever an exception is detected. */
1303 if (call_trace_protected(tstate->c_tracefunc,
1304 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001305 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* Trace function raised an error */
1307 goto exit_eval_frame;
1308 }
1309 }
1310 if (tstate->c_profilefunc != NULL) {
1311 /* Similar for c_profilefunc, except it needn't
1312 return itself and isn't called for "line" events */
1313 if (call_trace_protected(tstate->c_profilefunc,
1314 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001315 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Profile function raised an error */
1317 goto exit_eval_frame;
1318 }
1319 }
1320 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001321
Łukasz Langaa785c872016-09-09 17:37:37 -07001322 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1323 dtrace_function_entry(f);
1324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 co = f->f_code;
1326 names = co->co_names;
1327 consts = co->co_consts;
1328 fastlocals = f->f_localsplus;
1329 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001330 assert(PyBytes_Check(co->co_code));
1331 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001332 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1333 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1334 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001335 /*
1336 f->f_lasti refers to the index of the last instruction,
1337 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001338
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001339 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001340 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 When the PREDICT() macros are enabled, some opcode pairs follow in
1343 direct succession without updating f->f_lasti. A successful
1344 prediction effectively links the two codes together as if they
1345 were a single new opcode; accordingly,f->f_lasti will point to
1346 the first code in the pair (for instance, GET_ITER followed by
1347 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001348 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001350 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001351 next_instr = first_instr;
1352 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001353 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1354 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 stack_pointer = f->f_stacktop;
1357 assert(stack_pointer != NULL);
1358 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001359 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001360
Inada Naoki91234a12019-06-03 21:30:58 +09001361 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1362 co->co_opcache_flag++;
1363 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1364 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001365 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001366 }
1367#if OPCACHE_STATS
1368 opcache_code_objects_extra_mem +=
1369 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1370 sizeof(_PyOpcache) * co->co_opcache_size;
1371 opcache_code_objects++;
1372#endif
1373 }
1374 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001375
Tim Peters5ca576e2001-06-18 22:08:13 +00001376#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001377 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001378#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 if (throwflag) /* support for generator.throw() */
1381 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382
Victor Stinnerace47d72013-07-18 01:41:08 +02001383#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001384 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001385 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001386 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001387 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001388#endif
1389
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001390main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1393 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001394 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* Do periodic things. Doing this every time through
1397 the loop would add too much overhead, so we do it
1398 only every Nth instruction. We also do it if
1399 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1400 event needs attention (e.g. a signal handler or
1401 async I/O handler); see Py_AddPendingCall() and
1402 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Eric Snow7bda9de2019-03-08 17:25:54 -07001404 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001405 opcode = _Py_OPCODE(*next_instr);
1406 if (opcode == SETUP_FINALLY ||
1407 opcode == SETUP_WITH ||
1408 opcode == BEFORE_ASYNC_WITH ||
1409 opcode == YIELD_FROM) {
1410 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001411 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001412 - If we're about to enter the 'with:'. It will prevent
1413 emitting a resource warning in the common idiom
1414 'with open(path) as file:'.
1415 - If we're about to enter the 'async with:'.
1416 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001417 *very* useful, but might help in some cases and it's
1418 traditional)
1419 - If we're resuming a chain of nested 'yield from' or
1420 'await' calls, then each frame is parked with YIELD_FROM
1421 as its next opcode. If the user hit control-C we want to
1422 wait until we've reached the innermost frame before
1423 running the signal handler and raising KeyboardInterrupt
1424 (see bpo-30039).
1425 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 goto fast_next_opcode;
1427 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001428
Victor Stinnerda2914d2020-03-20 09:29:08 +01001429 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 }
1432 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 fast_next_opcode:
1435 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Łukasz Langaa785c872016-09-09 17:37:37 -07001437 if (PyDTrace_LINE_ENABLED())
1438 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001441
Victor Stinnerdab84232020-03-17 18:56:44 +01001442 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001443 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 /* see maybe_call_line_trace
1446 for expository comments */
1447 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 err = maybe_call_line_trace(tstate->c_tracefunc,
1450 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001451 tstate, f,
1452 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 /* Reload possibly changed frame fields */
1454 JUMPTO(f->f_lasti);
1455 if (f->f_stacktop != NULL) {
1456 stack_pointer = f->f_stacktop;
1457 f->f_stacktop = NULL;
1458 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001465
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001466 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001467 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001468#ifdef DYNAMIC_EXECUTION_PROFILE
1469#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 dxpairs[lastopcode][opcode]++;
1471 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001472#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001474#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001475
Guido van Rossum96a42c81992-01-12 02:29:51 +00001476#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (lltrace) {
1480 if (HAS_ARG(opcode)) {
1481 printf("%d: %d, %d\n",
1482 f->f_lasti, opcode, oparg);
1483 }
1484 else {
1485 printf("%d: %d\n",
1486 f->f_lasti, opcode);
1487 }
1488 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001489#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001494 It is essential that any operation that fails must goto error
1495 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001499 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *value = GETLOCAL(oparg);
1503 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001504 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 UNBOUNDLOCAL_ERROR_MSG,
1506 PyTuple_GetItem(co->co_varnames, oparg));
1507 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 Py_INCREF(value);
1510 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 }
1513
Benjamin Petersonddd19492018-09-16 22:38:02 -07001514 case TARGET(LOAD_CONST): {
1515 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 PyObject *value = GETITEM(consts, oparg);
1517 Py_INCREF(value);
1518 PUSH(value);
1519 FAST_DISPATCH();
1520 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(STORE_FAST): {
1523 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 PyObject *value = POP();
1525 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001528
Benjamin Petersonddd19492018-09-16 22:38:02 -07001529 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 PyObject *value = POP();
1531 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Petersonddd19492018-09-16 22:38:02 -07001535 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 PyObject *top = TOP();
1537 PyObject *second = SECOND();
1538 SET_TOP(second);
1539 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001542
Benjamin Petersonddd19492018-09-16 22:38:02 -07001543 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001544 PyObject *top = TOP();
1545 PyObject *second = SECOND();
1546 PyObject *third = THIRD();
1547 SET_TOP(second);
1548 SET_SECOND(third);
1549 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Benjamin Petersonddd19492018-09-16 22:38:02 -07001553 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001554 PyObject *top = TOP();
1555 PyObject *second = SECOND();
1556 PyObject *third = THIRD();
1557 PyObject *fourth = FOURTH();
1558 SET_TOP(second);
1559 SET_SECOND(third);
1560 SET_THIRD(fourth);
1561 SET_FOURTH(top);
1562 FAST_DISPATCH();
1563 }
1564
Benjamin Petersonddd19492018-09-16 22:38:02 -07001565 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 PyObject *top = TOP();
1567 Py_INCREF(top);
1568 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001575 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001577 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 SET_TOP(top);
1579 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001580 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001582
Benjamin Petersonddd19492018-09-16 22:38:02 -07001583 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 PyObject *value = TOP();
1585 PyObject *res = PyNumber_Positive(value);
1586 Py_DECREF(value);
1587 SET_TOP(res);
1588 if (res == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Petersonddd19492018-09-16 22:38:02 -07001593 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 PyObject *value = TOP();
1595 PyObject *res = PyNumber_Negative(value);
1596 Py_DECREF(value);
1597 SET_TOP(res);
1598 if (res == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *value = TOP();
1605 int err = PyObject_IsTrue(value);
1606 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (err == 0) {
1608 Py_INCREF(Py_True);
1609 SET_TOP(Py_True);
1610 DISPATCH();
1611 }
1612 else if (err > 0) {
1613 Py_INCREF(Py_False);
1614 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 DISPATCH();
1616 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001617 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001618 goto error;
1619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Benjamin Petersonddd19492018-09-16 22:38:02 -07001621 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 PyObject *value = TOP();
1623 PyObject *res = PyNumber_Invert(value);
1624 Py_DECREF(value);
1625 SET_TOP(res);
1626 if (res == NULL)
1627 goto error;
1628 DISPATCH();
1629 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Benjamin Petersonddd19492018-09-16 22:38:02 -07001631 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 PyObject *exp = POP();
1633 PyObject *base = TOP();
1634 PyObject *res = PyNumber_Power(base, exp, Py_None);
1635 Py_DECREF(base);
1636 Py_DECREF(exp);
1637 SET_TOP(res);
1638 if (res == NULL)
1639 goto error;
1640 DISPATCH();
1641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Benjamin Petersonddd19492018-09-16 22:38:02 -07001643 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001644 PyObject *right = POP();
1645 PyObject *left = TOP();
1646 PyObject *res = PyNumber_Multiply(left, right);
1647 Py_DECREF(left);
1648 Py_DECREF(right);
1649 SET_TOP(res);
1650 if (res == NULL)
1651 goto error;
1652 DISPATCH();
1653 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Benjamin Petersonddd19492018-09-16 22:38:02 -07001655 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001656 PyObject *right = POP();
1657 PyObject *left = TOP();
1658 PyObject *res = PyNumber_MatrixMultiply(left, right);
1659 Py_DECREF(left);
1660 Py_DECREF(right);
1661 SET_TOP(res);
1662 if (res == NULL)
1663 goto error;
1664 DISPATCH();
1665 }
1666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *divisor = POP();
1669 PyObject *dividend = TOP();
1670 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1671 Py_DECREF(dividend);
1672 Py_DECREF(divisor);
1673 SET_TOP(quotient);
1674 if (quotient == NULL)
1675 goto error;
1676 DISPATCH();
1677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Benjamin Petersonddd19492018-09-16 22:38:02 -07001679 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 PyObject *divisor = POP();
1681 PyObject *dividend = TOP();
1682 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1683 Py_DECREF(dividend);
1684 Py_DECREF(divisor);
1685 SET_TOP(quotient);
1686 if (quotient == NULL)
1687 goto error;
1688 DISPATCH();
1689 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *divisor = POP();
1693 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001694 PyObject *res;
1695 if (PyUnicode_CheckExact(dividend) && (
1696 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1697 // fast path; string formatting, but not if the RHS is a str subclass
1698 // (see issue28598)
1699 res = PyUnicode_Format(dividend, divisor);
1700 } else {
1701 res = PyNumber_Remainder(dividend, divisor);
1702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 Py_DECREF(divisor);
1704 Py_DECREF(dividend);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 PyObject *right = POP();
1713 PyObject *left = TOP();
1714 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001715 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1716 CPython using bytecode, it is simply worthless.
1717 See http://bugs.python.org/issue21955 and
1718 http://bugs.python.org/issue10044 for the discussion. In short,
1719 no patch shown any impact on a realistic benchmark, only a minor
1720 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 if (PyUnicode_CheckExact(left) &&
1722 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001723 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001724 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001725 }
1726 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 sum = PyNumber_Add(left, right);
1728 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001729 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 Py_DECREF(right);
1731 SET_TOP(sum);
1732 if (sum == NULL)
1733 goto error;
1734 DISPATCH();
1735 }
1736
Benjamin Petersonddd19492018-09-16 22:38:02 -07001737 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 PyObject *right = POP();
1739 PyObject *left = TOP();
1740 PyObject *diff = PyNumber_Subtract(left, right);
1741 Py_DECREF(right);
1742 Py_DECREF(left);
1743 SET_TOP(diff);
1744 if (diff == NULL)
1745 goto error;
1746 DISPATCH();
1747 }
1748
Benjamin Petersonddd19492018-09-16 22:38:02 -07001749 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 PyObject *sub = POP();
1751 PyObject *container = TOP();
1752 PyObject *res = PyObject_GetItem(container, sub);
1753 Py_DECREF(container);
1754 Py_DECREF(sub);
1755 SET_TOP(res);
1756 if (res == NULL)
1757 goto error;
1758 DISPATCH();
1759 }
1760
Benjamin Petersonddd19492018-09-16 22:38:02 -07001761 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 PyObject *right = POP();
1763 PyObject *left = TOP();
1764 PyObject *res = PyNumber_Lshift(left, right);
1765 Py_DECREF(left);
1766 Py_DECREF(right);
1767 SET_TOP(res);
1768 if (res == NULL)
1769 goto error;
1770 DISPATCH();
1771 }
1772
Benjamin Petersonddd19492018-09-16 22:38:02 -07001773 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 PyObject *right = POP();
1775 PyObject *left = TOP();
1776 PyObject *res = PyNumber_Rshift(left, right);
1777 Py_DECREF(left);
1778 Py_DECREF(right);
1779 SET_TOP(res);
1780 if (res == NULL)
1781 goto error;
1782 DISPATCH();
1783 }
1784
Benjamin Petersonddd19492018-09-16 22:38:02 -07001785 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 PyObject *right = POP();
1787 PyObject *left = TOP();
1788 PyObject *res = PyNumber_And(left, right);
1789 Py_DECREF(left);
1790 Py_DECREF(right);
1791 SET_TOP(res);
1792 if (res == NULL)
1793 goto error;
1794 DISPATCH();
1795 }
1796
Benjamin Petersonddd19492018-09-16 22:38:02 -07001797 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 PyObject *right = POP();
1799 PyObject *left = TOP();
1800 PyObject *res = PyNumber_Xor(left, right);
1801 Py_DECREF(left);
1802 Py_DECREF(right);
1803 SET_TOP(res);
1804 if (res == NULL)
1805 goto error;
1806 DISPATCH();
1807 }
1808
Benjamin Petersonddd19492018-09-16 22:38:02 -07001809 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 PyObject *right = POP();
1811 PyObject *left = TOP();
1812 PyObject *res = PyNumber_Or(left, right);
1813 Py_DECREF(left);
1814 Py_DECREF(right);
1815 SET_TOP(res);
1816 if (res == NULL)
1817 goto error;
1818 DISPATCH();
1819 }
1820
Benjamin Petersonddd19492018-09-16 22:38:02 -07001821 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 PyObject *v = POP();
1823 PyObject *list = PEEK(oparg);
1824 int err;
1825 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 if (err != 0)
1828 goto error;
1829 PREDICT(JUMP_ABSOLUTE);
1830 DISPATCH();
1831 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001832
Benjamin Petersonddd19492018-09-16 22:38:02 -07001833 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001835 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 int err;
1837 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 if (err != 0)
1840 goto error;
1841 PREDICT(JUMP_ABSOLUTE);
1842 DISPATCH();
1843 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001844
Benjamin Petersonddd19492018-09-16 22:38:02 -07001845 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 PyObject *exp = POP();
1847 PyObject *base = TOP();
1848 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1849 Py_DECREF(base);
1850 Py_DECREF(exp);
1851 SET_TOP(res);
1852 if (res == NULL)
1853 goto error;
1854 DISPATCH();
1855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Benjamin Petersonddd19492018-09-16 22:38:02 -07001857 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 PyObject *right = POP();
1859 PyObject *left = TOP();
1860 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1861 Py_DECREF(left);
1862 Py_DECREF(right);
1863 SET_TOP(res);
1864 if (res == NULL)
1865 goto error;
1866 DISPATCH();
1867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001870 PyObject *right = POP();
1871 PyObject *left = TOP();
1872 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1873 Py_DECREF(left);
1874 Py_DECREF(right);
1875 SET_TOP(res);
1876 if (res == NULL)
1877 goto error;
1878 DISPATCH();
1879 }
1880
Benjamin Petersonddd19492018-09-16 22:38:02 -07001881 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 PyObject *divisor = POP();
1883 PyObject *dividend = TOP();
1884 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1885 Py_DECREF(dividend);
1886 Py_DECREF(divisor);
1887 SET_TOP(quotient);
1888 if (quotient == NULL)
1889 goto error;
1890 DISPATCH();
1891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyObject *divisor = POP();
1895 PyObject *dividend = TOP();
1896 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1897 Py_DECREF(dividend);
1898 Py_DECREF(divisor);
1899 SET_TOP(quotient);
1900 if (quotient == NULL)
1901 goto error;
1902 DISPATCH();
1903 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Benjamin Petersonddd19492018-09-16 22:38:02 -07001905 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PyObject *right = POP();
1907 PyObject *left = TOP();
1908 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1909 Py_DECREF(left);
1910 Py_DECREF(right);
1911 SET_TOP(mod);
1912 if (mod == NULL)
1913 goto error;
1914 DISPATCH();
1915 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 PyObject *right = POP();
1919 PyObject *left = TOP();
1920 PyObject *sum;
1921 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001922 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001923 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001924 }
1925 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 sum = PyNumber_InPlaceAdd(left, right);
1927 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 Py_DECREF(right);
1930 SET_TOP(sum);
1931 if (sum == NULL)
1932 goto error;
1933 DISPATCH();
1934 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Benjamin Petersonddd19492018-09-16 22:38:02 -07001936 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 PyObject *right = POP();
1938 PyObject *left = TOP();
1939 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1940 Py_DECREF(left);
1941 Py_DECREF(right);
1942 SET_TOP(diff);
1943 if (diff == NULL)
1944 goto error;
1945 DISPATCH();
1946 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Benjamin Petersonddd19492018-09-16 22:38:02 -07001948 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001949 PyObject *right = POP();
1950 PyObject *left = TOP();
1951 PyObject *res = PyNumber_InPlaceLshift(left, right);
1952 Py_DECREF(left);
1953 Py_DECREF(right);
1954 SET_TOP(res);
1955 if (res == NULL)
1956 goto error;
1957 DISPATCH();
1958 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PyObject *right = POP();
1962 PyObject *left = TOP();
1963 PyObject *res = PyNumber_InPlaceRshift(left, right);
1964 Py_DECREF(left);
1965 Py_DECREF(right);
1966 SET_TOP(res);
1967 if (res == NULL)
1968 goto error;
1969 DISPATCH();
1970 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Benjamin Petersonddd19492018-09-16 22:38:02 -07001972 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 PyObject *right = POP();
1974 PyObject *left = TOP();
1975 PyObject *res = PyNumber_InPlaceAnd(left, right);
1976 Py_DECREF(left);
1977 Py_DECREF(right);
1978 SET_TOP(res);
1979 if (res == NULL)
1980 goto error;
1981 DISPATCH();
1982 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001983
Benjamin Petersonddd19492018-09-16 22:38:02 -07001984 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 PyObject *right = POP();
1986 PyObject *left = TOP();
1987 PyObject *res = PyNumber_InPlaceXor(left, right);
1988 Py_DECREF(left);
1989 Py_DECREF(right);
1990 SET_TOP(res);
1991 if (res == NULL)
1992 goto error;
1993 DISPATCH();
1994 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001995
Benjamin Petersonddd19492018-09-16 22:38:02 -07001996 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 PyObject *right = POP();
1998 PyObject *left = TOP();
1999 PyObject *res = PyNumber_InPlaceOr(left, right);
2000 Py_DECREF(left);
2001 Py_DECREF(right);
2002 SET_TOP(res);
2003 if (res == NULL)
2004 goto error;
2005 DISPATCH();
2006 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002007
Benjamin Petersonddd19492018-09-16 22:38:02 -07002008 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 PyObject *sub = TOP();
2010 PyObject *container = SECOND();
2011 PyObject *v = THIRD();
2012 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002013 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002014 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 Py_DECREF(container);
2018 Py_DECREF(sub);
2019 if (err != 0)
2020 goto error;
2021 DISPATCH();
2022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 PyObject *sub = TOP();
2026 PyObject *container = SECOND();
2027 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002028 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002029 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 err = PyObject_DelItem(container, sub);
2031 Py_DECREF(container);
2032 Py_DECREF(sub);
2033 if (err != 0)
2034 goto error;
2035 DISPATCH();
2036 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002039 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002041 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002042 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002044 _PyErr_SetString(tstate, PyExc_RuntimeError,
2045 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 Py_DECREF(value);
2047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002049 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 Py_DECREF(value);
2051 if (res == NULL)
2052 goto error;
2053 Py_DECREF(res);
2054 DISPATCH();
2055 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002056
Benjamin Petersonddd19492018-09-16 22:38:02 -07002057 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 switch (oparg) {
2060 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002062 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002065 /* fall through */
2066 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002067 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002068 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 break;
2071 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002072 _PyErr_SetString(tstate, PyExc_SystemError,
2073 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 break;
2075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 goto error;
2077 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Benjamin Petersonddd19492018-09-16 22:38:02 -07002079 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002081 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002082 assert(EMPTY());
2083 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002087 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002088 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002089 PyObject *obj = TOP();
2090 PyTypeObject *type = Py_TYPE(obj);
2091
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002092 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002093 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002094 }
Yury Selivanov75445082015-05-11 22:57:16 -04002095
2096 if (getter != NULL) {
2097 iter = (*getter)(obj);
2098 Py_DECREF(obj);
2099 if (iter == NULL) {
2100 SET_TOP(NULL);
2101 goto error;
2102 }
2103 }
2104 else {
2105 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002106 _PyErr_Format(tstate, PyExc_TypeError,
2107 "'async for' requires an object with "
2108 "__aiter__ method, got %.100s",
2109 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002110 Py_DECREF(obj);
2111 goto error;
2112 }
2113
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002114 if (Py_TYPE(iter)->tp_as_async == NULL ||
2115 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002116
Yury Selivanov398ff912017-03-02 22:20:00 -05002117 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002118 _PyErr_Format(tstate, PyExc_TypeError,
2119 "'async for' received an object from __aiter__ "
2120 "that does not implement __anext__: %.100s",
2121 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002122 Py_DECREF(iter);
2123 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002124 }
2125
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002126 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002127 DISPATCH();
2128 }
2129
Benjamin Petersonddd19492018-09-16 22:38:02 -07002130 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002131 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002132 PyObject *next_iter = NULL;
2133 PyObject *awaitable = NULL;
2134 PyObject *aiter = TOP();
2135 PyTypeObject *type = Py_TYPE(aiter);
2136
Yury Selivanoveb636452016-09-08 22:01:51 -07002137 if (PyAsyncGen_CheckExact(aiter)) {
2138 awaitable = type->tp_as_async->am_anext(aiter);
2139 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002140 goto error;
2141 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002142 } else {
2143 if (type->tp_as_async != NULL){
2144 getter = type->tp_as_async->am_anext;
2145 }
Yury Selivanov75445082015-05-11 22:57:16 -04002146
Yury Selivanoveb636452016-09-08 22:01:51 -07002147 if (getter != NULL) {
2148 next_iter = (*getter)(aiter);
2149 if (next_iter == NULL) {
2150 goto error;
2151 }
2152 }
2153 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002154 _PyErr_Format(tstate, PyExc_TypeError,
2155 "'async for' requires an iterator with "
2156 "__anext__ method, got %.100s",
2157 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002158 goto error;
2159 }
Yury Selivanov75445082015-05-11 22:57:16 -04002160
Yury Selivanoveb636452016-09-08 22:01:51 -07002161 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2162 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002163 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002164 PyExc_TypeError,
2165 "'async for' received an invalid object "
2166 "from __anext__: %.100s",
2167 Py_TYPE(next_iter)->tp_name);
2168
2169 Py_DECREF(next_iter);
2170 goto error;
2171 } else {
2172 Py_DECREF(next_iter);
2173 }
2174 }
Yury Selivanov75445082015-05-11 22:57:16 -04002175
2176 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002177 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002178 DISPATCH();
2179 }
2180
Benjamin Petersonddd19492018-09-16 22:38:02 -07002181 case TARGET(GET_AWAITABLE): {
2182 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002183 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002184 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002185
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002186 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002187 int opcode_at_minus_3 = 0;
2188 if ((next_instr - first_instr) > 2) {
2189 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2190 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002191 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002192 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002193 _Py_OPCODE(next_instr[-2]));
2194 }
2195
Yury Selivanov75445082015-05-11 22:57:16 -04002196 Py_DECREF(iterable);
2197
Yury Selivanovc724bae2016-03-02 11:30:46 -05002198 if (iter != NULL && PyCoro_CheckExact(iter)) {
2199 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2200 if (yf != NULL) {
2201 /* `iter` is a coroutine object that is being
2202 awaited, `yf` is a pointer to the current awaitable
2203 being awaited on. */
2204 Py_DECREF(yf);
2205 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002206 _PyErr_SetString(tstate, PyExc_RuntimeError,
2207 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002208 /* The code below jumps to `error` if `iter` is NULL. */
2209 }
2210 }
2211
Yury Selivanov75445082015-05-11 22:57:16 -04002212 SET_TOP(iter); /* Even if it's NULL */
2213
2214 if (iter == NULL) {
2215 goto error;
2216 }
2217
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002218 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002219 DISPATCH();
2220 }
2221
Benjamin Petersonddd19492018-09-16 22:38:02 -07002222 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002224 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002226 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2227 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002228 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002229 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002231 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002232 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002233 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 Py_DECREF(v);
2236 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002237 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002238 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002239 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002240 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002241 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 if (err < 0)
2243 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002244 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 SET_TOP(val);
2246 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002247 }
Martin Panter95f53c12016-07-18 08:23:26 +00002248 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002249 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002250 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002251 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002252 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002253 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002255
Benjamin Petersonddd19492018-09-16 22:38:02 -07002256 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002258
2259 if (co->co_flags & CO_ASYNC_GENERATOR) {
2260 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2261 Py_DECREF(retval);
2262 if (w == NULL) {
2263 retval = NULL;
2264 goto error;
2265 }
2266 retval = w;
2267 }
2268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002270 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002272
Benjamin Petersonddd19492018-09-16 22:38:02 -07002273 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002274 PyObject *type, *value, *traceback;
2275 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 PyTryBlock *b = PyFrame_BlockPop(f);
2277 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002278 _PyErr_SetString(tstate, PyExc_SystemError,
2279 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002282 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2283 STACK_LEVEL() <= (b)->b_level + 4);
2284 exc_info = tstate->exc_info;
2285 type = exc_info->exc_type;
2286 value = exc_info->exc_value;
2287 traceback = exc_info->exc_traceback;
2288 exc_info->exc_type = POP();
2289 exc_info->exc_value = POP();
2290 exc_info->exc_traceback = POP();
2291 Py_XDECREF(type);
2292 Py_XDECREF(value);
2293 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002296
Benjamin Petersonddd19492018-09-16 22:38:02 -07002297 case TARGET(POP_BLOCK): {
2298 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002299 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002302
Mark Shannonfee55262019-11-21 09:11:43 +00002303 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002304 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002305 PyObject *val = POP();
2306 PyObject *tb = POP();
2307 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002308 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002309 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002313 PyObject *exc = POP();
2314 assert(PyExceptionClass_Check(exc));
2315 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2316 PyTryBlock *b = PyFrame_BlockPop(f);
2317 assert(b->b_type == EXCEPT_HANDLER);
2318 Py_DECREF(exc);
2319 UNWIND_EXCEPT_HANDLER(b);
2320 Py_DECREF(POP());
2321 JUMPBY(oparg);
2322 FAST_DISPATCH();
2323 }
2324 else {
2325 PyObject *val = POP();
2326 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002327 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002328 goto exception_unwind;
2329 }
2330 }
2331
Zackery Spytzce6a0702019-08-25 03:44:09 -06002332 case TARGET(LOAD_ASSERTION_ERROR): {
2333 PyObject *value = PyExc_AssertionError;
2334 Py_INCREF(value);
2335 PUSH(value);
2336 FAST_DISPATCH();
2337 }
2338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002340 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002341
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002343 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002344 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002346 if (!_PyErr_Occurred(tstate)) {
2347 _PyErr_SetString(tstate, PyExc_NameError,
2348 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002351 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002353 }
2354 else {
2355 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2356 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002357 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2359 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002360 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2361 _PyErr_SetString(tstate, PyExc_NameError,
2362 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002367 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Benjamin Petersonddd19492018-09-16 22:38:02 -07002370 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 PyObject *name = GETITEM(names, oparg);
2372 PyObject *v = POP();
2373 PyObject *ns = f->f_locals;
2374 int err;
2375 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002376 _PyErr_Format(tstate, PyExc_SystemError,
2377 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 if (PyDict_CheckExact(ns))
2382 err = PyDict_SetItem(ns, name, v);
2383 else
2384 err = PyObject_SetItem(ns, name, v);
2385 Py_DECREF(v);
2386 if (err != 0)
2387 goto error;
2388 DISPATCH();
2389 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Benjamin Petersonddd19492018-09-16 22:38:02 -07002391 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 PyObject *name = GETITEM(names, oparg);
2393 PyObject *ns = f->f_locals;
2394 int err;
2395 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002396 _PyErr_Format(tstate, PyExc_SystemError,
2397 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 err = PyObject_DelItem(ns, name);
2401 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002402 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 NAME_ERROR_MSG,
2404 name);
2405 goto error;
2406 }
2407 DISPATCH();
2408 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002409
Benjamin Petersonddd19492018-09-16 22:38:02 -07002410 case TARGET(UNPACK_SEQUENCE): {
2411 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 PyObject *seq = POP(), *item, **items;
2413 if (PyTuple_CheckExact(seq) &&
2414 PyTuple_GET_SIZE(seq) == oparg) {
2415 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 item = items[oparg];
2418 Py_INCREF(item);
2419 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 } else if (PyList_CheckExact(seq) &&
2422 PyList_GET_SIZE(seq) == oparg) {
2423 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 item = items[oparg];
2426 Py_INCREF(item);
2427 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002431 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 } else {
2433 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 Py_DECREF(seq);
2435 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002438 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002440
Benjamin Petersonddd19492018-09-16 22:38:02 -07002441 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2443 PyObject *seq = POP();
2444
Victor Stinner438a12d2019-05-24 17:01:38 +02002445 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 stack_pointer + totalargs)) {
2447 stack_pointer += totalargs;
2448 } else {
2449 Py_DECREF(seq);
2450 goto error;
2451 }
2452 Py_DECREF(seq);
2453 DISPATCH();
2454 }
2455
Benjamin Petersonddd19492018-09-16 22:38:02 -07002456 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 PyObject *name = GETITEM(names, oparg);
2458 PyObject *owner = TOP();
2459 PyObject *v = SECOND();
2460 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002461 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 Py_DECREF(owner);
2465 if (err != 0)
2466 goto error;
2467 DISPATCH();
2468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002469
Benjamin Petersonddd19492018-09-16 22:38:02 -07002470 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 PyObject *name = GETITEM(names, oparg);
2472 PyObject *owner = POP();
2473 int err;
2474 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2475 Py_DECREF(owner);
2476 if (err != 0)
2477 goto error;
2478 DISPATCH();
2479 }
2480
Benjamin Petersonddd19492018-09-16 22:38:02 -07002481 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 PyObject *name = GETITEM(names, oparg);
2483 PyObject *v = POP();
2484 int err;
2485 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 if (err != 0)
2488 goto error;
2489 DISPATCH();
2490 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002491
Benjamin Petersonddd19492018-09-16 22:38:02 -07002492 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002493 PyObject *name = GETITEM(names, oparg);
2494 int err;
2495 err = PyDict_DelItem(f->f_globals, name);
2496 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002497 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2498 format_exc_check_arg(tstate, PyExc_NameError,
2499 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002500 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002502 }
2503 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002505
Benjamin Petersonddd19492018-09-16 22:38:02 -07002506 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 PyObject *name = GETITEM(names, oparg);
2508 PyObject *locals = f->f_locals;
2509 PyObject *v;
2510 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002511 _PyErr_Format(tstate, PyExc_SystemError,
2512 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002516 v = PyDict_GetItemWithError(locals, name);
2517 if (v != NULL) {
2518 Py_INCREF(v);
2519 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002520 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002521 goto error;
2522 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 }
2524 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002526 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002527 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002528 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 }
2531 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002533 v = PyDict_GetItemWithError(f->f_globals, name);
2534 if (v != NULL) {
2535 Py_INCREF(v);
2536 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002537 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002538 goto error;
2539 }
2540 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002541 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002542 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002545 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002546 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002548 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002550 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002552 }
2553 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 v = PyObject_GetItem(f->f_builtins, name);
2555 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002557 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002558 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002562 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002569
Benjamin Petersonddd19492018-09-16 22:38:02 -07002570 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002571 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002573 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002574 && PyDict_CheckExact(f->f_builtins))
2575 {
Inada Naoki91234a12019-06-03 21:30:58 +09002576 OPCACHE_CHECK();
2577 if (co_opcache != NULL && co_opcache->optimized > 0) {
2578 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2579
2580 if (lg->globals_ver ==
2581 ((PyDictObject *)f->f_globals)->ma_version_tag
2582 && lg->builtins_ver ==
2583 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2584 {
2585 PyObject *ptr = lg->ptr;
2586 OPCACHE_STAT_GLOBAL_HIT();
2587 assert(ptr != NULL);
2588 Py_INCREF(ptr);
2589 PUSH(ptr);
2590 DISPATCH();
2591 }
2592 }
2593
2594 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002596 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 name);
2598 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002599 if (!_PyErr_OCCURRED()) {
2600 /* _PyDict_LoadGlobal() returns NULL without raising
2601 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002602 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002603 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002604 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 }
Inada Naoki91234a12019-06-03 21:30:58 +09002607
2608 if (co_opcache != NULL) {
2609 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2610
2611 if (co_opcache->optimized == 0) {
2612 /* Wasn't optimized before. */
2613 OPCACHE_STAT_GLOBAL_OPT();
2614 } else {
2615 OPCACHE_STAT_GLOBAL_MISS();
2616 }
2617
2618 co_opcache->optimized = 1;
2619 lg->globals_ver =
2620 ((PyDictObject *)f->f_globals)->ma_version_tag;
2621 lg->builtins_ver =
2622 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2623 lg->ptr = v; /* borrowed */
2624 }
2625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002628 else {
2629 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002630
2631 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002632 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 v = PyObject_GetItem(f->f_globals, name);
2634 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002635 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002636 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002637 }
2638 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002639
Victor Stinnerb4efc962015-11-20 09:24:02 +01002640 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 v = PyObject_GetItem(f->f_builtins, name);
2642 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002643 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002644 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002646 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002649 }
2650 }
2651 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002655
Benjamin Petersonddd19492018-09-16 22:38:02 -07002656 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 PyObject *v = GETLOCAL(oparg);
2658 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 SETLOCAL(oparg, NULL);
2660 DISPATCH();
2661 }
2662 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002663 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 UNBOUNDLOCAL_ERROR_MSG,
2665 PyTuple_GetItem(co->co_varnames, oparg)
2666 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 goto error;
2668 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002669
Benjamin Petersonddd19492018-09-16 22:38:02 -07002670 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002672 PyObject *oldobj = PyCell_GET(cell);
2673 if (oldobj != NULL) {
2674 PyCell_SET(cell, NULL);
2675 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002676 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002677 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002678 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 goto error;
2680 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002681
Benjamin Petersonddd19492018-09-16 22:38:02 -07002682 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 PyObject *cell = freevars[oparg];
2684 Py_INCREF(cell);
2685 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002688
Benjamin Petersonddd19492018-09-16 22:38:02 -07002689 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002690 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002691 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002692 assert(locals);
2693 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2694 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2695 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2696 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2697 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002698 value = PyDict_GetItemWithError(locals, name);
2699 if (value != NULL) {
2700 Py_INCREF(value);
2701 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002702 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002703 goto error;
2704 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002705 }
2706 else {
2707 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002708 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002709 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002710 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002711 }
2712 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002713 }
2714 }
2715 if (!value) {
2716 PyObject *cell = freevars[oparg];
2717 value = PyCell_GET(cell);
2718 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002719 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002720 goto error;
2721 }
2722 Py_INCREF(value);
2723 }
2724 PUSH(value);
2725 DISPATCH();
2726 }
2727
Benjamin Petersonddd19492018-09-16 22:38:02 -07002728 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 PyObject *cell = freevars[oparg];
2730 PyObject *value = PyCell_GET(cell);
2731 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002732 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 Py_INCREF(value);
2736 PUSH(value);
2737 DISPATCH();
2738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002739
Benjamin Petersonddd19492018-09-16 22:38:02 -07002740 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 PyObject *v = POP();
2742 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002743 PyObject *oldobj = PyCell_GET(cell);
2744 PyCell_SET(cell, v);
2745 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 DISPATCH();
2747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002748
Benjamin Petersonddd19492018-09-16 22:38:02 -07002749 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002750 PyObject *str;
2751 PyObject *empty = PyUnicode_New(0, 0);
2752 if (empty == NULL) {
2753 goto error;
2754 }
2755 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2756 Py_DECREF(empty);
2757 if (str == NULL)
2758 goto error;
2759 while (--oparg >= 0) {
2760 PyObject *item = POP();
2761 Py_DECREF(item);
2762 }
2763 PUSH(str);
2764 DISPATCH();
2765 }
2766
Benjamin Petersonddd19492018-09-16 22:38:02 -07002767 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 PyObject *tup = PyTuple_New(oparg);
2769 if (tup == NULL)
2770 goto error;
2771 while (--oparg >= 0) {
2772 PyObject *item = POP();
2773 PyTuple_SET_ITEM(tup, oparg, item);
2774 }
2775 PUSH(tup);
2776 DISPATCH();
2777 }
2778
Benjamin Petersonddd19492018-09-16 22:38:02 -07002779 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *list = PyList_New(oparg);
2781 if (list == NULL)
2782 goto error;
2783 while (--oparg >= 0) {
2784 PyObject *item = POP();
2785 PyList_SET_ITEM(list, oparg, item);
2786 }
2787 PUSH(list);
2788 DISPATCH();
2789 }
2790
Mark Shannon13bc1392020-01-23 09:25:17 +00002791 case TARGET(LIST_TO_TUPLE): {
2792 PyObject *list = POP();
2793 PyObject *tuple = PyList_AsTuple(list);
2794 Py_DECREF(list);
2795 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002797 }
2798 PUSH(tuple);
2799 DISPATCH();
2800 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801
Mark Shannon13bc1392020-01-23 09:25:17 +00002802 case TARGET(LIST_EXTEND): {
2803 PyObject *iterable = POP();
2804 PyObject *list = PEEK(oparg);
2805 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2806 if (none_val == NULL) {
2807 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002808 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002809 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002810 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002811 _PyErr_Format(tstate, PyExc_TypeError,
2812 "Value after * must be an iterable, not %.200s",
2813 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002814 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002815 Py_DECREF(iterable);
2816 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002817 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002818 Py_DECREF(none_val);
2819 Py_DECREF(iterable);
2820 DISPATCH();
2821 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002822
Mark Shannon13bc1392020-01-23 09:25:17 +00002823 case TARGET(SET_UPDATE): {
2824 PyObject *iterable = POP();
2825 PyObject *set = PEEK(oparg);
2826 int err = _PySet_Update(set, iterable);
2827 Py_DECREF(iterable);
2828 if (err < 0) {
2829 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002830 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002831 DISPATCH();
2832 }
2833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PyObject *set = PySet_New(NULL);
2836 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002837 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 if (set == NULL)
2839 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002840 for (i = oparg; i > 0; i--) {
2841 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 if (err == 0)
2843 err = PySet_Add(set, item);
2844 Py_DECREF(item);
2845 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002846 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 if (err != 0) {
2848 Py_DECREF(set);
2849 goto error;
2850 }
2851 PUSH(set);
2852 DISPATCH();
2853 }
2854
Benjamin Petersonddd19492018-09-16 22:38:02 -07002855 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002856 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2858 if (map == NULL)
2859 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002860 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002861 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002862 PyObject *key = PEEK(2*i);
2863 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002864 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002865 if (err != 0) {
2866 Py_DECREF(map);
2867 goto error;
2868 }
2869 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002870
2871 while (oparg--) {
2872 Py_DECREF(POP());
2873 Py_DECREF(POP());
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 PUSH(map);
2876 DISPATCH();
2877 }
2878
Benjamin Petersonddd19492018-09-16 22:38:02 -07002879 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002880 _Py_IDENTIFIER(__annotations__);
2881 int err;
2882 PyObject *ann_dict;
2883 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002884 _PyErr_Format(tstate, PyExc_SystemError,
2885 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002886 goto error;
2887 }
2888 /* check if __annotations__ in locals()... */
2889 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002890 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002891 &PyId___annotations__);
2892 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002893 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002894 goto error;
2895 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002896 /* ...if not, create a new one */
2897 ann_dict = PyDict_New();
2898 if (ann_dict == NULL) {
2899 goto error;
2900 }
2901 err = _PyDict_SetItemId(f->f_locals,
2902 &PyId___annotations__, ann_dict);
2903 Py_DECREF(ann_dict);
2904 if (err != 0) {
2905 goto error;
2906 }
2907 }
2908 }
2909 else {
2910 /* do the same if locals() is not a dict */
2911 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2912 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002913 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002914 }
2915 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2916 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002917 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002918 goto error;
2919 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002920 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002921 ann_dict = PyDict_New();
2922 if (ann_dict == NULL) {
2923 goto error;
2924 }
2925 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2926 Py_DECREF(ann_dict);
2927 if (err != 0) {
2928 goto error;
2929 }
2930 }
2931 else {
2932 Py_DECREF(ann_dict);
2933 }
2934 }
2935 DISPATCH();
2936 }
2937
Benjamin Petersonddd19492018-09-16 22:38:02 -07002938 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002939 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002940 PyObject *map;
2941 PyObject *keys = TOP();
2942 if (!PyTuple_CheckExact(keys) ||
2943 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002944 _PyErr_SetString(tstate, PyExc_SystemError,
2945 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002946 goto error;
2947 }
2948 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2949 if (map == NULL) {
2950 goto error;
2951 }
2952 for (i = oparg; i > 0; i--) {
2953 int err;
2954 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2955 PyObject *value = PEEK(i + 1);
2956 err = PyDict_SetItem(map, key, value);
2957 if (err != 0) {
2958 Py_DECREF(map);
2959 goto error;
2960 }
2961 }
2962
2963 Py_DECREF(POP());
2964 while (oparg--) {
2965 Py_DECREF(POP());
2966 }
2967 PUSH(map);
2968 DISPATCH();
2969 }
2970
Mark Shannon8a4cd702020-01-27 09:57:45 +00002971 case TARGET(DICT_UPDATE): {
2972 PyObject *update = POP();
2973 PyObject *dict = PEEK(oparg);
2974 if (PyDict_Update(dict, update) < 0) {
2975 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2976 _PyErr_Format(tstate, PyExc_TypeError,
2977 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002978 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002979 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002980 Py_DECREF(update);
2981 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002982 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002983 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002984 DISPATCH();
2985 }
2986
Mark Shannon8a4cd702020-01-27 09:57:45 +00002987 case TARGET(DICT_MERGE): {
2988 PyObject *update = POP();
2989 PyObject *dict = PEEK(oparg);
2990
2991 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2992 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2993 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002994 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002995 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002996 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002997 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002998 DISPATCH();
2999 }
3000
Benjamin Petersonddd19492018-09-16 22:38:02 -07003001 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003002 PyObject *value = TOP();
3003 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *map;
3005 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003006 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003007 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003009 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 Py_DECREF(value);
3011 Py_DECREF(key);
3012 if (err != 0)
3013 goto error;
3014 PREDICT(JUMP_ABSOLUTE);
3015 DISPATCH();
3016 }
3017
Benjamin Petersonddd19492018-09-16 22:38:02 -07003018 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 PyObject *name = GETITEM(names, oparg);
3020 PyObject *owner = TOP();
3021 PyObject *res = PyObject_GetAttr(owner, name);
3022 Py_DECREF(owner);
3023 SET_TOP(res);
3024 if (res == NULL)
3025 goto error;
3026 DISPATCH();
3027 }
3028
Benjamin Petersonddd19492018-09-16 22:38:02 -07003029 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003030 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 PyObject *right = POP();
3032 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003033 PyObject *res = PyObject_RichCompare(left, right, oparg);
3034 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 Py_DECREF(left);
3036 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 if (res == NULL)
3038 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 PREDICT(POP_JUMP_IF_FALSE);
3040 PREDICT(POP_JUMP_IF_TRUE);
3041 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003043
Mark Shannon9af0e472020-01-14 10:12:45 +00003044 case TARGET(IS_OP): {
3045 PyObject *right = POP();
3046 PyObject *left = TOP();
3047 int res = (left == right)^oparg;
3048 PyObject *b = res ? Py_True : Py_False;
3049 Py_INCREF(b);
3050 SET_TOP(b);
3051 Py_DECREF(left);
3052 Py_DECREF(right);
3053 PREDICT(POP_JUMP_IF_FALSE);
3054 PREDICT(POP_JUMP_IF_TRUE);
3055 FAST_DISPATCH();
3056 }
3057
3058 case TARGET(CONTAINS_OP): {
3059 PyObject *right = POP();
3060 PyObject *left = POP();
3061 int res = PySequence_Contains(right, left);
3062 Py_DECREF(left);
3063 Py_DECREF(right);
3064 if (res < 0) {
3065 goto error;
3066 }
3067 PyObject *b = (res^oparg) ? Py_True : Py_False;
3068 Py_INCREF(b);
3069 PUSH(b);
3070 PREDICT(POP_JUMP_IF_FALSE);
3071 PREDICT(POP_JUMP_IF_TRUE);
3072 FAST_DISPATCH();
3073 }
3074
3075#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3076 "BaseException is not allowed"
3077
3078 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3079 PyObject *right = POP();
3080 PyObject *left = POP();
3081 if (PyTuple_Check(right)) {
3082 Py_ssize_t i, length;
3083 length = PyTuple_GET_SIZE(right);
3084 for (i = 0; i < length; i++) {
3085 PyObject *exc = PyTuple_GET_ITEM(right, i);
3086 if (!PyExceptionClass_Check(exc)) {
3087 _PyErr_SetString(tstate, PyExc_TypeError,
3088 CANNOT_CATCH_MSG);
3089 Py_DECREF(left);
3090 Py_DECREF(right);
3091 goto error;
3092 }
3093 }
3094 }
3095 else {
3096 if (!PyExceptionClass_Check(right)) {
3097 _PyErr_SetString(tstate, PyExc_TypeError,
3098 CANNOT_CATCH_MSG);
3099 Py_DECREF(left);
3100 Py_DECREF(right);
3101 goto error;
3102 }
3103 }
3104 int res = PyErr_GivenExceptionMatches(left, right);
3105 Py_DECREF(left);
3106 Py_DECREF(right);
3107 if (res > 0) {
3108 /* Exception matches -- Do nothing */;
3109 }
3110 else if (res == 0) {
3111 JUMPTO(oparg);
3112 }
3113 else {
3114 goto error;
3115 }
3116 DISPATCH();
3117 }
3118
Benjamin Petersonddd19492018-09-16 22:38:02 -07003119 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003121 PyObject *fromlist = POP();
3122 PyObject *level = TOP();
3123 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003124 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003125 Py_DECREF(level);
3126 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 SET_TOP(res);
3128 if (res == NULL)
3129 goto error;
3130 DISPATCH();
3131 }
3132
Benjamin Petersonddd19492018-09-16 22:38:02 -07003133 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PyObject *from = POP(), *locals;
3135 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003136 if (PyFrame_FastToLocalsWithError(f) < 0) {
3137 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003138 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003139 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 locals = f->f_locals;
3142 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003143 _PyErr_SetString(tstate, PyExc_SystemError,
3144 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003145 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003148 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 Py_DECREF(from);
3151 if (err != 0)
3152 goto error;
3153 DISPATCH();
3154 }
Guido van Rossum25831651993-05-19 14:50:45 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PyObject *name = GETITEM(names, oparg);
3158 PyObject *from = TOP();
3159 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003160 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 PUSH(res);
3162 if (res == NULL)
3163 goto error;
3164 DISPATCH();
3165 }
Thomas Wouters52152252000-08-17 22:55:00 +00003166
Benjamin Petersonddd19492018-09-16 22:38:02 -07003167 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 JUMPBY(oparg);
3169 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003171
Benjamin Petersonddd19492018-09-16 22:38:02 -07003172 case TARGET(POP_JUMP_IF_FALSE): {
3173 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 PyObject *cond = POP();
3175 int err;
3176 if (cond == Py_True) {
3177 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 FAST_DISPATCH();
3179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 if (cond == Py_False) {
3181 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 JUMPTO(oparg);
3183 FAST_DISPATCH();
3184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 err = PyObject_IsTrue(cond);
3186 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003188 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 else if (err == 0)
3190 JUMPTO(oparg);
3191 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003195
Benjamin Petersonddd19492018-09-16 22:38:02 -07003196 case TARGET(POP_JUMP_IF_TRUE): {
3197 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 PyObject *cond = POP();
3199 int err;
3200 if (cond == Py_False) {
3201 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 FAST_DISPATCH();
3203 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 if (cond == Py_True) {
3205 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 JUMPTO(oparg);
3207 FAST_DISPATCH();
3208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 err = PyObject_IsTrue(cond);
3210 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 JUMPTO(oparg);
3213 }
3214 else if (err == 0)
3215 ;
3216 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003220
Benjamin Petersonddd19492018-09-16 22:38:02 -07003221 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 PyObject *cond = TOP();
3223 int err;
3224 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003225 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 FAST_DISPATCH();
3228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 JUMPTO(oparg);
3231 FAST_DISPATCH();
3232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003235 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 }
3238 else if (err == 0)
3239 JUMPTO(oparg);
3240 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003244
Benjamin Petersonddd19492018-09-16 22:38:02 -07003245 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 PyObject *cond = TOP();
3247 int err;
3248 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003249 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 FAST_DISPATCH();
3252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 JUMPTO(oparg);
3255 FAST_DISPATCH();
3256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003257 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 JUMPTO(oparg);
3260 }
3261 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003262 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 }
3265 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003269
Benjamin Petersonddd19492018-09-16 22:38:02 -07003270 case TARGET(JUMP_ABSOLUTE): {
3271 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003273#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 /* Enabling this path speeds-up all while and for-loops by bypassing
3275 the per-loop checks for signals. By default, this should be turned-off
3276 because it prevents detection of a control-break in tight loops like
3277 "while 1: pass". Compile with this option turned-on when you need
3278 the speed-up and do not need break checking inside tight loops (ones
3279 that contain only instructions ending with FAST_DISPATCH).
3280 */
3281 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003282#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003284#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003286
Benjamin Petersonddd19492018-09-16 22:38:02 -07003287 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003289 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003290 PyObject *iter = PyObject_GetIter(iterable);
3291 Py_DECREF(iterable);
3292 SET_TOP(iter);
3293 if (iter == NULL)
3294 goto error;
3295 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003296 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003297 DISPATCH();
3298 }
3299
Benjamin Petersonddd19492018-09-16 22:38:02 -07003300 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003301 /* before: [obj]; after [getiter(obj)] */
3302 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003303 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003304 if (PyCoro_CheckExact(iterable)) {
3305 /* `iterable` is a coroutine */
3306 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3307 /* and it is used in a 'yield from' expression of a
3308 regular generator. */
3309 Py_DECREF(iterable);
3310 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003311 _PyErr_SetString(tstate, PyExc_TypeError,
3312 "cannot 'yield from' a coroutine object "
3313 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003314 goto error;
3315 }
3316 }
3317 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003318 /* `iterable` is not a generator. */
3319 iter = PyObject_GetIter(iterable);
3320 Py_DECREF(iterable);
3321 SET_TOP(iter);
3322 if (iter == NULL)
3323 goto error;
3324 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003325 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 DISPATCH();
3327 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003328
Benjamin Petersonddd19492018-09-16 22:38:02 -07003329 case TARGET(FOR_ITER): {
3330 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003333 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 if (next != NULL) {
3335 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 PREDICT(STORE_FAST);
3337 PREDICT(UNPACK_SEQUENCE);
3338 DISPATCH();
3339 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003340 if (_PyErr_Occurred(tstate)) {
3341 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003343 }
3344 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003345 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003346 }
3347 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 }
3349 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003350 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003353 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003356
Benjamin Petersonddd19492018-09-16 22:38:02 -07003357 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003358 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 STACK_LEVEL());
3360 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003362
Benjamin Petersonddd19492018-09-16 22:38:02 -07003363 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003364 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003365 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003366 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003367 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003368 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003369 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003370 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003371 }
3372 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3373 if (exit == NULL) {
3374 Py_DECREF(enter);
3375 goto error;
3376 }
Yury Selivanov75445082015-05-11 22:57:16 -04003377 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003378 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003379 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003380 Py_DECREF(enter);
3381 if (res == NULL)
3382 goto error;
3383 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003384 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003385 DISPATCH();
3386 }
3387
Benjamin Petersonddd19492018-09-16 22:38:02 -07003388 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003389 PyObject *res = POP();
3390 /* Setup the finally block before pushing the result
3391 of __aenter__ on the stack. */
3392 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3393 STACK_LEVEL());
3394 PUSH(res);
3395 DISPATCH();
3396 }
3397
Benjamin Petersonddd19492018-09-16 22:38:02 -07003398 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003399 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003400 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003402 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003404 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003405 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003406 }
3407 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003408 if (exit == NULL) {
3409 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003411 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003414 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 Py_DECREF(enter);
3416 if (res == NULL)
3417 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 /* Setup the finally block before pushing the result
3419 of __enter__ on the stack. */
3420 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3421 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 DISPATCH();
3425 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003426
Mark Shannonfee55262019-11-21 09:11:43 +00003427 case TARGET(WITH_EXCEPT_START): {
3428 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003430 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3431 - SEVENTH: the context.__exit__ bound method
3432 We call SEVENTH(TOP, SECOND, THIRD).
3433 Then we push again the TOP exception and the __exit__
3434 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003437 PyObject *exc, *val, *tb, *res;
3438
Victor Stinner842cfff2016-12-01 14:45:31 +01003439 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003440 val = SECOND();
3441 tb = THIRD();
3442 assert(exc != Py_None);
3443 assert(!PyLong_Check(exc));
3444 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003445 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003446 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003447 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 if (res == NULL)
3449 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003450
Yury Selivanov75445082015-05-11 22:57:16 -04003451 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003454
Benjamin Petersonddd19492018-09-16 22:38:02 -07003455 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003456 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003457 PyObject *name = GETITEM(names, oparg);
3458 PyObject *obj = TOP();
3459 PyObject *meth = NULL;
3460
3461 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3462
Yury Selivanovf2392132016-12-13 19:03:51 -05003463 if (meth == NULL) {
3464 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003465 goto error;
3466 }
3467
3468 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003469 /* We can bypass temporary bound method object.
3470 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003471
INADA Naoki015bce62017-01-16 17:23:30 +09003472 meth | self | arg1 | ... | argN
3473 */
3474 SET_TOP(meth);
3475 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003476 }
3477 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003478 /* meth is not an unbound method (but a regular attr, or
3479 something was returned by a descriptor protocol). Set
3480 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003481 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003482
3483 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 */
INADA Naoki015bce62017-01-16 17:23:30 +09003485 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003487 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 }
3489 DISPATCH();
3490 }
3491
Benjamin Petersonddd19492018-09-16 22:38:02 -07003492 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003493 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003494 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003495
3496 sp = stack_pointer;
3497
INADA Naoki015bce62017-01-16 17:23:30 +09003498 meth = PEEK(oparg + 2);
3499 if (meth == NULL) {
3500 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3501 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003502
3503 Stack layout:
3504
INADA Naoki015bce62017-01-16 17:23:30 +09003505 ... | NULL | callable | arg1 | ... | argN
3506 ^- TOP()
3507 ^- (-oparg)
3508 ^- (-oparg-1)
3509 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003510
Ville Skyttä49b27342017-08-03 09:00:59 +03003511 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003512 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003513 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003514 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003515 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003516 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003517 }
3518 else {
3519 /* This is a method call. Stack layout:
3520
INADA Naoki015bce62017-01-16 17:23:30 +09003521 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003522 ^- TOP()
3523 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003524 ^- (-oparg-1)
3525 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003526
INADA Naoki015bce62017-01-16 17:23:30 +09003527 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003528 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003529 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003530 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003531 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003532 stack_pointer = sp;
3533 }
3534
3535 PUSH(res);
3536 if (res == NULL)
3537 goto error;
3538 DISPATCH();
3539 }
3540
Benjamin Petersonddd19492018-09-16 22:38:02 -07003541 case TARGET(CALL_FUNCTION): {
3542 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003545 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003548 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003550 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003553
Benjamin Petersonddd19492018-09-16 22:38:02 -07003554 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003555 PyObject **sp, *res, *names;
3556
3557 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003558 assert(PyTuple_Check(names));
3559 assert(PyTuple_GET_SIZE(names) <= oparg);
3560 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003562 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003565 Py_DECREF(names);
3566
3567 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003569 }
3570 DISPATCH();
3571 }
3572
Benjamin Petersonddd19492018-09-16 22:38:02 -07003573 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003574 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003575 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003576 if (oparg & 0x01) {
3577 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003578 if (!PyDict_CheckExact(kwargs)) {
3579 PyObject *d = PyDict_New();
3580 if (d == NULL)
3581 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003582 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003583 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003584 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003585 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003586 goto error;
3587 }
3588 Py_DECREF(kwargs);
3589 kwargs = d;
3590 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003591 assert(PyDict_CheckExact(kwargs));
3592 }
3593 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003594 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003595 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003596 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003597 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003598 goto error;
3599 }
3600 Py_SETREF(callargs, PySequence_Tuple(callargs));
3601 if (callargs == NULL) {
3602 goto error;
3603 }
3604 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003605 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003606
Victor Stinner09532fe2019-05-10 23:39:09 +02003607 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003608 Py_DECREF(func);
3609 Py_DECREF(callargs);
3610 Py_XDECREF(kwargs);
3611
3612 SET_TOP(result);
3613 if (result == NULL) {
3614 goto error;
3615 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003616 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003618
Benjamin Petersonddd19492018-09-16 22:38:02 -07003619 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003620 PyObject *qualname = POP();
3621 PyObject *codeobj = POP();
3622 PyFunctionObject *func = (PyFunctionObject *)
3623 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003624
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003625 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003626 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003627 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003628 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003630
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003631 if (oparg & 0x08) {
3632 assert(PyTuple_CheckExact(TOP()));
3633 func ->func_closure = POP();
3634 }
3635 if (oparg & 0x04) {
3636 assert(PyDict_CheckExact(TOP()));
3637 func->func_annotations = POP();
3638 }
3639 if (oparg & 0x02) {
3640 assert(PyDict_CheckExact(TOP()));
3641 func->func_kwdefaults = POP();
3642 }
3643 if (oparg & 0x01) {
3644 assert(PyTuple_CheckExact(TOP()));
3645 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003647
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003648 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003649 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003651
Benjamin Petersonddd19492018-09-16 22:38:02 -07003652 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003653 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003655 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003657 step = NULL;
3658 stop = POP();
3659 start = TOP();
3660 slice = PySlice_New(start, stop, step);
3661 Py_DECREF(start);
3662 Py_DECREF(stop);
3663 Py_XDECREF(step);
3664 SET_TOP(slice);
3665 if (slice == NULL)
3666 goto error;
3667 DISPATCH();
3668 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003669
Benjamin Petersonddd19492018-09-16 22:38:02 -07003670 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003671 /* Handles f-string value formatting. */
3672 PyObject *result;
3673 PyObject *fmt_spec;
3674 PyObject *value;
3675 PyObject *(*conv_fn)(PyObject *);
3676 int which_conversion = oparg & FVC_MASK;
3677 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3678
3679 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003680 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003681
3682 /* See if any conversion is specified. */
3683 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003684 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003685 case FVC_STR: conv_fn = PyObject_Str; break;
3686 case FVC_REPR: conv_fn = PyObject_Repr; break;
3687 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003688 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003689 _PyErr_Format(tstate, PyExc_SystemError,
3690 "unexpected conversion flag %d",
3691 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003692 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003693 }
3694
3695 /* If there's a conversion function, call it and replace
3696 value with that result. Otherwise, just use value,
3697 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003698 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003699 result = conv_fn(value);
3700 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003701 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003702 Py_XDECREF(fmt_spec);
3703 goto error;
3704 }
3705 value = result;
3706 }
3707
3708 /* If value is a unicode object, and there's no fmt_spec,
3709 then we know the result of format(value) is value
3710 itself. In that case, skip calling format(). I plan to
3711 move this optimization in to PyObject_Format()
3712 itself. */
3713 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3714 /* Do nothing, just transfer ownership to result. */
3715 result = value;
3716 } else {
3717 /* Actually call format(). */
3718 result = PyObject_Format(value, fmt_spec);
3719 Py_DECREF(value);
3720 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003721 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003722 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003723 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003724 }
3725
Eric V. Smith135d5f42016-02-05 18:23:08 -05003726 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003727 DISPATCH();
3728 }
3729
Benjamin Petersonddd19492018-09-16 22:38:02 -07003730 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003731 int oldoparg = oparg;
3732 NEXTOPARG();
3733 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003735 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003736
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003737
Antoine Pitrou042b1282010-08-13 21:15:58 +00003738#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003740#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 default:
3742 fprintf(stderr,
3743 "XXX lineno: %d, opcode: %d\n",
3744 PyFrame_GetLineNumber(f),
3745 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003746 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003747 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003750
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003751 /* This should never be reached. Every opcode should end with DISPATCH()
3752 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003753 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003754
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003755error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003756 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003757#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003758 if (!_PyErr_Occurred(tstate)) {
3759 _PyErr_SetString(tstate, PyExc_SystemError,
3760 "error return without exception set");
3761 }
Victor Stinner365b6932013-07-12 00:11:58 +02003762#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003763 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003764#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003765
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003766 /* Log traceback info. */
3767 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003768
Benjamin Peterson51f46162013-01-23 08:38:47 -05003769 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003770 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3771 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003772
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003773exception_unwind:
3774 /* Unwind stacks if an exception occurred */
3775 while (f->f_iblock > 0) {
3776 /* Pop the current block. */
3777 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 if (b->b_type == EXCEPT_HANDLER) {
3780 UNWIND_EXCEPT_HANDLER(b);
3781 continue;
3782 }
3783 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003784 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 PyObject *exc, *val, *tb;
3786 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003787 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 /* Beware, this invalidates all b->b_* fields */
3789 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003790 PUSH(exc_info->exc_traceback);
3791 PUSH(exc_info->exc_value);
3792 if (exc_info->exc_type != NULL) {
3793 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
3795 else {
3796 Py_INCREF(Py_None);
3797 PUSH(Py_None);
3798 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003799 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 /* Make the raw exception data
3801 available to the handler,
3802 so a program can emulate the
3803 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003804 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003805 if (tb != NULL)
3806 PyException_SetTraceback(val, tb);
3807 else
3808 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003810 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003812 exc_info->exc_value = val;
3813 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 if (tb == NULL)
3815 tb = Py_None;
3816 Py_INCREF(tb);
3817 PUSH(tb);
3818 PUSH(val);
3819 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003821 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003822 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3823 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3824 /* Make sure that we trace line after exception if we are in a new execution
3825 * window or we don't need a line update and we are not in the first instruction
3826 * of the line. */
3827 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3828 instr_prev = INT_MAX;
3829 }
Mark Shannonfee55262019-11-21 09:11:43 +00003830 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003831 /* Resume normal execution */
3832 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 }
3834 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003835
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003836 /* End the loop as we still have an error */
3837 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003839
Pablo Galindof00828a2019-05-09 16:52:02 +01003840 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003841 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 /* Pop remaining stack entries. */
3844 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003845 PyObject *o = POP();
3846 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003848
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003849exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003851 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003852 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3853 tstate, f, PyTrace_RETURN, retval)) {
3854 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 }
3856 }
3857 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003858 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3859 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003860 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 }
3862 }
3863 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003866exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003867 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3868 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003869 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003870 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003872
Victor Stinner0b72b232020-03-12 23:18:39 +01003873 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003874}
3875
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003877format_missing(PyThreadState *tstate, const char *kind,
3878 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003879{
3880 int err;
3881 Py_ssize_t len = PyList_GET_SIZE(names);
3882 PyObject *name_str, *comma, *tail, *tmp;
3883
3884 assert(PyList_CheckExact(names));
3885 assert(len >= 1);
3886 /* Deal with the joys of natural language. */
3887 switch (len) {
3888 case 1:
3889 name_str = PyList_GET_ITEM(names, 0);
3890 Py_INCREF(name_str);
3891 break;
3892 case 2:
3893 name_str = PyUnicode_FromFormat("%U and %U",
3894 PyList_GET_ITEM(names, len - 2),
3895 PyList_GET_ITEM(names, len - 1));
3896 break;
3897 default:
3898 tail = PyUnicode_FromFormat(", %U, and %U",
3899 PyList_GET_ITEM(names, len - 2),
3900 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003901 if (tail == NULL)
3902 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003903 /* Chop off the last two objects in the list. This shouldn't actually
3904 fail, but we can't be too careful. */
3905 err = PyList_SetSlice(names, len - 2, len, NULL);
3906 if (err == -1) {
3907 Py_DECREF(tail);
3908 return;
3909 }
3910 /* Stitch everything up into a nice comma-separated list. */
3911 comma = PyUnicode_FromString(", ");
3912 if (comma == NULL) {
3913 Py_DECREF(tail);
3914 return;
3915 }
3916 tmp = PyUnicode_Join(comma, names);
3917 Py_DECREF(comma);
3918 if (tmp == NULL) {
3919 Py_DECREF(tail);
3920 return;
3921 }
3922 name_str = PyUnicode_Concat(tmp, tail);
3923 Py_DECREF(tmp);
3924 Py_DECREF(tail);
3925 break;
3926 }
3927 if (name_str == NULL)
3928 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003929 _PyErr_Format(tstate, PyExc_TypeError,
3930 "%U() missing %i required %s argument%s: %U",
3931 co->co_name,
3932 len,
3933 kind,
3934 len == 1 ? "" : "s",
3935 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003936 Py_DECREF(name_str);
3937}
3938
3939static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003940missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3941 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003942 PyObject **fastlocals)
3943{
Victor Stinner74319ae2016-08-25 00:04:09 +02003944 Py_ssize_t i, j = 0;
3945 Py_ssize_t start, end;
3946 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003947 const char *kind = positional ? "positional" : "keyword-only";
3948 PyObject *missing_names;
3949
3950 /* Compute the names of the arguments that are missing. */
3951 missing_names = PyList_New(missing);
3952 if (missing_names == NULL)
3953 return;
3954 if (positional) {
3955 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003956 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003957 }
3958 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003959 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003960 end = start + co->co_kwonlyargcount;
3961 }
3962 for (i = start; i < end; i++) {
3963 if (GETLOCAL(i) == NULL) {
3964 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3965 PyObject *name = PyObject_Repr(raw);
3966 if (name == NULL) {
3967 Py_DECREF(missing_names);
3968 return;
3969 }
3970 PyList_SET_ITEM(missing_names, j++, name);
3971 }
3972 }
3973 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003974 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003975 Py_DECREF(missing_names);
3976}
3977
3978static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003979too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3980 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003981 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003982{
3983 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003984 Py_ssize_t kwonly_given = 0;
3985 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003987 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988
Benjamin Petersone109c702011-06-24 09:37:26 -05003989 assert((co->co_flags & CO_VARARGS) == 0);
3990 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003991 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003992 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003994 }
3995 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003996 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003997 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003999 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 }
4001 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004002 plural = (co_argcount != 1);
4003 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 }
4005 if (sig == NULL)
4006 return;
4007 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004008 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4009 kwonly_sig = PyUnicode_FromFormat(format,
4010 given != 1 ? "s" : "",
4011 kwonly_given,
4012 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 if (kwonly_sig == NULL) {
4014 Py_DECREF(sig);
4015 return;
4016 }
4017 }
4018 else {
4019 /* This will not fail. */
4020 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004021 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004023 _PyErr_Format(tstate, PyExc_TypeError,
4024 "%U() takes %U positional argument%s but %zd%U %s given",
4025 co->co_name,
4026 sig,
4027 plural ? "s" : "",
4028 given,
4029 kwonly_sig,
4030 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 Py_DECREF(sig);
4032 Py_DECREF(kwonly_sig);
4033}
4034
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004035static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004036positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
4037 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004038{
4039 int posonly_conflicts = 0;
4040 PyObject* posonly_names = PyList_New(0);
4041
4042 for(int k=0; k < co->co_posonlyargcount; k++){
4043 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4044
4045 for (int k2=0; k2<kwcount; k2++){
4046 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4047 PyObject* kwname = kwnames[k2];
4048 if (kwname == posonly_name){
4049 if(PyList_Append(posonly_names, kwname) != 0) {
4050 goto fail;
4051 }
4052 posonly_conflicts++;
4053 continue;
4054 }
4055
4056 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4057
4058 if ( cmp > 0) {
4059 if(PyList_Append(posonly_names, kwname) != 0) {
4060 goto fail;
4061 }
4062 posonly_conflicts++;
4063 } else if (cmp < 0) {
4064 goto fail;
4065 }
4066
4067 }
4068 }
4069 if (posonly_conflicts) {
4070 PyObject* comma = PyUnicode_FromString(", ");
4071 if (comma == NULL) {
4072 goto fail;
4073 }
4074 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4075 Py_DECREF(comma);
4076 if (error_names == NULL) {
4077 goto fail;
4078 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004079 _PyErr_Format(tstate, PyExc_TypeError,
4080 "%U() got some positional-only arguments passed"
4081 " as keyword arguments: '%U'",
4082 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004083 Py_DECREF(error_names);
4084 goto fail;
4085 }
4086
4087 Py_DECREF(posonly_names);
4088 return 0;
4089
4090fail:
4091 Py_XDECREF(posonly_names);
4092 return 1;
4093
4094}
4095
Guido van Rossumc2e20742006-02-27 22:32:47 +00004096/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004097 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004098 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004099
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004100PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004101_PyEval_EvalCode(PyThreadState *tstate,
4102 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004103 PyObject *const *args, Py_ssize_t argcount,
4104 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004105 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004106 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004107 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004108 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004109{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004110 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004111
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004112 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004113 PyFrameObject *f;
4114 PyObject *retval = NULL;
4115 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004117 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004118 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004119 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004122 _PyErr_SetString(tstate, PyExc_SystemError,
4123 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 return NULL;
4125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004126
Victor Stinnerc7020012016-08-16 23:40:29 +02004127 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004128 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004129 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 fastlocals = f->f_localsplus;
4133 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004134
Victor Stinnerc7020012016-08-16 23:40:29 +02004135 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 if (co->co_flags & CO_VARKEYWORDS) {
4137 kwdict = PyDict_New();
4138 if (kwdict == NULL)
4139 goto fail;
4140 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004141 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004143 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004144 SETLOCAL(i, kwdict);
4145 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004146 else {
4147 kwdict = NULL;
4148 }
4149
Pablo Galindocd74e662019-06-01 18:08:04 +01004150 /* Copy all positional arguments into local variables */
4151 if (argcount > co->co_argcount) {
4152 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004153 }
4154 else {
4155 n = argcount;
4156 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004157 for (j = 0; j < n; j++) {
4158 x = args[j];
4159 Py_INCREF(x);
4160 SETLOCAL(j, x);
4161 }
4162
Victor Stinnerc7020012016-08-16 23:40:29 +02004163 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004165 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004166 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004167 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004168 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004170 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004171
Serhiy Storchakab7281052016-09-12 00:52:40 +03004172 /* Handle keyword arguments passed as two strided arrays */
4173 kwcount *= kwstep;
4174 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004176 PyObject *keyword = kwnames[i];
4177 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004178 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004179
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004181 _PyErr_Format(tstate, PyExc_TypeError,
4182 "%U() keywords must be strings",
4183 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004184 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004186
Benjamin Petersonb204a422011-06-05 22:04:07 -05004187 /* Speed hack: do raw pointer compares. As names are
4188 normally interned this should almost always hit. */
4189 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004190 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004191 PyObject *name = co_varnames[j];
4192 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004193 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004194 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004196
Benjamin Petersonb204a422011-06-05 22:04:07 -05004197 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004198 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004199 PyObject *name = co_varnames[j];
4200 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4201 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004202 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004203 }
4204 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004206 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004207 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004208
Victor Stinner231d1f32017-01-11 02:12:06 +01004209 assert(j >= total_args);
4210 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004211
Victor Stinner438a12d2019-05-24 17:01:38 +02004212 if (co->co_posonlyargcount
4213 && positional_only_passed_as_keyword(tstate, co,
4214 kwcount, kwnames))
4215 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004216 goto fail;
4217 }
4218
Victor Stinner438a12d2019-05-24 17:01:38 +02004219 _PyErr_Format(tstate, PyExc_TypeError,
4220 "%U() got an unexpected keyword argument '%S'",
4221 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004222 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004224
Christian Heimes0bd447f2013-07-20 14:48:10 +02004225 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4226 goto fail;
4227 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004228 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004229
Benjamin Petersonb204a422011-06-05 22:04:07 -05004230 kw_found:
4231 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004232 _PyErr_Format(tstate, PyExc_TypeError,
4233 "%U() got multiple values for argument '%S'",
4234 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004235 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004237 Py_INCREF(value);
4238 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004240
4241 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004242 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004243 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 goto fail;
4245 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004246
4247 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004248 if (argcount < co->co_argcount) {
4249 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004250 Py_ssize_t missing = 0;
4251 for (i = argcount; i < m; i++) {
4252 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004253 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004254 }
4255 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004256 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004257 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004258 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004259 }
4260 if (n > m)
4261 i = n - m;
4262 else
4263 i = 0;
4264 for (; i < defcount; i++) {
4265 if (GETLOCAL(m+i) == NULL) {
4266 PyObject *def = defs[i];
4267 Py_INCREF(def);
4268 SETLOCAL(m+i, def);
4269 }
4270 }
4271 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004272
4273 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004274 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004275 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004276 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004277 PyObject *name;
4278 if (GETLOCAL(i) != NULL)
4279 continue;
4280 name = PyTuple_GET_ITEM(co->co_varnames, i);
4281 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004282 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004283 if (def) {
4284 Py_INCREF(def);
4285 SETLOCAL(i, def);
4286 continue;
4287 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004288 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004289 goto fail;
4290 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004291 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004292 missing++;
4293 }
4294 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004295 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004296 goto fail;
4297 }
4298 }
4299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004301 vars into frame. */
4302 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004304 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004305 /* Possibly account for the cell variable being an argument. */
4306 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004307 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004308 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004309 /* Clear the local copy. */
4310 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004311 }
4312 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004313 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004314 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004315 if (c == NULL)
4316 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004317 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004319
4320 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004321 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4322 PyObject *o = PyTuple_GET_ITEM(closure, i);
4323 Py_INCREF(o);
4324 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004326
Yury Selivanoveb636452016-09-08 22:01:51 -07004327 /* Handle generator/coroutine/asynchronous generator */
4328 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004329 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004330 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 /* Don't need to keep the reference to f_back, it will be set
4333 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004334 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 /* Create a new generator that owns the ready to run frame
4337 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004338 if (is_coro) {
4339 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004340 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4341 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004342 } else {
4343 gen = PyGen_NewWithQualName(f, name, qualname);
4344 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004345 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004346 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004347 }
INADA Naoki9c157762016-12-26 18:52:46 +09004348
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004349 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004350
Yury Selivanov75445082015-05-11 22:57:16 -04004351 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004353
Victor Stinnerb9e68122019-11-14 12:20:46 +01004354 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004355
Thomas Woutersce272b62007-09-19 21:19:28 +00004356fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 /* decref'ing the frame can cause __del__ methods to get invoked,
4359 which can call back into Python. While we're done with the
4360 current Python frame (f), the associated C stack is still in use,
4361 so recursion_depth must be boosted for the duration.
4362 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004363 if (Py_REFCNT(f) > 1) {
4364 Py_DECREF(f);
4365 _PyObject_GC_TRACK(f);
4366 }
4367 else {
4368 ++tstate->recursion_depth;
4369 Py_DECREF(f);
4370 --tstate->recursion_depth;
4371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004373}
4374
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004375
4376PyObject *
4377_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4378 PyObject *const *args, Py_ssize_t argcount,
4379 PyObject *const *kwnames, PyObject *const *kwargs,
4380 Py_ssize_t kwcount, int kwstep,
4381 PyObject *const *defs, Py_ssize_t defcount,
4382 PyObject *kwdefs, PyObject *closure,
4383 PyObject *name, PyObject *qualname)
4384{
4385 PyThreadState *tstate = _PyThreadState_GET();
4386 return _PyEval_EvalCode(tstate, _co, globals, locals,
4387 args, argcount,
4388 kwnames, kwargs,
4389 kwcount, kwstep,
4390 defs, defcount,
4391 kwdefs, closure,
4392 name, qualname);
4393}
4394
Victor Stinner40ee3012014-06-16 15:59:28 +02004395PyObject *
4396PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004397 PyObject *const *args, int argcount,
4398 PyObject *const *kws, int kwcount,
4399 PyObject *const *defs, int defcount,
4400 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004401{
4402 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004403 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004404 kws, kws != NULL ? kws + 1 : NULL,
4405 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004406 defs, defcount,
4407 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004408 NULL, NULL);
4409}
Tim Peters5ca576e2001-06-18 22:08:13 +00004410
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004411static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004412special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004415 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004416 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004417 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 return NULL;
4419 }
4420 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004421}
4422
4423
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004424/* Logic for the raise statement (too complicated for inlining).
4425 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004426static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004427do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 if (exc == NULL) {
4432 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004433 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004435 type = exc_info->exc_type;
4436 value = exc_info->exc_value;
4437 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004438 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004439 _PyErr_SetString(tstate, PyExc_RuntimeError,
4440 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004441 return 0;
4442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 Py_XINCREF(type);
4444 Py_XINCREF(value);
4445 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004446 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004447 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 /* We support the following forms of raise:
4451 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004452 raise <instance>
4453 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (PyExceptionClass_Check(exc)) {
4456 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004457 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (value == NULL)
4459 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004460 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004461 _PyErr_Format(tstate, PyExc_TypeError,
4462 "calling %R should have returned an instance of "
4463 "BaseException, not %R",
4464 type, Py_TYPE(value));
4465 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 }
4468 else if (PyExceptionInstance_Check(exc)) {
4469 value = exc;
4470 type = PyExceptionInstance_Class(exc);
4471 Py_INCREF(type);
4472 }
4473 else {
4474 /* Not something you can raise. You get an exception
4475 anyway, just not what you specified :-) */
4476 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004477 _PyErr_SetString(tstate, PyExc_TypeError,
4478 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 goto raise_error;
4480 }
Collin Winter828f04a2007-08-31 00:04:24 +00004481
Serhiy Storchakac0191582016-09-27 11:37:10 +03004482 assert(type != NULL);
4483 assert(value != NULL);
4484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 if (cause) {
4486 PyObject *fixed_cause;
4487 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004488 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (fixed_cause == NULL)
4490 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004491 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004493 else if (PyExceptionInstance_Check(cause)) {
4494 fixed_cause = cause;
4495 }
4496 else if (cause == Py_None) {
4497 Py_DECREF(cause);
4498 fixed_cause = NULL;
4499 }
4500 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_SetString(tstate, PyExc_TypeError,
4502 "exception causes must derive from "
4503 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 goto raise_error;
4505 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004506 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 }
Collin Winter828f04a2007-08-31 00:04:24 +00004508
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004510 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004511 Py_DECREF(value);
4512 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004513 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004514
4515raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 Py_XDECREF(value);
4517 Py_XDECREF(type);
4518 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004519 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004520}
4521
Tim Petersd6d010b2001-06-21 02:49:55 +00004522/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004523 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004524
Guido van Rossum0368b722007-05-11 16:50:42 +00004525 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4526 with a variable target.
4527*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004528
Barry Warsawe42b18f1997-08-25 22:13:04 +00004529static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004530unpack_iterable(PyThreadState *tstate, PyObject *v,
4531 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 int i = 0, j = 0;
4534 Py_ssize_t ll = 0;
4535 PyObject *it; /* iter(v) */
4536 PyObject *w;
4537 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004542 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004544 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004545 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004546 _PyErr_Format(tstate, PyExc_TypeError,
4547 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004548 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004549 }
4550 return 0;
4551 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 for (; i < argcnt; i++) {
4554 w = PyIter_Next(it);
4555 if (w == NULL) {
4556 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004557 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004558 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004559 _PyErr_Format(tstate, PyExc_ValueError,
4560 "not enough values to unpack "
4561 "(expected %d, got %d)",
4562 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004563 }
4564 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004565 _PyErr_Format(tstate, PyExc_ValueError,
4566 "not enough values to unpack "
4567 "(expected at least %d, got %d)",
4568 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 }
4571 goto Error;
4572 }
4573 *--sp = w;
4574 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 if (argcntafter == -1) {
4577 /* We better have exhausted the iterator now. */
4578 w = PyIter_Next(it);
4579 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004580 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 goto Error;
4582 Py_DECREF(it);
4583 return 1;
4584 }
4585 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004586 _PyErr_Format(tstate, PyExc_ValueError,
4587 "too many values to unpack (expected %d)",
4588 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 goto Error;
4590 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 l = PySequence_List(it);
4593 if (l == NULL)
4594 goto Error;
4595 *--sp = l;
4596 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 ll = PyList_GET_SIZE(l);
4599 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004600 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004601 "not enough values to unpack (expected at least %d, got %zd)",
4602 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 goto Error;
4604 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 /* Pop the "after-variable" args off the list. */
4607 for (j = argcntafter; j > 0; j--, i++) {
4608 *--sp = PyList_GET_ITEM(l, ll - j);
4609 }
4610 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004611 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 Py_DECREF(it);
4613 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004614
Tim Petersd6d010b2001-06-21 02:49:55 +00004615Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 for (; i > 0; i--, sp++)
4617 Py_DECREF(*sp);
4618 Py_XDECREF(it);
4619 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004620}
4621
4622
Guido van Rossum96a42c81992-01-12 02:29:51 +00004623#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004624static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004625prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004628 if (PyObject_Print(v, stdout, 0) != 0) {
4629 /* Don't know what else to do */
4630 _PyErr_Clear(tstate);
4631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 printf("\n");
4633 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004634}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004635#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004636
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004637static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004638call_exc_trace(Py_tracefunc func, PyObject *self,
4639 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004640{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004641 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004643 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 if (value == NULL) {
4645 value = Py_None;
4646 Py_INCREF(value);
4647 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004648 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004649 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 arg = PyTuple_Pack(3, type, value, traceback);
4651 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004652 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 return;
4654 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004655 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004657 if (err == 0) {
4658 _PyErr_Restore(tstate, type, value, orig_traceback);
4659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 else {
4661 Py_XDECREF(type);
4662 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004663 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004665}
4666
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004667static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004668call_trace_protected(Py_tracefunc func, PyObject *obj,
4669 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyObject *type, *value, *traceback;
4673 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004674 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004675 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 if (err == 0)
4677 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004678 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 return 0;
4680 }
4681 else {
4682 Py_XDECREF(type);
4683 Py_XDECREF(value);
4684 Py_XDECREF(traceback);
4685 return -1;
4686 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004687}
4688
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004689static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004690call_trace(Py_tracefunc func, PyObject *obj,
4691 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 int result;
4695 if (tstate->tracing)
4696 return 0;
4697 tstate->tracing++;
4698 tstate->use_tracing = 0;
4699 result = func(obj, frame, what, arg);
4700 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4701 || (tstate->c_profilefunc != NULL));
4702 tstate->tracing--;
4703 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004704}
4705
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004706PyObject *
4707_PyEval_CallTracing(PyObject *func, PyObject *args)
4708{
Victor Stinner50b48572018-11-01 01:51:40 +01004709 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 int save_tracing = tstate->tracing;
4711 int save_use_tracing = tstate->use_tracing;
4712 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 tstate->tracing = 0;
4715 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4716 || (tstate->c_profilefunc != NULL));
4717 result = PyObject_Call(func, args, NULL);
4718 tstate->tracing = save_tracing;
4719 tstate->use_tracing = save_use_tracing;
4720 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004721}
4722
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004723/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004724static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004725maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004726 PyThreadState *tstate, PyFrameObject *frame,
4727 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 int result = 0;
4730 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 /* If the last instruction executed isn't in the current
4733 instruction window, reset the window.
4734 */
4735 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4736 PyAddrPair bounds;
4737 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4738 &bounds);
4739 *instr_lb = bounds.ap_lower;
4740 *instr_ub = bounds.ap_upper;
4741 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004742 /* If the last instruction falls at the start of a line or if it
4743 represents a jump backwards, update the frame's line number and
4744 then call the trace function if we're tracing source lines.
4745 */
4746 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004748 if (frame->f_trace_lines) {
4749 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4750 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 }
George King20faa682017-10-18 17:44:22 -07004752 /* Always emit an opcode event if we're tracing all opcodes. */
4753 if (frame->f_trace_opcodes) {
4754 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 *instr_prev = frame->f_lasti;
4757 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004758}
4759
Victor Stinner309d7cc2020-03-13 16:39:12 +01004760int
4761_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4762{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004763 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004764 /* The caller must hold the GIL */
4765 assert(PyGILState_Check());
4766
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004767 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004768 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004769 PyThreadState *current_tstate = _PyThreadState_GET();
4770 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004771 return -1;
4772 }
4773
4774 PyObject *profileobj = tstate->c_profileobj;
4775
4776 tstate->c_profilefunc = NULL;
4777 tstate->c_profileobj = NULL;
4778 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4779 tstate->use_tracing = tstate->c_tracefunc != NULL;
4780 Py_XDECREF(profileobj);
4781
4782 Py_XINCREF(arg);
4783 tstate->c_profileobj = arg;
4784 tstate->c_profilefunc = func;
4785
4786 /* Flag that tracing or profiling is turned on */
4787 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4788 return 0;
4789}
4790
Fred Drake5755ce62001-06-27 19:19:46 +00004791void
4792PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004793{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004794 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004795 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004796 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004797 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4798 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004799}
4800
4801int
4802_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4803{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004804 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004805 /* The caller must hold the GIL */
4806 assert(PyGILState_Check());
4807
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004808 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004809 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004810 PyThreadState *current_tstate = _PyThreadState_GET();
4811 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004812 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004813 }
4814
Victor Stinnerda2914d2020-03-20 09:29:08 +01004815 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004816 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004817 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004818
4819 tstate->c_tracefunc = NULL;
4820 tstate->c_traceobj = NULL;
4821 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4822 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4823 Py_XDECREF(traceobj);
4824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004826 tstate->c_traceobj = arg;
4827 tstate->c_tracefunc = func;
4828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004830 tstate->use_tracing = ((func != NULL)
4831 || (tstate->c_profilefunc != NULL));
4832
4833 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004834}
4835
4836void
4837PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4838{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004839 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004840 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004841 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004842 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4843 }
Fred Draked0838392001-06-16 21:02:31 +00004844}
4845
Victor Stinner309d7cc2020-03-13 16:39:12 +01004846
Yury Selivanov75445082015-05-11 22:57:16 -04004847void
Victor Stinner838f2642019-06-13 22:41:23 +02004848_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004849{
4850 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004851 tstate->coroutine_origin_tracking_depth = new_depth;
4852}
4853
4854int
4855_PyEval_GetCoroutineOriginTrackingDepth(void)
4856{
Victor Stinner50b48572018-11-01 01:51:40 +01004857 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004858 return tstate->coroutine_origin_tracking_depth;
4859}
4860
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004861int
Yury Selivanoveb636452016-09-08 22:01:51 -07004862_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4863{
Victor Stinner50b48572018-11-01 01:51:40 +01004864 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004865
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004866 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004867 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004868 }
4869
Yury Selivanoveb636452016-09-08 22:01:51 -07004870 Py_XINCREF(firstiter);
4871 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004872 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004873}
4874
4875PyObject *
4876_PyEval_GetAsyncGenFirstiter(void)
4877{
Victor Stinner50b48572018-11-01 01:51:40 +01004878 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004879 return tstate->async_gen_firstiter;
4880}
4881
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004882int
Yury Selivanoveb636452016-09-08 22:01:51 -07004883_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4884{
Victor Stinner50b48572018-11-01 01:51:40 +01004885 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004886
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004887 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004888 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004889 }
4890
Yury Selivanoveb636452016-09-08 22:01:51 -07004891 Py_XINCREF(finalizer);
4892 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004893 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004894}
4895
4896PyObject *
4897_PyEval_GetAsyncGenFinalizer(void)
4898{
Victor Stinner50b48572018-11-01 01:51:40 +01004899 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004900 return tstate->async_gen_finalizer;
4901}
4902
Victor Stinner438a12d2019-05-24 17:01:38 +02004903PyFrameObject *
4904PyEval_GetFrame(void)
4905{
4906 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004907 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004908}
4909
Guido van Rossumb209a111997-04-29 18:18:01 +00004910PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004911PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004912{
Victor Stinner438a12d2019-05-24 17:01:38 +02004913 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004914 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004916 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 else
4918 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004919}
4920
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004921/* Convenience function to get a builtin from its name */
4922PyObject *
4923_PyEval_GetBuiltinId(_Py_Identifier *name)
4924{
Victor Stinner438a12d2019-05-24 17:01:38 +02004925 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004926 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4927 if (attr) {
4928 Py_INCREF(attr);
4929 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004930 else if (!_PyErr_Occurred(tstate)) {
4931 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004932 }
4933 return attr;
4934}
4935
Guido van Rossumb209a111997-04-29 18:18:01 +00004936PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004937PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004938{
Victor Stinner438a12d2019-05-24 17:01:38 +02004939 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004940 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004941 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004942 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004944 }
4945
Victor Stinner438a12d2019-05-24 17:01:38 +02004946 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004947 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004948 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004949
4950 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004952}
4953
Guido van Rossumb209a111997-04-29 18:18:01 +00004954PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004955PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004956{
Victor Stinner438a12d2019-05-24 17:01:38 +02004957 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004958 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004959 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004962
4963 assert(current_frame->f_globals != NULL);
4964 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004965}
4966
Guido van Rossum6135a871995-01-09 17:53:26 +00004967int
Tim Peters5ba58662001-07-16 02:29:45 +00004968PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004969{
Victor Stinner438a12d2019-05-24 17:01:38 +02004970 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004971 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 if (current_frame != NULL) {
4975 const int codeflags = current_frame->f_code->co_flags;
4976 const int compilerflags = codeflags & PyCF_MASK;
4977 if (compilerflags) {
4978 result = 1;
4979 cf->cf_flags |= compilerflags;
4980 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004981#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 if (codeflags & CO_GENERATOR_ALLOWED) {
4983 result = 1;
4984 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4985 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004986#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 }
4988 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004989}
4990
Guido van Rossum3f5da241990-12-20 15:06:42 +00004991
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004992const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004993PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 if (PyMethod_Check(func))
4996 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4997 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004998 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 else if (PyCFunction_Check(func))
5000 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5001 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005002 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005003}
5004
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005005const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005006PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (PyMethod_Check(func))
5009 return "()";
5010 else if (PyFunction_Check(func))
5011 return "()";
5012 else if (PyCFunction_Check(func))
5013 return "()";
5014 else
5015 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005016}
5017
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005018#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005019if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005020 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5021 tstate, tstate->frame, \
5022 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 x = NULL; \
5024 } \
5025 else { \
5026 x = call; \
5027 if (tstate->c_profilefunc != NULL) { \
5028 if (x == NULL) { \
5029 call_trace_protected(tstate->c_profilefunc, \
5030 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005031 tstate, tstate->frame, \
5032 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 /* XXX should pass (type, value, tb) */ \
5034 } else { \
5035 if (call_trace(tstate->c_profilefunc, \
5036 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005037 tstate, tstate->frame, \
5038 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 Py_DECREF(x); \
5040 x = NULL; \
5041 } \
5042 } \
5043 } \
5044 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005045} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 x = call; \
5047 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005048
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005049
5050static PyObject *
5051trace_call_function(PyThreadState *tstate,
5052 PyObject *func,
5053 PyObject **args, Py_ssize_t nargs,
5054 PyObject *kwnames)
5055{
5056 PyObject *x;
5057 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005058 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005059 return x;
5060 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005061 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005062 /* We need to create a temporary bound method as argument
5063 for profiling.
5064
5065 If nargs == 0, then this cannot work because we have no
5066 "self". In any case, the call itself would raise
5067 TypeError (foo needs an argument), so we just skip
5068 profiling. */
5069 PyObject *self = args[0];
5070 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5071 if (func == NULL) {
5072 return NULL;
5073 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005074 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005075 args+1, nargs-1,
5076 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005077 Py_DECREF(func);
5078 return x;
5079 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005080 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005081}
5082
Victor Stinner415c5102017-01-11 00:54:57 +01005083/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5084 to reduce the stack consumption. */
5085Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005086call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005087{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005088 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 PyObject *func = *pfunc;
5090 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005091 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5092 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005093 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005094
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005095 if (tstate->use_tracing) {
5096 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005097 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005098 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005099 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005101
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005103
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005104 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 while ((*pp_stack) > pfunc) {
5106 w = EXT_POP(*pp_stack);
5107 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005111}
5112
Jeremy Hylton52820442001-01-03 23:52:36 +00005113static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005114do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005115{
jdemeyere89de732018-09-19 12:06:20 +02005116 PyObject *result;
5117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005119 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005120 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005122 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005123 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5124 if (nargs > 0 && tstate->use_tracing) {
5125 /* We need to create a temporary bound method as argument
5126 for profiling.
5127
5128 If nargs == 0, then this cannot work because we have no
5129 "self". In any case, the call itself would raise
5130 TypeError (foo needs an argument), so we just skip
5131 profiling. */
5132 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5133 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5134 if (func == NULL) {
5135 return NULL;
5136 }
5137
Victor Stinner4d231bc2019-11-14 13:36:21 +01005138 C_TRACE(result, _PyObject_FastCallDictTstate(
5139 tstate, func,
5140 &_PyTuple_ITEMS(callargs)[1],
5141 nargs - 1,
5142 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005143 Py_DECREF(func);
5144 return result;
5145 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005146 }
jdemeyere89de732018-09-19 12:06:20 +02005147 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005148}
5149
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005150/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005151 nb_index slot defined, and store in *pi.
5152 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005153 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005154 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005155*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005156int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005157_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005158{
Victor Stinner438a12d2019-05-24 17:01:38 +02005159 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005160 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005162 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005164 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 return 0;
5166 }
5167 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005168 _PyErr_SetString(tstate, PyExc_TypeError,
5169 "slice indices must be integers or "
5170 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 return 0;
5172 }
5173 *pi = x;
5174 }
5175 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005176}
5177
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005178int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005179_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005180{
Victor Stinner438a12d2019-05-24 17:01:38 +02005181 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005182 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005183 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005184 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005185 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005186 return 0;
5187 }
5188 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005189 _PyErr_SetString(tstate, PyExc_TypeError,
5190 "slice indices must be integers or "
5191 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005192 return 0;
5193 }
5194 *pi = x;
5195 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005196}
5197
Thomas Wouters52152252000-08-17 22:55:00 +00005198static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005199import_name(PyThreadState *tstate, PyFrameObject *f,
5200 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005201{
5202 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005203 PyObject *import_func, *res;
5204 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005205
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005206 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005207 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005208 if (!_PyErr_Occurred(tstate)) {
5209 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005210 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005211 return NULL;
5212 }
5213
5214 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005215 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005216 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005217 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005218 return NULL;
5219 }
5220 res = PyImport_ImportModuleLevelObject(
5221 name,
5222 f->f_globals,
5223 f->f_locals == NULL ? Py_None : f->f_locals,
5224 fromlist,
5225 ilevel);
5226 return res;
5227 }
5228
5229 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005230
5231 stack[0] = name;
5232 stack[1] = f->f_globals;
5233 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5234 stack[3] = fromlist;
5235 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005236 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005237 Py_DECREF(import_func);
5238 return res;
5239}
5240
5241static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005242import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005245 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005246
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005247 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005248 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005249 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005250 /* Issue #17636: in case this failed because of a circular relative
5251 import, try to fallback on reading the module directly from
5252 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005253 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005254 if (pkgname == NULL) {
5255 goto error;
5256 }
Oren Milman6db70332017-09-19 14:23:01 +03005257 if (!PyUnicode_Check(pkgname)) {
5258 Py_CLEAR(pkgname);
5259 goto error;
5260 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005261 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005262 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005263 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005264 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005265 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005266 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005267 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005268 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005269 goto error;
5270 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005271 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005273 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005274 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005275 if (pkgname == NULL) {
5276 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5277 if (pkgname_or_unknown == NULL) {
5278 Py_XDECREF(pkgpath);
5279 return NULL;
5280 }
5281 } else {
5282 pkgname_or_unknown = pkgname;
5283 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005284
5285 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005286 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005287 errmsg = PyUnicode_FromFormat(
5288 "cannot import name %R from %R (unknown location)",
5289 name, pkgname_or_unknown
5290 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005291 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005292 PyErr_SetImportError(errmsg, pkgname, NULL);
5293 }
5294 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005295 _Py_IDENTIFIER(__spec__);
5296 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005297 const char *fmt =
5298 _PyModuleSpec_IsInitializing(spec) ?
5299 "cannot import name %R from partially initialized module %R "
5300 "(most likely due to a circular import) (%S)" :
5301 "cannot import name %R from %R (%S)";
5302 Py_XDECREF(spec);
5303
5304 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005305 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005306 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005307 }
5308
Xiang Zhang4830f582017-03-21 11:13:42 +08005309 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005310 Py_XDECREF(pkgname_or_unknown);
5311 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005312 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005313}
Guido van Rossumac7be682001-01-17 15:42:30 +00005314
Thomas Wouters52152252000-08-17 22:55:00 +00005315static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005316import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005317{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005318 _Py_IDENTIFIER(__all__);
5319 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005320 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 int skip_leading_underscores = 0;
5322 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005323
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005324 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5325 return -1; /* Unexpected error */
5326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005328 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5329 return -1;
5330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005332 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005333 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 return -1;
5335 }
5336 all = PyMapping_Keys(dict);
5337 Py_DECREF(dict);
5338 if (all == NULL)
5339 return -1;
5340 skip_leading_underscores = 1;
5341 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 for (pos = 0, err = 0; ; pos++) {
5344 name = PySequence_GetItem(all, pos);
5345 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005346 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005348 }
5349 else {
5350 _PyErr_Clear(tstate);
5351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005352 break;
5353 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005354 if (!PyUnicode_Check(name)) {
5355 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5356 if (modname == NULL) {
5357 Py_DECREF(name);
5358 err = -1;
5359 break;
5360 }
5361 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005362 _PyErr_Format(tstate, PyExc_TypeError,
5363 "module __name__ must be a string, not %.100s",
5364 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005365 }
5366 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005367 _PyErr_Format(tstate, PyExc_TypeError,
5368 "%s in %U.%s must be str, not %.100s",
5369 skip_leading_underscores ? "Key" : "Item",
5370 modname,
5371 skip_leading_underscores ? "__dict__" : "__all__",
5372 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005373 }
5374 Py_DECREF(modname);
5375 Py_DECREF(name);
5376 err = -1;
5377 break;
5378 }
5379 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005380 if (PyUnicode_READY(name) == -1) {
5381 Py_DECREF(name);
5382 err = -1;
5383 break;
5384 }
5385 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5386 Py_DECREF(name);
5387 continue;
5388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 }
5390 value = PyObject_GetAttr(v, name);
5391 if (value == NULL)
5392 err = -1;
5393 else if (PyDict_CheckExact(locals))
5394 err = PyDict_SetItem(locals, name, value);
5395 else
5396 err = PyObject_SetItem(locals, name, value);
5397 Py_DECREF(name);
5398 Py_XDECREF(value);
5399 if (err != 0)
5400 break;
5401 }
5402 Py_DECREF(all);
5403 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005404}
5405
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005406static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005407check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005408{
Victor Stinnera102ed72020-02-07 02:24:48 +01005409 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005410 /* check_args_iterable() may be called with a live exception:
5411 * clear it to prevent calling _PyObject_FunctionStr() with an
5412 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005413 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005414 PyObject *funcstr = _PyObject_FunctionStr(func);
5415 if (funcstr != NULL) {
5416 _PyErr_Format(tstate, PyExc_TypeError,
5417 "%U argument after * must be an iterable, not %.200s",
5418 funcstr, Py_TYPE(args)->tp_name);
5419 Py_DECREF(funcstr);
5420 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005421 return -1;
5422 }
5423 return 0;
5424}
5425
5426static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005427format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005428{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005429 /* _PyDict_MergeEx raises attribute
5430 * error (percolated from an attempt
5431 * to get 'keys' attribute) instead of
5432 * a type error if its second argument
5433 * is not a mapping.
5434 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005435 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005436 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005437 PyObject *funcstr = _PyObject_FunctionStr(func);
5438 if (funcstr != NULL) {
5439 _PyErr_Format(
5440 tstate, PyExc_TypeError,
5441 "%U argument after ** must be a mapping, not %.200s",
5442 funcstr, Py_TYPE(kwargs)->tp_name);
5443 Py_DECREF(funcstr);
5444 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005445 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005446 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005447 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005449 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005450 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005451 PyObject *funcstr = _PyObject_FunctionStr(func);
5452 if (funcstr != NULL) {
5453 PyObject *key = PyTuple_GET_ITEM(val, 0);
5454 _PyErr_Format(
5455 tstate, PyExc_TypeError,
5456 "%U got multiple values for keyword argument '%S'",
5457 funcstr, key);
5458 Py_DECREF(funcstr);
5459 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005460 Py_XDECREF(exc);
5461 Py_XDECREF(val);
5462 Py_XDECREF(tb);
5463 }
5464 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005465 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005466 }
5467 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005468}
5469
Guido van Rossumac7be682001-01-17 15:42:30 +00005470static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005471format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5472 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 if (!obj)
5477 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005478
Serhiy Storchaka06515832016-11-20 09:13:07 +02005479 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 if (!obj_str)
5481 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005482
Victor Stinner438a12d2019-05-24 17:01:38 +02005483 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005484}
Guido van Rossum950361c1997-01-24 13:49:28 +00005485
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005486static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005487format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005488{
5489 PyObject *name;
5490 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005491 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005492 return;
5493 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5494 name = PyTuple_GET_ITEM(co->co_cellvars,
5495 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005496 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005497 PyExc_UnboundLocalError,
5498 UNBOUNDLOCAL_ERROR_MSG,
5499 name);
5500 } else {
5501 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5502 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005503 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005504 UNBOUNDFREE_ERROR_MSG, name);
5505 }
5506}
5507
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005508static void
Mark Shannonfee55262019-11-21 09:11:43 +00005509format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005510{
5511 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5512 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005513 _PyErr_Format(tstate, PyExc_TypeError,
5514 "'async with' received an object from __aenter__ "
5515 "that does not implement __await__: %.100s",
5516 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005517 }
Mark Shannonfee55262019-11-21 09:11:43 +00005518 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005519 _PyErr_Format(tstate, PyExc_TypeError,
5520 "'async with' received an object from __aexit__ "
5521 "that does not implement __await__: %.100s",
5522 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005523 }
5524 }
5525}
5526
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005527static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005528unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005529 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005530{
5531 PyObject *res;
5532 if (Py_REFCNT(v) == 2) {
5533 /* In the common case, there are 2 references to the value
5534 * stored in 'variable' when the += is performed: one on the
5535 * value stack (in 'v') and one still stored in the
5536 * 'variable'. We try to delete the variable now to reduce
5537 * the refcnt to 1.
5538 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005539 int opcode, oparg;
5540 NEXTOPARG();
5541 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005542 case STORE_FAST:
5543 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005544 PyObject **fastlocals = f->f_localsplus;
5545 if (GETLOCAL(oparg) == v)
5546 SETLOCAL(oparg, NULL);
5547 break;
5548 }
5549 case STORE_DEREF:
5550 {
5551 PyObject **freevars = (f->f_localsplus +
5552 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005553 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005554 if (PyCell_GET(c) == v) {
5555 PyCell_SET(c, NULL);
5556 Py_DECREF(v);
5557 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005558 break;
5559 }
5560 case STORE_NAME:
5561 {
5562 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005563 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005564 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005565 if (locals && PyDict_CheckExact(locals)) {
5566 PyObject *w = PyDict_GetItemWithError(locals, name);
5567 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005568 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005569 {
5570 Py_DECREF(v);
5571 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005572 }
5573 }
5574 break;
5575 }
5576 }
5577 }
5578 res = v;
5579 PyUnicode_Append(&res, w);
5580 return res;
5581}
5582
Guido van Rossum950361c1997-01-24 13:49:28 +00005583#ifdef DYNAMIC_EXECUTION_PROFILE
5584
Skip Montanarof118cb12001-10-15 20:51:38 +00005585static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005586getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005588 int i;
5589 PyObject *l = PyList_New(256);
5590 if (l == NULL) return NULL;
5591 for (i = 0; i < 256; i++) {
5592 PyObject *x = PyLong_FromLong(a[i]);
5593 if (x == NULL) {
5594 Py_DECREF(l);
5595 return NULL;
5596 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005597 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005598 }
5599 for (i = 0; i < 256; i++)
5600 a[i] = 0;
5601 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005602}
5603
5604PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005605_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005606{
5607#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005609#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005610 int i;
5611 PyObject *l = PyList_New(257);
5612 if (l == NULL) return NULL;
5613 for (i = 0; i < 257; i++) {
5614 PyObject *x = getarray(dxpairs[i]);
5615 if (x == NULL) {
5616 Py_DECREF(l);
5617 return NULL;
5618 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005619 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005620 }
5621 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005622#endif
5623}
5624
5625#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005626
5627Py_ssize_t
5628_PyEval_RequestCodeExtraIndex(freefunc free)
5629{
Victor Stinner81a7be32020-04-14 15:14:01 +02005630 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005631 Py_ssize_t new_index;
5632
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005633 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005634 return -1;
5635 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005636 new_index = interp->co_extra_user_count++;
5637 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005638 return new_index;
5639}
Łukasz Langaa785c872016-09-09 17:37:37 -07005640
5641static void
5642dtrace_function_entry(PyFrameObject *f)
5643{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005644 const char *filename;
5645 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005646 int lineno;
5647
Victor Stinner6d86a232020-04-29 00:56:58 +02005648 PyCodeObject *code = f->f_code;
5649 filename = PyUnicode_AsUTF8(code->co_filename);
5650 funcname = PyUnicode_AsUTF8(code->co_name);
5651 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005652
Andy Lestere6be9b52020-02-11 20:28:35 -06005653 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005654}
5655
5656static void
5657dtrace_function_return(PyFrameObject *f)
5658{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005659 const char *filename;
5660 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005661 int lineno;
5662
Victor Stinner6d86a232020-04-29 00:56:58 +02005663 PyCodeObject *code = f->f_code;
5664 filename = PyUnicode_AsUTF8(code->co_filename);
5665 funcname = PyUnicode_AsUTF8(code->co_name);
5666 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005667
Andy Lestere6be9b52020-02-11 20:28:35 -06005668 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005669}
5670
5671/* DTrace equivalent of maybe_call_line_trace. */
5672static void
5673maybe_dtrace_line(PyFrameObject *frame,
5674 int *instr_lb, int *instr_ub, int *instr_prev)
5675{
5676 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005677 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005678
5679 /* If the last instruction executed isn't in the current
5680 instruction window, reset the window.
5681 */
5682 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5683 PyAddrPair bounds;
5684 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5685 &bounds);
5686 *instr_lb = bounds.ap_lower;
5687 *instr_ub = bounds.ap_upper;
5688 }
5689 /* If the last instruction falls at the start of a line or if
5690 it represents a jump backwards, update the frame's line
5691 number and call the trace function. */
5692 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5693 frame->f_lineno = line;
5694 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5695 if (!co_filename)
5696 co_filename = "?";
5697 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5698 if (!co_name)
5699 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005700 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005701 }
5702 *instr_prev = frame->f_lasti;
5703}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005704
5705
5706/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5707 for the limited API. */
5708
5709#undef Py_EnterRecursiveCall
5710
5711int Py_EnterRecursiveCall(const char *where)
5712{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005713 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005714}
5715
5716#undef Py_LeaveRecursiveCall
5717
5718void Py_LeaveRecursiveCall(void)
5719{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005720 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005721}