blob: e15d7e0b4603d26304cf7602e688f84de38673b6 [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 Stinnerda2914d2020-03-20 09:29:08 +0100146 struct _ceval_runtime_state *ceval,
147 struct _ceval_state *ceval2)
148{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval->gil_drop_request)
151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152 && _Py_ThreadCanHandleSignals(interp))
Victor Stinnerd8316882020-03-20 14:50:35 +0100153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155 | ceval2->pending.async_exc);
156}
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 Stinnerb54a99d2020-04-08 23:35:05 +0200162 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
163 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100164 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 1);
165 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
166}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Victor Stinnerda2914d2020-03-20 09:29:08 +0100169static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200170RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100171{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200172 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
173 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100174 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200175 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100176}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Eric Snowfdf282d2019-01-11 14:26:55 -0700178
Victor Stinnerda2914d2020-03-20 09:29:08 +0100179static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200180SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200182 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
183 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100184 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200185 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100186}
Eric Snowfdf282d2019-01-11 14:26:55 -0700187
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000188
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200190UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200192 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
193 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100194 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200195 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100196}
197
198
199static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200200SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100201{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200202 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
203 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100204 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
205 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200206 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100207}
208
209
210static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200211UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100212{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200213 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
214 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100215 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200216 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100217}
218
219
220static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200221SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100222{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200223 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100224 ceval2->pending.async_exc = 1;
225 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
226}
227
228
229static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200230UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100231{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200232 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
233 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100234 ceval2->pending.async_exc = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200235 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100236}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237
238
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000239#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000240#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000241#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100244static void
245ensure_tstate_not_null(const char *func, PyThreadState *tstate)
246{
247 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100248 _Py_FatalErrorFunc(func,
249 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100250 }
251}
252
253
Tim Peters7f468f22004-10-11 02:40:51 +0000254int
Victor Stinner175a7042020-03-10 00:37:48 +0100255_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
256{
257 return gil_created(&runtime->ceval.gil);
258}
259
260int
Tim Peters7f468f22004-10-11 02:40:51 +0000261PyEval_ThreadsInitialized(void)
262{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100263 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100264 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000265}
266
Victor Stinner111e4ee2020-03-09 21:24:14 +0100267PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200268_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200270 if (!_Py_IsMainInterpreter(tstate)) {
271 /* Currently, the GIL is shared by all interpreters,
272 and only the main interpreter is responsible to create
273 and destroy it. */
274 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100275 }
276
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200277 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
278 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100279
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200280 PyThread_init_thread();
281 create_gil(gil);
282
283 take_gil(tstate);
284
285 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100286 return _PyStatus_OK();
287}
288
289void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200290_PyEval_FiniGIL(PyThreadState *tstate)
291{
292 if (!_Py_IsMainInterpreter(tstate)) {
293 /* Currently, the GIL is shared by all interpreters,
294 and only the main interpreter is responsible to create
295 and destroy it. */
296 return;
297 }
298
299 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
300 if (!gil_created(gil)) {
301 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
302 yet: do nothing. */
303 return;
304 }
305
306 destroy_gil(gil);
307 assert(!gil_created(gil));
308}
309
310void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100311PyEval_InitThreads(void)
312{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100313 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000315
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000316void
Inada Naoki91234a12019-06-03 21:30:58 +0900317_PyEval_Fini(void)
318{
319#if OPCACHE_STATS
320 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
321 opcache_code_objects);
322
323 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
324 opcache_code_objects_extra_mem);
325
326 fprintf(stderr, "\n");
327
328 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
329 opcache_global_hits,
330 (int) (100.0 * opcache_global_hits /
331 (opcache_global_hits + opcache_global_misses)));
332
333 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
334 opcache_global_misses,
335 (int) (100.0 * opcache_global_misses /
336 (opcache_global_hits + opcache_global_misses)));
337
338 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
339 opcache_global_opts);
340
341 fprintf(stderr, "\n");
342#endif
343}
344
345void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000347{
Victor Stinner09532fe2019-05-10 23:39:09 +0200348 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100350 ensure_tstate_not_null(__func__, tstate);
351
Victor Stinner85f5a692020-03-09 22:12:04 +0100352 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000353}
354
355void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357{
Victor Stinner09532fe2019-05-10 23:39:09 +0200358 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200359 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100361 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100362 in debug mode. */
363 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000364}
365
366void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100367_PyEval_ReleaseLock(PyThreadState *tstate)
368{
369 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100370 drop_gil(ceval, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100371}
372
373void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000375{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100376 ensure_tstate_not_null(__func__, tstate);
377
Victor Stinner85f5a692020-03-09 22:12:04 +0100378 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200379
Victor Stinner85f5a692020-03-09 22:12:04 +0100380 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
381 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100382 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200383 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000384}
385
386void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000388{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100389 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200390
Victor Stinner01b1cc12019-11-20 02:27:56 +0100391 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200392 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
393 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100394 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200395 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100396 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000397}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000398
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900399#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200400/* This function is called from PyOS_AfterFork_Child to destroy all threads
401 * which are not running in the child process, and clear internal locks
402 * which might be held by those threads.
403 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000404
405void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200406_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000407{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100408 struct _gil_runtime_state *gil = &runtime->ceval.gil;
409 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200411 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100412 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100413 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
414 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100415
416 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700417
Victor Stinner50e6e992020-03-19 02:41:21 +0100418 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900419 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700420 Py_FatalError("Can't initialize threads for pending calls");
421 }
Jesse Nollera8513972008-07-17 16:49:17 +0000422
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200423 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100424 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000425}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900426#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000427
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000428/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600429 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000430
431void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100432_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000433{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200434 assert(is_tstate_valid(tstate));
435 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000436}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000438PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440{
Victor Stinner09532fe2019-05-10 23:39:09 +0200441 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200442 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100443
Victor Stinner09532fe2019-05-10 23:39:09 +0200444 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100445 ensure_tstate_not_null(__func__, tstate);
446
Victor Stinnere225beb2019-06-03 18:14:24 +0200447 assert(gil_created(&ceval->gil));
Victor Stinnerda2914d2020-03-20 09:29:08 +0100448 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450}
451
452void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000453PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100455 ensure_tstate_not_null(__func__, tstate);
456
Victor Stinner85f5a692020-03-09 22:12:04 +0100457 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100458
Victor Stinner85f5a692020-03-09 22:12:04 +0100459 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
460 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000461}
462
463
Guido van Rossuma9672091994-09-14 13:31:22 +0000464/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
465 signal handlers or Mac I/O completion routines) can schedule calls
466 to a function to be called synchronously.
467 The synchronous function is called with one void* argument.
468 It should return 0 for success or -1 for failure -- failure should
469 be accompanied by an exception.
470
471 If registry succeeds, the registry function returns 0; if it fails
472 (e.g. due to too many pending calls) it returns -1 (without setting
473 an exception condition).
474
475 Note that because registry may occur from within signal handlers,
476 or other asynchronous events, calling malloc() is unsafe!
477
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 Any thread can schedule pending calls, but only the main thread
479 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 There is no facility to schedule calls to a particular thread, but
481 that should be easy to change, should that ever be required. In
482 that case, the static variables here should go into the python
483 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000485
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200486void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200487_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200488{
489 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100490 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200491 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200492 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200493}
494
Eric Snow5be45a62019-03-08 22:47:07 -0700495/* Push one item onto the queue while holding the lock. */
496static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200497_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600498 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700499{
Eric Snow842a2f02019-03-15 15:47:51 -0600500 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700501 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600502 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700503 return -1; /* Queue full */
504 }
Eric Snow842a2f02019-03-15 15:47:51 -0600505 pending->calls[i].func = func;
506 pending->calls[i].arg = arg;
507 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700508 return 0;
509}
510
511/* Pop one item off the queue while holding the lock. */
512static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200513_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600514 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700515{
Eric Snow842a2f02019-03-15 15:47:51 -0600516 int i = pending->first;
517 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700518 return; /* Queue empty */
519 }
520
Eric Snow842a2f02019-03-15 15:47:51 -0600521 *func = pending->calls[i].func;
522 *arg = pending->calls[i].arg;
523 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700524}
525
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200526/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000527 scheduling to be made from any thread, and even from an executing
528 callback.
529 */
530
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000531int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200532_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200533 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000534{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200535 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600536
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200537 /* Ensure that _PyEval_InitPendingCalls() was called
538 and that _PyEval_FiniPendingCalls() is not called yet. */
539 assert(pending->lock != NULL);
540
Eric Snow842a2f02019-03-15 15:47:51 -0600541 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200542 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600543 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700544
Victor Stinnere225beb2019-06-03 18:14:24 +0200545 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200546 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000548}
549
Victor Stinner09532fe2019-05-10 23:39:09 +0200550int
551Py_AddPendingCall(int (*func)(void *), void *arg)
552{
Victor Stinner50e6e992020-03-19 02:41:21 +0100553 /* Best-effort to support subinterpreters and calls with the GIL released.
554
555 First attempt _PyThreadState_GET() since it supports subinterpreters.
556
557 If the GIL is released, _PyThreadState_GET() returns NULL . In this
558 case, use PyGILState_GetThisThreadState() which works even if the GIL
559 is released.
560
561 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
562 see bpo-10915 and bpo-15751.
563
Victor Stinner8849e592020-03-18 19:28:53 +0100564 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100565 PyThreadState *tstate = _PyThreadState_GET();
566 if (tstate == NULL) {
567 tstate = PyGILState_GetThisThreadState();
568 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200569
570 PyInterpreterState *interp;
571 if (tstate != NULL) {
572 interp = tstate->interp;
573 }
574 else {
575 /* Last resort: use the main interpreter */
576 interp = _PyRuntime.interpreters.main;
577 }
578 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200579}
580
Eric Snowfdf282d2019-01-11 14:26:55 -0700581static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100582handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700583{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200584 assert(is_tstate_valid(tstate));
585 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700586 return 0;
587 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700588
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200589 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100590 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
591 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200592 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700593 return -1;
594 }
595 return 0;
596}
597
598static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100599make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000600{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200601 assert(is_tstate_valid(tstate));
602
Victor Stinnerd8316882020-03-20 14:50:35 +0100603 /* only execute pending calls on main thread */
604 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200605 return 0;
606 }
607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100609 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700610 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700612 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200613 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100614
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200615 /* unsignal before starting to call callbacks, so that any callback
616 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200617 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700618 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100621 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700622 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700623 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 void *arg = NULL;
625
626 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600627 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200628 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600629 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700630
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100631 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700632 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100633 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700634 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700635 res = func(arg);
636 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200637 goto error;
638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200640
Charles-François Natalif23339a2011-07-23 18:15:43 +0200641 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700642 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200643
644error:
645 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200646 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700647 return res;
648}
649
Eric Snow842a2f02019-03-15 15:47:51 -0600650void
Victor Stinner2b1df452020-01-13 18:46:59 +0100651_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600652{
Eric Snow842a2f02019-03-15 15:47:51 -0600653 assert(PyGILState_Check());
654
Victor Stinner50e6e992020-03-19 02:41:21 +0100655 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200656
Eric Snow842a2f02019-03-15 15:47:51 -0600657 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
658 return;
659 }
660
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100661 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200662 PyObject *exc, *val, *tb;
663 _PyErr_Fetch(tstate, &exc, &val, &tb);
664 PyErr_BadInternalCall();
665 _PyErr_ChainExceptions(exc, val, tb);
666 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600667 }
668}
669
Eric Snowfdf282d2019-01-11 14:26:55 -0700670/* Py_MakePendingCalls() is a simple wrapper for the sake
671 of backward-compatibility. */
672int
673Py_MakePendingCalls(void)
674{
675 assert(PyGILState_Check());
676
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100677 PyThreadState *tstate = _PyThreadState_GET();
678
Eric Snowfdf282d2019-01-11 14:26:55 -0700679 /* Python signal handler doesn't really queue a callback: it only signals
680 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100681 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700682 if (res != 0) {
683 return res;
684 }
685
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100686 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600687 if (res != 0) {
688 return res;
689 }
690
691 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000692}
693
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694/* The interpreter's recursion limit */
695
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000696#ifndef Py_DEFAULT_RECURSION_LIMIT
697#define Py_DEFAULT_RECURSION_LIMIT 1000
698#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600699
Eric Snow05351c12017-09-05 21:43:08 -0700700int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000701
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600702void
Victor Stinnerdab84232020-03-17 18:56:44 +0100703_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600704{
Victor Stinnerdab84232020-03-17 18:56:44 +0100705 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600706 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100707 _gil_initialize(&ceval->gil);
708}
709
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200710int
Victor Stinnerdab84232020-03-17 18:56:44 +0100711_PyEval_InitState(struct _ceval_state *ceval)
712{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200713 struct _pending_calls *pending = &ceval->pending;
714 assert(pending->lock == NULL);
715
716 pending->lock = PyThread_allocate_lock();
717 if (pending->lock == NULL) {
718 return -1;
719 }
720 return 0;
721}
722
723void
724_PyEval_FiniState(struct _ceval_state *ceval)
725{
726 struct _pending_calls *pending = &ceval->pending;
727 if (pending->lock != NULL) {
728 PyThread_free_lock(pending->lock);
729 pending->lock = NULL;
730 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600731}
732
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000733int
734Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000735{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100736 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
737 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000738}
739
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000740void
741Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000742{
Victor Stinnere225beb2019-06-03 18:14:24 +0200743 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
744 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100745 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000746}
747
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100748/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000749 if the recursion_depth reaches _Py_CheckRecursionLimit.
750 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
751 to guarantee that _Py_CheckRecursiveCall() is regularly called.
752 Without USE_STACKCHECK, there is no need for this. */
753int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100754_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000755{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100756 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200757 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000758
759#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700760 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 if (PyOS_CheckStack()) {
762 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200763 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return -1;
765 }
pdox18967932017-10-25 23:03:01 -0700766 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700767 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700768#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (tstate->recursion_critical)
770 /* Somebody asked that we don't check for recursion. */
771 return 0;
772 if (tstate->overflowed) {
773 if (tstate->recursion_depth > recursion_limit + 50) {
774 /* Overflowing while handling an overflow. Give up. */
775 Py_FatalError("Cannot recover from stack overflow.");
776 }
777 return 0;
778 }
779 if (tstate->recursion_depth > recursion_limit) {
780 --tstate->recursion_depth;
781 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200782 _PyErr_Format(tstate, PyExc_RecursionError,
783 "maximum recursion depth exceeded%s",
784 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 return -1;
786 }
787 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000788}
789
Victor Stinner09532fe2019-05-10 23:39:09 +0200790static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200791static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000792
Victor Stinnere225beb2019-06-03 18:14:24 +0200793#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000794
Guido van Rossum374a9221991-04-04 10:40:29 +0000795
Guido van Rossumb209a111997-04-29 18:18:01 +0000796PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000797PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 return PyEval_EvalCodeEx(co,
800 globals, locals,
801 (PyObject **)NULL, 0,
802 (PyObject **)NULL, 0,
803 (PyObject **)NULL, 0,
804 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000805}
806
807
808/* Interpreter main loop */
809
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000810PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100811PyEval_EvalFrame(PyFrameObject *f)
812{
Victor Stinner0b72b232020-03-12 23:18:39 +0100813 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100814 PyThreadState *tstate = _PyThreadState_GET();
815 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000816}
817
818PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000820{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100821 PyThreadState *tstate = _PyThreadState_GET();
822 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700823}
824
Victor Stinnerda2914d2020-03-20 09:29:08 +0100825
826/* Handle signals, pending calls, GIL drop request
827 and asynchronous exception */
828static int
829eval_frame_handle_pending(PyThreadState *tstate)
830{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100831 _PyRuntimeState * const runtime = &_PyRuntime;
832 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200833
834 /* Pending signals */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100835 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
836 if (handle_signals(tstate) != 0) {
837 return -1;
838 }
839 }
840
841 /* Pending calls */
842 struct _ceval_state *ceval2 = &tstate->interp->ceval;
843 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
844 if (make_pending_calls(tstate) != 0) {
845 return -1;
846 }
847 }
848
849 /* GIL drop request */
850 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
851 /* Give another thread a chance */
852 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
853 Py_FatalError("tstate mix-up");
854 }
855 drop_gil(ceval, tstate);
856
857 /* Other threads may run now */
858
859 take_gil(tstate);
860
861 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
862 Py_FatalError("orphan tstate");
863 }
864 }
865
866 /* Check for asynchronous exception. */
867 if (tstate->async_exc != NULL) {
868 PyObject *exc = tstate->async_exc;
869 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200870 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100871 _PyErr_SetNone(tstate, exc);
872 Py_DECREF(exc);
873 return -1;
874 }
875
876 return 0;
877}
878
Victor Stinnerc6944e72016-11-11 02:13:35 +0100879PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100880_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700881{
Victor Stinner0b72b232020-03-12 23:18:39 +0100882 ensure_tstate_not_null(__func__, tstate);
883
Guido van Rossum950361c1997-01-24 13:49:28 +0000884#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000886#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200887 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300888 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200889 int opcode; /* Current opcode */
890 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200891 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100893 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100894 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 is true when the line being executed has changed. The
902 initial values are such as to make this false the first
903 time it is tested. */
904 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000905
Serhiy Storchakaab874002016-09-11 13:48:15 +0300906 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *names;
908 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900909 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000910
Brett Cannon368b4b72012-04-02 12:17:59 -0400911#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200912 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400913#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200914
Antoine Pitroub52ec782009-01-25 16:34:23 +0000915/* Computed GOTOs, or
916 the-optimization-commonly-but-improperly-known-as-"threaded code"
917 using gcc's labels-as-values extension
918 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
919
920 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922 combined with a lookup table of jump addresses. However, since the
923 indirect jump instruction is shared by all opcodes, the CPU will have a
924 hard time making the right prediction for where to jump next (actually,
925 it will be always wrong except in the uncommon case of a sequence of
926 several identical opcodes).
927
928 "Threaded code" in contrast, uses an explicit jump table and an explicit
929 indirect jump instruction at the end of each opcode. Since the jump
930 instruction is at a different address for each opcode, the CPU will make a
931 separate prediction for each of these instructions, which is equivalent to
932 predicting the second opcode of each opcode pair. These predictions have
933 a much better chance to turn out valid, especially in small bytecode loops.
934
935 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000937 and potentially many more instructions (depending on the pipeline width).
938 A correctly predicted branch, however, is nearly free.
939
940 At the time of this writing, the "threaded code" version is up to 15-20%
941 faster than the normal "switch" version, depending on the compiler and the
942 CPU architecture.
943
944 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
945 because it would render the measurements invalid.
946
947
948 NOTE: care must be taken that the compiler doesn't try to "optimize" the
949 indirect jumps by sharing them between all opcodes. Such optimizations
950 can be disabled on gcc by using the -fno-gcse flag (or possibly
951 -fno-crossjumping).
952*/
953
Antoine Pitrou042b1282010-08-13 21:15:58 +0000954#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000955#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000956#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000957#endif
958
Antoine Pitrou042b1282010-08-13 21:15:58 +0000959#ifdef HAVE_COMPUTED_GOTOS
960 #ifndef USE_COMPUTED_GOTOS
961 #define USE_COMPUTED_GOTOS 1
962 #endif
963#else
964 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
965 #error "Computed gotos are not supported on this compiler."
966 #endif
967 #undef USE_COMPUTED_GOTOS
968 #define USE_COMPUTED_GOTOS 0
969#endif
970
971#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000972/* Import the static jump table */
973#include "opcode_targets.h"
974
Antoine Pitroub52ec782009-01-25 16:34:23 +0000975#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700976 op: \
977 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000978
Antoine Pitroub52ec782009-01-25 16:34:23 +0000979#ifdef LLTRACE
980#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100982 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300984 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 } \
987 goto fast_next_opcode; \
988 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000989#else
990#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100992 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300994 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300995 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 } \
997 goto fast_next_opcode; \
998 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999#endif
1000
Victor Stinner09532fe2019-05-10 23:39:09 +02001001#define DISPATCH() \
1002 { \
1003 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1004 FAST_DISPATCH(); \
1005 } \
1006 continue; \
1007 }
1008
Antoine Pitroub52ec782009-01-25 16:34:23 +00001009#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001010#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001011#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001012#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001013#endif
1014
1015
Neal Norwitza81d2202002-07-14 00:27:26 +00001016/* Tuple access macros */
1017
1018#ifndef Py_DEBUG
1019#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1020#else
1021#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1022#endif
1023
Guido van Rossum374a9221991-04-04 10:40:29 +00001024/* Code access macros */
1025
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001026/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001027#define INSTR_OFFSET() \
1028 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001029#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001030 _Py_CODEUNIT word = *next_instr; \
1031 opcode = _Py_OPCODE(word); \
1032 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001033 next_instr++; \
1034 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001035#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1036#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001037
Raymond Hettingerf606f872003-03-16 03:11:04 +00001038/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 Some opcodes tend to come in pairs thus making it possible to
1040 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001041 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 Verifying the prediction costs a single high-speed test of a register
1044 variable against a constant. If the pairing was good, then the
1045 processor's own internal branch predication has a high likelihood of
1046 success, resulting in a nearly zero-overhead transition to the
1047 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001048 including its unpredictable switch-case branch. Combined with the
1049 processor's internal branch prediction, a successful PREDICT has the
1050 effect of making the two opcodes run as if they were a single new opcode
1051 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001052
Georg Brandl86b2fb92008-07-16 03:43:04 +00001053 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 predictions turned-on and interpret the results as if some opcodes
1055 had been combined or turn-off predictions so that the opcode frequency
1056 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001057
1058 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 the CPU to record separate branch prediction information for each
1060 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001061
Raymond Hettingerf606f872003-03-16 03:11:04 +00001062*/
1063
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001064#define PREDICT_ID(op) PRED_##op
1065
Antoine Pitrou042b1282010-08-13 21:15:58 +00001066#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001067#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001068#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001069#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001070 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001071 _Py_CODEUNIT word = *next_instr; \
1072 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001073 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001074 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001075 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001076 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001077 } \
1078 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001079#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001080#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001081
Raymond Hettingerf606f872003-03-16 03:11:04 +00001082
Guido van Rossum374a9221991-04-04 10:40:29 +00001083/* Stack manipulation macros */
1084
Martin v. Löwis18e16552006-02-15 17:27:45 +00001085/* The stack can grow at most MAXINT deep, as co_nlocals and
1086 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001087#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1088#define EMPTY() (STACK_LEVEL() == 0)
1089#define TOP() (stack_pointer[-1])
1090#define SECOND() (stack_pointer[-2])
1091#define THIRD() (stack_pointer[-3])
1092#define FOURTH() (stack_pointer[-4])
1093#define PEEK(n) (stack_pointer[-(n)])
1094#define SET_TOP(v) (stack_pointer[-1] = (v))
1095#define SET_SECOND(v) (stack_pointer[-2] = (v))
1096#define SET_THIRD(v) (stack_pointer[-3] = (v))
1097#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1098#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1099#define BASIC_STACKADJ(n) (stack_pointer += n)
1100#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1101#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001102
Guido van Rossum96a42c81992-01-12 02:29:51 +00001103#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001105 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001106 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001107#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001108 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001109#define STACK_GROW(n) do { \
1110 assert(n >= 0); \
1111 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001112 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001113 assert(STACK_LEVEL() <= co->co_stacksize); \
1114 } while (0)
1115#define STACK_SHRINK(n) do { \
1116 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001117 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001118 (void)(BASIC_STACKADJ(-n)); \
1119 assert(STACK_LEVEL() <= co->co_stacksize); \
1120 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001121#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001122 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001123 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001124#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001125#define PUSH(v) BASIC_PUSH(v)
1126#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001127#define STACK_GROW(n) BASIC_STACKADJ(n)
1128#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001129#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001130#endif
1131
Guido van Rossum681d79a1995-07-18 14:51:37 +00001132/* Local variable macros */
1133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001135
1136/* The SETLOCAL() macro must not DECREF the local variable in-place and
1137 then store the new value; it must copy the old value to a temporary
1138 value, then store the new value, and then DECREF the temporary value.
1139 This is because it is possible that during the DECREF the frame is
1140 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1141 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001143 GETLOCAL(i) = value; \
1144 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001145
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001146
1147#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 while (STACK_LEVEL() > (b)->b_level) { \
1149 PyObject *v = POP(); \
1150 Py_XDECREF(v); \
1151 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001152
1153#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001154 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001156 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1158 while (STACK_LEVEL() > (b)->b_level + 3) { \
1159 value = POP(); \
1160 Py_XDECREF(value); \
1161 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001162 exc_info = tstate->exc_info; \
1163 type = exc_info->exc_type; \
1164 value = exc_info->exc_value; \
1165 traceback = exc_info->exc_traceback; \
1166 exc_info->exc_type = POP(); \
1167 exc_info->exc_value = POP(); \
1168 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 Py_XDECREF(type); \
1170 Py_XDECREF(value); \
1171 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001172 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001173
Inada Naoki91234a12019-06-03 21:30:58 +09001174 /* macros for opcode cache */
1175#define OPCACHE_CHECK() \
1176 do { \
1177 co_opcache = NULL; \
1178 if (co->co_opcache != NULL) { \
1179 unsigned char co_opt_offset = \
1180 co->co_opcache_map[next_instr - first_instr]; \
1181 if (co_opt_offset > 0) { \
1182 assert(co_opt_offset <= co->co_opcache_size); \
1183 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1184 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001185 } \
1186 } \
1187 } while (0)
1188
1189#if OPCACHE_STATS
1190
1191#define OPCACHE_STAT_GLOBAL_HIT() \
1192 do { \
1193 if (co->co_opcache != NULL) opcache_global_hits++; \
1194 } while (0)
1195
1196#define OPCACHE_STAT_GLOBAL_MISS() \
1197 do { \
1198 if (co->co_opcache != NULL) opcache_global_misses++; \
1199 } while (0)
1200
1201#define OPCACHE_STAT_GLOBAL_OPT() \
1202 do { \
1203 if (co->co_opcache != NULL) opcache_global_opts++; \
1204 } while (0)
1205
1206#else /* OPCACHE_STATS */
1207
1208#define OPCACHE_STAT_GLOBAL_HIT()
1209#define OPCACHE_STAT_GLOBAL_MISS()
1210#define OPCACHE_STAT_GLOBAL_OPT()
1211
1212#endif
1213
Guido van Rossuma027efa1997-05-05 20:56:21 +00001214/* Start of code */
1215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001217 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001219 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (tstate->use_tracing) {
1224 if (tstate->c_tracefunc != NULL) {
1225 /* tstate->c_tracefunc, if defined, is a
1226 function that will be called on *every* entry
1227 to a code block. Its return value, if not
1228 None, is a function that will be called at
1229 the start of each executed line of code.
1230 (Actually, the function must return itself
1231 in order to continue tracing.) The trace
1232 functions are called with three arguments:
1233 a pointer to the current frame, a string
1234 indicating why the function is called, and
1235 an argument which depends on the situation.
1236 The global trace function is also called
1237 whenever an exception is detected. */
1238 if (call_trace_protected(tstate->c_tracefunc,
1239 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001240 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* Trace function raised an error */
1242 goto exit_eval_frame;
1243 }
1244 }
1245 if (tstate->c_profilefunc != NULL) {
1246 /* Similar for c_profilefunc, except it needn't
1247 return itself and isn't called for "line" events */
1248 if (call_trace_protected(tstate->c_profilefunc,
1249 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001250 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 /* Profile function raised an error */
1252 goto exit_eval_frame;
1253 }
1254 }
1255 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001256
Łukasz Langaa785c872016-09-09 17:37:37 -07001257 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1258 dtrace_function_entry(f);
1259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 co = f->f_code;
1261 names = co->co_names;
1262 consts = co->co_consts;
1263 fastlocals = f->f_localsplus;
1264 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001265 assert(PyBytes_Check(co->co_code));
1266 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001267 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1268 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1269 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001270 /*
1271 f->f_lasti refers to the index of the last instruction,
1272 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001273
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001274 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001275 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 When the PREDICT() macros are enabled, some opcode pairs follow in
1278 direct succession without updating f->f_lasti. A successful
1279 prediction effectively links the two codes together as if they
1280 were a single new opcode; accordingly,f->f_lasti will point to
1281 the first code in the pair (for instance, GET_ITER followed by
1282 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001283 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001285 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001286 next_instr = first_instr;
1287 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001288 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1289 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 stack_pointer = f->f_stacktop;
1292 assert(stack_pointer != NULL);
1293 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001294 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001295
Inada Naoki91234a12019-06-03 21:30:58 +09001296 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1297 co->co_opcache_flag++;
1298 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1299 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001300 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001301 }
1302#if OPCACHE_STATS
1303 opcache_code_objects_extra_mem +=
1304 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1305 sizeof(_PyOpcache) * co->co_opcache_size;
1306 opcache_code_objects++;
1307#endif
1308 }
1309 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001310
Tim Peters5ca576e2001-06-18 22:08:13 +00001311#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001312 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001313#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001314
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001315 if (throwflag) /* support for generator.throw() */
1316 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317
Victor Stinnerace47d72013-07-18 01:41:08 +02001318#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001319 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001320 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001321 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001322 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001323#endif
1324
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001325main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1328 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001329 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* Do periodic things. Doing this every time through
1332 the loop would add too much overhead, so we do it
1333 only every Nth instruction. We also do it if
1334 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1335 event needs attention (e.g. a signal handler or
1336 async I/O handler); see Py_AddPendingCall() and
1337 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Eric Snow7bda9de2019-03-08 17:25:54 -07001339 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001340 opcode = _Py_OPCODE(*next_instr);
1341 if (opcode == SETUP_FINALLY ||
1342 opcode == SETUP_WITH ||
1343 opcode == BEFORE_ASYNC_WITH ||
1344 opcode == YIELD_FROM) {
1345 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001346 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001347 - If we're about to enter the 'with:'. It will prevent
1348 emitting a resource warning in the common idiom
1349 'with open(path) as file:'.
1350 - If we're about to enter the 'async with:'.
1351 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001352 *very* useful, but might help in some cases and it's
1353 traditional)
1354 - If we're resuming a chain of nested 'yield from' or
1355 'await' calls, then each frame is parked with YIELD_FROM
1356 as its next opcode. If the user hit control-C we want to
1357 wait until we've reached the innermost frame before
1358 running the signal handler and raising KeyboardInterrupt
1359 (see bpo-30039).
1360 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 goto fast_next_opcode;
1362 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001363
Victor Stinnerda2914d2020-03-20 09:29:08 +01001364 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 }
1367 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 fast_next_opcode:
1370 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Łukasz Langaa785c872016-09-09 17:37:37 -07001372 if (PyDTrace_LINE_ENABLED())
1373 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001376
Victor Stinnerdab84232020-03-17 18:56:44 +01001377 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001378 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* see maybe_call_line_trace
1381 for expository comments */
1382 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 err = maybe_call_line_trace(tstate->c_tracefunc,
1385 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001386 tstate, f,
1387 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* Reload possibly changed frame fields */
1389 JUMPTO(f->f_lasti);
1390 if (f->f_stacktop != NULL) {
1391 stack_pointer = f->f_stacktop;
1392 f->f_stacktop = NULL;
1393 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001400
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001401 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001402 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001403#ifdef DYNAMIC_EXECUTION_PROFILE
1404#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 dxpairs[lastopcode][opcode]++;
1406 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001407#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001409#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001410
Guido van Rossum96a42c81992-01-12 02:29:51 +00001411#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (lltrace) {
1415 if (HAS_ARG(opcode)) {
1416 printf("%d: %d, %d\n",
1417 f->f_lasti, opcode, oparg);
1418 }
1419 else {
1420 printf("%d: %d\n",
1421 f->f_lasti, opcode);
1422 }
1423 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001424#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001429 It is essential that any operation that fails must goto error
1430 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Benjamin Petersonddd19492018-09-16 22:38:02 -07001432 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001435
Benjamin Petersonddd19492018-09-16 22:38:02 -07001436 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 PyObject *value = GETLOCAL(oparg);
1438 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001439 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001440 UNBOUNDLOCAL_ERROR_MSG,
1441 PyTuple_GetItem(co->co_varnames, oparg));
1442 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 Py_INCREF(value);
1445 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 }
1448
Benjamin Petersonddd19492018-09-16 22:38:02 -07001449 case TARGET(LOAD_CONST): {
1450 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 PyObject *value = GETITEM(consts, oparg);
1452 Py_INCREF(value);
1453 PUSH(value);
1454 FAST_DISPATCH();
1455 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(STORE_FAST): {
1458 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *value = POP();
1460 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *value = POP();
1466 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *top = TOP();
1472 PyObject *second = SECOND();
1473 SET_TOP(second);
1474 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Benjamin Petersonddd19492018-09-16 22:38:02 -07001478 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001479 PyObject *top = TOP();
1480 PyObject *second = SECOND();
1481 PyObject *third = THIRD();
1482 SET_TOP(second);
1483 SET_SECOND(third);
1484 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001489 PyObject *top = TOP();
1490 PyObject *second = SECOND();
1491 PyObject *third = THIRD();
1492 PyObject *fourth = FOURTH();
1493 SET_TOP(second);
1494 SET_SECOND(third);
1495 SET_THIRD(fourth);
1496 SET_FOURTH(top);
1497 FAST_DISPATCH();
1498 }
1499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *top = TOP();
1502 Py_INCREF(top);
1503 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Petersonddd19492018-09-16 22:38:02 -07001507 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001510 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001512 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 SET_TOP(top);
1514 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001515 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *value = TOP();
1520 PyObject *res = PyNumber_Positive(value);
1521 Py_DECREF(value);
1522 SET_TOP(res);
1523 if (res == NULL)
1524 goto error;
1525 DISPATCH();
1526 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *value = TOP();
1530 PyObject *res = PyNumber_Negative(value);
1531 Py_DECREF(value);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *value = TOP();
1540 int err = PyObject_IsTrue(value);
1541 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (err == 0) {
1543 Py_INCREF(Py_True);
1544 SET_TOP(Py_True);
1545 DISPATCH();
1546 }
1547 else if (err > 0) {
1548 Py_INCREF(Py_False);
1549 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 DISPATCH();
1551 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001552 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001553 goto error;
1554 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Benjamin Petersonddd19492018-09-16 22:38:02 -07001556 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 PyObject *value = TOP();
1558 PyObject *res = PyNumber_Invert(value);
1559 Py_DECREF(value);
1560 SET_TOP(res);
1561 if (res == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *exp = POP();
1568 PyObject *base = TOP();
1569 PyObject *res = PyNumber_Power(base, exp, Py_None);
1570 Py_DECREF(base);
1571 Py_DECREF(exp);
1572 SET_TOP(res);
1573 if (res == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *right = POP();
1580 PyObject *left = TOP();
1581 PyObject *res = PyNumber_Multiply(left, right);
1582 Py_DECREF(left);
1583 Py_DECREF(right);
1584 SET_TOP(res);
1585 if (res == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *res = PyNumber_MatrixMultiply(left, right);
1594 Py_DECREF(left);
1595 Py_DECREF(right);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
1601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *divisor = POP();
1604 PyObject *dividend = TOP();
1605 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1606 Py_DECREF(dividend);
1607 Py_DECREF(divisor);
1608 SET_TOP(quotient);
1609 if (quotient == NULL)
1610 goto error;
1611 DISPATCH();
1612 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 PyObject *divisor = POP();
1616 PyObject *dividend = TOP();
1617 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1618 Py_DECREF(dividend);
1619 Py_DECREF(divisor);
1620 SET_TOP(quotient);
1621 if (quotient == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001625
Benjamin Petersonddd19492018-09-16 22:38:02 -07001626 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 PyObject *divisor = POP();
1628 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001629 PyObject *res;
1630 if (PyUnicode_CheckExact(dividend) && (
1631 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1632 // fast path; string formatting, but not if the RHS is a str subclass
1633 // (see issue28598)
1634 res = PyUnicode_Format(dividend, divisor);
1635 } else {
1636 res = PyNumber_Remainder(dividend, divisor);
1637 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 Py_DECREF(divisor);
1639 Py_DECREF(dividend);
1640 SET_TOP(res);
1641 if (res == NULL)
1642 goto error;
1643 DISPATCH();
1644 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001645
Benjamin Petersonddd19492018-09-16 22:38:02 -07001646 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 PyObject *right = POP();
1648 PyObject *left = TOP();
1649 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001650 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1651 CPython using bytecode, it is simply worthless.
1652 See http://bugs.python.org/issue21955 and
1653 http://bugs.python.org/issue10044 for the discussion. In short,
1654 no patch shown any impact on a realistic benchmark, only a minor
1655 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 if (PyUnicode_CheckExact(left) &&
1657 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001658 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001659 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001660 }
1661 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 sum = PyNumber_Add(left, right);
1663 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 Py_DECREF(right);
1666 SET_TOP(sum);
1667 if (sum == NULL)
1668 goto error;
1669 DISPATCH();
1670 }
1671
Benjamin Petersonddd19492018-09-16 22:38:02 -07001672 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 PyObject *right = POP();
1674 PyObject *left = TOP();
1675 PyObject *diff = PyNumber_Subtract(left, right);
1676 Py_DECREF(right);
1677 Py_DECREF(left);
1678 SET_TOP(diff);
1679 if (diff == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
1683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *sub = POP();
1686 PyObject *container = TOP();
1687 PyObject *res = PyObject_GetItem(container, sub);
1688 Py_DECREF(container);
1689 Py_DECREF(sub);
1690 SET_TOP(res);
1691 if (res == NULL)
1692 goto error;
1693 DISPATCH();
1694 }
1695
Benjamin Petersonddd19492018-09-16 22:38:02 -07001696 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 PyObject *right = POP();
1698 PyObject *left = TOP();
1699 PyObject *res = PyNumber_Lshift(left, right);
1700 Py_DECREF(left);
1701 Py_DECREF(right);
1702 SET_TOP(res);
1703 if (res == NULL)
1704 goto error;
1705 DISPATCH();
1706 }
1707
Benjamin Petersonddd19492018-09-16 22:38:02 -07001708 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 PyObject *right = POP();
1710 PyObject *left = TOP();
1711 PyObject *res = PyNumber_Rshift(left, right);
1712 Py_DECREF(left);
1713 Py_DECREF(right);
1714 SET_TOP(res);
1715 if (res == NULL)
1716 goto error;
1717 DISPATCH();
1718 }
1719
Benjamin Petersonddd19492018-09-16 22:38:02 -07001720 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 PyObject *right = POP();
1722 PyObject *left = TOP();
1723 PyObject *res = PyNumber_And(left, right);
1724 Py_DECREF(left);
1725 Py_DECREF(right);
1726 SET_TOP(res);
1727 if (res == NULL)
1728 goto error;
1729 DISPATCH();
1730 }
1731
Benjamin Petersonddd19492018-09-16 22:38:02 -07001732 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 PyObject *right = POP();
1734 PyObject *left = TOP();
1735 PyObject *res = PyNumber_Xor(left, right);
1736 Py_DECREF(left);
1737 Py_DECREF(right);
1738 SET_TOP(res);
1739 if (res == NULL)
1740 goto error;
1741 DISPATCH();
1742 }
1743
Benjamin Petersonddd19492018-09-16 22:38:02 -07001744 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 PyObject *right = POP();
1746 PyObject *left = TOP();
1747 PyObject *res = PyNumber_Or(left, right);
1748 Py_DECREF(left);
1749 Py_DECREF(right);
1750 SET_TOP(res);
1751 if (res == NULL)
1752 goto error;
1753 DISPATCH();
1754 }
1755
Benjamin Petersonddd19492018-09-16 22:38:02 -07001756 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 PyObject *v = POP();
1758 PyObject *list = PEEK(oparg);
1759 int err;
1760 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 if (err != 0)
1763 goto error;
1764 PREDICT(JUMP_ABSOLUTE);
1765 DISPATCH();
1766 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001767
Benjamin Petersonddd19492018-09-16 22:38:02 -07001768 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001770 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 int err;
1772 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 if (err != 0)
1775 goto error;
1776 PREDICT(JUMP_ABSOLUTE);
1777 DISPATCH();
1778 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Benjamin Petersonddd19492018-09-16 22:38:02 -07001780 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 PyObject *exp = POP();
1782 PyObject *base = TOP();
1783 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1784 Py_DECREF(base);
1785 Py_DECREF(exp);
1786 SET_TOP(res);
1787 if (res == NULL)
1788 goto error;
1789 DISPATCH();
1790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Benjamin Petersonddd19492018-09-16 22:38:02 -07001792 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001793 PyObject *right = POP();
1794 PyObject *left = TOP();
1795 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1796 Py_DECREF(left);
1797 Py_DECREF(right);
1798 SET_TOP(res);
1799 if (res == NULL)
1800 goto error;
1801 DISPATCH();
1802 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001803
Benjamin Petersonddd19492018-09-16 22:38:02 -07001804 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001805 PyObject *right = POP();
1806 PyObject *left = TOP();
1807 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1808 Py_DECREF(left);
1809 Py_DECREF(right);
1810 SET_TOP(res);
1811 if (res == NULL)
1812 goto error;
1813 DISPATCH();
1814 }
1815
Benjamin Petersonddd19492018-09-16 22:38:02 -07001816 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 PyObject *divisor = POP();
1818 PyObject *dividend = TOP();
1819 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1820 Py_DECREF(dividend);
1821 Py_DECREF(divisor);
1822 SET_TOP(quotient);
1823 if (quotient == NULL)
1824 goto error;
1825 DISPATCH();
1826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Benjamin Petersonddd19492018-09-16 22:38:02 -07001828 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001829 PyObject *divisor = POP();
1830 PyObject *dividend = TOP();
1831 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1832 Py_DECREF(dividend);
1833 Py_DECREF(divisor);
1834 SET_TOP(quotient);
1835 if (quotient == NULL)
1836 goto error;
1837 DISPATCH();
1838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Benjamin Petersonddd19492018-09-16 22:38:02 -07001840 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 PyObject *right = POP();
1842 PyObject *left = TOP();
1843 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1844 Py_DECREF(left);
1845 Py_DECREF(right);
1846 SET_TOP(mod);
1847 if (mod == NULL)
1848 goto error;
1849 DISPATCH();
1850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *right = POP();
1854 PyObject *left = TOP();
1855 PyObject *sum;
1856 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001857 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001858 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001859 }
1860 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 sum = PyNumber_InPlaceAdd(left, right);
1862 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001863 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 Py_DECREF(right);
1865 SET_TOP(sum);
1866 if (sum == NULL)
1867 goto error;
1868 DISPATCH();
1869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 PyObject *right = POP();
1873 PyObject *left = TOP();
1874 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1875 Py_DECREF(left);
1876 Py_DECREF(right);
1877 SET_TOP(diff);
1878 if (diff == NULL)
1879 goto error;
1880 DISPATCH();
1881 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Benjamin Petersonddd19492018-09-16 22:38:02 -07001883 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *right = POP();
1885 PyObject *left = TOP();
1886 PyObject *res = PyNumber_InPlaceLshift(left, right);
1887 Py_DECREF(left);
1888 Py_DECREF(right);
1889 SET_TOP(res);
1890 if (res == NULL)
1891 goto error;
1892 DISPATCH();
1893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Benjamin Petersonddd19492018-09-16 22:38:02 -07001895 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 PyObject *right = POP();
1897 PyObject *left = TOP();
1898 PyObject *res = PyNumber_InPlaceRshift(left, right);
1899 Py_DECREF(left);
1900 Py_DECREF(right);
1901 SET_TOP(res);
1902 if (res == NULL)
1903 goto error;
1904 DISPATCH();
1905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 PyObject *right = POP();
1909 PyObject *left = TOP();
1910 PyObject *res = PyNumber_InPlaceAnd(left, right);
1911 Py_DECREF(left);
1912 Py_DECREF(right);
1913 SET_TOP(res);
1914 if (res == NULL)
1915 goto error;
1916 DISPATCH();
1917 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 PyObject *right = POP();
1921 PyObject *left = TOP();
1922 PyObject *res = PyNumber_InPlaceXor(left, right);
1923 Py_DECREF(left);
1924 Py_DECREF(right);
1925 SET_TOP(res);
1926 if (res == NULL)
1927 goto error;
1928 DISPATCH();
1929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Benjamin Petersonddd19492018-09-16 22:38:02 -07001931 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 PyObject *right = POP();
1933 PyObject *left = TOP();
1934 PyObject *res = PyNumber_InPlaceOr(left, right);
1935 Py_DECREF(left);
1936 Py_DECREF(right);
1937 SET_TOP(res);
1938 if (res == NULL)
1939 goto error;
1940 DISPATCH();
1941 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001942
Benjamin Petersonddd19492018-09-16 22:38:02 -07001943 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 PyObject *sub = TOP();
1945 PyObject *container = SECOND();
1946 PyObject *v = THIRD();
1947 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001948 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001949 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 Py_DECREF(container);
1953 Py_DECREF(sub);
1954 if (err != 0)
1955 goto error;
1956 DISPATCH();
1957 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Benjamin Petersonddd19492018-09-16 22:38:02 -07001959 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PyObject *sub = TOP();
1961 PyObject *container = SECOND();
1962 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001963 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001964 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 err = PyObject_DelItem(container, sub);
1966 Py_DECREF(container);
1967 Py_DECREF(sub);
1968 if (err != 0)
1969 goto error;
1970 DISPATCH();
1971 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001972
Benjamin Petersonddd19492018-09-16 22:38:02 -07001973 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001974 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001976 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001977 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001979 _PyErr_SetString(tstate, PyExc_RuntimeError,
1980 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 Py_DECREF(value);
1982 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001984 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 Py_DECREF(value);
1986 if (res == NULL)
1987 goto error;
1988 Py_DECREF(res);
1989 DISPATCH();
1990 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001991
Benjamin Petersonddd19492018-09-16 22:38:02 -07001992 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 switch (oparg) {
1995 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001997 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002000 /* fall through */
2001 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002002 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002003 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 break;
2006 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002007 _PyErr_SetString(tstate, PyExc_SystemError,
2008 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 break;
2010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 goto error;
2012 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002016 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002017 assert(EMPTY());
2018 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002020
Benjamin Petersonddd19492018-09-16 22:38:02 -07002021 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002022 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002023 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002024 PyObject *obj = TOP();
2025 PyTypeObject *type = Py_TYPE(obj);
2026
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002027 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002028 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002029 }
Yury Selivanov75445082015-05-11 22:57:16 -04002030
2031 if (getter != NULL) {
2032 iter = (*getter)(obj);
2033 Py_DECREF(obj);
2034 if (iter == NULL) {
2035 SET_TOP(NULL);
2036 goto error;
2037 }
2038 }
2039 else {
2040 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002041 _PyErr_Format(tstate, PyExc_TypeError,
2042 "'async for' requires an object with "
2043 "__aiter__ method, got %.100s",
2044 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002045 Py_DECREF(obj);
2046 goto error;
2047 }
2048
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002049 if (Py_TYPE(iter)->tp_as_async == NULL ||
2050 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002051
Yury Selivanov398ff912017-03-02 22:20:00 -05002052 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002053 _PyErr_Format(tstate, PyExc_TypeError,
2054 "'async for' received an object from __aiter__ "
2055 "that does not implement __anext__: %.100s",
2056 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002057 Py_DECREF(iter);
2058 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002059 }
2060
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002061 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002062 DISPATCH();
2063 }
2064
Benjamin Petersonddd19492018-09-16 22:38:02 -07002065 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002066 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002067 PyObject *next_iter = NULL;
2068 PyObject *awaitable = NULL;
2069 PyObject *aiter = TOP();
2070 PyTypeObject *type = Py_TYPE(aiter);
2071
Yury Selivanoveb636452016-09-08 22:01:51 -07002072 if (PyAsyncGen_CheckExact(aiter)) {
2073 awaitable = type->tp_as_async->am_anext(aiter);
2074 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002075 goto error;
2076 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002077 } else {
2078 if (type->tp_as_async != NULL){
2079 getter = type->tp_as_async->am_anext;
2080 }
Yury Selivanov75445082015-05-11 22:57:16 -04002081
Yury Selivanoveb636452016-09-08 22:01:51 -07002082 if (getter != NULL) {
2083 next_iter = (*getter)(aiter);
2084 if (next_iter == NULL) {
2085 goto error;
2086 }
2087 }
2088 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002089 _PyErr_Format(tstate, PyExc_TypeError,
2090 "'async for' requires an iterator with "
2091 "__anext__ method, got %.100s",
2092 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002093 goto error;
2094 }
Yury Selivanov75445082015-05-11 22:57:16 -04002095
Yury Selivanoveb636452016-09-08 22:01:51 -07002096 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2097 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002098 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002099 PyExc_TypeError,
2100 "'async for' received an invalid object "
2101 "from __anext__: %.100s",
2102 Py_TYPE(next_iter)->tp_name);
2103
2104 Py_DECREF(next_iter);
2105 goto error;
2106 } else {
2107 Py_DECREF(next_iter);
2108 }
2109 }
Yury Selivanov75445082015-05-11 22:57:16 -04002110
2111 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002112 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002113 DISPATCH();
2114 }
2115
Benjamin Petersonddd19492018-09-16 22:38:02 -07002116 case TARGET(GET_AWAITABLE): {
2117 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002118 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002119 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002120
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002121 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002122 int opcode_at_minus_3 = 0;
2123 if ((next_instr - first_instr) > 2) {
2124 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2125 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002126 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002127 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002128 _Py_OPCODE(next_instr[-2]));
2129 }
2130
Yury Selivanov75445082015-05-11 22:57:16 -04002131 Py_DECREF(iterable);
2132
Yury Selivanovc724bae2016-03-02 11:30:46 -05002133 if (iter != NULL && PyCoro_CheckExact(iter)) {
2134 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2135 if (yf != NULL) {
2136 /* `iter` is a coroutine object that is being
2137 awaited, `yf` is a pointer to the current awaitable
2138 being awaited on. */
2139 Py_DECREF(yf);
2140 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002141 _PyErr_SetString(tstate, PyExc_RuntimeError,
2142 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002143 /* The code below jumps to `error` if `iter` is NULL. */
2144 }
2145 }
2146
Yury Selivanov75445082015-05-11 22:57:16 -04002147 SET_TOP(iter); /* Even if it's NULL */
2148
2149 if (iter == NULL) {
2150 goto error;
2151 }
2152
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002153 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002154 DISPATCH();
2155 }
2156
Benjamin Petersonddd19492018-09-16 22:38:02 -07002157 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002159 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002161 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2162 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002163 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002164 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002166 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002167 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002168 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 Py_DECREF(v);
2171 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002172 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002173 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002174 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002175 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002176 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 if (err < 0)
2178 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002179 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 SET_TOP(val);
2181 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002182 }
Martin Panter95f53c12016-07-18 08:23:26 +00002183 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002184 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002185 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002186 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002187 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002188 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002190
Benjamin Petersonddd19492018-09-16 22:38:02 -07002191 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002193
2194 if (co->co_flags & CO_ASYNC_GENERATOR) {
2195 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2196 Py_DECREF(retval);
2197 if (w == NULL) {
2198 retval = NULL;
2199 goto error;
2200 }
2201 retval = w;
2202 }
2203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002205 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002207
Benjamin Petersonddd19492018-09-16 22:38:02 -07002208 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002209 PyObject *type, *value, *traceback;
2210 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 PyTryBlock *b = PyFrame_BlockPop(f);
2212 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002213 _PyErr_SetString(tstate, PyExc_SystemError,
2214 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002217 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2218 STACK_LEVEL() <= (b)->b_level + 4);
2219 exc_info = tstate->exc_info;
2220 type = exc_info->exc_type;
2221 value = exc_info->exc_value;
2222 traceback = exc_info->exc_traceback;
2223 exc_info->exc_type = POP();
2224 exc_info->exc_value = POP();
2225 exc_info->exc_traceback = POP();
2226 Py_XDECREF(type);
2227 Py_XDECREF(value);
2228 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002231
Benjamin Petersonddd19492018-09-16 22:38:02 -07002232 case TARGET(POP_BLOCK): {
2233 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002234 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Mark Shannonfee55262019-11-21 09:11:43 +00002238 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002239 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002240 PyObject *val = POP();
2241 PyObject *tb = POP();
2242 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002243 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002244 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002246
Benjamin Petersonddd19492018-09-16 22:38:02 -07002247 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002248 PyObject *exc = POP();
2249 assert(PyExceptionClass_Check(exc));
2250 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2251 PyTryBlock *b = PyFrame_BlockPop(f);
2252 assert(b->b_type == EXCEPT_HANDLER);
2253 Py_DECREF(exc);
2254 UNWIND_EXCEPT_HANDLER(b);
2255 Py_DECREF(POP());
2256 JUMPBY(oparg);
2257 FAST_DISPATCH();
2258 }
2259 else {
2260 PyObject *val = POP();
2261 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002262 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002263 goto exception_unwind;
2264 }
2265 }
2266
Zackery Spytzce6a0702019-08-25 03:44:09 -06002267 case TARGET(LOAD_ASSERTION_ERROR): {
2268 PyObject *value = PyExc_AssertionError;
2269 Py_INCREF(value);
2270 PUSH(value);
2271 FAST_DISPATCH();
2272 }
2273
Benjamin Petersonddd19492018-09-16 22:38:02 -07002274 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002275 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002276
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002278 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002279 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002281 if (!_PyErr_Occurred(tstate)) {
2282 _PyErr_SetString(tstate, PyExc_NameError,
2283 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002284 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002288 }
2289 else {
2290 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2291 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002292 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2294 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002295 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2296 _PyErr_SetString(tstate, PyExc_NameError,
2297 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002302 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002303 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Benjamin Petersonddd19492018-09-16 22:38:02 -07002305 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 PyObject *name = GETITEM(names, oparg);
2307 PyObject *v = POP();
2308 PyObject *ns = f->f_locals;
2309 int err;
2310 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002311 _PyErr_Format(tstate, PyExc_SystemError,
2312 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 if (PyDict_CheckExact(ns))
2317 err = PyDict_SetItem(ns, name, v);
2318 else
2319 err = PyObject_SetItem(ns, name, v);
2320 Py_DECREF(v);
2321 if (err != 0)
2322 goto error;
2323 DISPATCH();
2324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Petersonddd19492018-09-16 22:38:02 -07002326 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 PyObject *name = GETITEM(names, oparg);
2328 PyObject *ns = f->f_locals;
2329 int err;
2330 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002331 _PyErr_Format(tstate, PyExc_SystemError,
2332 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 err = PyObject_DelItem(ns, name);
2336 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002337 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 NAME_ERROR_MSG,
2339 name);
2340 goto error;
2341 }
2342 DISPATCH();
2343 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002344
Benjamin Petersonddd19492018-09-16 22:38:02 -07002345 case TARGET(UNPACK_SEQUENCE): {
2346 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 PyObject *seq = POP(), *item, **items;
2348 if (PyTuple_CheckExact(seq) &&
2349 PyTuple_GET_SIZE(seq) == oparg) {
2350 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 item = items[oparg];
2353 Py_INCREF(item);
2354 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 } else if (PyList_CheckExact(seq) &&
2357 PyList_GET_SIZE(seq) == oparg) {
2358 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 item = items[oparg];
2361 Py_INCREF(item);
2362 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002364 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002366 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 } else {
2368 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 Py_DECREF(seq);
2370 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002373 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002375
Benjamin Petersonddd19492018-09-16 22:38:02 -07002376 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2378 PyObject *seq = POP();
2379
Victor Stinner438a12d2019-05-24 17:01:38 +02002380 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 stack_pointer + totalargs)) {
2382 stack_pointer += totalargs;
2383 } else {
2384 Py_DECREF(seq);
2385 goto error;
2386 }
2387 Py_DECREF(seq);
2388 DISPATCH();
2389 }
2390
Benjamin Petersonddd19492018-09-16 22:38:02 -07002391 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 PyObject *name = GETITEM(names, oparg);
2393 PyObject *owner = TOP();
2394 PyObject *v = SECOND();
2395 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002396 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 Py_DECREF(owner);
2400 if (err != 0)
2401 goto error;
2402 DISPATCH();
2403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Benjamin Petersonddd19492018-09-16 22:38:02 -07002405 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 PyObject *name = GETITEM(names, oparg);
2407 PyObject *owner = POP();
2408 int err;
2409 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2410 Py_DECREF(owner);
2411 if (err != 0)
2412 goto error;
2413 DISPATCH();
2414 }
2415
Benjamin Petersonddd19492018-09-16 22:38:02 -07002416 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 PyObject *name = GETITEM(names, oparg);
2418 PyObject *v = POP();
2419 int err;
2420 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 if (err != 0)
2423 goto error;
2424 DISPATCH();
2425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002426
Benjamin Petersonddd19492018-09-16 22:38:02 -07002427 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 PyObject *name = GETITEM(names, oparg);
2429 int err;
2430 err = PyDict_DelItem(f->f_globals, name);
2431 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002432 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2433 format_exc_check_arg(tstate, PyExc_NameError,
2434 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002435 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002437 }
2438 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002440
Benjamin Petersonddd19492018-09-16 22:38:02 -07002441 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 PyObject *name = GETITEM(names, oparg);
2443 PyObject *locals = f->f_locals;
2444 PyObject *v;
2445 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002446 _PyErr_Format(tstate, PyExc_SystemError,
2447 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002451 v = PyDict_GetItemWithError(locals, name);
2452 if (v != NULL) {
2453 Py_INCREF(v);
2454 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002455 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002456 goto error;
2457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 }
2459 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002461 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002462 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002463 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002464 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 }
2466 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002468 v = PyDict_GetItemWithError(f->f_globals, name);
2469 if (v != NULL) {
2470 Py_INCREF(v);
2471 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002473 goto error;
2474 }
2475 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002476 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002477 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002480 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002481 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002483 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002485 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002487 }
2488 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 v = PyObject_GetItem(f->f_builtins, name);
2490 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002491 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002492 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002497 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002504
Benjamin Petersonddd19492018-09-16 22:38:02 -07002505 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002506 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002508 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002509 && PyDict_CheckExact(f->f_builtins))
2510 {
Inada Naoki91234a12019-06-03 21:30:58 +09002511 OPCACHE_CHECK();
2512 if (co_opcache != NULL && co_opcache->optimized > 0) {
2513 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2514
2515 if (lg->globals_ver ==
2516 ((PyDictObject *)f->f_globals)->ma_version_tag
2517 && lg->builtins_ver ==
2518 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2519 {
2520 PyObject *ptr = lg->ptr;
2521 OPCACHE_STAT_GLOBAL_HIT();
2522 assert(ptr != NULL);
2523 Py_INCREF(ptr);
2524 PUSH(ptr);
2525 DISPATCH();
2526 }
2527 }
2528
2529 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002531 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 name);
2533 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002534 if (!_PyErr_OCCURRED()) {
2535 /* _PyDict_LoadGlobal() returns NULL without raising
2536 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002537 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002538 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002539 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 }
Inada Naoki91234a12019-06-03 21:30:58 +09002542
2543 if (co_opcache != NULL) {
2544 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2545
2546 if (co_opcache->optimized == 0) {
2547 /* Wasn't optimized before. */
2548 OPCACHE_STAT_GLOBAL_OPT();
2549 } else {
2550 OPCACHE_STAT_GLOBAL_MISS();
2551 }
2552
2553 co_opcache->optimized = 1;
2554 lg->globals_ver =
2555 ((PyDictObject *)f->f_globals)->ma_version_tag;
2556 lg->builtins_ver =
2557 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2558 lg->ptr = v; /* borrowed */
2559 }
2560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002563 else {
2564 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002565
2566 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002567 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 v = PyObject_GetItem(f->f_globals, name);
2569 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002570 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002571 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002572 }
2573 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002574
Victor Stinnerb4efc962015-11-20 09:24:02 +01002575 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 v = PyObject_GetItem(f->f_builtins, name);
2577 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002578 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002579 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002581 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002582 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002584 }
2585 }
2586 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002590
Benjamin Petersonddd19492018-09-16 22:38:02 -07002591 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 PyObject *v = GETLOCAL(oparg);
2593 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 SETLOCAL(oparg, NULL);
2595 DISPATCH();
2596 }
2597 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002598 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 UNBOUNDLOCAL_ERROR_MSG,
2600 PyTuple_GetItem(co->co_varnames, oparg)
2601 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 goto error;
2603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002604
Benjamin Petersonddd19492018-09-16 22:38:02 -07002605 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002607 PyObject *oldobj = PyCell_GET(cell);
2608 if (oldobj != NULL) {
2609 PyCell_SET(cell, NULL);
2610 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002611 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002612 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002613 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 goto error;
2615 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002616
Benjamin Petersonddd19492018-09-16 22:38:02 -07002617 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 PyObject *cell = freevars[oparg];
2619 Py_INCREF(cell);
2620 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002623
Benjamin Petersonddd19492018-09-16 22:38:02 -07002624 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002625 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002626 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002627 assert(locals);
2628 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2629 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2630 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2631 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2632 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002633 value = PyDict_GetItemWithError(locals, name);
2634 if (value != NULL) {
2635 Py_INCREF(value);
2636 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002637 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002638 goto error;
2639 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002640 }
2641 else {
2642 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002643 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002644 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002645 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002646 }
2647 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002648 }
2649 }
2650 if (!value) {
2651 PyObject *cell = freevars[oparg];
2652 value = PyCell_GET(cell);
2653 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002655 goto error;
2656 }
2657 Py_INCREF(value);
2658 }
2659 PUSH(value);
2660 DISPATCH();
2661 }
2662
Benjamin Petersonddd19492018-09-16 22:38:02 -07002663 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 PyObject *cell = freevars[oparg];
2665 PyObject *value = PyCell_GET(cell);
2666 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002667 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 Py_INCREF(value);
2671 PUSH(value);
2672 DISPATCH();
2673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002674
Benjamin Petersonddd19492018-09-16 22:38:02 -07002675 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 PyObject *v = POP();
2677 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002678 PyObject *oldobj = PyCell_GET(cell);
2679 PyCell_SET(cell, v);
2680 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 DISPATCH();
2682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002683
Benjamin Petersonddd19492018-09-16 22:38:02 -07002684 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002685 PyObject *str;
2686 PyObject *empty = PyUnicode_New(0, 0);
2687 if (empty == NULL) {
2688 goto error;
2689 }
2690 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2691 Py_DECREF(empty);
2692 if (str == NULL)
2693 goto error;
2694 while (--oparg >= 0) {
2695 PyObject *item = POP();
2696 Py_DECREF(item);
2697 }
2698 PUSH(str);
2699 DISPATCH();
2700 }
2701
Benjamin Petersonddd19492018-09-16 22:38:02 -07002702 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 PyObject *tup = PyTuple_New(oparg);
2704 if (tup == NULL)
2705 goto error;
2706 while (--oparg >= 0) {
2707 PyObject *item = POP();
2708 PyTuple_SET_ITEM(tup, oparg, item);
2709 }
2710 PUSH(tup);
2711 DISPATCH();
2712 }
2713
Benjamin Petersonddd19492018-09-16 22:38:02 -07002714 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 PyObject *list = PyList_New(oparg);
2716 if (list == NULL)
2717 goto error;
2718 while (--oparg >= 0) {
2719 PyObject *item = POP();
2720 PyList_SET_ITEM(list, oparg, item);
2721 }
2722 PUSH(list);
2723 DISPATCH();
2724 }
2725
Mark Shannon13bc1392020-01-23 09:25:17 +00002726 case TARGET(LIST_TO_TUPLE): {
2727 PyObject *list = POP();
2728 PyObject *tuple = PyList_AsTuple(list);
2729 Py_DECREF(list);
2730 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002732 }
2733 PUSH(tuple);
2734 DISPATCH();
2735 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736
Mark Shannon13bc1392020-01-23 09:25:17 +00002737 case TARGET(LIST_EXTEND): {
2738 PyObject *iterable = POP();
2739 PyObject *list = PEEK(oparg);
2740 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2741 if (none_val == NULL) {
2742 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002743 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002744 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002745 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002746 _PyErr_Format(tstate, PyExc_TypeError,
2747 "Value after * must be an iterable, not %.200s",
2748 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002749 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002750 Py_DECREF(iterable);
2751 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002753 Py_DECREF(none_val);
2754 Py_DECREF(iterable);
2755 DISPATCH();
2756 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757
Mark Shannon13bc1392020-01-23 09:25:17 +00002758 case TARGET(SET_UPDATE): {
2759 PyObject *iterable = POP();
2760 PyObject *set = PEEK(oparg);
2761 int err = _PySet_Update(set, iterable);
2762 Py_DECREF(iterable);
2763 if (err < 0) {
2764 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 DISPATCH();
2767 }
2768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 PyObject *set = PySet_New(NULL);
2771 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002772 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 if (set == NULL)
2774 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002775 for (i = oparg; i > 0; i--) {
2776 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 if (err == 0)
2778 err = PySet_Add(set, item);
2779 Py_DECREF(item);
2780 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002781 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 if (err != 0) {
2783 Py_DECREF(set);
2784 goto error;
2785 }
2786 PUSH(set);
2787 DISPATCH();
2788 }
2789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002791 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2793 if (map == NULL)
2794 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002795 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002797 PyObject *key = PEEK(2*i);
2798 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 if (err != 0) {
2801 Py_DECREF(map);
2802 goto error;
2803 }
2804 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002805
2806 while (oparg--) {
2807 Py_DECREF(POP());
2808 Py_DECREF(POP());
2809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 PUSH(map);
2811 DISPATCH();
2812 }
2813
Benjamin Petersonddd19492018-09-16 22:38:02 -07002814 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002815 _Py_IDENTIFIER(__annotations__);
2816 int err;
2817 PyObject *ann_dict;
2818 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002819 _PyErr_Format(tstate, PyExc_SystemError,
2820 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002821 goto error;
2822 }
2823 /* check if __annotations__ in locals()... */
2824 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002825 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002826 &PyId___annotations__);
2827 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002828 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002829 goto error;
2830 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002831 /* ...if not, create a new one */
2832 ann_dict = PyDict_New();
2833 if (ann_dict == NULL) {
2834 goto error;
2835 }
2836 err = _PyDict_SetItemId(f->f_locals,
2837 &PyId___annotations__, ann_dict);
2838 Py_DECREF(ann_dict);
2839 if (err != 0) {
2840 goto error;
2841 }
2842 }
2843 }
2844 else {
2845 /* do the same if locals() is not a dict */
2846 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2847 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002848 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002849 }
2850 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2851 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002852 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002853 goto error;
2854 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002855 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002856 ann_dict = PyDict_New();
2857 if (ann_dict == NULL) {
2858 goto error;
2859 }
2860 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2861 Py_DECREF(ann_dict);
2862 if (err != 0) {
2863 goto error;
2864 }
2865 }
2866 else {
2867 Py_DECREF(ann_dict);
2868 }
2869 }
2870 DISPATCH();
2871 }
2872
Benjamin Petersonddd19492018-09-16 22:38:02 -07002873 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002874 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002875 PyObject *map;
2876 PyObject *keys = TOP();
2877 if (!PyTuple_CheckExact(keys) ||
2878 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002879 _PyErr_SetString(tstate, PyExc_SystemError,
2880 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002881 goto error;
2882 }
2883 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2884 if (map == NULL) {
2885 goto error;
2886 }
2887 for (i = oparg; i > 0; i--) {
2888 int err;
2889 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2890 PyObject *value = PEEK(i + 1);
2891 err = PyDict_SetItem(map, key, value);
2892 if (err != 0) {
2893 Py_DECREF(map);
2894 goto error;
2895 }
2896 }
2897
2898 Py_DECREF(POP());
2899 while (oparg--) {
2900 Py_DECREF(POP());
2901 }
2902 PUSH(map);
2903 DISPATCH();
2904 }
2905
Mark Shannon8a4cd702020-01-27 09:57:45 +00002906 case TARGET(DICT_UPDATE): {
2907 PyObject *update = POP();
2908 PyObject *dict = PEEK(oparg);
2909 if (PyDict_Update(dict, update) < 0) {
2910 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2911 _PyErr_Format(tstate, PyExc_TypeError,
2912 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002913 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002914 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002915 Py_DECREF(update);
2916 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002917 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002918 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002919 DISPATCH();
2920 }
2921
Mark Shannon8a4cd702020-01-27 09:57:45 +00002922 case TARGET(DICT_MERGE): {
2923 PyObject *update = POP();
2924 PyObject *dict = PEEK(oparg);
2925
2926 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2927 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2928 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002929 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002930 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002931 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002932 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002933 DISPATCH();
2934 }
2935
Benjamin Petersonddd19492018-09-16 22:38:02 -07002936 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002937 PyObject *value = TOP();
2938 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 PyObject *map;
2940 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002941 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002942 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002944 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 Py_DECREF(value);
2946 Py_DECREF(key);
2947 if (err != 0)
2948 goto error;
2949 PREDICT(JUMP_ABSOLUTE);
2950 DISPATCH();
2951 }
2952
Benjamin Petersonddd19492018-09-16 22:38:02 -07002953 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PyObject *name = GETITEM(names, oparg);
2955 PyObject *owner = TOP();
2956 PyObject *res = PyObject_GetAttr(owner, name);
2957 Py_DECREF(owner);
2958 SET_TOP(res);
2959 if (res == NULL)
2960 goto error;
2961 DISPATCH();
2962 }
2963
Benjamin Petersonddd19492018-09-16 22:38:02 -07002964 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002965 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 PyObject *right = POP();
2967 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002968 PyObject *res = PyObject_RichCompare(left, right, oparg);
2969 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 Py_DECREF(left);
2971 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 if (res == NULL)
2973 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 PREDICT(POP_JUMP_IF_FALSE);
2975 PREDICT(POP_JUMP_IF_TRUE);
2976 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002977 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002978
Mark Shannon9af0e472020-01-14 10:12:45 +00002979 case TARGET(IS_OP): {
2980 PyObject *right = POP();
2981 PyObject *left = TOP();
2982 int res = (left == right)^oparg;
2983 PyObject *b = res ? Py_True : Py_False;
2984 Py_INCREF(b);
2985 SET_TOP(b);
2986 Py_DECREF(left);
2987 Py_DECREF(right);
2988 PREDICT(POP_JUMP_IF_FALSE);
2989 PREDICT(POP_JUMP_IF_TRUE);
2990 FAST_DISPATCH();
2991 }
2992
2993 case TARGET(CONTAINS_OP): {
2994 PyObject *right = POP();
2995 PyObject *left = POP();
2996 int res = PySequence_Contains(right, left);
2997 Py_DECREF(left);
2998 Py_DECREF(right);
2999 if (res < 0) {
3000 goto error;
3001 }
3002 PyObject *b = (res^oparg) ? Py_True : Py_False;
3003 Py_INCREF(b);
3004 PUSH(b);
3005 PREDICT(POP_JUMP_IF_FALSE);
3006 PREDICT(POP_JUMP_IF_TRUE);
3007 FAST_DISPATCH();
3008 }
3009
3010#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3011 "BaseException is not allowed"
3012
3013 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3014 PyObject *right = POP();
3015 PyObject *left = POP();
3016 if (PyTuple_Check(right)) {
3017 Py_ssize_t i, length;
3018 length = PyTuple_GET_SIZE(right);
3019 for (i = 0; i < length; i++) {
3020 PyObject *exc = PyTuple_GET_ITEM(right, i);
3021 if (!PyExceptionClass_Check(exc)) {
3022 _PyErr_SetString(tstate, PyExc_TypeError,
3023 CANNOT_CATCH_MSG);
3024 Py_DECREF(left);
3025 Py_DECREF(right);
3026 goto error;
3027 }
3028 }
3029 }
3030 else {
3031 if (!PyExceptionClass_Check(right)) {
3032 _PyErr_SetString(tstate, PyExc_TypeError,
3033 CANNOT_CATCH_MSG);
3034 Py_DECREF(left);
3035 Py_DECREF(right);
3036 goto error;
3037 }
3038 }
3039 int res = PyErr_GivenExceptionMatches(left, right);
3040 Py_DECREF(left);
3041 Py_DECREF(right);
3042 if (res > 0) {
3043 /* Exception matches -- Do nothing */;
3044 }
3045 else if (res == 0) {
3046 JUMPTO(oparg);
3047 }
3048 else {
3049 goto error;
3050 }
3051 DISPATCH();
3052 }
3053
Benjamin Petersonddd19492018-09-16 22:38:02 -07003054 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003056 PyObject *fromlist = POP();
3057 PyObject *level = TOP();
3058 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003059 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003060 Py_DECREF(level);
3061 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 SET_TOP(res);
3063 if (res == NULL)
3064 goto error;
3065 DISPATCH();
3066 }
3067
Benjamin Petersonddd19492018-09-16 22:38:02 -07003068 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 PyObject *from = POP(), *locals;
3070 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003071 if (PyFrame_FastToLocalsWithError(f) < 0) {
3072 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003073 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003074 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003075
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 locals = f->f_locals;
3077 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003078 _PyErr_SetString(tstate, PyExc_SystemError,
3079 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003080 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003083 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 Py_DECREF(from);
3086 if (err != 0)
3087 goto error;
3088 DISPATCH();
3089 }
Guido van Rossum25831651993-05-19 14:50:45 +00003090
Benjamin Petersonddd19492018-09-16 22:38:02 -07003091 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 PyObject *name = GETITEM(names, oparg);
3093 PyObject *from = TOP();
3094 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003095 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 PUSH(res);
3097 if (res == NULL)
3098 goto error;
3099 DISPATCH();
3100 }
Thomas Wouters52152252000-08-17 22:55:00 +00003101
Benjamin Petersonddd19492018-09-16 22:38:02 -07003102 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 JUMPBY(oparg);
3104 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003106
Benjamin Petersonddd19492018-09-16 22:38:02 -07003107 case TARGET(POP_JUMP_IF_FALSE): {
3108 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 PyObject *cond = POP();
3110 int err;
3111 if (cond == Py_True) {
3112 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 FAST_DISPATCH();
3114 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 if (cond == Py_False) {
3116 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 JUMPTO(oparg);
3118 FAST_DISPATCH();
3119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 err = PyObject_IsTrue(cond);
3121 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003123 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 else if (err == 0)
3125 JUMPTO(oparg);
3126 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(POP_JUMP_IF_TRUE): {
3132 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 PyObject *cond = POP();
3134 int err;
3135 if (cond == Py_False) {
3136 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 FAST_DISPATCH();
3138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 if (cond == Py_True) {
3140 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 JUMPTO(oparg);
3142 FAST_DISPATCH();
3143 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 err = PyObject_IsTrue(cond);
3145 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 JUMPTO(oparg);
3148 }
3149 else if (err == 0)
3150 ;
3151 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 PyObject *cond = TOP();
3158 int err;
3159 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003160 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 FAST_DISPATCH();
3163 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 JUMPTO(oparg);
3166 FAST_DISPATCH();
3167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003170 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 }
3173 else if (err == 0)
3174 JUMPTO(oparg);
3175 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003179
Benjamin Petersonddd19492018-09-16 22:38:02 -07003180 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 PyObject *cond = TOP();
3182 int err;
3183 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003184 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 FAST_DISPATCH();
3187 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 JUMPTO(oparg);
3190 FAST_DISPATCH();
3191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 JUMPTO(oparg);
3195 }
3196 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003197 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 }
3200 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(JUMP_ABSOLUTE): {
3206 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003208#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 /* Enabling this path speeds-up all while and for-loops by bypassing
3210 the per-loop checks for signals. By default, this should be turned-off
3211 because it prevents detection of a control-break in tight loops like
3212 "while 1: pass". Compile with this option turned-on when you need
3213 the speed-up and do not need break checking inside tight loops (ones
3214 that contain only instructions ending with FAST_DISPATCH).
3215 */
3216 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003217#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003219#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003221
Benjamin Petersonddd19492018-09-16 22:38:02 -07003222 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003225 PyObject *iter = PyObject_GetIter(iterable);
3226 Py_DECREF(iterable);
3227 SET_TOP(iter);
3228 if (iter == NULL)
3229 goto error;
3230 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003231 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003232 DISPATCH();
3233 }
3234
Benjamin Petersonddd19492018-09-16 22:38:02 -07003235 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003236 /* before: [obj]; after [getiter(obj)] */
3237 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003238 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003239 if (PyCoro_CheckExact(iterable)) {
3240 /* `iterable` is a coroutine */
3241 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3242 /* and it is used in a 'yield from' expression of a
3243 regular generator. */
3244 Py_DECREF(iterable);
3245 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003246 _PyErr_SetString(tstate, PyExc_TypeError,
3247 "cannot 'yield from' a coroutine object "
3248 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003249 goto error;
3250 }
3251 }
3252 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003253 /* `iterable` is not a generator. */
3254 iter = PyObject_GetIter(iterable);
3255 Py_DECREF(iterable);
3256 SET_TOP(iter);
3257 if (iter == NULL)
3258 goto error;
3259 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003260 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003261 DISPATCH();
3262 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003263
Benjamin Petersonddd19492018-09-16 22:38:02 -07003264 case TARGET(FOR_ITER): {
3265 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003268 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 if (next != NULL) {
3270 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 PREDICT(STORE_FAST);
3272 PREDICT(UNPACK_SEQUENCE);
3273 DISPATCH();
3274 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 if (_PyErr_Occurred(tstate)) {
3276 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003278 }
3279 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003280 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003281 }
3282 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 }
3284 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003285 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003288 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003291
Benjamin Petersonddd19492018-09-16 22:38:02 -07003292 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003293 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 STACK_LEVEL());
3295 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003299 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003300 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003301 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003302 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003303 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003304 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003305 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003306 }
3307 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3308 if (exit == NULL) {
3309 Py_DECREF(enter);
3310 goto error;
3311 }
Yury Selivanov75445082015-05-11 22:57:16 -04003312 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003313 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003314 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003315 Py_DECREF(enter);
3316 if (res == NULL)
3317 goto error;
3318 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003319 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003320 DISPATCH();
3321 }
3322
Benjamin Petersonddd19492018-09-16 22:38:02 -07003323 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003324 PyObject *res = POP();
3325 /* Setup the finally block before pushing the result
3326 of __aenter__ on the stack. */
3327 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3328 STACK_LEVEL());
3329 PUSH(res);
3330 DISPATCH();
3331 }
3332
Benjamin Petersonddd19492018-09-16 22:38:02 -07003333 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003334 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003335 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003336 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003337 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003339 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003340 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003341 }
3342 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003343 if (exit == NULL) {
3344 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003346 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003349 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 Py_DECREF(enter);
3351 if (res == NULL)
3352 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 /* Setup the finally block before pushing the result
3354 of __enter__ on the stack. */
3355 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3356 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003357
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 DISPATCH();
3360 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003361
Mark Shannonfee55262019-11-21 09:11:43 +00003362 case TARGET(WITH_EXCEPT_START): {
3363 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003365 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3366 - SEVENTH: the context.__exit__ bound method
3367 We call SEVENTH(TOP, SECOND, THIRD).
3368 Then we push again the TOP exception and the __exit__
3369 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003372 PyObject *exc, *val, *tb, *res;
3373
Victor Stinner842cfff2016-12-01 14:45:31 +01003374 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003375 val = SECOND();
3376 tb = THIRD();
3377 assert(exc != Py_None);
3378 assert(!PyLong_Check(exc));
3379 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003380 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003381 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003382 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 if (res == NULL)
3384 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003385
Yury Selivanov75445082015-05-11 22:57:16 -04003386 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003389
Benjamin Petersonddd19492018-09-16 22:38:02 -07003390 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003391 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003392 PyObject *name = GETITEM(names, oparg);
3393 PyObject *obj = TOP();
3394 PyObject *meth = NULL;
3395
3396 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3397
Yury Selivanovf2392132016-12-13 19:03:51 -05003398 if (meth == NULL) {
3399 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003400 goto error;
3401 }
3402
3403 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003404 /* We can bypass temporary bound method object.
3405 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003406
INADA Naoki015bce62017-01-16 17:23:30 +09003407 meth | self | arg1 | ... | argN
3408 */
3409 SET_TOP(meth);
3410 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003411 }
3412 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003413 /* meth is not an unbound method (but a regular attr, or
3414 something was returned by a descriptor protocol). Set
3415 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003416 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003417
3418 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003419 */
INADA Naoki015bce62017-01-16 17:23:30 +09003420 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003421 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003422 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003423 }
3424 DISPATCH();
3425 }
3426
Benjamin Petersonddd19492018-09-16 22:38:02 -07003427 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003428 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003429 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003430
3431 sp = stack_pointer;
3432
INADA Naoki015bce62017-01-16 17:23:30 +09003433 meth = PEEK(oparg + 2);
3434 if (meth == NULL) {
3435 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3436 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003437
3438 Stack layout:
3439
INADA Naoki015bce62017-01-16 17:23:30 +09003440 ... | NULL | callable | arg1 | ... | argN
3441 ^- TOP()
3442 ^- (-oparg)
3443 ^- (-oparg-1)
3444 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003445
Ville Skyttä49b27342017-08-03 09:00:59 +03003446 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003447 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003448 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003449 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003450 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003451 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003452 }
3453 else {
3454 /* This is a method call. Stack layout:
3455
INADA Naoki015bce62017-01-16 17:23:30 +09003456 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003457 ^- TOP()
3458 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003459 ^- (-oparg-1)
3460 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003461
INADA Naoki015bce62017-01-16 17:23:30 +09003462 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003463 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003464 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003465 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003466 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003467 stack_pointer = sp;
3468 }
3469
3470 PUSH(res);
3471 if (res == NULL)
3472 goto error;
3473 DISPATCH();
3474 }
3475
Benjamin Petersonddd19492018-09-16 22:38:02 -07003476 case TARGET(CALL_FUNCTION): {
3477 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003480 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003482 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003483 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003485 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003488
Benjamin Petersonddd19492018-09-16 22:38:02 -07003489 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003490 PyObject **sp, *res, *names;
3491
3492 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003493 assert(PyTuple_Check(names));
3494 assert(PyTuple_GET_SIZE(names) <= oparg);
3495 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003497 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003500 Py_DECREF(names);
3501
3502 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003503 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003504 }
3505 DISPATCH();
3506 }
3507
Benjamin Petersonddd19492018-09-16 22:38:02 -07003508 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003509 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003510 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003511 if (oparg & 0x01) {
3512 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003513 if (!PyDict_CheckExact(kwargs)) {
3514 PyObject *d = PyDict_New();
3515 if (d == NULL)
3516 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003517 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003518 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003519 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003520 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003521 goto error;
3522 }
3523 Py_DECREF(kwargs);
3524 kwargs = d;
3525 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003526 assert(PyDict_CheckExact(kwargs));
3527 }
3528 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003529 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003530 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003531 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003532 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003533 goto error;
3534 }
3535 Py_SETREF(callargs, PySequence_Tuple(callargs));
3536 if (callargs == NULL) {
3537 goto error;
3538 }
3539 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003540 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003541
Victor Stinner09532fe2019-05-10 23:39:09 +02003542 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003543 Py_DECREF(func);
3544 Py_DECREF(callargs);
3545 Py_XDECREF(kwargs);
3546
3547 SET_TOP(result);
3548 if (result == NULL) {
3549 goto error;
3550 }
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(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003555 PyObject *qualname = POP();
3556 PyObject *codeobj = POP();
3557 PyFunctionObject *func = (PyFunctionObject *)
3558 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003559
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003560 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003562 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003565
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003566 if (oparg & 0x08) {
3567 assert(PyTuple_CheckExact(TOP()));
3568 func ->func_closure = POP();
3569 }
3570 if (oparg & 0x04) {
3571 assert(PyDict_CheckExact(TOP()));
3572 func->func_annotations = POP();
3573 }
3574 if (oparg & 0x02) {
3575 assert(PyDict_CheckExact(TOP()));
3576 func->func_kwdefaults = POP();
3577 }
3578 if (oparg & 0x01) {
3579 assert(PyTuple_CheckExact(TOP()));
3580 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003582
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003583 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003586
Benjamin Petersonddd19492018-09-16 22:38:02 -07003587 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003588 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003590 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 step = NULL;
3593 stop = POP();
3594 start = TOP();
3595 slice = PySlice_New(start, stop, step);
3596 Py_DECREF(start);
3597 Py_DECREF(stop);
3598 Py_XDECREF(step);
3599 SET_TOP(slice);
3600 if (slice == NULL)
3601 goto error;
3602 DISPATCH();
3603 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003604
Benjamin Petersonddd19492018-09-16 22:38:02 -07003605 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003606 /* Handles f-string value formatting. */
3607 PyObject *result;
3608 PyObject *fmt_spec;
3609 PyObject *value;
3610 PyObject *(*conv_fn)(PyObject *);
3611 int which_conversion = oparg & FVC_MASK;
3612 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3613
3614 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003615 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003616
3617 /* See if any conversion is specified. */
3618 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003619 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003620 case FVC_STR: conv_fn = PyObject_Str; break;
3621 case FVC_REPR: conv_fn = PyObject_Repr; break;
3622 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003623 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003624 _PyErr_Format(tstate, PyExc_SystemError,
3625 "unexpected conversion flag %d",
3626 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003627 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003628 }
3629
3630 /* If there's a conversion function, call it and replace
3631 value with that result. Otherwise, just use value,
3632 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003633 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003634 result = conv_fn(value);
3635 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003636 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003637 Py_XDECREF(fmt_spec);
3638 goto error;
3639 }
3640 value = result;
3641 }
3642
3643 /* If value is a unicode object, and there's no fmt_spec,
3644 then we know the result of format(value) is value
3645 itself. In that case, skip calling format(). I plan to
3646 move this optimization in to PyObject_Format()
3647 itself. */
3648 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3649 /* Do nothing, just transfer ownership to result. */
3650 result = value;
3651 } else {
3652 /* Actually call format(). */
3653 result = PyObject_Format(value, fmt_spec);
3654 Py_DECREF(value);
3655 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003656 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003657 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003658 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003659 }
3660
Eric V. Smith135d5f42016-02-05 18:23:08 -05003661 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003662 DISPATCH();
3663 }
3664
Benjamin Petersonddd19492018-09-16 22:38:02 -07003665 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003666 int oldoparg = oparg;
3667 NEXTOPARG();
3668 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003670 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003671
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003672
Antoine Pitrou042b1282010-08-13 21:15:58 +00003673#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003675#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 default:
3677 fprintf(stderr,
3678 "XXX lineno: %d, opcode: %d\n",
3679 PyFrame_GetLineNumber(f),
3680 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003681 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003682 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003685
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003686 /* This should never be reached. Every opcode should end with DISPATCH()
3687 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003688 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003690error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003691 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003692#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003693 if (!_PyErr_Occurred(tstate)) {
3694 _PyErr_SetString(tstate, PyExc_SystemError,
3695 "error return without exception set");
3696 }
Victor Stinner365b6932013-07-12 00:11:58 +02003697#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003698 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003699#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 /* Log traceback info. */
3702 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003703
Benjamin Peterson51f46162013-01-23 08:38:47 -05003704 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003705 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3706 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003707
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003708exception_unwind:
3709 /* Unwind stacks if an exception occurred */
3710 while (f->f_iblock > 0) {
3711 /* Pop the current block. */
3712 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 if (b->b_type == EXCEPT_HANDLER) {
3715 UNWIND_EXCEPT_HANDLER(b);
3716 continue;
3717 }
3718 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003719 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 PyObject *exc, *val, *tb;
3721 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003722 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 /* Beware, this invalidates all b->b_* fields */
3724 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003725 PUSH(exc_info->exc_traceback);
3726 PUSH(exc_info->exc_value);
3727 if (exc_info->exc_type != NULL) {
3728 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 }
3730 else {
3731 Py_INCREF(Py_None);
3732 PUSH(Py_None);
3733 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003734 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 /* Make the raw exception data
3736 available to the handler,
3737 so a program can emulate the
3738 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003739 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003740 if (tb != NULL)
3741 PyException_SetTraceback(val, tb);
3742 else
3743 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003745 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003747 exc_info->exc_value = val;
3748 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 if (tb == NULL)
3750 tb = Py_None;
3751 Py_INCREF(tb);
3752 PUSH(tb);
3753 PUSH(val);
3754 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003756 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003757 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3758 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3759 /* Make sure that we trace line after exception if we are in a new execution
3760 * window or we don't need a line update and we are not in the first instruction
3761 * of the line. */
3762 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3763 instr_prev = INT_MAX;
3764 }
Mark Shannonfee55262019-11-21 09:11:43 +00003765 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003766 /* Resume normal execution */
3767 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 }
3769 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003770
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003771 /* End the loop as we still have an error */
3772 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003774
Pablo Galindof00828a2019-05-09 16:52:02 +01003775 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003776 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 /* Pop remaining stack entries. */
3779 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003780 PyObject *o = POP();
3781 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003783
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003784exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003786 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003787 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3788 tstate, f, PyTrace_RETURN, retval)) {
3789 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
3791 }
3792 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003793 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3794 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003795 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 }
3797 }
3798 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003801exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003802 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3803 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003804 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003805 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003807
Victor Stinner0b72b232020-03-12 23:18:39 +01003808 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003809}
3810
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003812format_missing(PyThreadState *tstate, const char *kind,
3813 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003814{
3815 int err;
3816 Py_ssize_t len = PyList_GET_SIZE(names);
3817 PyObject *name_str, *comma, *tail, *tmp;
3818
3819 assert(PyList_CheckExact(names));
3820 assert(len >= 1);
3821 /* Deal with the joys of natural language. */
3822 switch (len) {
3823 case 1:
3824 name_str = PyList_GET_ITEM(names, 0);
3825 Py_INCREF(name_str);
3826 break;
3827 case 2:
3828 name_str = PyUnicode_FromFormat("%U and %U",
3829 PyList_GET_ITEM(names, len - 2),
3830 PyList_GET_ITEM(names, len - 1));
3831 break;
3832 default:
3833 tail = PyUnicode_FromFormat(", %U, and %U",
3834 PyList_GET_ITEM(names, len - 2),
3835 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003836 if (tail == NULL)
3837 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003838 /* Chop off the last two objects in the list. This shouldn't actually
3839 fail, but we can't be too careful. */
3840 err = PyList_SetSlice(names, len - 2, len, NULL);
3841 if (err == -1) {
3842 Py_DECREF(tail);
3843 return;
3844 }
3845 /* Stitch everything up into a nice comma-separated list. */
3846 comma = PyUnicode_FromString(", ");
3847 if (comma == NULL) {
3848 Py_DECREF(tail);
3849 return;
3850 }
3851 tmp = PyUnicode_Join(comma, names);
3852 Py_DECREF(comma);
3853 if (tmp == NULL) {
3854 Py_DECREF(tail);
3855 return;
3856 }
3857 name_str = PyUnicode_Concat(tmp, tail);
3858 Py_DECREF(tmp);
3859 Py_DECREF(tail);
3860 break;
3861 }
3862 if (name_str == NULL)
3863 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003864 _PyErr_Format(tstate, PyExc_TypeError,
3865 "%U() missing %i required %s argument%s: %U",
3866 co->co_name,
3867 len,
3868 kind,
3869 len == 1 ? "" : "s",
3870 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003871 Py_DECREF(name_str);
3872}
3873
3874static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003875missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3876 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003877 PyObject **fastlocals)
3878{
Victor Stinner74319ae2016-08-25 00:04:09 +02003879 Py_ssize_t i, j = 0;
3880 Py_ssize_t start, end;
3881 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003882 const char *kind = positional ? "positional" : "keyword-only";
3883 PyObject *missing_names;
3884
3885 /* Compute the names of the arguments that are missing. */
3886 missing_names = PyList_New(missing);
3887 if (missing_names == NULL)
3888 return;
3889 if (positional) {
3890 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003891 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003892 }
3893 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003894 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003895 end = start + co->co_kwonlyargcount;
3896 }
3897 for (i = start; i < end; i++) {
3898 if (GETLOCAL(i) == NULL) {
3899 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3900 PyObject *name = PyObject_Repr(raw);
3901 if (name == NULL) {
3902 Py_DECREF(missing_names);
3903 return;
3904 }
3905 PyList_SET_ITEM(missing_names, j++, name);
3906 }
3907 }
3908 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003909 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 Py_DECREF(missing_names);
3911}
3912
3913static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003914too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3915 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003916 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917{
3918 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003919 Py_ssize_t kwonly_given = 0;
3920 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003921 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003922 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923
Benjamin Petersone109c702011-06-24 09:37:26 -05003924 assert((co->co_flags & CO_VARARGS) == 0);
3925 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003926 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003927 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003929 }
3930 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003931 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003932 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003934 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935 }
3936 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003937 plural = (co_argcount != 1);
3938 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003939 }
3940 if (sig == NULL)
3941 return;
3942 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003943 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3944 kwonly_sig = PyUnicode_FromFormat(format,
3945 given != 1 ? "s" : "",
3946 kwonly_given,
3947 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 if (kwonly_sig == NULL) {
3949 Py_DECREF(sig);
3950 return;
3951 }
3952 }
3953 else {
3954 /* This will not fail. */
3955 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003958 _PyErr_Format(tstate, PyExc_TypeError,
3959 "%U() takes %U positional argument%s but %zd%U %s given",
3960 co->co_name,
3961 sig,
3962 plural ? "s" : "",
3963 given,
3964 kwonly_sig,
3965 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 Py_DECREF(sig);
3967 Py_DECREF(kwonly_sig);
3968}
3969
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003970static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003971positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3972 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003973{
3974 int posonly_conflicts = 0;
3975 PyObject* posonly_names = PyList_New(0);
3976
3977 for(int k=0; k < co->co_posonlyargcount; k++){
3978 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3979
3980 for (int k2=0; k2<kwcount; k2++){
3981 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3982 PyObject* kwname = kwnames[k2];
3983 if (kwname == posonly_name){
3984 if(PyList_Append(posonly_names, kwname) != 0) {
3985 goto fail;
3986 }
3987 posonly_conflicts++;
3988 continue;
3989 }
3990
3991 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3992
3993 if ( cmp > 0) {
3994 if(PyList_Append(posonly_names, kwname) != 0) {
3995 goto fail;
3996 }
3997 posonly_conflicts++;
3998 } else if (cmp < 0) {
3999 goto fail;
4000 }
4001
4002 }
4003 }
4004 if (posonly_conflicts) {
4005 PyObject* comma = PyUnicode_FromString(", ");
4006 if (comma == NULL) {
4007 goto fail;
4008 }
4009 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4010 Py_DECREF(comma);
4011 if (error_names == NULL) {
4012 goto fail;
4013 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004014 _PyErr_Format(tstate, PyExc_TypeError,
4015 "%U() got some positional-only arguments passed"
4016 " as keyword arguments: '%U'",
4017 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004018 Py_DECREF(error_names);
4019 goto fail;
4020 }
4021
4022 Py_DECREF(posonly_names);
4023 return 0;
4024
4025fail:
4026 Py_XDECREF(posonly_names);
4027 return 1;
4028
4029}
4030
Guido van Rossumc2e20742006-02-27 22:32:47 +00004031/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004032 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004033 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004034
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004035PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004036_PyEval_EvalCode(PyThreadState *tstate,
4037 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004038 PyObject *const *args, Py_ssize_t argcount,
4039 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004040 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004041 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004042 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004043 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004044{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004045 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004046
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004047 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004048 PyFrameObject *f;
4049 PyObject *retval = NULL;
4050 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004052 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004053 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004054 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004057 _PyErr_SetString(tstate, PyExc_SystemError,
4058 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 return NULL;
4060 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004061
Victor Stinnerc7020012016-08-16 23:40:29 +02004062 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004063 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004064 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 fastlocals = f->f_localsplus;
4068 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004069
Victor Stinnerc7020012016-08-16 23:40:29 +02004070 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 if (co->co_flags & CO_VARKEYWORDS) {
4072 kwdict = PyDict_New();
4073 if (kwdict == NULL)
4074 goto fail;
4075 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004076 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004078 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 SETLOCAL(i, kwdict);
4080 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004081 else {
4082 kwdict = NULL;
4083 }
4084
Pablo Galindocd74e662019-06-01 18:08:04 +01004085 /* Copy all positional arguments into local variables */
4086 if (argcount > co->co_argcount) {
4087 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004088 }
4089 else {
4090 n = argcount;
4091 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004092 for (j = 0; j < n; j++) {
4093 x = args[j];
4094 Py_INCREF(x);
4095 SETLOCAL(j, x);
4096 }
4097
Victor Stinnerc7020012016-08-16 23:40:29 +02004098 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004100 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004101 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004102 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004103 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004106
Serhiy Storchakab7281052016-09-12 00:52:40 +03004107 /* Handle keyword arguments passed as two strided arrays */
4108 kwcount *= kwstep;
4109 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004111 PyObject *keyword = kwnames[i];
4112 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004113 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004114
Benjamin Petersonb204a422011-06-05 22:04:07 -05004115 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004116 _PyErr_Format(tstate, PyExc_TypeError,
4117 "%U() keywords must be strings",
4118 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004119 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004121
Benjamin Petersonb204a422011-06-05 22:04:07 -05004122 /* Speed hack: do raw pointer compares. As names are
4123 normally interned this should almost always hit. */
4124 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004125 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004126 PyObject *name = co_varnames[j];
4127 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004128 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004129 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004130 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004131
Benjamin Petersonb204a422011-06-05 22:04:07 -05004132 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004133 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004134 PyObject *name = co_varnames[j];
4135 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4136 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004138 }
4139 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004141 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004143
Victor Stinner231d1f32017-01-11 02:12:06 +01004144 assert(j >= total_args);
4145 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004146
Victor Stinner438a12d2019-05-24 17:01:38 +02004147 if (co->co_posonlyargcount
4148 && positional_only_passed_as_keyword(tstate, co,
4149 kwcount, kwnames))
4150 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004151 goto fail;
4152 }
4153
Victor Stinner438a12d2019-05-24 17:01:38 +02004154 _PyErr_Format(tstate, PyExc_TypeError,
4155 "%U() got an unexpected keyword argument '%S'",
4156 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004157 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004159
Christian Heimes0bd447f2013-07-20 14:48:10 +02004160 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4161 goto fail;
4162 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004163 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004164
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 kw_found:
4166 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004167 _PyErr_Format(tstate, PyExc_TypeError,
4168 "%U() got multiple values for argument '%S'",
4169 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004170 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004172 Py_INCREF(value);
4173 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004175
4176 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004177 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004178 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 goto fail;
4180 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004181
4182 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004183 if (argcount < co->co_argcount) {
4184 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004185 Py_ssize_t missing = 0;
4186 for (i = argcount; i < m; i++) {
4187 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004188 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004189 }
4190 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004191 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004192 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004193 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004194 }
4195 if (n > m)
4196 i = n - m;
4197 else
4198 i = 0;
4199 for (; i < defcount; i++) {
4200 if (GETLOCAL(m+i) == NULL) {
4201 PyObject *def = defs[i];
4202 Py_INCREF(def);
4203 SETLOCAL(m+i, def);
4204 }
4205 }
4206 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004207
4208 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004209 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004210 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004211 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004212 PyObject *name;
4213 if (GETLOCAL(i) != NULL)
4214 continue;
4215 name = PyTuple_GET_ITEM(co->co_varnames, i);
4216 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004217 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004218 if (def) {
4219 Py_INCREF(def);
4220 SETLOCAL(i, def);
4221 continue;
4222 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004223 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004224 goto fail;
4225 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004226 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004227 missing++;
4228 }
4229 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004230 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004231 goto fail;
4232 }
4233 }
4234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004236 vars into frame. */
4237 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004239 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004240 /* Possibly account for the cell variable being an argument. */
4241 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004242 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004243 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004244 /* Clear the local copy. */
4245 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004246 }
4247 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004248 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004249 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004250 if (c == NULL)
4251 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004252 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004254
4255 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004256 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4257 PyObject *o = PyTuple_GET_ITEM(closure, i);
4258 Py_INCREF(o);
4259 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004261
Yury Selivanoveb636452016-09-08 22:01:51 -07004262 /* Handle generator/coroutine/asynchronous generator */
4263 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004264 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004265 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 /* Don't need to keep the reference to f_back, it will be set
4268 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004269 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 /* Create a new generator that owns the ready to run frame
4272 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004273 if (is_coro) {
4274 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004275 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4276 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004277 } else {
4278 gen = PyGen_NewWithQualName(f, name, qualname);
4279 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004280 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004281 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004282 }
INADA Naoki9c157762016-12-26 18:52:46 +09004283
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004284 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004285
Yury Selivanov75445082015-05-11 22:57:16 -04004286 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004288
Victor Stinnerb9e68122019-11-14 12:20:46 +01004289 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004290
Thomas Woutersce272b62007-09-19 21:19:28 +00004291fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 /* decref'ing the frame can cause __del__ methods to get invoked,
4294 which can call back into Python. While we're done with the
4295 current Python frame (f), the associated C stack is still in use,
4296 so recursion_depth must be boosted for the duration.
4297 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004298 if (Py_REFCNT(f) > 1) {
4299 Py_DECREF(f);
4300 _PyObject_GC_TRACK(f);
4301 }
4302 else {
4303 ++tstate->recursion_depth;
4304 Py_DECREF(f);
4305 --tstate->recursion_depth;
4306 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004308}
4309
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004310
4311PyObject *
4312_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4313 PyObject *const *args, Py_ssize_t argcount,
4314 PyObject *const *kwnames, PyObject *const *kwargs,
4315 Py_ssize_t kwcount, int kwstep,
4316 PyObject *const *defs, Py_ssize_t defcount,
4317 PyObject *kwdefs, PyObject *closure,
4318 PyObject *name, PyObject *qualname)
4319{
4320 PyThreadState *tstate = _PyThreadState_GET();
4321 return _PyEval_EvalCode(tstate, _co, globals, locals,
4322 args, argcount,
4323 kwnames, kwargs,
4324 kwcount, kwstep,
4325 defs, defcount,
4326 kwdefs, closure,
4327 name, qualname);
4328}
4329
Victor Stinner40ee3012014-06-16 15:59:28 +02004330PyObject *
4331PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004332 PyObject *const *args, int argcount,
4333 PyObject *const *kws, int kwcount,
4334 PyObject *const *defs, int defcount,
4335 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004336{
4337 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004338 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004339 kws, kws != NULL ? kws + 1 : NULL,
4340 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004341 defs, defcount,
4342 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004343 NULL, NULL);
4344}
Tim Peters5ca576e2001-06-18 22:08:13 +00004345
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004346static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004347special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004350 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004351 if (res == NULL && !_PyErr_Occurred(tstate)) {
4352 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 return NULL;
4354 }
4355 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004356}
4357
4358
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004359/* Logic for the raise statement (too complicated for inlining).
4360 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004361static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004362do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 if (exc == NULL) {
4367 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004368 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004370 type = exc_info->exc_type;
4371 value = exc_info->exc_value;
4372 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004373 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004374 _PyErr_SetString(tstate, PyExc_RuntimeError,
4375 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004376 return 0;
4377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 Py_XINCREF(type);
4379 Py_XINCREF(value);
4380 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004381 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004382 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 /* We support the following forms of raise:
4386 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004387 raise <instance>
4388 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 if (PyExceptionClass_Check(exc)) {
4391 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004392 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 if (value == NULL)
4394 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004395 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004396 _PyErr_Format(tstate, PyExc_TypeError,
4397 "calling %R should have returned an instance of "
4398 "BaseException, not %R",
4399 type, Py_TYPE(value));
4400 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 }
4403 else if (PyExceptionInstance_Check(exc)) {
4404 value = exc;
4405 type = PyExceptionInstance_Class(exc);
4406 Py_INCREF(type);
4407 }
4408 else {
4409 /* Not something you can raise. You get an exception
4410 anyway, just not what you specified :-) */
4411 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004412 _PyErr_SetString(tstate, PyExc_TypeError,
4413 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 goto raise_error;
4415 }
Collin Winter828f04a2007-08-31 00:04:24 +00004416
Serhiy Storchakac0191582016-09-27 11:37:10 +03004417 assert(type != NULL);
4418 assert(value != NULL);
4419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 if (cause) {
4421 PyObject *fixed_cause;
4422 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004423 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 if (fixed_cause == NULL)
4425 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004426 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004428 else if (PyExceptionInstance_Check(cause)) {
4429 fixed_cause = cause;
4430 }
4431 else if (cause == Py_None) {
4432 Py_DECREF(cause);
4433 fixed_cause = NULL;
4434 }
4435 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004436 _PyErr_SetString(tstate, PyExc_TypeError,
4437 "exception causes must derive from "
4438 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 goto raise_error;
4440 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004441 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 }
Collin Winter828f04a2007-08-31 00:04:24 +00004443
Victor Stinner438a12d2019-05-24 17:01:38 +02004444 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004445 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004446 Py_DECREF(value);
4447 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004448 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004449
4450raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 Py_XDECREF(value);
4452 Py_XDECREF(type);
4453 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004454 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004455}
4456
Tim Petersd6d010b2001-06-21 02:49:55 +00004457/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004458 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004459
Guido van Rossum0368b722007-05-11 16:50:42 +00004460 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4461 with a variable target.
4462*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004463
Barry Warsawe42b18f1997-08-25 22:13:04 +00004464static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004465unpack_iterable(PyThreadState *tstate, PyObject *v,
4466 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 int i = 0, j = 0;
4469 Py_ssize_t ll = 0;
4470 PyObject *it; /* iter(v) */
4471 PyObject *w;
4472 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004477 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004478 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004479 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004480 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004481 _PyErr_Format(tstate, PyExc_TypeError,
4482 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004483 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004484 }
4485 return 0;
4486 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 for (; i < argcnt; i++) {
4489 w = PyIter_Next(it);
4490 if (w == NULL) {
4491 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004492 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004493 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004494 _PyErr_Format(tstate, PyExc_ValueError,
4495 "not enough values to unpack "
4496 "(expected %d, got %d)",
4497 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004498 }
4499 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004500 _PyErr_Format(tstate, PyExc_ValueError,
4501 "not enough values to unpack "
4502 "(expected at least %d, got %d)",
4503 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 }
4506 goto Error;
4507 }
4508 *--sp = w;
4509 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 if (argcntafter == -1) {
4512 /* We better have exhausted the iterator now. */
4513 w = PyIter_Next(it);
4514 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004515 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 goto Error;
4517 Py_DECREF(it);
4518 return 1;
4519 }
4520 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004521 _PyErr_Format(tstate, PyExc_ValueError,
4522 "too many values to unpack (expected %d)",
4523 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 goto Error;
4525 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 l = PySequence_List(it);
4528 if (l == NULL)
4529 goto Error;
4530 *--sp = l;
4531 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 ll = PyList_GET_SIZE(l);
4534 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004535 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004536 "not enough values to unpack (expected at least %d, got %zd)",
4537 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 goto Error;
4539 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 /* Pop the "after-variable" args off the list. */
4542 for (j = argcntafter; j > 0; j--, i++) {
4543 *--sp = PyList_GET_ITEM(l, ll - j);
4544 }
4545 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004546 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 Py_DECREF(it);
4548 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004549
Tim Petersd6d010b2001-06-21 02:49:55 +00004550Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 for (; i > 0; i--, sp++)
4552 Py_DECREF(*sp);
4553 Py_XDECREF(it);
4554 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004555}
4556
4557
Guido van Rossum96a42c81992-01-12 02:29:51 +00004558#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004559static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004560prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004563 if (PyObject_Print(v, stdout, 0) != 0) {
4564 /* Don't know what else to do */
4565 _PyErr_Clear(tstate);
4566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 printf("\n");
4568 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004569}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004570#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004571
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004572static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004573call_exc_trace(Py_tracefunc func, PyObject *self,
4574 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004575{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004576 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004578 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (value == NULL) {
4580 value = Py_None;
4581 Py_INCREF(value);
4582 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004583 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004584 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 arg = PyTuple_Pack(3, type, value, traceback);
4586 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004587 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 return;
4589 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004590 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004592 if (err == 0) {
4593 _PyErr_Restore(tstate, type, value, orig_traceback);
4594 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 else {
4596 Py_XDECREF(type);
4597 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004598 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004600}
4601
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004602static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004603call_trace_protected(Py_tracefunc func, PyObject *obj,
4604 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 PyObject *type, *value, *traceback;
4608 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004609 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004610 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 if (err == 0)
4612 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004613 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 return 0;
4615 }
4616 else {
4617 Py_XDECREF(type);
4618 Py_XDECREF(value);
4619 Py_XDECREF(traceback);
4620 return -1;
4621 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004622}
4623
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004624static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004625call_trace(Py_tracefunc func, PyObject *obj,
4626 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 int result;
4630 if (tstate->tracing)
4631 return 0;
4632 tstate->tracing++;
4633 tstate->use_tracing = 0;
4634 result = func(obj, frame, what, arg);
4635 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4636 || (tstate->c_profilefunc != NULL));
4637 tstate->tracing--;
4638 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004639}
4640
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004641PyObject *
4642_PyEval_CallTracing(PyObject *func, PyObject *args)
4643{
Victor Stinner50b48572018-11-01 01:51:40 +01004644 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 int save_tracing = tstate->tracing;
4646 int save_use_tracing = tstate->use_tracing;
4647 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 tstate->tracing = 0;
4650 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4651 || (tstate->c_profilefunc != NULL));
4652 result = PyObject_Call(func, args, NULL);
4653 tstate->tracing = save_tracing;
4654 tstate->use_tracing = save_use_tracing;
4655 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004656}
4657
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004658/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004659static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004660maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004661 PyThreadState *tstate, PyFrameObject *frame,
4662 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 int result = 0;
4665 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 /* If the last instruction executed isn't in the current
4668 instruction window, reset the window.
4669 */
4670 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4671 PyAddrPair bounds;
4672 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4673 &bounds);
4674 *instr_lb = bounds.ap_lower;
4675 *instr_ub = bounds.ap_upper;
4676 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004677 /* If the last instruction falls at the start of a line or if it
4678 represents a jump backwards, update the frame's line number and
4679 then call the trace function if we're tracing source lines.
4680 */
4681 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004683 if (frame->f_trace_lines) {
4684 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 }
George King20faa682017-10-18 17:44:22 -07004687 /* Always emit an opcode event if we're tracing all opcodes. */
4688 if (frame->f_trace_opcodes) {
4689 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 *instr_prev = frame->f_lasti;
4692 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004693}
4694
Victor Stinner309d7cc2020-03-13 16:39:12 +01004695int
4696_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4697{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004698 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004699 /* The caller must hold the GIL */
4700 assert(PyGILState_Check());
4701
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004702 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004703 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004704 PyThreadState *current_tstate = _PyThreadState_GET();
4705 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004706 return -1;
4707 }
4708
4709 PyObject *profileobj = tstate->c_profileobj;
4710
4711 tstate->c_profilefunc = NULL;
4712 tstate->c_profileobj = NULL;
4713 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4714 tstate->use_tracing = tstate->c_tracefunc != NULL;
4715 Py_XDECREF(profileobj);
4716
4717 Py_XINCREF(arg);
4718 tstate->c_profileobj = arg;
4719 tstate->c_profilefunc = func;
4720
4721 /* Flag that tracing or profiling is turned on */
4722 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4723 return 0;
4724}
4725
Fred Drake5755ce62001-06-27 19:19:46 +00004726void
4727PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004728{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004729 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004730 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004731 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004732 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4733 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004734}
4735
4736int
4737_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4738{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004739 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004740 /* The caller must hold the GIL */
4741 assert(PyGILState_Check());
4742
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004743 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004744 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004745 PyThreadState *current_tstate = _PyThreadState_GET();
4746 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004747 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004748 }
4749
Victor Stinnerda2914d2020-03-20 09:29:08 +01004750 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004751 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004752 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004753
4754 tstate->c_tracefunc = NULL;
4755 tstate->c_traceobj = NULL;
4756 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4757 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4758 Py_XDECREF(traceobj);
4759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004761 tstate->c_traceobj = arg;
4762 tstate->c_tracefunc = func;
4763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004765 tstate->use_tracing = ((func != NULL)
4766 || (tstate->c_profilefunc != NULL));
4767
4768 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004769}
4770
4771void
4772PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4773{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004774 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004775 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004776 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004777 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4778 }
Fred Draked0838392001-06-16 21:02:31 +00004779}
4780
Victor Stinner309d7cc2020-03-13 16:39:12 +01004781
Yury Selivanov75445082015-05-11 22:57:16 -04004782void
Victor Stinner838f2642019-06-13 22:41:23 +02004783_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004784{
4785 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004786 tstate->coroutine_origin_tracking_depth = new_depth;
4787}
4788
4789int
4790_PyEval_GetCoroutineOriginTrackingDepth(void)
4791{
Victor Stinner50b48572018-11-01 01:51:40 +01004792 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004793 return tstate->coroutine_origin_tracking_depth;
4794}
4795
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004796int
Yury Selivanoveb636452016-09-08 22:01:51 -07004797_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4798{
Victor Stinner50b48572018-11-01 01:51:40 +01004799 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004800
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004801 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004802 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004803 }
4804
Yury Selivanoveb636452016-09-08 22:01:51 -07004805 Py_XINCREF(firstiter);
4806 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004807 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004808}
4809
4810PyObject *
4811_PyEval_GetAsyncGenFirstiter(void)
4812{
Victor Stinner50b48572018-11-01 01:51:40 +01004813 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004814 return tstate->async_gen_firstiter;
4815}
4816
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004817int
Yury Selivanoveb636452016-09-08 22:01:51 -07004818_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4819{
Victor Stinner50b48572018-11-01 01:51:40 +01004820 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004821
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004822 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004823 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004824 }
4825
Yury Selivanoveb636452016-09-08 22:01:51 -07004826 Py_XINCREF(finalizer);
4827 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004828 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004829}
4830
4831PyObject *
4832_PyEval_GetAsyncGenFinalizer(void)
4833{
Victor Stinner50b48572018-11-01 01:51:40 +01004834 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004835 return tstate->async_gen_finalizer;
4836}
4837
Victor Stinner438a12d2019-05-24 17:01:38 +02004838PyFrameObject *
4839PyEval_GetFrame(void)
4840{
4841 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004842 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004843}
4844
Guido van Rossumb209a111997-04-29 18:18:01 +00004845PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004846PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004847{
Victor Stinner438a12d2019-05-24 17:01:38 +02004848 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004849 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004850 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004851 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 else
4853 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004854}
4855
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004856/* Convenience function to get a builtin from its name */
4857PyObject *
4858_PyEval_GetBuiltinId(_Py_Identifier *name)
4859{
Victor Stinner438a12d2019-05-24 17:01:38 +02004860 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004861 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4862 if (attr) {
4863 Py_INCREF(attr);
4864 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004865 else if (!_PyErr_Occurred(tstate)) {
4866 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004867 }
4868 return attr;
4869}
4870
Guido van Rossumb209a111997-04-29 18:18:01 +00004871PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004872PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004873{
Victor Stinner438a12d2019-05-24 17:01:38 +02004874 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004875 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004876 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004877 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004879 }
4880
Victor Stinner438a12d2019-05-24 17:01:38 +02004881 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004882 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004883 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004884
4885 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004887}
4888
Guido van Rossumb209a111997-04-29 18:18:01 +00004889PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004890PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004891{
Victor Stinner438a12d2019-05-24 17:01:38 +02004892 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004893 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004894 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004896 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004897
4898 assert(current_frame->f_globals != NULL);
4899 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004900}
4901
Guido van Rossum6135a871995-01-09 17:53:26 +00004902int
Tim Peters5ba58662001-07-16 02:29:45 +00004903PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004904{
Victor Stinner438a12d2019-05-24 17:01:38 +02004905 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004906 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 if (current_frame != NULL) {
4910 const int codeflags = current_frame->f_code->co_flags;
4911 const int compilerflags = codeflags & PyCF_MASK;
4912 if (compilerflags) {
4913 result = 1;
4914 cf->cf_flags |= compilerflags;
4915 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004916#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 if (codeflags & CO_GENERATOR_ALLOWED) {
4918 result = 1;
4919 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4920 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004921#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 }
4923 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004924}
4925
Guido van Rossum3f5da241990-12-20 15:06:42 +00004926
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004927const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004928PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 if (PyMethod_Check(func))
4931 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4932 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004933 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 else if (PyCFunction_Check(func))
4935 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4936 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004937 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004938}
4939
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004940const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004941PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 if (PyMethod_Check(func))
4944 return "()";
4945 else if (PyFunction_Check(func))
4946 return "()";
4947 else if (PyCFunction_Check(func))
4948 return "()";
4949 else
4950 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004951}
4952
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004953#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004954if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004955 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4956 tstate, tstate->frame, \
4957 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 x = NULL; \
4959 } \
4960 else { \
4961 x = call; \
4962 if (tstate->c_profilefunc != NULL) { \
4963 if (x == NULL) { \
4964 call_trace_protected(tstate->c_profilefunc, \
4965 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004966 tstate, tstate->frame, \
4967 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 /* XXX should pass (type, value, tb) */ \
4969 } else { \
4970 if (call_trace(tstate->c_profilefunc, \
4971 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004972 tstate, tstate->frame, \
4973 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 Py_DECREF(x); \
4975 x = NULL; \
4976 } \
4977 } \
4978 } \
4979 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004980} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 x = call; \
4982 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004983
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004984
4985static PyObject *
4986trace_call_function(PyThreadState *tstate,
4987 PyObject *func,
4988 PyObject **args, Py_ssize_t nargs,
4989 PyObject *kwnames)
4990{
4991 PyObject *x;
4992 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004993 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004994 return x;
4995 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004996 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004997 /* We need to create a temporary bound method as argument
4998 for profiling.
4999
5000 If nargs == 0, then this cannot work because we have no
5001 "self". In any case, the call itself would raise
5002 TypeError (foo needs an argument), so we just skip
5003 profiling. */
5004 PyObject *self = args[0];
5005 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5006 if (func == NULL) {
5007 return NULL;
5008 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005009 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005010 args+1, nargs-1,
5011 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005012 Py_DECREF(func);
5013 return x;
5014 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005015 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005016}
5017
Victor Stinner415c5102017-01-11 00:54:57 +01005018/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5019 to reduce the stack consumption. */
5020Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005021call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005022{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005023 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005024 PyObject *func = *pfunc;
5025 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005026 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5027 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005028 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005029
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005030 if (tstate->use_tracing) {
5031 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005032 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005033 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005034 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005036
Victor Stinner438a12d2019-05-24 17:01:38 +02005037 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005038
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005039 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 while ((*pp_stack) > pfunc) {
5041 w = EXT_POP(*pp_stack);
5042 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005046}
5047
Jeremy Hylton52820442001-01-03 23:52:36 +00005048static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005049do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005050{
jdemeyere89de732018-09-19 12:06:20 +02005051 PyObject *result;
5052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005054 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005055 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005057 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005058 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5059 if (nargs > 0 && tstate->use_tracing) {
5060 /* We need to create a temporary bound method as argument
5061 for profiling.
5062
5063 If nargs == 0, then this cannot work because we have no
5064 "self". In any case, the call itself would raise
5065 TypeError (foo needs an argument), so we just skip
5066 profiling. */
5067 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5068 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5069 if (func == NULL) {
5070 return NULL;
5071 }
5072
Victor Stinner4d231bc2019-11-14 13:36:21 +01005073 C_TRACE(result, _PyObject_FastCallDictTstate(
5074 tstate, func,
5075 &_PyTuple_ITEMS(callargs)[1],
5076 nargs - 1,
5077 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005078 Py_DECREF(func);
5079 return result;
5080 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005081 }
jdemeyere89de732018-09-19 12:06:20 +02005082 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005083}
5084
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005085/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005086 nb_index slot defined, and store in *pi.
5087 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005088 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005089 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005090*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005091int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005092_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005093{
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005095 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005097 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005099 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 return 0;
5101 }
5102 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005103 _PyErr_SetString(tstate, PyExc_TypeError,
5104 "slice indices must be integers or "
5105 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 return 0;
5107 }
5108 *pi = x;
5109 }
5110 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005111}
5112
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005113int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005114_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005115{
Victor Stinner438a12d2019-05-24 17:01:38 +02005116 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005117 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005118 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005119 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005120 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005121 return 0;
5122 }
5123 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005124 _PyErr_SetString(tstate, PyExc_TypeError,
5125 "slice indices must be integers or "
5126 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005127 return 0;
5128 }
5129 *pi = x;
5130 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005131}
5132
Thomas Wouters52152252000-08-17 22:55:00 +00005133static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005134import_name(PyThreadState *tstate, PyFrameObject *f,
5135 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005136{
5137 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005138 PyObject *import_func, *res;
5139 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005140
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005141 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005142 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005143 if (!_PyErr_Occurred(tstate)) {
5144 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005145 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005146 return NULL;
5147 }
5148
5149 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005150 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005151 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005152 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005153 return NULL;
5154 }
5155 res = PyImport_ImportModuleLevelObject(
5156 name,
5157 f->f_globals,
5158 f->f_locals == NULL ? Py_None : f->f_locals,
5159 fromlist,
5160 ilevel);
5161 return res;
5162 }
5163
5164 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005165
5166 stack[0] = name;
5167 stack[1] = f->f_globals;
5168 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5169 stack[3] = fromlist;
5170 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005171 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005172 Py_DECREF(import_func);
5173 return res;
5174}
5175
5176static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005177import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005180 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005181
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005182 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005183 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005184 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005185 /* Issue #17636: in case this failed because of a circular relative
5186 import, try to fallback on reading the module directly from
5187 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005188 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005189 if (pkgname == NULL) {
5190 goto error;
5191 }
Oren Milman6db70332017-09-19 14:23:01 +03005192 if (!PyUnicode_Check(pkgname)) {
5193 Py_CLEAR(pkgname);
5194 goto error;
5195 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005196 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005197 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005198 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005199 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005200 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005201 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005202 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005203 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005204 goto error;
5205 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005206 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005208 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005209 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005210 if (pkgname == NULL) {
5211 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5212 if (pkgname_or_unknown == NULL) {
5213 Py_XDECREF(pkgpath);
5214 return NULL;
5215 }
5216 } else {
5217 pkgname_or_unknown = pkgname;
5218 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005219
5220 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005221 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005222 errmsg = PyUnicode_FromFormat(
5223 "cannot import name %R from %R (unknown location)",
5224 name, pkgname_or_unknown
5225 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005226 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005227 PyErr_SetImportError(errmsg, pkgname, NULL);
5228 }
5229 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005230 _Py_IDENTIFIER(__spec__);
5231 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005232 const char *fmt =
5233 _PyModuleSpec_IsInitializing(spec) ?
5234 "cannot import name %R from partially initialized module %R "
5235 "(most likely due to a circular import) (%S)" :
5236 "cannot import name %R from %R (%S)";
5237 Py_XDECREF(spec);
5238
5239 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005240 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005241 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005242 }
5243
Xiang Zhang4830f582017-03-21 11:13:42 +08005244 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005245 Py_XDECREF(pkgname_or_unknown);
5246 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005247 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005248}
Guido van Rossumac7be682001-01-17 15:42:30 +00005249
Thomas Wouters52152252000-08-17 22:55:00 +00005250static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005251import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005252{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005253 _Py_IDENTIFIER(__all__);
5254 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005255 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 int skip_leading_underscores = 0;
5257 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005258
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005259 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5260 return -1; /* Unexpected error */
5261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005263 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5264 return -1;
5265 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005267 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005268 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 return -1;
5270 }
5271 all = PyMapping_Keys(dict);
5272 Py_DECREF(dict);
5273 if (all == NULL)
5274 return -1;
5275 skip_leading_underscores = 1;
5276 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 for (pos = 0, err = 0; ; pos++) {
5279 name = PySequence_GetItem(all, pos);
5280 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005281 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005283 }
5284 else {
5285 _PyErr_Clear(tstate);
5286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 break;
5288 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005289 if (!PyUnicode_Check(name)) {
5290 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5291 if (modname == NULL) {
5292 Py_DECREF(name);
5293 err = -1;
5294 break;
5295 }
5296 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 _PyErr_Format(tstate, PyExc_TypeError,
5298 "module __name__ must be a string, not %.100s",
5299 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005300 }
5301 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005302 _PyErr_Format(tstate, PyExc_TypeError,
5303 "%s in %U.%s must be str, not %.100s",
5304 skip_leading_underscores ? "Key" : "Item",
5305 modname,
5306 skip_leading_underscores ? "__dict__" : "__all__",
5307 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005308 }
5309 Py_DECREF(modname);
5310 Py_DECREF(name);
5311 err = -1;
5312 break;
5313 }
5314 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005315 if (PyUnicode_READY(name) == -1) {
5316 Py_DECREF(name);
5317 err = -1;
5318 break;
5319 }
5320 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5321 Py_DECREF(name);
5322 continue;
5323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 }
5325 value = PyObject_GetAttr(v, name);
5326 if (value == NULL)
5327 err = -1;
5328 else if (PyDict_CheckExact(locals))
5329 err = PyDict_SetItem(locals, name, value);
5330 else
5331 err = PyObject_SetItem(locals, name, value);
5332 Py_DECREF(name);
5333 Py_XDECREF(value);
5334 if (err != 0)
5335 break;
5336 }
5337 Py_DECREF(all);
5338 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005339}
5340
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005341static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005342check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005343{
Victor Stinnera102ed72020-02-07 02:24:48 +01005344 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005345 /* check_args_iterable() may be called with a live exception:
5346 * clear it to prevent calling _PyObject_FunctionStr() with an
5347 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005348 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005349 PyObject *funcstr = _PyObject_FunctionStr(func);
5350 if (funcstr != NULL) {
5351 _PyErr_Format(tstate, PyExc_TypeError,
5352 "%U argument after * must be an iterable, not %.200s",
5353 funcstr, Py_TYPE(args)->tp_name);
5354 Py_DECREF(funcstr);
5355 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005356 return -1;
5357 }
5358 return 0;
5359}
5360
5361static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005362format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005363{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005364 /* _PyDict_MergeEx raises attribute
5365 * error (percolated from an attempt
5366 * to get 'keys' attribute) instead of
5367 * a type error if its second argument
5368 * is not a mapping.
5369 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005370 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005371 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005372 PyObject *funcstr = _PyObject_FunctionStr(func);
5373 if (funcstr != NULL) {
5374 _PyErr_Format(
5375 tstate, PyExc_TypeError,
5376 "%U argument after ** must be a mapping, not %.200s",
5377 funcstr, Py_TYPE(kwargs)->tp_name);
5378 Py_DECREF(funcstr);
5379 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005380 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005381 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005382 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005383 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005384 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005385 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005386 PyObject *funcstr = _PyObject_FunctionStr(func);
5387 if (funcstr != NULL) {
5388 PyObject *key = PyTuple_GET_ITEM(val, 0);
5389 _PyErr_Format(
5390 tstate, PyExc_TypeError,
5391 "%U got multiple values for keyword argument '%S'",
5392 funcstr, key);
5393 Py_DECREF(funcstr);
5394 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005395 Py_XDECREF(exc);
5396 Py_XDECREF(val);
5397 Py_XDECREF(tb);
5398 }
5399 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005400 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005401 }
5402 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005403}
5404
Guido van Rossumac7be682001-01-17 15:42:30 +00005405static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005406format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5407 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 if (!obj)
5412 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005413
Serhiy Storchaka06515832016-11-20 09:13:07 +02005414 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 if (!obj_str)
5416 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005417
Victor Stinner438a12d2019-05-24 17:01:38 +02005418 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005419}
Guido van Rossum950361c1997-01-24 13:49:28 +00005420
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005421static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005422format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005423{
5424 PyObject *name;
5425 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005427 return;
5428 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5429 name = PyTuple_GET_ITEM(co->co_cellvars,
5430 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005431 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005432 PyExc_UnboundLocalError,
5433 UNBOUNDLOCAL_ERROR_MSG,
5434 name);
5435 } else {
5436 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5437 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005438 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005439 UNBOUNDFREE_ERROR_MSG, name);
5440 }
5441}
5442
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005443static void
Mark Shannonfee55262019-11-21 09:11:43 +00005444format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005445{
5446 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5447 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 _PyErr_Format(tstate, PyExc_TypeError,
5449 "'async with' received an object from __aenter__ "
5450 "that does not implement __await__: %.100s",
5451 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005452 }
Mark Shannonfee55262019-11-21 09:11:43 +00005453 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005454 _PyErr_Format(tstate, PyExc_TypeError,
5455 "'async with' received an object from __aexit__ "
5456 "that does not implement __await__: %.100s",
5457 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005458 }
5459 }
5460}
5461
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005462static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005463unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005464 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005465{
5466 PyObject *res;
5467 if (Py_REFCNT(v) == 2) {
5468 /* In the common case, there are 2 references to the value
5469 * stored in 'variable' when the += is performed: one on the
5470 * value stack (in 'v') and one still stored in the
5471 * 'variable'. We try to delete the variable now to reduce
5472 * the refcnt to 1.
5473 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005474 int opcode, oparg;
5475 NEXTOPARG();
5476 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005477 case STORE_FAST:
5478 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005479 PyObject **fastlocals = f->f_localsplus;
5480 if (GETLOCAL(oparg) == v)
5481 SETLOCAL(oparg, NULL);
5482 break;
5483 }
5484 case STORE_DEREF:
5485 {
5486 PyObject **freevars = (f->f_localsplus +
5487 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005488 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005489 if (PyCell_GET(c) == v) {
5490 PyCell_SET(c, NULL);
5491 Py_DECREF(v);
5492 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005493 break;
5494 }
5495 case STORE_NAME:
5496 {
5497 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005498 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005499 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005500 if (locals && PyDict_CheckExact(locals)) {
5501 PyObject *w = PyDict_GetItemWithError(locals, name);
5502 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005503 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005504 {
5505 Py_DECREF(v);
5506 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005507 }
5508 }
5509 break;
5510 }
5511 }
5512 }
5513 res = v;
5514 PyUnicode_Append(&res, w);
5515 return res;
5516}
5517
Guido van Rossum950361c1997-01-24 13:49:28 +00005518#ifdef DYNAMIC_EXECUTION_PROFILE
5519
Skip Montanarof118cb12001-10-15 20:51:38 +00005520static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005521getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 int i;
5524 PyObject *l = PyList_New(256);
5525 if (l == NULL) return NULL;
5526 for (i = 0; i < 256; i++) {
5527 PyObject *x = PyLong_FromLong(a[i]);
5528 if (x == NULL) {
5529 Py_DECREF(l);
5530 return NULL;
5531 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005532 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 }
5534 for (i = 0; i < 256; i++)
5535 a[i] = 0;
5536 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005537}
5538
5539PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005540_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005541{
5542#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005544#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 int i;
5546 PyObject *l = PyList_New(257);
5547 if (l == NULL) return NULL;
5548 for (i = 0; i < 257; i++) {
5549 PyObject *x = getarray(dxpairs[i]);
5550 if (x == NULL) {
5551 Py_DECREF(l);
5552 return NULL;
5553 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005554 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 }
5556 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005557#endif
5558}
5559
5560#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005561
5562Py_ssize_t
5563_PyEval_RequestCodeExtraIndex(freefunc free)
5564{
Victor Stinner81a7be32020-04-14 15:14:01 +02005565 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005566 Py_ssize_t new_index;
5567
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005568 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005569 return -1;
5570 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005571 new_index = interp->co_extra_user_count++;
5572 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005573 return new_index;
5574}
Łukasz Langaa785c872016-09-09 17:37:37 -07005575
5576static void
5577dtrace_function_entry(PyFrameObject *f)
5578{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005579 const char *filename;
5580 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005581 int lineno;
5582
Victor Stinner6d86a232020-04-29 00:56:58 +02005583 PyCodeObject *code = f->f_code;
5584 filename = PyUnicode_AsUTF8(code->co_filename);
5585 funcname = PyUnicode_AsUTF8(code->co_name);
5586 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005587
Andy Lestere6be9b52020-02-11 20:28:35 -06005588 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005589}
5590
5591static void
5592dtrace_function_return(PyFrameObject *f)
5593{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005594 const char *filename;
5595 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005596 int lineno;
5597
Victor Stinner6d86a232020-04-29 00:56:58 +02005598 PyCodeObject *code = f->f_code;
5599 filename = PyUnicode_AsUTF8(code->co_filename);
5600 funcname = PyUnicode_AsUTF8(code->co_name);
5601 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005602
Andy Lestere6be9b52020-02-11 20:28:35 -06005603 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005604}
5605
5606/* DTrace equivalent of maybe_call_line_trace. */
5607static void
5608maybe_dtrace_line(PyFrameObject *frame,
5609 int *instr_lb, int *instr_ub, int *instr_prev)
5610{
5611 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005612 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005613
5614 /* If the last instruction executed isn't in the current
5615 instruction window, reset the window.
5616 */
5617 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5618 PyAddrPair bounds;
5619 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5620 &bounds);
5621 *instr_lb = bounds.ap_lower;
5622 *instr_ub = bounds.ap_upper;
5623 }
5624 /* If the last instruction falls at the start of a line or if
5625 it represents a jump backwards, update the frame's line
5626 number and call the trace function. */
5627 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5628 frame->f_lineno = line;
5629 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5630 if (!co_filename)
5631 co_filename = "?";
5632 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5633 if (!co_name)
5634 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005635 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005636 }
5637 *instr_prev = frame->f_lasti;
5638}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005639
5640
5641/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5642 for the limited API. */
5643
5644#undef Py_EnterRecursiveCall
5645
5646int Py_EnterRecursiveCall(const char *where)
5647{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005648 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005649}
5650
5651#undef Py_LeaveRecursiveCall
5652
5653void Py_LeaveRecursiveCall(void)
5654{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005655 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005656}