blob: 0c08a76f7d11304f59a938ca9700edfd936e3737 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner4d231bc2019-11-14 13:36:21 +010014#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020015#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090016#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010017#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010018#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020019#include "pycore_pyerrors.h"
20#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner1c1e68c2020-03-27 15:11:45 +010023#include "pycore_sysmodule.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010024#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossumc6004111993-11-05 10:22:19 +000033#include <ctype.h>
34
Guido van Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000036/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define LLTRACE 1 /* Low-level trace feature */
38#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#endif
40
Victor Stinner5c75f372019-04-17 23:02:26 +020041#if !defined(Py_BUILD_CORE)
42# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43#endif
44
Hai Shi46874c22020-01-30 17:20:25 -060045_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000046
Guido van Rossum374a9221991-04-04 10:40:29 +000047/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020048Py_LOCAL_INLINE(PyObject *) call_function(
49 PyThreadState *tstate, PyObject ***pp_stack,
50 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
52 PyThreadState *tstate, PyObject *func,
53 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000054
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000056static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020057static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059static int call_trace(Py_tracefunc, PyObject *,
60 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000062static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010063 PyThreadState *, PyFrameObject *,
64 int, PyObject *);
65static void call_exc_trace(Py_tracefunc, PyObject *,
66 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000067static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068 PyThreadState *, PyFrameObject *,
69 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070070static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
71static void dtrace_function_entry(PyFrameObject *);
72static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000073
Victor Stinner438a12d2019-05-24 17:01:38 +020074static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000085static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900106#ifdef Py_DEBUG
107// --with-pydebug is used to find memory leak. opcache makes it harder.
108// So we disable opcache when Py_DEBUG is defined.
109// See bpo-37146
110#define OPCACHE_MIN_RUNS 0 /* disable opcache */
111#else
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900113#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900114#define OPCACHE_STATS 0 /* Enable stats */
115
116#if OPCACHE_STATS
117static size_t opcache_code_objects = 0;
118static size_t opcache_code_objects_extra_mem = 0;
119
120static size_t opcache_global_opts = 0;
121static size_t opcache_global_hits = 0;
122static size_t opcache_global_misses = 0;
123#endif
124
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100125
Victor Stinnerda2914d2020-03-20 09:29:08 +0100126#ifndef NDEBUG
127/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
128 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
129 when a thread continues to run after Python finalization, especially
130 daemon threads. */
131static int
132is_tstate_valid(PyThreadState *tstate)
133{
134 assert(!_PyMem_IsPtrFreed(tstate));
135 assert(!_PyMem_IsPtrFreed(tstate->interp));
136 return 1;
137}
138#endif
139
140
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000141/* This can set eval_breaker to 0 even though gil_drop_request became
142 1. We believe this is all right because the eval loop will release
143 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100144static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200145COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200146 struct _ceval_runtime_state *ceval,
147 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100148{
Victor Stinner299b8c62020-05-05 17:40:18 +0200149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200155 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100156}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200160SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100161{
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 struct _ceval_state *ceval2 = &interp->ceval;
163 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
164 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200169RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170{
Victor Stinner299b8c62020-05-05 17:40:18 +0200171 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
172 struct _ceval_state *ceval2 = &interp->ceval;
173 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
174 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Eric Snowfdf282d2019-01-11 14:26:55 -0700177
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200179SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180{
Victor Stinner299b8c62020-05-05 17:40:18 +0200181 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
184 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Eric Snowfdf282d2019-01-11 14:26:55 -0700186
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
196
197
198static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200199SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200203 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100204 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinner299b8c62020-05-05 17:40:18 +0200205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
207
208
209static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200210UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200214 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200215 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216}
217
218
219static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200220SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223 ceval2->pending.async_exc = 1;
224 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
225}
226
227
228static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200229UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100230{
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
232 struct _ceval_state *ceval2 = &interp->ceval;
233 ceval2->pending.async_exc = 0;
234 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236
237
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000240#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100243static void
244ensure_tstate_not_null(const char *func, PyThreadState *tstate)
245{
246 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100247 _Py_FatalErrorFunc(func,
248 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100249 }
250}
251
252
Tim Peters7f468f22004-10-11 02:40:51 +0000253int
Victor Stinner175a7042020-03-10 00:37:48 +0100254_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
255{
256 return gil_created(&runtime->ceval.gil);
257}
258
259int
Tim Peters7f468f22004-10-11 02:40:51 +0000260PyEval_ThreadsInitialized(void)
261{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100262 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100263 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000264}
265
Victor Stinner111e4ee2020-03-09 21:24:14 +0100266PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200267_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200269 if (!_Py_IsMainInterpreter(tstate)) {
270 /* Currently, the GIL is shared by all interpreters,
271 and only the main interpreter is responsible to create
272 and destroy it. */
273 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100274 }
275
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200276 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
277 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100278
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200279 PyThread_init_thread();
280 create_gil(gil);
281
282 take_gil(tstate);
283
284 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100285 return _PyStatus_OK();
286}
287
288void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200289_PyEval_FiniGIL(PyThreadState *tstate)
290{
291 if (!_Py_IsMainInterpreter(tstate)) {
292 /* Currently, the GIL is shared by all interpreters,
293 and only the main interpreter is responsible to create
294 and destroy it. */
295 return;
296 }
297
298 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
299 if (!gil_created(gil)) {
300 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
301 yet: do nothing. */
302 return;
303 }
304
305 destroy_gil(gil);
306 assert(!gil_created(gil));
307}
308
309void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100310PyEval_InitThreads(void)
311{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100312 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Inada Naoki91234a12019-06-03 21:30:58 +0900316_PyEval_Fini(void)
317{
318#if OPCACHE_STATS
319 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
320 opcache_code_objects);
321
322 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
323 opcache_code_objects_extra_mem);
324
325 fprintf(stderr, "\n");
326
327 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
328 opcache_global_hits,
329 (int) (100.0 * opcache_global_hits /
330 (opcache_global_hits + opcache_global_misses)));
331
332 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
333 opcache_global_misses,
334 (int) (100.0 * opcache_global_misses /
335 (opcache_global_hits + opcache_global_misses)));
336
337 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
338 opcache_global_opts);
339
340 fprintf(stderr, "\n");
341#endif
342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000346{
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200348 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100349 ensure_tstate_not_null(__func__, tstate);
350
Victor Stinner85f5a692020-03-09 22:12:04 +0100351 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352}
353
354void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000355PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356{
Victor Stinner09532fe2019-05-10 23:39:09 +0200357 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200358 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100360 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100361 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200362 struct _ceval_runtime_state *ceval = &runtime->ceval;
363 struct _ceval_state *ceval2 = &tstate->interp->ceval;
364 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365}
366
367void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100368_PyEval_ReleaseLock(PyThreadState *tstate)
369{
370 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200371 struct _ceval_state *ceval2 = &tstate->interp->ceval;
372 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100373}
374
375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000377{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100378 ensure_tstate_not_null(__func__, tstate);
379
Victor Stinner85f5a692020-03-09 22:12:04 +0100380 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200381
Victor Stinner85f5a692020-03-09 22:12:04 +0100382 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
383 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100384 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200385 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000386}
387
388void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000390{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100391 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200392
Victor Stinner01b1cc12019-11-20 02:27:56 +0100393 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200394 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
395 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100396 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200397 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200398 struct _ceval_runtime_state *ceval = &runtime->ceval;
399 struct _ceval_state *ceval2 = &tstate->interp->ceval;
400 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000401}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900403#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200404/* This function is called from PyOS_AfterFork_Child to destroy all threads
405 * which are not running in the child process, and clear internal locks
406 * which might be held by those threads.
407 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000408
409void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200410_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000411{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100412 struct _gil_runtime_state *gil = &runtime->ceval.gil;
413 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200415 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100416 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100417 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
418 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100419
420 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700421
Victor Stinner50e6e992020-03-19 02:41:21 +0100422 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900423 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700424 Py_FatalError("Can't initialize threads for pending calls");
425 }
Jesse Nollera8513972008-07-17 16:49:17 +0000426
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200427 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100428 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000429}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900430#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000431
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000432/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600433 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000434
435void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100436_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000437{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200438 assert(is_tstate_valid(tstate));
439 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000440}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000441
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000442PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000444{
Victor Stinner09532fe2019-05-10 23:39:09 +0200445 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200446 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100447 ensure_tstate_not_null(__func__, tstate);
448
Victor Stinner0b1e3302020-05-05 16:14:31 +0200449 struct _ceval_runtime_state *ceval = &runtime->ceval;
450 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200451 assert(gil_created(&ceval->gil));
Victor Stinner0b1e3302020-05-05 16:14:31 +0200452 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000457PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100459 ensure_tstate_not_null(__func__, tstate);
460
Victor Stinner85f5a692020-03-09 22:12:04 +0100461 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100462
Victor Stinner85f5a692020-03-09 22:12:04 +0100463 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
464 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000465}
466
467
Guido van Rossuma9672091994-09-14 13:31:22 +0000468/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
469 signal handlers or Mac I/O completion routines) can schedule calls
470 to a function to be called synchronously.
471 The synchronous function is called with one void* argument.
472 It should return 0 for success or -1 for failure -- failure should
473 be accompanied by an exception.
474
475 If registry succeeds, the registry function returns 0; if it fails
476 (e.g. due to too many pending calls) it returns -1 (without setting
477 an exception condition).
478
479 Note that because registry may occur from within signal handlers,
480 or other asynchronous events, calling malloc() is unsafe!
481
Guido van Rossuma9672091994-09-14 13:31:22 +0000482 Any thread can schedule pending calls, but only the main thread
483 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484 There is no facility to schedule calls to a particular thread, but
485 that should be easy to change, should that ever be required. In
486 that case, the static variables here should go into the python
487 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000489
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200490void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200491_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200492{
493 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100494 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200495 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200496 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200497}
498
Eric Snow5be45a62019-03-08 22:47:07 -0700499/* Push one item onto the queue while holding the lock. */
500static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200501_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600502 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700503{
Eric Snow842a2f02019-03-15 15:47:51 -0600504 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700505 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600506 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700507 return -1; /* Queue full */
508 }
Eric Snow842a2f02019-03-15 15:47:51 -0600509 pending->calls[i].func = func;
510 pending->calls[i].arg = arg;
511 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700512 return 0;
513}
514
515/* Pop one item off the queue while holding the lock. */
516static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200517_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600518 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700519{
Eric Snow842a2f02019-03-15 15:47:51 -0600520 int i = pending->first;
521 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700522 return; /* Queue empty */
523 }
524
Eric Snow842a2f02019-03-15 15:47:51 -0600525 *func = pending->calls[i].func;
526 *arg = pending->calls[i].arg;
527 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700528}
529
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200530/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000531 scheduling to be made from any thread, and even from an executing
532 callback.
533 */
534
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200536_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200537 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000538{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200539 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600540
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200541 /* Ensure that _PyEval_InitPendingCalls() was called
542 and that _PyEval_FiniPendingCalls() is not called yet. */
543 assert(pending->lock != NULL);
544
Eric Snow842a2f02019-03-15 15:47:51 -0600545 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200546 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600547 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700548
Victor Stinnere225beb2019-06-03 18:14:24 +0200549 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200550 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000552}
553
Victor Stinner09532fe2019-05-10 23:39:09 +0200554int
555Py_AddPendingCall(int (*func)(void *), void *arg)
556{
Victor Stinner50e6e992020-03-19 02:41:21 +0100557 /* Best-effort to support subinterpreters and calls with the GIL released.
558
559 First attempt _PyThreadState_GET() since it supports subinterpreters.
560
561 If the GIL is released, _PyThreadState_GET() returns NULL . In this
562 case, use PyGILState_GetThisThreadState() which works even if the GIL
563 is released.
564
565 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
566 see bpo-10915 and bpo-15751.
567
Victor Stinner8849e592020-03-18 19:28:53 +0100568 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100569 PyThreadState *tstate = _PyThreadState_GET();
570 if (tstate == NULL) {
571 tstate = PyGILState_GetThisThreadState();
572 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200573
574 PyInterpreterState *interp;
575 if (tstate != NULL) {
576 interp = tstate->interp;
577 }
578 else {
579 /* Last resort: use the main interpreter */
580 interp = _PyRuntime.interpreters.main;
581 }
582 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200583}
584
Eric Snowfdf282d2019-01-11 14:26:55 -0700585static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100586handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700587{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200588 assert(is_tstate_valid(tstate));
589 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700590 return 0;
591 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700592
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200593 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100594 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
595 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200596 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700597 return -1;
598 }
599 return 0;
600}
601
602static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100603make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200605 assert(is_tstate_valid(tstate));
606
Victor Stinnerd8316882020-03-20 14:50:35 +0100607 /* only execute pending calls on main thread */
608 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200609 return 0;
610 }
611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100613 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700614 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700616 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200617 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100618
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200619 /* unsignal before starting to call callbacks, so that any callback
620 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200621 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700622 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100625 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700626 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700627 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 void *arg = NULL;
629
630 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600631 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200632 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600633 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700634
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100635 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700636 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100637 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700638 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700639 res = func(arg);
640 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200641 goto error;
642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200644
Charles-François Natalif23339a2011-07-23 18:15:43 +0200645 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700646 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200647
648error:
649 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200650 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700651 return res;
652}
653
Eric Snow842a2f02019-03-15 15:47:51 -0600654void
Victor Stinner2b1df452020-01-13 18:46:59 +0100655_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600656{
Eric Snow842a2f02019-03-15 15:47:51 -0600657 assert(PyGILState_Check());
658
Victor Stinner50e6e992020-03-19 02:41:21 +0100659 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200660
Eric Snow842a2f02019-03-15 15:47:51 -0600661 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
662 return;
663 }
664
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100665 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200666 PyObject *exc, *val, *tb;
667 _PyErr_Fetch(tstate, &exc, &val, &tb);
668 PyErr_BadInternalCall();
669 _PyErr_ChainExceptions(exc, val, tb);
670 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600671 }
672}
673
Eric Snowfdf282d2019-01-11 14:26:55 -0700674/* Py_MakePendingCalls() is a simple wrapper for the sake
675 of backward-compatibility. */
676int
677Py_MakePendingCalls(void)
678{
679 assert(PyGILState_Check());
680
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100681 PyThreadState *tstate = _PyThreadState_GET();
682
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 /* Python signal handler doesn't really queue a callback: it only signals
684 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100685 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700686 if (res != 0) {
687 return res;
688 }
689
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100690 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600691 if (res != 0) {
692 return res;
693 }
694
695 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000696}
697
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698/* The interpreter's recursion limit */
699
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000700#ifndef Py_DEFAULT_RECURSION_LIMIT
701#define Py_DEFAULT_RECURSION_LIMIT 1000
702#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600703
Eric Snow05351c12017-09-05 21:43:08 -0700704int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600706void
Victor Stinnerdab84232020-03-17 18:56:44 +0100707_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600708{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600709 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100710 _gil_initialize(&ceval->gil);
711}
712
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200713int
Victor Stinnerdab84232020-03-17 18:56:44 +0100714_PyEval_InitState(struct _ceval_state *ceval)
715{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200716 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
717
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200718 struct _pending_calls *pending = &ceval->pending;
719 assert(pending->lock == NULL);
720
721 pending->lock = PyThread_allocate_lock();
722 if (pending->lock == NULL) {
723 return -1;
724 }
725 return 0;
726}
727
728void
729_PyEval_FiniState(struct _ceval_state *ceval)
730{
731 struct _pending_calls *pending = &ceval->pending;
732 if (pending->lock != NULL) {
733 PyThread_free_lock(pending->lock);
734 pending->lock = NULL;
735 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600736}
737
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000738int
739Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000740{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200741 PyThreadState *tstate = _PyThreadState_GET();
742 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000743}
744
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000745void
746Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000747{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200748 PyThreadState *tstate = _PyThreadState_GET();
749 tstate->interp->ceval.recursion_limit = new_limit;
750 if (_Py_IsMainInterpreter(tstate)) {
751 _Py_CheckRecursionLimit = new_limit;
752 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000753}
754
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100755/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000756 if the recursion_depth reaches _Py_CheckRecursionLimit.
757 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
758 to guarantee that _Py_CheckRecursiveCall() is regularly called.
759 Without USE_STACKCHECK, there is no need for this. */
760int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100761_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000762{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200763 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000764
765#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700766 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (PyOS_CheckStack()) {
768 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200769 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return -1;
771 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200772 if (_Py_IsMainInterpreter(tstate)) {
773 /* Needed for ABI backwards-compatibility (see bpo-31857) */
774 _Py_CheckRecursionLimit = recursion_limit;
775 }
pdox18967932017-10-25 23:03:01 -0700776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (tstate->recursion_critical)
778 /* Somebody asked that we don't check for recursion. */
779 return 0;
780 if (tstate->overflowed) {
781 if (tstate->recursion_depth > recursion_limit + 50) {
782 /* Overflowing while handling an overflow. Give up. */
783 Py_FatalError("Cannot recover from stack overflow.");
784 }
785 return 0;
786 }
787 if (tstate->recursion_depth > recursion_limit) {
788 --tstate->recursion_depth;
789 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200790 _PyErr_Format(tstate, PyExc_RecursionError,
791 "maximum recursion depth exceeded%s",
792 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 return -1;
794 }
795 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000796}
797
Victor Stinner09532fe2019-05-10 23:39:09 +0200798static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200799static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000800
Victor Stinnere225beb2019-06-03 18:14:24 +0200801#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000802
Guido van Rossum374a9221991-04-04 10:40:29 +0000803
Guido van Rossumb209a111997-04-29 18:18:01 +0000804PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000805PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 return PyEval_EvalCodeEx(co,
808 globals, locals,
809 (PyObject **)NULL, 0,
810 (PyObject **)NULL, 0,
811 (PyObject **)NULL, 0,
812 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000813}
814
815
816/* Interpreter main loop */
817
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000818PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100819PyEval_EvalFrame(PyFrameObject *f)
820{
Victor Stinner0b72b232020-03-12 23:18:39 +0100821 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100822 PyThreadState *tstate = _PyThreadState_GET();
823 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000824}
825
826PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000827PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000828{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100829 PyThreadState *tstate = _PyThreadState_GET();
830 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700831}
832
Victor Stinnerda2914d2020-03-20 09:29:08 +0100833
834/* Handle signals, pending calls, GIL drop request
835 and asynchronous exception */
836static int
837eval_frame_handle_pending(PyThreadState *tstate)
838{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100839 _PyRuntimeState * const runtime = &_PyRuntime;
840 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200841
842 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200843 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100844 if (handle_signals(tstate) != 0) {
845 return -1;
846 }
847 }
848
849 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200850 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100851 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
852 if (make_pending_calls(tstate) != 0) {
853 return -1;
854 }
855 }
856
857 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200858 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100859 /* Give another thread a chance */
860 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
861 Py_FatalError("tstate mix-up");
862 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200863 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100864
865 /* Other threads may run now */
866
867 take_gil(tstate);
868
869 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
870 Py_FatalError("orphan tstate");
871 }
872 }
873
874 /* Check for asynchronous exception. */
875 if (tstate->async_exc != NULL) {
876 PyObject *exc = tstate->async_exc;
877 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200878 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100879 _PyErr_SetNone(tstate, exc);
880 Py_DECREF(exc);
881 return -1;
882 }
883
884 return 0;
885}
886
Victor Stinnerc6944e72016-11-11 02:13:35 +0100887PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100888_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700889{
Victor Stinner0b72b232020-03-12 23:18:39 +0100890 ensure_tstate_not_null(__func__, tstate);
891
Guido van Rossum950361c1997-01-24 13:49:28 +0000892#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000894#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200895 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300896 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200897 int opcode; /* Current opcode */
898 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200899 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100901 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100902 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 is true when the line being executed has changed. The
910 initial values are such as to make this false the first
911 time it is tested. */
912 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000913
Serhiy Storchakaab874002016-09-11 13:48:15 +0300914 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PyObject *names;
916 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900917 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000918
Brett Cannon368b4b72012-04-02 12:17:59 -0400919#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200920 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400921#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200922
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923/* Computed GOTOs, or
924 the-optimization-commonly-but-improperly-known-as-"threaded code"
925 using gcc's labels-as-values extension
926 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
927
928 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000930 combined with a lookup table of jump addresses. However, since the
931 indirect jump instruction is shared by all opcodes, the CPU will have a
932 hard time making the right prediction for where to jump next (actually,
933 it will be always wrong except in the uncommon case of a sequence of
934 several identical opcodes).
935
936 "Threaded code" in contrast, uses an explicit jump table and an explicit
937 indirect jump instruction at the end of each opcode. Since the jump
938 instruction is at a different address for each opcode, the CPU will make a
939 separate prediction for each of these instructions, which is equivalent to
940 predicting the second opcode of each opcode pair. These predictions have
941 a much better chance to turn out valid, especially in small bytecode loops.
942
943 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000945 and potentially many more instructions (depending on the pipeline width).
946 A correctly predicted branch, however, is nearly free.
947
948 At the time of this writing, the "threaded code" version is up to 15-20%
949 faster than the normal "switch" version, depending on the compiler and the
950 CPU architecture.
951
952 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
953 because it would render the measurements invalid.
954
955
956 NOTE: care must be taken that the compiler doesn't try to "optimize" the
957 indirect jumps by sharing them between all opcodes. Such optimizations
958 can be disabled on gcc by using the -fno-gcse flag (or possibly
959 -fno-crossjumping).
960*/
961
Antoine Pitrou042b1282010-08-13 21:15:58 +0000962#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000963#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000964#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000965#endif
966
Antoine Pitrou042b1282010-08-13 21:15:58 +0000967#ifdef HAVE_COMPUTED_GOTOS
968 #ifndef USE_COMPUTED_GOTOS
969 #define USE_COMPUTED_GOTOS 1
970 #endif
971#else
972 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
973 #error "Computed gotos are not supported on this compiler."
974 #endif
975 #undef USE_COMPUTED_GOTOS
976 #define USE_COMPUTED_GOTOS 0
977#endif
978
979#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000980/* Import the static jump table */
981#include "opcode_targets.h"
982
Antoine Pitroub52ec782009-01-25 16:34:23 +0000983#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700984 op: \
985 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000986
Antoine Pitroub52ec782009-01-25 16:34:23 +0000987#ifdef LLTRACE
988#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100990 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300992 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300993 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 } \
995 goto fast_next_opcode; \
996 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000997#else
998#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001000 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001002 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001003 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 } \
1005 goto fast_next_opcode; \
1006 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001007#endif
1008
Victor Stinner09532fe2019-05-10 23:39:09 +02001009#define DISPATCH() \
1010 { \
1011 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1012 FAST_DISPATCH(); \
1013 } \
1014 continue; \
1015 }
1016
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001018#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001020#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021#endif
1022
1023
Neal Norwitza81d2202002-07-14 00:27:26 +00001024/* Tuple access macros */
1025
1026#ifndef Py_DEBUG
1027#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1028#else
1029#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1030#endif
1031
Guido van Rossum374a9221991-04-04 10:40:29 +00001032/* Code access macros */
1033
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001034/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001035#define INSTR_OFFSET() \
1036 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001037#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001038 _Py_CODEUNIT word = *next_instr; \
1039 opcode = _Py_OPCODE(word); \
1040 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001041 next_instr++; \
1042 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001043#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1044#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001045
Raymond Hettingerf606f872003-03-16 03:11:04 +00001046/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 Some opcodes tend to come in pairs thus making it possible to
1048 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001049 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 Verifying the prediction costs a single high-speed test of a register
1052 variable against a constant. If the pairing was good, then the
1053 processor's own internal branch predication has a high likelihood of
1054 success, resulting in a nearly zero-overhead transition to the
1055 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001056 including its unpredictable switch-case branch. Combined with the
1057 processor's internal branch prediction, a successful PREDICT has the
1058 effect of making the two opcodes run as if they were a single new opcode
1059 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001060
Georg Brandl86b2fb92008-07-16 03:43:04 +00001061 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 predictions turned-on and interpret the results as if some opcodes
1063 had been combined or turn-off predictions so that the opcode frequency
1064 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001065
1066 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 the CPU to record separate branch prediction information for each
1068 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001069
Raymond Hettingerf606f872003-03-16 03:11:04 +00001070*/
1071
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001072#define PREDICT_ID(op) PRED_##op
1073
Antoine Pitrou042b1282010-08-13 21:15:58 +00001074#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001075#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001076#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001077#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001078 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001079 _Py_CODEUNIT word = *next_instr; \
1080 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001081 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001082 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001083 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001084 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001085 } \
1086 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001087#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001088#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001089
Raymond Hettingerf606f872003-03-16 03:11:04 +00001090
Guido van Rossum374a9221991-04-04 10:40:29 +00001091/* Stack manipulation macros */
1092
Martin v. Löwis18e16552006-02-15 17:27:45 +00001093/* The stack can grow at most MAXINT deep, as co_nlocals and
1094 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001095#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1096#define EMPTY() (STACK_LEVEL() == 0)
1097#define TOP() (stack_pointer[-1])
1098#define SECOND() (stack_pointer[-2])
1099#define THIRD() (stack_pointer[-3])
1100#define FOURTH() (stack_pointer[-4])
1101#define PEEK(n) (stack_pointer[-(n)])
1102#define SET_TOP(v) (stack_pointer[-1] = (v))
1103#define SET_SECOND(v) (stack_pointer[-2] = (v))
1104#define SET_THIRD(v) (stack_pointer[-3] = (v))
1105#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1106#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1107#define BASIC_STACKADJ(n) (stack_pointer += n)
1108#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1109#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001110
Guido van Rossum96a42c81992-01-12 02:29:51 +00001111#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001113 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001114 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001115#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001116 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001117#define STACK_GROW(n) do { \
1118 assert(n >= 0); \
1119 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001120 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001121 assert(STACK_LEVEL() <= co->co_stacksize); \
1122 } while (0)
1123#define STACK_SHRINK(n) do { \
1124 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001125 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001126 (void)(BASIC_STACKADJ(-n)); \
1127 assert(STACK_LEVEL() <= co->co_stacksize); \
1128 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001129#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001130 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001131 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001132#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001133#define PUSH(v) BASIC_PUSH(v)
1134#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001135#define STACK_GROW(n) BASIC_STACKADJ(n)
1136#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001137#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001138#endif
1139
Guido van Rossum681d79a1995-07-18 14:51:37 +00001140/* Local variable macros */
1141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001143
1144/* The SETLOCAL() macro must not DECREF the local variable in-place and
1145 then store the new value; it must copy the old value to a temporary
1146 value, then store the new value, and then DECREF the temporary value.
1147 This is because it is possible that during the DECREF the frame is
1148 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1149 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001151 GETLOCAL(i) = value; \
1152 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001153
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001154
1155#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 while (STACK_LEVEL() > (b)->b_level) { \
1157 PyObject *v = POP(); \
1158 Py_XDECREF(v); \
1159 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001160
1161#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001162 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001164 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1166 while (STACK_LEVEL() > (b)->b_level + 3) { \
1167 value = POP(); \
1168 Py_XDECREF(value); \
1169 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001170 exc_info = tstate->exc_info; \
1171 type = exc_info->exc_type; \
1172 value = exc_info->exc_value; \
1173 traceback = exc_info->exc_traceback; \
1174 exc_info->exc_type = POP(); \
1175 exc_info->exc_value = POP(); \
1176 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 Py_XDECREF(type); \
1178 Py_XDECREF(value); \
1179 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001180 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001181
Inada Naoki91234a12019-06-03 21:30:58 +09001182 /* macros for opcode cache */
1183#define OPCACHE_CHECK() \
1184 do { \
1185 co_opcache = NULL; \
1186 if (co->co_opcache != NULL) { \
1187 unsigned char co_opt_offset = \
1188 co->co_opcache_map[next_instr - first_instr]; \
1189 if (co_opt_offset > 0) { \
1190 assert(co_opt_offset <= co->co_opcache_size); \
1191 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1192 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001193 } \
1194 } \
1195 } while (0)
1196
1197#if OPCACHE_STATS
1198
1199#define OPCACHE_STAT_GLOBAL_HIT() \
1200 do { \
1201 if (co->co_opcache != NULL) opcache_global_hits++; \
1202 } while (0)
1203
1204#define OPCACHE_STAT_GLOBAL_MISS() \
1205 do { \
1206 if (co->co_opcache != NULL) opcache_global_misses++; \
1207 } while (0)
1208
1209#define OPCACHE_STAT_GLOBAL_OPT() \
1210 do { \
1211 if (co->co_opcache != NULL) opcache_global_opts++; \
1212 } while (0)
1213
1214#else /* OPCACHE_STATS */
1215
1216#define OPCACHE_STAT_GLOBAL_HIT()
1217#define OPCACHE_STAT_GLOBAL_MISS()
1218#define OPCACHE_STAT_GLOBAL_OPT()
1219
1220#endif
1221
Guido van Rossuma027efa1997-05-05 20:56:21 +00001222/* Start of code */
1223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001225 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001227 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (tstate->use_tracing) {
1232 if (tstate->c_tracefunc != NULL) {
1233 /* tstate->c_tracefunc, if defined, is a
1234 function that will be called on *every* entry
1235 to a code block. Its return value, if not
1236 None, is a function that will be called at
1237 the start of each executed line of code.
1238 (Actually, the function must return itself
1239 in order to continue tracing.) The trace
1240 functions are called with three arguments:
1241 a pointer to the current frame, a string
1242 indicating why the function is called, and
1243 an argument which depends on the situation.
1244 The global trace function is also called
1245 whenever an exception is detected. */
1246 if (call_trace_protected(tstate->c_tracefunc,
1247 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001248 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 /* Trace function raised an error */
1250 goto exit_eval_frame;
1251 }
1252 }
1253 if (tstate->c_profilefunc != NULL) {
1254 /* Similar for c_profilefunc, except it needn't
1255 return itself and isn't called for "line" events */
1256 if (call_trace_protected(tstate->c_profilefunc,
1257 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001258 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Profile function raised an error */
1260 goto exit_eval_frame;
1261 }
1262 }
1263 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001264
Łukasz Langaa785c872016-09-09 17:37:37 -07001265 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1266 dtrace_function_entry(f);
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 co = f->f_code;
1269 names = co->co_names;
1270 consts = co->co_consts;
1271 fastlocals = f->f_localsplus;
1272 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001273 assert(PyBytes_Check(co->co_code));
1274 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001275 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1276 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1277 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001278 /*
1279 f->f_lasti refers to the index of the last instruction,
1280 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001281
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001282 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001283 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 When the PREDICT() macros are enabled, some opcode pairs follow in
1286 direct succession without updating f->f_lasti. A successful
1287 prediction effectively links the two codes together as if they
1288 were a single new opcode; accordingly,f->f_lasti will point to
1289 the first code in the pair (for instance, GET_ITER followed by
1290 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001291 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001293 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001294 next_instr = first_instr;
1295 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001296 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1297 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 stack_pointer = f->f_stacktop;
1300 assert(stack_pointer != NULL);
1301 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001302 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001303
Inada Naoki91234a12019-06-03 21:30:58 +09001304 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1305 co->co_opcache_flag++;
1306 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1307 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001308 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001309 }
1310#if OPCACHE_STATS
1311 opcache_code_objects_extra_mem +=
1312 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1313 sizeof(_PyOpcache) * co->co_opcache_size;
1314 opcache_code_objects++;
1315#endif
1316 }
1317 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001318
Tim Peters5ca576e2001-06-18 22:08:13 +00001319#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001320 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001321#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001323 if (throwflag) /* support for generator.throw() */
1324 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001325
Victor Stinnerace47d72013-07-18 01:41:08 +02001326#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001327 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001328 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001329 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001330 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001331#endif
1332
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001333main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1336 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001337 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 /* Do periodic things. Doing this every time through
1340 the loop would add too much overhead, so we do it
1341 only every Nth instruction. We also do it if
1342 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1343 event needs attention (e.g. a signal handler or
1344 async I/O handler); see Py_AddPendingCall() and
1345 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001346
Eric Snow7bda9de2019-03-08 17:25:54 -07001347 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001348 opcode = _Py_OPCODE(*next_instr);
1349 if (opcode == SETUP_FINALLY ||
1350 opcode == SETUP_WITH ||
1351 opcode == BEFORE_ASYNC_WITH ||
1352 opcode == YIELD_FROM) {
1353 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001354 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001355 - If we're about to enter the 'with:'. It will prevent
1356 emitting a resource warning in the common idiom
1357 'with open(path) as file:'.
1358 - If we're about to enter the 'async with:'.
1359 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001360 *very* useful, but might help in some cases and it's
1361 traditional)
1362 - If we're resuming a chain of nested 'yield from' or
1363 'await' calls, then each frame is parked with YIELD_FROM
1364 as its next opcode. If the user hit control-C we want to
1365 wait until we've reached the innermost frame before
1366 running the signal handler and raising KeyboardInterrupt
1367 (see bpo-30039).
1368 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 goto fast_next_opcode;
1370 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001371
Victor Stinnerda2914d2020-03-20 09:29:08 +01001372 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 }
1375 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 fast_next_opcode:
1378 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Łukasz Langaa785c872016-09-09 17:37:37 -07001380 if (PyDTrace_LINE_ENABLED())
1381 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001384
Victor Stinnerdab84232020-03-17 18:56:44 +01001385 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001386 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* see maybe_call_line_trace
1389 for expository comments */
1390 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 err = maybe_call_line_trace(tstate->c_tracefunc,
1393 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001394 tstate, f,
1395 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* Reload possibly changed frame fields */
1397 JUMPTO(f->f_lasti);
1398 if (f->f_stacktop != NULL) {
1399 stack_pointer = f->f_stacktop;
1400 f->f_stacktop = NULL;
1401 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001408
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001409 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001410 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001411#ifdef DYNAMIC_EXECUTION_PROFILE
1412#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 dxpairs[lastopcode][opcode]++;
1414 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001415#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001417#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001418
Guido van Rossum96a42c81992-01-12 02:29:51 +00001419#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 if (lltrace) {
1423 if (HAS_ARG(opcode)) {
1424 printf("%d: %d, %d\n",
1425 f->f_lasti, opcode, oparg);
1426 }
1427 else {
1428 printf("%d: %d\n",
1429 f->f_lasti, opcode);
1430 }
1431 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001432#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001437 It is essential that any operation that fails must goto error
1438 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Benjamin Petersonddd19492018-09-16 22:38:02 -07001440 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001442 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001443
Benjamin Petersonddd19492018-09-16 22:38:02 -07001444 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 PyObject *value = GETLOCAL(oparg);
1446 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001447 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 UNBOUNDLOCAL_ERROR_MSG,
1449 PyTuple_GetItem(co->co_varnames, oparg));
1450 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 Py_INCREF(value);
1453 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 }
1456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(LOAD_CONST): {
1458 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *value = GETITEM(consts, oparg);
1460 Py_INCREF(value);
1461 PUSH(value);
1462 FAST_DISPATCH();
1463 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(STORE_FAST): {
1466 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001467 PyObject *value = POP();
1468 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *value = POP();
1474 Py_DECREF(value);
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_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001479 PyObject *top = TOP();
1480 PyObject *second = SECOND();
1481 SET_TOP(second);
1482 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 PyObject *top = TOP();
1488 PyObject *second = SECOND();
1489 PyObject *third = THIRD();
1490 SET_TOP(second);
1491 SET_SECOND(third);
1492 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001497 PyObject *top = TOP();
1498 PyObject *second = SECOND();
1499 PyObject *third = THIRD();
1500 PyObject *fourth = FOURTH();
1501 SET_TOP(second);
1502 SET_SECOND(third);
1503 SET_THIRD(fourth);
1504 SET_FOURTH(top);
1505 FAST_DISPATCH();
1506 }
1507
Benjamin Petersonddd19492018-09-16 22:38:02 -07001508 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *top = TOP();
1510 Py_INCREF(top);
1511 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Benjamin Petersonddd19492018-09-16 22:38:02 -07001515 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001516 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001518 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001520 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 SET_TOP(top);
1522 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001523 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001525
Benjamin Petersonddd19492018-09-16 22:38:02 -07001526 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 PyObject *value = TOP();
1528 PyObject *res = PyNumber_Positive(value);
1529 Py_DECREF(value);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 PyObject *value = TOP();
1538 PyObject *res = PyNumber_Negative(value);
1539 Py_DECREF(value);
1540 SET_TOP(res);
1541 if (res == NULL)
1542 goto error;
1543 DISPATCH();
1544 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Benjamin Petersonddd19492018-09-16 22:38:02 -07001546 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 PyObject *value = TOP();
1548 int err = PyObject_IsTrue(value);
1549 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (err == 0) {
1551 Py_INCREF(Py_True);
1552 SET_TOP(Py_True);
1553 DISPATCH();
1554 }
1555 else if (err > 0) {
1556 Py_INCREF(Py_False);
1557 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 DISPATCH();
1559 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001560 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 goto error;
1562 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Benjamin Petersonddd19492018-09-16 22:38:02 -07001564 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 PyObject *value = TOP();
1566 PyObject *res = PyNumber_Invert(value);
1567 Py_DECREF(value);
1568 SET_TOP(res);
1569 if (res == NULL)
1570 goto error;
1571 DISPATCH();
1572 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Benjamin Petersonddd19492018-09-16 22:38:02 -07001574 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 PyObject *exp = POP();
1576 PyObject *base = TOP();
1577 PyObject *res = PyNumber_Power(base, exp, Py_None);
1578 Py_DECREF(base);
1579 Py_DECREF(exp);
1580 SET_TOP(res);
1581 if (res == NULL)
1582 goto error;
1583 DISPATCH();
1584 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Benjamin Petersonddd19492018-09-16 22:38:02 -07001586 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001587 PyObject *right = POP();
1588 PyObject *left = TOP();
1589 PyObject *res = PyNumber_Multiply(left, right);
1590 Py_DECREF(left);
1591 Py_DECREF(right);
1592 SET_TOP(res);
1593 if (res == NULL)
1594 goto error;
1595 DISPATCH();
1596 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Benjamin Petersonddd19492018-09-16 22:38:02 -07001598 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001599 PyObject *right = POP();
1600 PyObject *left = TOP();
1601 PyObject *res = PyNumber_MatrixMultiply(left, right);
1602 Py_DECREF(left);
1603 Py_DECREF(right);
1604 SET_TOP(res);
1605 if (res == NULL)
1606 goto error;
1607 DISPATCH();
1608 }
1609
Benjamin Petersonddd19492018-09-16 22:38:02 -07001610 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 PyObject *divisor = POP();
1612 PyObject *dividend = TOP();
1613 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1614 Py_DECREF(dividend);
1615 Py_DECREF(divisor);
1616 SET_TOP(quotient);
1617 if (quotient == NULL)
1618 goto error;
1619 DISPATCH();
1620 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Benjamin Petersonddd19492018-09-16 22:38:02 -07001622 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001623 PyObject *divisor = POP();
1624 PyObject *dividend = TOP();
1625 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1626 Py_DECREF(dividend);
1627 Py_DECREF(divisor);
1628 SET_TOP(quotient);
1629 if (quotient == NULL)
1630 goto error;
1631 DISPATCH();
1632 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001633
Benjamin Petersonddd19492018-09-16 22:38:02 -07001634 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 PyObject *divisor = POP();
1636 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001637 PyObject *res;
1638 if (PyUnicode_CheckExact(dividend) && (
1639 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1640 // fast path; string formatting, but not if the RHS is a str subclass
1641 // (see issue28598)
1642 res = PyUnicode_Format(dividend, divisor);
1643 } else {
1644 res = PyNumber_Remainder(dividend, divisor);
1645 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 Py_DECREF(divisor);
1647 Py_DECREF(dividend);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Benjamin Petersonddd19492018-09-16 22:38:02 -07001654 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001658 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1659 CPython using bytecode, it is simply worthless.
1660 See http://bugs.python.org/issue21955 and
1661 http://bugs.python.org/issue10044 for the discussion. In short,
1662 no patch shown any impact on a realistic benchmark, only a minor
1663 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 if (PyUnicode_CheckExact(left) &&
1665 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001666 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001667 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001668 }
1669 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 sum = PyNumber_Add(left, right);
1671 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001672 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 Py_DECREF(right);
1674 SET_TOP(sum);
1675 if (sum == NULL)
1676 goto error;
1677 DISPATCH();
1678 }
1679
Benjamin Petersonddd19492018-09-16 22:38:02 -07001680 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 PyObject *right = POP();
1682 PyObject *left = TOP();
1683 PyObject *diff = PyNumber_Subtract(left, right);
1684 Py_DECREF(right);
1685 Py_DECREF(left);
1686 SET_TOP(diff);
1687 if (diff == NULL)
1688 goto error;
1689 DISPATCH();
1690 }
1691
Benjamin Petersonddd19492018-09-16 22:38:02 -07001692 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 PyObject *sub = POP();
1694 PyObject *container = TOP();
1695 PyObject *res = PyObject_GetItem(container, sub);
1696 Py_DECREF(container);
1697 Py_DECREF(sub);
1698 SET_TOP(res);
1699 if (res == NULL)
1700 goto error;
1701 DISPATCH();
1702 }
1703
Benjamin Petersonddd19492018-09-16 22:38:02 -07001704 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 PyObject *right = POP();
1706 PyObject *left = TOP();
1707 PyObject *res = PyNumber_Lshift(left, right);
1708 Py_DECREF(left);
1709 Py_DECREF(right);
1710 SET_TOP(res);
1711 if (res == NULL)
1712 goto error;
1713 DISPATCH();
1714 }
1715
Benjamin Petersonddd19492018-09-16 22:38:02 -07001716 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 PyObject *right = POP();
1718 PyObject *left = TOP();
1719 PyObject *res = PyNumber_Rshift(left, right);
1720 Py_DECREF(left);
1721 Py_DECREF(right);
1722 SET_TOP(res);
1723 if (res == NULL)
1724 goto error;
1725 DISPATCH();
1726 }
1727
Benjamin Petersonddd19492018-09-16 22:38:02 -07001728 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 PyObject *right = POP();
1730 PyObject *left = TOP();
1731 PyObject *res = PyNumber_And(left, right);
1732 Py_DECREF(left);
1733 Py_DECREF(right);
1734 SET_TOP(res);
1735 if (res == NULL)
1736 goto error;
1737 DISPATCH();
1738 }
1739
Benjamin Petersonddd19492018-09-16 22:38:02 -07001740 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001741 PyObject *right = POP();
1742 PyObject *left = TOP();
1743 PyObject *res = PyNumber_Xor(left, right);
1744 Py_DECREF(left);
1745 Py_DECREF(right);
1746 SET_TOP(res);
1747 if (res == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
1751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 PyObject *right = POP();
1754 PyObject *left = TOP();
1755 PyObject *res = PyNumber_Or(left, right);
1756 Py_DECREF(left);
1757 Py_DECREF(right);
1758 SET_TOP(res);
1759 if (res == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
1763
Benjamin Petersonddd19492018-09-16 22:38:02 -07001764 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001765 PyObject *v = POP();
1766 PyObject *list = PEEK(oparg);
1767 int err;
1768 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 if (err != 0)
1771 goto error;
1772 PREDICT(JUMP_ABSOLUTE);
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Petersonddd19492018-09-16 22:38:02 -07001776 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001778 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 int err;
1780 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 if (err != 0)
1783 goto error;
1784 PREDICT(JUMP_ABSOLUTE);
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Petersonddd19492018-09-16 22:38:02 -07001788 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 PyObject *exp = POP();
1790 PyObject *base = TOP();
1791 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1792 Py_DECREF(base);
1793 Py_DECREF(exp);
1794 SET_TOP(res);
1795 if (res == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Petersonddd19492018-09-16 22:38:02 -07001800 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Petersonddd19492018-09-16 22:38:02 -07001812 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
1823
Benjamin Petersonddd19492018-09-16 22:38:02 -07001824 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 PyObject *divisor = POP();
1826 PyObject *dividend = TOP();
1827 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1828 Py_DECREF(dividend);
1829 Py_DECREF(divisor);
1830 SET_TOP(quotient);
1831 if (quotient == NULL)
1832 goto error;
1833 DISPATCH();
1834 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Benjamin Petersonddd19492018-09-16 22:38:02 -07001836 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 PyObject *divisor = POP();
1838 PyObject *dividend = TOP();
1839 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1840 Py_DECREF(dividend);
1841 Py_DECREF(divisor);
1842 SET_TOP(quotient);
1843 if (quotient == NULL)
1844 goto error;
1845 DISPATCH();
1846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Benjamin Petersonddd19492018-09-16 22:38:02 -07001848 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001849 PyObject *right = POP();
1850 PyObject *left = TOP();
1851 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1852 Py_DECREF(left);
1853 Py_DECREF(right);
1854 SET_TOP(mod);
1855 if (mod == NULL)
1856 goto error;
1857 DISPATCH();
1858 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Benjamin Petersonddd19492018-09-16 22:38:02 -07001860 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 PyObject *right = POP();
1862 PyObject *left = TOP();
1863 PyObject *sum;
1864 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001865 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001866 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001867 }
1868 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 sum = PyNumber_InPlaceAdd(left, right);
1870 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001871 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 Py_DECREF(right);
1873 SET_TOP(sum);
1874 if (sum == NULL)
1875 goto error;
1876 DISPATCH();
1877 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *right = POP();
1881 PyObject *left = TOP();
1882 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1883 Py_DECREF(left);
1884 Py_DECREF(right);
1885 SET_TOP(diff);
1886 if (diff == NULL)
1887 goto error;
1888 DISPATCH();
1889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *right = POP();
1893 PyObject *left = TOP();
1894 PyObject *res = PyNumber_InPlaceLshift(left, right);
1895 Py_DECREF(left);
1896 Py_DECREF(right);
1897 SET_TOP(res);
1898 if (res == NULL)
1899 goto error;
1900 DISPATCH();
1901 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 PyObject *right = POP();
1905 PyObject *left = TOP();
1906 PyObject *res = PyNumber_InPlaceRshift(left, right);
1907 Py_DECREF(left);
1908 Py_DECREF(right);
1909 SET_TOP(res);
1910 if (res == NULL)
1911 goto error;
1912 DISPATCH();
1913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001914
Benjamin Petersonddd19492018-09-16 22:38:02 -07001915 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 PyObject *right = POP();
1917 PyObject *left = TOP();
1918 PyObject *res = PyNumber_InPlaceAnd(left, right);
1919 Py_DECREF(left);
1920 Py_DECREF(right);
1921 SET_TOP(res);
1922 if (res == NULL)
1923 goto error;
1924 DISPATCH();
1925 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 PyObject *right = POP();
1929 PyObject *left = TOP();
1930 PyObject *res = PyNumber_InPlaceXor(left, right);
1931 Py_DECREF(left);
1932 Py_DECREF(right);
1933 SET_TOP(res);
1934 if (res == NULL)
1935 goto error;
1936 DISPATCH();
1937 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001938
Benjamin Petersonddd19492018-09-16 22:38:02 -07001939 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 PyObject *right = POP();
1941 PyObject *left = TOP();
1942 PyObject *res = PyNumber_InPlaceOr(left, right);
1943 Py_DECREF(left);
1944 Py_DECREF(right);
1945 SET_TOP(res);
1946 if (res == NULL)
1947 goto error;
1948 DISPATCH();
1949 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001950
Benjamin Petersonddd19492018-09-16 22:38:02 -07001951 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 PyObject *sub = TOP();
1953 PyObject *container = SECOND();
1954 PyObject *v = THIRD();
1955 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001956 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001957 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 Py_DECREF(container);
1961 Py_DECREF(sub);
1962 if (err != 0)
1963 goto error;
1964 DISPATCH();
1965 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Benjamin Petersonddd19492018-09-16 22:38:02 -07001967 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 PyObject *sub = TOP();
1969 PyObject *container = SECOND();
1970 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001971 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001972 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 err = PyObject_DelItem(container, sub);
1974 Py_DECREF(container);
1975 Py_DECREF(sub);
1976 if (err != 0)
1977 goto error;
1978 DISPATCH();
1979 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001980
Benjamin Petersonddd19492018-09-16 22:38:02 -07001981 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001982 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001984 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001985 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001987 _PyErr_SetString(tstate, PyExc_RuntimeError,
1988 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 Py_DECREF(value);
1990 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001992 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 Py_DECREF(value);
1994 if (res == NULL)
1995 goto error;
1996 Py_DECREF(res);
1997 DISPATCH();
1998 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001999
Benjamin Petersonddd19492018-09-16 22:38:02 -07002000 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 switch (oparg) {
2003 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002005 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002008 /* fall through */
2009 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002010 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002011 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 break;
2014 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002015 _PyErr_SetString(tstate, PyExc_SystemError,
2016 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 break;
2018 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 goto error;
2020 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002024 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002025 assert(EMPTY());
2026 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002028
Benjamin Petersonddd19492018-09-16 22:38:02 -07002029 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002030 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002031 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002032 PyObject *obj = TOP();
2033 PyTypeObject *type = Py_TYPE(obj);
2034
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002035 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002036 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002037 }
Yury Selivanov75445082015-05-11 22:57:16 -04002038
2039 if (getter != NULL) {
2040 iter = (*getter)(obj);
2041 Py_DECREF(obj);
2042 if (iter == NULL) {
2043 SET_TOP(NULL);
2044 goto error;
2045 }
2046 }
2047 else {
2048 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002049 _PyErr_Format(tstate, PyExc_TypeError,
2050 "'async for' requires an object with "
2051 "__aiter__ method, got %.100s",
2052 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002053 Py_DECREF(obj);
2054 goto error;
2055 }
2056
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002057 if (Py_TYPE(iter)->tp_as_async == NULL ||
2058 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002059
Yury Selivanov398ff912017-03-02 22:20:00 -05002060 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002061 _PyErr_Format(tstate, PyExc_TypeError,
2062 "'async for' received an object from __aiter__ "
2063 "that does not implement __anext__: %.100s",
2064 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002065 Py_DECREF(iter);
2066 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002067 }
2068
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002069 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002070 DISPATCH();
2071 }
2072
Benjamin Petersonddd19492018-09-16 22:38:02 -07002073 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002074 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002075 PyObject *next_iter = NULL;
2076 PyObject *awaitable = NULL;
2077 PyObject *aiter = TOP();
2078 PyTypeObject *type = Py_TYPE(aiter);
2079
Yury Selivanoveb636452016-09-08 22:01:51 -07002080 if (PyAsyncGen_CheckExact(aiter)) {
2081 awaitable = type->tp_as_async->am_anext(aiter);
2082 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002083 goto error;
2084 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002085 } else {
2086 if (type->tp_as_async != NULL){
2087 getter = type->tp_as_async->am_anext;
2088 }
Yury Selivanov75445082015-05-11 22:57:16 -04002089
Yury Selivanoveb636452016-09-08 22:01:51 -07002090 if (getter != NULL) {
2091 next_iter = (*getter)(aiter);
2092 if (next_iter == NULL) {
2093 goto error;
2094 }
2095 }
2096 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002097 _PyErr_Format(tstate, PyExc_TypeError,
2098 "'async for' requires an iterator with "
2099 "__anext__ method, got %.100s",
2100 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002101 goto error;
2102 }
Yury Selivanov75445082015-05-11 22:57:16 -04002103
Yury Selivanoveb636452016-09-08 22:01:51 -07002104 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2105 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002106 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002107 PyExc_TypeError,
2108 "'async for' received an invalid object "
2109 "from __anext__: %.100s",
2110 Py_TYPE(next_iter)->tp_name);
2111
2112 Py_DECREF(next_iter);
2113 goto error;
2114 } else {
2115 Py_DECREF(next_iter);
2116 }
2117 }
Yury Selivanov75445082015-05-11 22:57:16 -04002118
2119 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002120 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002121 DISPATCH();
2122 }
2123
Benjamin Petersonddd19492018-09-16 22:38:02 -07002124 case TARGET(GET_AWAITABLE): {
2125 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002126 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002127 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002128
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002129 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002130 int opcode_at_minus_3 = 0;
2131 if ((next_instr - first_instr) > 2) {
2132 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2133 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002134 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002135 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002136 _Py_OPCODE(next_instr[-2]));
2137 }
2138
Yury Selivanov75445082015-05-11 22:57:16 -04002139 Py_DECREF(iterable);
2140
Yury Selivanovc724bae2016-03-02 11:30:46 -05002141 if (iter != NULL && PyCoro_CheckExact(iter)) {
2142 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2143 if (yf != NULL) {
2144 /* `iter` is a coroutine object that is being
2145 awaited, `yf` is a pointer to the current awaitable
2146 being awaited on. */
2147 Py_DECREF(yf);
2148 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002149 _PyErr_SetString(tstate, PyExc_RuntimeError,
2150 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002151 /* The code below jumps to `error` if `iter` is NULL. */
2152 }
2153 }
2154
Yury Selivanov75445082015-05-11 22:57:16 -04002155 SET_TOP(iter); /* Even if it's NULL */
2156
2157 if (iter == NULL) {
2158 goto error;
2159 }
2160
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002161 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002162 DISPATCH();
2163 }
2164
Benjamin Petersonddd19492018-09-16 22:38:02 -07002165 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002167 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002169 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2170 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002171 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002172 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002174 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002175 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002176 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 Py_DECREF(v);
2179 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002180 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002181 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002182 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002183 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002184 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 if (err < 0)
2186 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002187 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 SET_TOP(val);
2189 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002190 }
Martin Panter95f53c12016-07-18 08:23:26 +00002191 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002192 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002193 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002194 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002195 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002196 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002201
2202 if (co->co_flags & CO_ASYNC_GENERATOR) {
2203 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2204 Py_DECREF(retval);
2205 if (w == NULL) {
2206 retval = NULL;
2207 goto error;
2208 }
2209 retval = w;
2210 }
2211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002213 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002215
Benjamin Petersonddd19492018-09-16 22:38:02 -07002216 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002217 PyObject *type, *value, *traceback;
2218 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 PyTryBlock *b = PyFrame_BlockPop(f);
2220 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002221 _PyErr_SetString(tstate, PyExc_SystemError,
2222 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002225 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2226 STACK_LEVEL() <= (b)->b_level + 4);
2227 exc_info = tstate->exc_info;
2228 type = exc_info->exc_type;
2229 value = exc_info->exc_value;
2230 traceback = exc_info->exc_traceback;
2231 exc_info->exc_type = POP();
2232 exc_info->exc_value = POP();
2233 exc_info->exc_traceback = POP();
2234 Py_XDECREF(type);
2235 Py_XDECREF(value);
2236 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(POP_BLOCK): {
2241 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002242 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Mark Shannonfee55262019-11-21 09:11:43 +00002246 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002247 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002248 PyObject *val = POP();
2249 PyObject *tb = POP();
2250 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002251 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002252 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Benjamin Petersonddd19492018-09-16 22:38:02 -07002255 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002256 PyObject *exc = POP();
2257 assert(PyExceptionClass_Check(exc));
2258 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2259 PyTryBlock *b = PyFrame_BlockPop(f);
2260 assert(b->b_type == EXCEPT_HANDLER);
2261 Py_DECREF(exc);
2262 UNWIND_EXCEPT_HANDLER(b);
2263 Py_DECREF(POP());
2264 JUMPBY(oparg);
2265 FAST_DISPATCH();
2266 }
2267 else {
2268 PyObject *val = POP();
2269 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002270 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002271 goto exception_unwind;
2272 }
2273 }
2274
Zackery Spytzce6a0702019-08-25 03:44:09 -06002275 case TARGET(LOAD_ASSERTION_ERROR): {
2276 PyObject *value = PyExc_AssertionError;
2277 Py_INCREF(value);
2278 PUSH(value);
2279 FAST_DISPATCH();
2280 }
2281
Benjamin Petersonddd19492018-09-16 22:38:02 -07002282 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002283 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002287 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002289 if (!_PyErr_Occurred(tstate)) {
2290 _PyErr_SetString(tstate, PyExc_NameError,
2291 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002292 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002296 }
2297 else {
2298 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2299 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002300 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2302 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002303 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2304 _PyErr_SetString(tstate, PyExc_NameError,
2305 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002310 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002311 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002312
Benjamin Petersonddd19492018-09-16 22:38:02 -07002313 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 PyObject *name = GETITEM(names, oparg);
2315 PyObject *v = POP();
2316 PyObject *ns = f->f_locals;
2317 int err;
2318 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002319 _PyErr_Format(tstate, PyExc_SystemError,
2320 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 if (PyDict_CheckExact(ns))
2325 err = PyDict_SetItem(ns, name, v);
2326 else
2327 err = PyObject_SetItem(ns, name, v);
2328 Py_DECREF(v);
2329 if (err != 0)
2330 goto error;
2331 DISPATCH();
2332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Benjamin Petersonddd19492018-09-16 22:38:02 -07002334 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 PyObject *name = GETITEM(names, oparg);
2336 PyObject *ns = f->f_locals;
2337 int err;
2338 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002339 _PyErr_Format(tstate, PyExc_SystemError,
2340 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 err = PyObject_DelItem(ns, name);
2344 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002345 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 NAME_ERROR_MSG,
2347 name);
2348 goto error;
2349 }
2350 DISPATCH();
2351 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002352
Benjamin Petersonddd19492018-09-16 22:38:02 -07002353 case TARGET(UNPACK_SEQUENCE): {
2354 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 PyObject *seq = POP(), *item, **items;
2356 if (PyTuple_CheckExact(seq) &&
2357 PyTuple_GET_SIZE(seq) == oparg) {
2358 items = ((PyTupleObject *)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 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 } else if (PyList_CheckExact(seq) &&
2365 PyList_GET_SIZE(seq) == oparg) {
2366 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 item = items[oparg];
2369 Py_INCREF(item);
2370 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002372 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002374 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 } else {
2376 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 Py_DECREF(seq);
2378 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002381 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002383
Benjamin Petersonddd19492018-09-16 22:38:02 -07002384 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2386 PyObject *seq = POP();
2387
Victor Stinner438a12d2019-05-24 17:01:38 +02002388 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 stack_pointer + totalargs)) {
2390 stack_pointer += totalargs;
2391 } else {
2392 Py_DECREF(seq);
2393 goto error;
2394 }
2395 Py_DECREF(seq);
2396 DISPATCH();
2397 }
2398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *name = GETITEM(names, oparg);
2401 PyObject *owner = TOP();
2402 PyObject *v = SECOND();
2403 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002404 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 Py_DECREF(owner);
2408 if (err != 0)
2409 goto error;
2410 DISPATCH();
2411 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002412
Benjamin Petersonddd19492018-09-16 22:38:02 -07002413 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 PyObject *name = GETITEM(names, oparg);
2415 PyObject *owner = POP();
2416 int err;
2417 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2418 Py_DECREF(owner);
2419 if (err != 0)
2420 goto error;
2421 DISPATCH();
2422 }
2423
Benjamin Petersonddd19492018-09-16 22:38:02 -07002424 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 PyObject *name = GETITEM(names, oparg);
2426 PyObject *v = POP();
2427 int err;
2428 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 if (err != 0)
2431 goto error;
2432 DISPATCH();
2433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002434
Benjamin Petersonddd19492018-09-16 22:38:02 -07002435 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 PyObject *name = GETITEM(names, oparg);
2437 int err;
2438 err = PyDict_DelItem(f->f_globals, name);
2439 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002440 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2441 format_exc_check_arg(tstate, PyExc_NameError,
2442 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002445 }
2446 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002448
Benjamin Petersonddd19492018-09-16 22:38:02 -07002449 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 PyObject *name = GETITEM(names, oparg);
2451 PyObject *locals = f->f_locals;
2452 PyObject *v;
2453 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002454 _PyErr_Format(tstate, PyExc_SystemError,
2455 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002459 v = PyDict_GetItemWithError(locals, name);
2460 if (v != NULL) {
2461 Py_INCREF(v);
2462 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002464 goto error;
2465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 }
2467 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002469 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002470 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002471 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 }
2474 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002476 v = PyDict_GetItemWithError(f->f_globals, name);
2477 if (v != NULL) {
2478 Py_INCREF(v);
2479 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002480 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002481 goto error;
2482 }
2483 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002484 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002485 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002487 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002488 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002489 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002491 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002493 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002495 }
2496 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 v = PyObject_GetItem(f->f_builtins, name);
2498 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002499 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002500 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002501 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002503 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002505 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002512
Benjamin Petersonddd19492018-09-16 22:38:02 -07002513 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002514 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002516 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002517 && PyDict_CheckExact(f->f_builtins))
2518 {
Inada Naoki91234a12019-06-03 21:30:58 +09002519 OPCACHE_CHECK();
2520 if (co_opcache != NULL && co_opcache->optimized > 0) {
2521 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2522
2523 if (lg->globals_ver ==
2524 ((PyDictObject *)f->f_globals)->ma_version_tag
2525 && lg->builtins_ver ==
2526 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2527 {
2528 PyObject *ptr = lg->ptr;
2529 OPCACHE_STAT_GLOBAL_HIT();
2530 assert(ptr != NULL);
2531 Py_INCREF(ptr);
2532 PUSH(ptr);
2533 DISPATCH();
2534 }
2535 }
2536
2537 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002539 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 name);
2541 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002542 if (!_PyErr_OCCURRED()) {
2543 /* _PyDict_LoadGlobal() returns NULL without raising
2544 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002545 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002546 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002547 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 }
Inada Naoki91234a12019-06-03 21:30:58 +09002550
2551 if (co_opcache != NULL) {
2552 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2553
2554 if (co_opcache->optimized == 0) {
2555 /* Wasn't optimized before. */
2556 OPCACHE_STAT_GLOBAL_OPT();
2557 } else {
2558 OPCACHE_STAT_GLOBAL_MISS();
2559 }
2560
2561 co_opcache->optimized = 1;
2562 lg->globals_ver =
2563 ((PyDictObject *)f->f_globals)->ma_version_tag;
2564 lg->builtins_ver =
2565 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2566 lg->ptr = v; /* borrowed */
2567 }
2568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002571 else {
2572 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002573
2574 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002575 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 v = PyObject_GetItem(f->f_globals, name);
2577 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002578 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002579 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 }
2581 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002582
Victor Stinnerb4efc962015-11-20 09:24:02 +01002583 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 v = PyObject_GetItem(f->f_builtins, name);
2585 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002586 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002587 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002588 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002589 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002590 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002592 }
2593 }
2594 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002598
Benjamin Petersonddd19492018-09-16 22:38:02 -07002599 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 PyObject *v = GETLOCAL(oparg);
2601 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 SETLOCAL(oparg, NULL);
2603 DISPATCH();
2604 }
2605 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002606 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 UNBOUNDLOCAL_ERROR_MSG,
2608 PyTuple_GetItem(co->co_varnames, oparg)
2609 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 goto error;
2611 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002612
Benjamin Petersonddd19492018-09-16 22:38:02 -07002613 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002615 PyObject *oldobj = PyCell_GET(cell);
2616 if (oldobj != NULL) {
2617 PyCell_SET(cell, NULL);
2618 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002619 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002620 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002621 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 goto error;
2623 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 PyObject *cell = freevars[oparg];
2627 Py_INCREF(cell);
2628 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002631
Benjamin Petersonddd19492018-09-16 22:38:02 -07002632 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002633 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002634 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002635 assert(locals);
2636 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2637 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2638 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2639 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2640 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002641 value = PyDict_GetItemWithError(locals, name);
2642 if (value != NULL) {
2643 Py_INCREF(value);
2644 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002646 goto error;
2647 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002648 }
2649 else {
2650 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002651 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002652 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002653 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 }
2655 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002656 }
2657 }
2658 if (!value) {
2659 PyObject *cell = freevars[oparg];
2660 value = PyCell_GET(cell);
2661 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002662 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002663 goto error;
2664 }
2665 Py_INCREF(value);
2666 }
2667 PUSH(value);
2668 DISPATCH();
2669 }
2670
Benjamin Petersonddd19492018-09-16 22:38:02 -07002671 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 PyObject *cell = freevars[oparg];
2673 PyObject *value = PyCell_GET(cell);
2674 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002675 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 Py_INCREF(value);
2679 PUSH(value);
2680 DISPATCH();
2681 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002682
Benjamin Petersonddd19492018-09-16 22:38:02 -07002683 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 PyObject *v = POP();
2685 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002686 PyObject *oldobj = PyCell_GET(cell);
2687 PyCell_SET(cell, v);
2688 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 DISPATCH();
2690 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002691
Benjamin Petersonddd19492018-09-16 22:38:02 -07002692 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002693 PyObject *str;
2694 PyObject *empty = PyUnicode_New(0, 0);
2695 if (empty == NULL) {
2696 goto error;
2697 }
2698 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2699 Py_DECREF(empty);
2700 if (str == NULL)
2701 goto error;
2702 while (--oparg >= 0) {
2703 PyObject *item = POP();
2704 Py_DECREF(item);
2705 }
2706 PUSH(str);
2707 DISPATCH();
2708 }
2709
Benjamin Petersonddd19492018-09-16 22:38:02 -07002710 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 PyObject *tup = PyTuple_New(oparg);
2712 if (tup == NULL)
2713 goto error;
2714 while (--oparg >= 0) {
2715 PyObject *item = POP();
2716 PyTuple_SET_ITEM(tup, oparg, item);
2717 }
2718 PUSH(tup);
2719 DISPATCH();
2720 }
2721
Benjamin Petersonddd19492018-09-16 22:38:02 -07002722 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 PyObject *list = PyList_New(oparg);
2724 if (list == NULL)
2725 goto error;
2726 while (--oparg >= 0) {
2727 PyObject *item = POP();
2728 PyList_SET_ITEM(list, oparg, item);
2729 }
2730 PUSH(list);
2731 DISPATCH();
2732 }
2733
Mark Shannon13bc1392020-01-23 09:25:17 +00002734 case TARGET(LIST_TO_TUPLE): {
2735 PyObject *list = POP();
2736 PyObject *tuple = PyList_AsTuple(list);
2737 Py_DECREF(list);
2738 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002740 }
2741 PUSH(tuple);
2742 DISPATCH();
2743 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744
Mark Shannon13bc1392020-01-23 09:25:17 +00002745 case TARGET(LIST_EXTEND): {
2746 PyObject *iterable = POP();
2747 PyObject *list = PEEK(oparg);
2748 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2749 if (none_val == NULL) {
2750 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002751 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002752 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002753 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002754 _PyErr_Format(tstate, PyExc_TypeError,
2755 "Value after * must be an iterable, not %.200s",
2756 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002758 Py_DECREF(iterable);
2759 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002761 Py_DECREF(none_val);
2762 Py_DECREF(iterable);
2763 DISPATCH();
2764 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765
Mark Shannon13bc1392020-01-23 09:25:17 +00002766 case TARGET(SET_UPDATE): {
2767 PyObject *iterable = POP();
2768 PyObject *set = PEEK(oparg);
2769 int err = _PySet_Update(set, iterable);
2770 Py_DECREF(iterable);
2771 if (err < 0) {
2772 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774 DISPATCH();
2775 }
2776
Benjamin Petersonddd19492018-09-16 22:38:02 -07002777 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 PyObject *set = PySet_New(NULL);
2779 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002780 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 if (set == NULL)
2782 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002783 for (i = oparg; i > 0; i--) {
2784 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 if (err == 0)
2786 err = PySet_Add(set, item);
2787 Py_DECREF(item);
2788 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002789 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 if (err != 0) {
2791 Py_DECREF(set);
2792 goto error;
2793 }
2794 PUSH(set);
2795 DISPATCH();
2796 }
2797
Benjamin Petersonddd19492018-09-16 22:38:02 -07002798 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002799 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2801 if (map == NULL)
2802 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002803 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002805 PyObject *key = PEEK(2*i);
2806 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002807 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002808 if (err != 0) {
2809 Py_DECREF(map);
2810 goto error;
2811 }
2812 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002813
2814 while (oparg--) {
2815 Py_DECREF(POP());
2816 Py_DECREF(POP());
2817 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 PUSH(map);
2819 DISPATCH();
2820 }
2821
Benjamin Petersonddd19492018-09-16 22:38:02 -07002822 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002823 _Py_IDENTIFIER(__annotations__);
2824 int err;
2825 PyObject *ann_dict;
2826 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002827 _PyErr_Format(tstate, PyExc_SystemError,
2828 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002829 goto error;
2830 }
2831 /* check if __annotations__ in locals()... */
2832 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002833 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002834 &PyId___annotations__);
2835 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002836 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002837 goto error;
2838 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002839 /* ...if not, create a new one */
2840 ann_dict = PyDict_New();
2841 if (ann_dict == NULL) {
2842 goto error;
2843 }
2844 err = _PyDict_SetItemId(f->f_locals,
2845 &PyId___annotations__, ann_dict);
2846 Py_DECREF(ann_dict);
2847 if (err != 0) {
2848 goto error;
2849 }
2850 }
2851 }
2852 else {
2853 /* do the same if locals() is not a dict */
2854 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2855 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002856 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002857 }
2858 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2859 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002860 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002861 goto error;
2862 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002863 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002864 ann_dict = PyDict_New();
2865 if (ann_dict == NULL) {
2866 goto error;
2867 }
2868 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2869 Py_DECREF(ann_dict);
2870 if (err != 0) {
2871 goto error;
2872 }
2873 }
2874 else {
2875 Py_DECREF(ann_dict);
2876 }
2877 }
2878 DISPATCH();
2879 }
2880
Benjamin Petersonddd19492018-09-16 22:38:02 -07002881 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002882 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002883 PyObject *map;
2884 PyObject *keys = TOP();
2885 if (!PyTuple_CheckExact(keys) ||
2886 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002887 _PyErr_SetString(tstate, PyExc_SystemError,
2888 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002889 goto error;
2890 }
2891 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2892 if (map == NULL) {
2893 goto error;
2894 }
2895 for (i = oparg; i > 0; i--) {
2896 int err;
2897 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2898 PyObject *value = PEEK(i + 1);
2899 err = PyDict_SetItem(map, key, value);
2900 if (err != 0) {
2901 Py_DECREF(map);
2902 goto error;
2903 }
2904 }
2905
2906 Py_DECREF(POP());
2907 while (oparg--) {
2908 Py_DECREF(POP());
2909 }
2910 PUSH(map);
2911 DISPATCH();
2912 }
2913
Mark Shannon8a4cd702020-01-27 09:57:45 +00002914 case TARGET(DICT_UPDATE): {
2915 PyObject *update = POP();
2916 PyObject *dict = PEEK(oparg);
2917 if (PyDict_Update(dict, update) < 0) {
2918 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2919 _PyErr_Format(tstate, PyExc_TypeError,
2920 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002921 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002922 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002923 Py_DECREF(update);
2924 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002925 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002926 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002927 DISPATCH();
2928 }
2929
Mark Shannon8a4cd702020-01-27 09:57:45 +00002930 case TARGET(DICT_MERGE): {
2931 PyObject *update = POP();
2932 PyObject *dict = PEEK(oparg);
2933
2934 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2935 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2936 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002937 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002938 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002939 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002940 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002941 DISPATCH();
2942 }
2943
Benjamin Petersonddd19492018-09-16 22:38:02 -07002944 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002945 PyObject *value = TOP();
2946 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 PyObject *map;
2948 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002949 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002950 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002952 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 Py_DECREF(value);
2954 Py_DECREF(key);
2955 if (err != 0)
2956 goto error;
2957 PREDICT(JUMP_ABSOLUTE);
2958 DISPATCH();
2959 }
2960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 PyObject *name = GETITEM(names, oparg);
2963 PyObject *owner = TOP();
2964 PyObject *res = PyObject_GetAttr(owner, name);
2965 Py_DECREF(owner);
2966 SET_TOP(res);
2967 if (res == NULL)
2968 goto error;
2969 DISPATCH();
2970 }
2971
Benjamin Petersonddd19492018-09-16 22:38:02 -07002972 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002973 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 PyObject *right = POP();
2975 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002976 PyObject *res = PyObject_RichCompare(left, right, oparg);
2977 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 Py_DECREF(left);
2979 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 if (res == NULL)
2981 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 PREDICT(POP_JUMP_IF_FALSE);
2983 PREDICT(POP_JUMP_IF_TRUE);
2984 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002985 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002986
Mark Shannon9af0e472020-01-14 10:12:45 +00002987 case TARGET(IS_OP): {
2988 PyObject *right = POP();
2989 PyObject *left = TOP();
2990 int res = (left == right)^oparg;
2991 PyObject *b = res ? Py_True : Py_False;
2992 Py_INCREF(b);
2993 SET_TOP(b);
2994 Py_DECREF(left);
2995 Py_DECREF(right);
2996 PREDICT(POP_JUMP_IF_FALSE);
2997 PREDICT(POP_JUMP_IF_TRUE);
2998 FAST_DISPATCH();
2999 }
3000
3001 case TARGET(CONTAINS_OP): {
3002 PyObject *right = POP();
3003 PyObject *left = POP();
3004 int res = PySequence_Contains(right, left);
3005 Py_DECREF(left);
3006 Py_DECREF(right);
3007 if (res < 0) {
3008 goto error;
3009 }
3010 PyObject *b = (res^oparg) ? Py_True : Py_False;
3011 Py_INCREF(b);
3012 PUSH(b);
3013 PREDICT(POP_JUMP_IF_FALSE);
3014 PREDICT(POP_JUMP_IF_TRUE);
3015 FAST_DISPATCH();
3016 }
3017
3018#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3019 "BaseException is not allowed"
3020
3021 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3022 PyObject *right = POP();
3023 PyObject *left = POP();
3024 if (PyTuple_Check(right)) {
3025 Py_ssize_t i, length;
3026 length = PyTuple_GET_SIZE(right);
3027 for (i = 0; i < length; i++) {
3028 PyObject *exc = PyTuple_GET_ITEM(right, i);
3029 if (!PyExceptionClass_Check(exc)) {
3030 _PyErr_SetString(tstate, PyExc_TypeError,
3031 CANNOT_CATCH_MSG);
3032 Py_DECREF(left);
3033 Py_DECREF(right);
3034 goto error;
3035 }
3036 }
3037 }
3038 else {
3039 if (!PyExceptionClass_Check(right)) {
3040 _PyErr_SetString(tstate, PyExc_TypeError,
3041 CANNOT_CATCH_MSG);
3042 Py_DECREF(left);
3043 Py_DECREF(right);
3044 goto error;
3045 }
3046 }
3047 int res = PyErr_GivenExceptionMatches(left, right);
3048 Py_DECREF(left);
3049 Py_DECREF(right);
3050 if (res > 0) {
3051 /* Exception matches -- Do nothing */;
3052 }
3053 else if (res == 0) {
3054 JUMPTO(oparg);
3055 }
3056 else {
3057 goto error;
3058 }
3059 DISPATCH();
3060 }
3061
Benjamin Petersonddd19492018-09-16 22:38:02 -07003062 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003064 PyObject *fromlist = POP();
3065 PyObject *level = TOP();
3066 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003067 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003068 Py_DECREF(level);
3069 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 SET_TOP(res);
3071 if (res == NULL)
3072 goto error;
3073 DISPATCH();
3074 }
3075
Benjamin Petersonddd19492018-09-16 22:38:02 -07003076 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *from = POP(), *locals;
3078 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003079 if (PyFrame_FastToLocalsWithError(f) < 0) {
3080 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003081 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003082 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 locals = f->f_locals;
3085 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003086 _PyErr_SetString(tstate, PyExc_SystemError,
3087 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003088 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003091 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 Py_DECREF(from);
3094 if (err != 0)
3095 goto error;
3096 DISPATCH();
3097 }
Guido van Rossum25831651993-05-19 14:50:45 +00003098
Benjamin Petersonddd19492018-09-16 22:38:02 -07003099 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 PyObject *name = GETITEM(names, oparg);
3101 PyObject *from = TOP();
3102 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003103 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 PUSH(res);
3105 if (res == NULL)
3106 goto error;
3107 DISPATCH();
3108 }
Thomas Wouters52152252000-08-17 22:55:00 +00003109
Benjamin Petersonddd19492018-09-16 22:38:02 -07003110 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 JUMPBY(oparg);
3112 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003114
Benjamin Petersonddd19492018-09-16 22:38:02 -07003115 case TARGET(POP_JUMP_IF_FALSE): {
3116 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PyObject *cond = POP();
3118 int err;
3119 if (cond == Py_True) {
3120 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 FAST_DISPATCH();
3122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 if (cond == Py_False) {
3124 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 JUMPTO(oparg);
3126 FAST_DISPATCH();
3127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 err = PyObject_IsTrue(cond);
3129 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003131 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 else if (err == 0)
3133 JUMPTO(oparg);
3134 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003138
Benjamin Petersonddd19492018-09-16 22:38:02 -07003139 case TARGET(POP_JUMP_IF_TRUE): {
3140 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 PyObject *cond = POP();
3142 int err;
3143 if (cond == Py_False) {
3144 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 FAST_DISPATCH();
3146 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 if (cond == Py_True) {
3148 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 JUMPTO(oparg);
3150 FAST_DISPATCH();
3151 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 err = PyObject_IsTrue(cond);
3153 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 JUMPTO(oparg);
3156 }
3157 else if (err == 0)
3158 ;
3159 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003163
Benjamin Petersonddd19492018-09-16 22:38:02 -07003164 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 PyObject *cond = TOP();
3166 int err;
3167 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003168 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 FAST_DISPATCH();
3171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 JUMPTO(oparg);
3174 FAST_DISPATCH();
3175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003178 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 }
3181 else if (err == 0)
3182 JUMPTO(oparg);
3183 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003187
Benjamin Petersonddd19492018-09-16 22:38:02 -07003188 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 PyObject *cond = TOP();
3190 int err;
3191 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003192 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 FAST_DISPATCH();
3195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 JUMPTO(oparg);
3198 FAST_DISPATCH();
3199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 JUMPTO(oparg);
3203 }
3204 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003205 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
3208 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003212
Benjamin Petersonddd19492018-09-16 22:38:02 -07003213 case TARGET(JUMP_ABSOLUTE): {
3214 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003216#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 /* Enabling this path speeds-up all while and for-loops by bypassing
3218 the per-loop checks for signals. By default, this should be turned-off
3219 because it prevents detection of a control-break in tight loops like
3220 "while 1: pass". Compile with this option turned-on when you need
3221 the speed-up and do not need break checking inside tight loops (ones
3222 that contain only instructions ending with FAST_DISPATCH).
3223 */
3224 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003225#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003227#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003229
Benjamin Petersonddd19492018-09-16 22:38:02 -07003230 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003233 PyObject *iter = PyObject_GetIter(iterable);
3234 Py_DECREF(iterable);
3235 SET_TOP(iter);
3236 if (iter == NULL)
3237 goto error;
3238 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003239 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003240 DISPATCH();
3241 }
3242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003244 /* before: [obj]; after [getiter(obj)] */
3245 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003246 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003247 if (PyCoro_CheckExact(iterable)) {
3248 /* `iterable` is a coroutine */
3249 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3250 /* and it is used in a 'yield from' expression of a
3251 regular generator. */
3252 Py_DECREF(iterable);
3253 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003254 _PyErr_SetString(tstate, PyExc_TypeError,
3255 "cannot 'yield from' a coroutine object "
3256 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003257 goto error;
3258 }
3259 }
3260 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003261 /* `iterable` is not a generator. */
3262 iter = PyObject_GetIter(iterable);
3263 Py_DECREF(iterable);
3264 SET_TOP(iter);
3265 if (iter == NULL)
3266 goto error;
3267 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003268 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 DISPATCH();
3270 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003271
Benjamin Petersonddd19492018-09-16 22:38:02 -07003272 case TARGET(FOR_ITER): {
3273 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003276 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 if (next != NULL) {
3278 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 PREDICT(STORE_FAST);
3280 PREDICT(UNPACK_SEQUENCE);
3281 DISPATCH();
3282 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003283 if (_PyErr_Occurred(tstate)) {
3284 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003286 }
3287 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003288 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003289 }
3290 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 }
3292 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003293 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003296 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003299
Benjamin Petersonddd19492018-09-16 22:38:02 -07003300 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003301 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 STACK_LEVEL());
3303 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003305
Benjamin Petersonddd19492018-09-16 22:38:02 -07003306 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003307 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003308 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003309 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003310 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003311 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003312 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003313 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003314 }
3315 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3316 if (exit == NULL) {
3317 Py_DECREF(enter);
3318 goto error;
3319 }
Yury Selivanov75445082015-05-11 22:57:16 -04003320 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003321 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003322 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003323 Py_DECREF(enter);
3324 if (res == NULL)
3325 goto error;
3326 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003327 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003328 DISPATCH();
3329 }
3330
Benjamin Petersonddd19492018-09-16 22:38:02 -07003331 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003332 PyObject *res = POP();
3333 /* Setup the finally block before pushing the result
3334 of __aenter__ on the stack. */
3335 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3336 STACK_LEVEL());
3337 PUSH(res);
3338 DISPATCH();
3339 }
3340
Benjamin Petersonddd19492018-09-16 22:38:02 -07003341 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003342 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003343 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003345 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003347 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003348 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003349 }
3350 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003351 if (exit == NULL) {
3352 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003357 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 Py_DECREF(enter);
3359 if (res == NULL)
3360 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 /* Setup the finally block before pushing the result
3362 of __enter__ on the stack. */
3363 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3364 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 DISPATCH();
3368 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003369
Mark Shannonfee55262019-11-21 09:11:43 +00003370 case TARGET(WITH_EXCEPT_START): {
3371 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003373 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3374 - SEVENTH: the context.__exit__ bound method
3375 We call SEVENTH(TOP, SECOND, THIRD).
3376 Then we push again the TOP exception and the __exit__
3377 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003380 PyObject *exc, *val, *tb, *res;
3381
Victor Stinner842cfff2016-12-01 14:45:31 +01003382 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003383 val = SECOND();
3384 tb = THIRD();
3385 assert(exc != Py_None);
3386 assert(!PyLong_Check(exc));
3387 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003388 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003389 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003390 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 if (res == NULL)
3392 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003393
Yury Selivanov75445082015-05-11 22:57:16 -04003394 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003397
Benjamin Petersonddd19492018-09-16 22:38:02 -07003398 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003399 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003400 PyObject *name = GETITEM(names, oparg);
3401 PyObject *obj = TOP();
3402 PyObject *meth = NULL;
3403
3404 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3405
Yury Selivanovf2392132016-12-13 19:03:51 -05003406 if (meth == NULL) {
3407 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003408 goto error;
3409 }
3410
3411 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003412 /* We can bypass temporary bound method object.
3413 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003414
INADA Naoki015bce62017-01-16 17:23:30 +09003415 meth | self | arg1 | ... | argN
3416 */
3417 SET_TOP(meth);
3418 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003419 }
3420 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003421 /* meth is not an unbound method (but a regular attr, or
3422 something was returned by a descriptor protocol). Set
3423 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003424 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003425
3426 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003427 */
INADA Naoki015bce62017-01-16 17:23:30 +09003428 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003429 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003430 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003431 }
3432 DISPATCH();
3433 }
3434
Benjamin Petersonddd19492018-09-16 22:38:02 -07003435 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003436 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003437 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003438
3439 sp = stack_pointer;
3440
INADA Naoki015bce62017-01-16 17:23:30 +09003441 meth = PEEK(oparg + 2);
3442 if (meth == NULL) {
3443 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3444 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003445
3446 Stack layout:
3447
INADA Naoki015bce62017-01-16 17:23:30 +09003448 ... | NULL | callable | arg1 | ... | argN
3449 ^- TOP()
3450 ^- (-oparg)
3451 ^- (-oparg-1)
3452 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003453
Ville Skyttä49b27342017-08-03 09:00:59 +03003454 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003455 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003456 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003457 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003458 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003459 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003460 }
3461 else {
3462 /* This is a method call. Stack layout:
3463
INADA Naoki015bce62017-01-16 17:23:30 +09003464 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003465 ^- TOP()
3466 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003467 ^- (-oparg-1)
3468 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003469
INADA Naoki015bce62017-01-16 17:23:30 +09003470 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003471 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003472 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003473 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003474 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003475 stack_pointer = sp;
3476 }
3477
3478 PUSH(res);
3479 if (res == NULL)
3480 goto error;
3481 DISPATCH();
3482 }
3483
Benjamin Petersonddd19492018-09-16 22:38:02 -07003484 case TARGET(CALL_FUNCTION): {
3485 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003488 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003490 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003491 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003493 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003496
Benjamin Petersonddd19492018-09-16 22:38:02 -07003497 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003498 PyObject **sp, *res, *names;
3499
3500 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003501 assert(PyTuple_Check(names));
3502 assert(PyTuple_GET_SIZE(names) <= oparg);
3503 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003505 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003507 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003508 Py_DECREF(names);
3509
3510 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003512 }
3513 DISPATCH();
3514 }
3515
Benjamin Petersonddd19492018-09-16 22:38:02 -07003516 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003517 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003518 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003519 if (oparg & 0x01) {
3520 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003521 if (!PyDict_CheckExact(kwargs)) {
3522 PyObject *d = PyDict_New();
3523 if (d == NULL)
3524 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003525 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003526 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003527 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003528 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003529 goto error;
3530 }
3531 Py_DECREF(kwargs);
3532 kwargs = d;
3533 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003534 assert(PyDict_CheckExact(kwargs));
3535 }
3536 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003537 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003538 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003539 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003540 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003541 goto error;
3542 }
3543 Py_SETREF(callargs, PySequence_Tuple(callargs));
3544 if (callargs == NULL) {
3545 goto error;
3546 }
3547 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003548 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003549
Victor Stinner09532fe2019-05-10 23:39:09 +02003550 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003551 Py_DECREF(func);
3552 Py_DECREF(callargs);
3553 Py_XDECREF(kwargs);
3554
3555 SET_TOP(result);
3556 if (result == NULL) {
3557 goto error;
3558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003561
Benjamin Petersonddd19492018-09-16 22:38:02 -07003562 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003563 PyObject *qualname = POP();
3564 PyObject *codeobj = POP();
3565 PyFunctionObject *func = (PyFunctionObject *)
3566 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003567
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003568 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003569 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003570 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003571 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003573
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003574 if (oparg & 0x08) {
3575 assert(PyTuple_CheckExact(TOP()));
3576 func ->func_closure = POP();
3577 }
3578 if (oparg & 0x04) {
3579 assert(PyDict_CheckExact(TOP()));
3580 func->func_annotations = POP();
3581 }
3582 if (oparg & 0x02) {
3583 assert(PyDict_CheckExact(TOP()));
3584 func->func_kwdefaults = POP();
3585 }
3586 if (oparg & 0x01) {
3587 assert(PyTuple_CheckExact(TOP()));
3588 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003590
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003591 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003594
Benjamin Petersonddd19492018-09-16 22:38:02 -07003595 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003596 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003598 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003600 step = NULL;
3601 stop = POP();
3602 start = TOP();
3603 slice = PySlice_New(start, stop, step);
3604 Py_DECREF(start);
3605 Py_DECREF(stop);
3606 Py_XDECREF(step);
3607 SET_TOP(slice);
3608 if (slice == NULL)
3609 goto error;
3610 DISPATCH();
3611 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003612
Benjamin Petersonddd19492018-09-16 22:38:02 -07003613 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003614 /* Handles f-string value formatting. */
3615 PyObject *result;
3616 PyObject *fmt_spec;
3617 PyObject *value;
3618 PyObject *(*conv_fn)(PyObject *);
3619 int which_conversion = oparg & FVC_MASK;
3620 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3621
3622 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003623 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003624
3625 /* See if any conversion is specified. */
3626 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003627 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003628 case FVC_STR: conv_fn = PyObject_Str; break;
3629 case FVC_REPR: conv_fn = PyObject_Repr; break;
3630 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003631 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003632 _PyErr_Format(tstate, PyExc_SystemError,
3633 "unexpected conversion flag %d",
3634 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003635 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003636 }
3637
3638 /* If there's a conversion function, call it and replace
3639 value with that result. Otherwise, just use value,
3640 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003641 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003642 result = conv_fn(value);
3643 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003644 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003645 Py_XDECREF(fmt_spec);
3646 goto error;
3647 }
3648 value = result;
3649 }
3650
3651 /* If value is a unicode object, and there's no fmt_spec,
3652 then we know the result of format(value) is value
3653 itself. In that case, skip calling format(). I plan to
3654 move this optimization in to PyObject_Format()
3655 itself. */
3656 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3657 /* Do nothing, just transfer ownership to result. */
3658 result = value;
3659 } else {
3660 /* Actually call format(). */
3661 result = PyObject_Format(value, fmt_spec);
3662 Py_DECREF(value);
3663 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003664 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003665 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003666 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003667 }
3668
Eric V. Smith135d5f42016-02-05 18:23:08 -05003669 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003670 DISPATCH();
3671 }
3672
Benjamin Petersonddd19492018-09-16 22:38:02 -07003673 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003674 int oldoparg = oparg;
3675 NEXTOPARG();
3676 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003678 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003679
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003680
Antoine Pitrou042b1282010-08-13 21:15:58 +00003681#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 default:
3685 fprintf(stderr,
3686 "XXX lineno: %d, opcode: %d\n",
3687 PyFrame_GetLineNumber(f),
3688 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003689 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003690 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003693
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003694 /* This should never be reached. Every opcode should end with DISPATCH()
3695 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003696 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003697
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003698error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003699 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003700#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003701 if (!_PyErr_Occurred(tstate)) {
3702 _PyErr_SetString(tstate, PyExc_SystemError,
3703 "error return without exception set");
3704 }
Victor Stinner365b6932013-07-12 00:11:58 +02003705#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003706 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003707#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003709 /* Log traceback info. */
3710 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003711
Benjamin Peterson51f46162013-01-23 08:38:47 -05003712 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003713 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3714 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003715
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003716exception_unwind:
3717 /* Unwind stacks if an exception occurred */
3718 while (f->f_iblock > 0) {
3719 /* Pop the current block. */
3720 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 if (b->b_type == EXCEPT_HANDLER) {
3723 UNWIND_EXCEPT_HANDLER(b);
3724 continue;
3725 }
3726 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003727 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyObject *exc, *val, *tb;
3729 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003730 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 /* Beware, this invalidates all b->b_* fields */
3732 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003733 PUSH(exc_info->exc_traceback);
3734 PUSH(exc_info->exc_value);
3735 if (exc_info->exc_type != NULL) {
3736 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 }
3738 else {
3739 Py_INCREF(Py_None);
3740 PUSH(Py_None);
3741 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003742 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* Make the raw exception data
3744 available to the handler,
3745 so a program can emulate the
3746 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003747 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003748 if (tb != NULL)
3749 PyException_SetTraceback(val, tb);
3750 else
3751 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003753 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003755 exc_info->exc_value = val;
3756 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 if (tb == NULL)
3758 tb = Py_None;
3759 Py_INCREF(tb);
3760 PUSH(tb);
3761 PUSH(val);
3762 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003764 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003765 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3766 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3767 /* Make sure that we trace line after exception if we are in a new execution
3768 * window or we don't need a line update and we are not in the first instruction
3769 * of the line. */
3770 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3771 instr_prev = INT_MAX;
3772 }
Mark Shannonfee55262019-11-21 09:11:43 +00003773 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003774 /* Resume normal execution */
3775 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 }
3777 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003778
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003779 /* End the loop as we still have an error */
3780 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003782
Pablo Galindof00828a2019-05-09 16:52:02 +01003783 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003784 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 /* Pop remaining stack entries. */
3787 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003788 PyObject *o = POP();
3789 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003791
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003792exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003794 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003795 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3796 tstate, f, PyTrace_RETURN, retval)) {
3797 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 }
3799 }
3800 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003801 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3802 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003803 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 }
3805 }
3806 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003809exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003810 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3811 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003812 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003813 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003815
Victor Stinner0b72b232020-03-12 23:18:39 +01003816 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003817}
3818
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003820format_missing(PyThreadState *tstate, const char *kind,
3821 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003822{
3823 int err;
3824 Py_ssize_t len = PyList_GET_SIZE(names);
3825 PyObject *name_str, *comma, *tail, *tmp;
3826
3827 assert(PyList_CheckExact(names));
3828 assert(len >= 1);
3829 /* Deal with the joys of natural language. */
3830 switch (len) {
3831 case 1:
3832 name_str = PyList_GET_ITEM(names, 0);
3833 Py_INCREF(name_str);
3834 break;
3835 case 2:
3836 name_str = PyUnicode_FromFormat("%U and %U",
3837 PyList_GET_ITEM(names, len - 2),
3838 PyList_GET_ITEM(names, len - 1));
3839 break;
3840 default:
3841 tail = PyUnicode_FromFormat(", %U, and %U",
3842 PyList_GET_ITEM(names, len - 2),
3843 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003844 if (tail == NULL)
3845 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003846 /* Chop off the last two objects in the list. This shouldn't actually
3847 fail, but we can't be too careful. */
3848 err = PyList_SetSlice(names, len - 2, len, NULL);
3849 if (err == -1) {
3850 Py_DECREF(tail);
3851 return;
3852 }
3853 /* Stitch everything up into a nice comma-separated list. */
3854 comma = PyUnicode_FromString(", ");
3855 if (comma == NULL) {
3856 Py_DECREF(tail);
3857 return;
3858 }
3859 tmp = PyUnicode_Join(comma, names);
3860 Py_DECREF(comma);
3861 if (tmp == NULL) {
3862 Py_DECREF(tail);
3863 return;
3864 }
3865 name_str = PyUnicode_Concat(tmp, tail);
3866 Py_DECREF(tmp);
3867 Py_DECREF(tail);
3868 break;
3869 }
3870 if (name_str == NULL)
3871 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003872 _PyErr_Format(tstate, PyExc_TypeError,
3873 "%U() missing %i required %s argument%s: %U",
3874 co->co_name,
3875 len,
3876 kind,
3877 len == 1 ? "" : "s",
3878 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003879 Py_DECREF(name_str);
3880}
3881
3882static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003883missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3884 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003885 PyObject **fastlocals)
3886{
Victor Stinner74319ae2016-08-25 00:04:09 +02003887 Py_ssize_t i, j = 0;
3888 Py_ssize_t start, end;
3889 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003890 const char *kind = positional ? "positional" : "keyword-only";
3891 PyObject *missing_names;
3892
3893 /* Compute the names of the arguments that are missing. */
3894 missing_names = PyList_New(missing);
3895 if (missing_names == NULL)
3896 return;
3897 if (positional) {
3898 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003899 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003900 }
3901 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003902 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003903 end = start + co->co_kwonlyargcount;
3904 }
3905 for (i = start; i < end; i++) {
3906 if (GETLOCAL(i) == NULL) {
3907 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3908 PyObject *name = PyObject_Repr(raw);
3909 if (name == NULL) {
3910 Py_DECREF(missing_names);
3911 return;
3912 }
3913 PyList_SET_ITEM(missing_names, j++, name);
3914 }
3915 }
3916 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003917 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003918 Py_DECREF(missing_names);
3919}
3920
3921static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003922too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3923 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003924 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003925{
3926 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003927 Py_ssize_t kwonly_given = 0;
3928 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003930 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931
Benjamin Petersone109c702011-06-24 09:37:26 -05003932 assert((co->co_flags & CO_VARARGS) == 0);
3933 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003934 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003935 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 }
3938 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003939 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003940 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003941 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003942 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 }
3944 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003945 plural = (co_argcount != 1);
3946 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003947 }
3948 if (sig == NULL)
3949 return;
3950 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003951 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3952 kwonly_sig = PyUnicode_FromFormat(format,
3953 given != 1 ? "s" : "",
3954 kwonly_given,
3955 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003956 if (kwonly_sig == NULL) {
3957 Py_DECREF(sig);
3958 return;
3959 }
3960 }
3961 else {
3962 /* This will not fail. */
3963 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003964 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003965 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003966 _PyErr_Format(tstate, PyExc_TypeError,
3967 "%U() takes %U positional argument%s but %zd%U %s given",
3968 co->co_name,
3969 sig,
3970 plural ? "s" : "",
3971 given,
3972 kwonly_sig,
3973 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 Py_DECREF(sig);
3975 Py_DECREF(kwonly_sig);
3976}
3977
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003978static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003979positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3980 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003981{
3982 int posonly_conflicts = 0;
3983 PyObject* posonly_names = PyList_New(0);
3984
3985 for(int k=0; k < co->co_posonlyargcount; k++){
3986 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3987
3988 for (int k2=0; k2<kwcount; k2++){
3989 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3990 PyObject* kwname = kwnames[k2];
3991 if (kwname == posonly_name){
3992 if(PyList_Append(posonly_names, kwname) != 0) {
3993 goto fail;
3994 }
3995 posonly_conflicts++;
3996 continue;
3997 }
3998
3999 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4000
4001 if ( cmp > 0) {
4002 if(PyList_Append(posonly_names, kwname) != 0) {
4003 goto fail;
4004 }
4005 posonly_conflicts++;
4006 } else if (cmp < 0) {
4007 goto fail;
4008 }
4009
4010 }
4011 }
4012 if (posonly_conflicts) {
4013 PyObject* comma = PyUnicode_FromString(", ");
4014 if (comma == NULL) {
4015 goto fail;
4016 }
4017 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4018 Py_DECREF(comma);
4019 if (error_names == NULL) {
4020 goto fail;
4021 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004022 _PyErr_Format(tstate, PyExc_TypeError,
4023 "%U() got some positional-only arguments passed"
4024 " as keyword arguments: '%U'",
4025 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004026 Py_DECREF(error_names);
4027 goto fail;
4028 }
4029
4030 Py_DECREF(posonly_names);
4031 return 0;
4032
4033fail:
4034 Py_XDECREF(posonly_names);
4035 return 1;
4036
4037}
4038
Guido van Rossumc2e20742006-02-27 22:32:47 +00004039/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004040 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004041 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004042
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004043PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004044_PyEval_EvalCode(PyThreadState *tstate,
4045 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004046 PyObject *const *args, Py_ssize_t argcount,
4047 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004048 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004049 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004050 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004051 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004052{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004053 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004054
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004055 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004056 PyFrameObject *f;
4057 PyObject *retval = NULL;
4058 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004060 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004061 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004062 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004065 _PyErr_SetString(tstate, PyExc_SystemError,
4066 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 return NULL;
4068 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004069
Victor Stinnerc7020012016-08-16 23:40:29 +02004070 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004071 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004072 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 fastlocals = f->f_localsplus;
4076 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004077
Victor Stinnerc7020012016-08-16 23:40:29 +02004078 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 if (co->co_flags & CO_VARKEYWORDS) {
4080 kwdict = PyDict_New();
4081 if (kwdict == NULL)
4082 goto fail;
4083 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004084 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004085 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004086 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004087 SETLOCAL(i, kwdict);
4088 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004089 else {
4090 kwdict = NULL;
4091 }
4092
Pablo Galindocd74e662019-06-01 18:08:04 +01004093 /* Copy all positional arguments into local variables */
4094 if (argcount > co->co_argcount) {
4095 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004096 }
4097 else {
4098 n = argcount;
4099 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004100 for (j = 0; j < n; j++) {
4101 x = args[j];
4102 Py_INCREF(x);
4103 SETLOCAL(j, x);
4104 }
4105
Victor Stinnerc7020012016-08-16 23:40:29 +02004106 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004107 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004108 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004109 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004111 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004113 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004114
Serhiy Storchakab7281052016-09-12 00:52:40 +03004115 /* Handle keyword arguments passed as two strided arrays */
4116 kwcount *= kwstep;
4117 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004118 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004119 PyObject *keyword = kwnames[i];
4120 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004121 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004122
Benjamin Petersonb204a422011-06-05 22:04:07 -05004123 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004124 _PyErr_Format(tstate, PyExc_TypeError,
4125 "%U() keywords must be strings",
4126 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004127 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004129
Benjamin Petersonb204a422011-06-05 22:04:07 -05004130 /* Speed hack: do raw pointer compares. As names are
4131 normally interned this should almost always hit. */
4132 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
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 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004137 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004138 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004139
Benjamin Petersonb204a422011-06-05 22:04:07 -05004140 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004141 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004142 PyObject *name = co_varnames[j];
4143 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4144 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004145 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004146 }
4147 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004149 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004150 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004151
Victor Stinner231d1f32017-01-11 02:12:06 +01004152 assert(j >= total_args);
4153 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004154
Victor Stinner438a12d2019-05-24 17:01:38 +02004155 if (co->co_posonlyargcount
4156 && positional_only_passed_as_keyword(tstate, co,
4157 kwcount, kwnames))
4158 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004159 goto fail;
4160 }
4161
Victor Stinner438a12d2019-05-24 17:01:38 +02004162 _PyErr_Format(tstate, PyExc_TypeError,
4163 "%U() got an unexpected keyword argument '%S'",
4164 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004167
Christian Heimes0bd447f2013-07-20 14:48:10 +02004168 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4169 goto fail;
4170 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004171 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004172
Benjamin Petersonb204a422011-06-05 22:04:07 -05004173 kw_found:
4174 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004175 _PyErr_Format(tstate, PyExc_TypeError,
4176 "%U() got multiple values for argument '%S'",
4177 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 Py_INCREF(value);
4181 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004183
4184 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004185 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004186 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 goto fail;
4188 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004189
4190 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004191 if (argcount < co->co_argcount) {
4192 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004193 Py_ssize_t missing = 0;
4194 for (i = argcount; i < m; i++) {
4195 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004196 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004197 }
4198 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004199 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004200 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004201 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004202 }
4203 if (n > m)
4204 i = n - m;
4205 else
4206 i = 0;
4207 for (; i < defcount; i++) {
4208 if (GETLOCAL(m+i) == NULL) {
4209 PyObject *def = defs[i];
4210 Py_INCREF(def);
4211 SETLOCAL(m+i, def);
4212 }
4213 }
4214 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004215
4216 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004217 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004218 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004219 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004220 PyObject *name;
4221 if (GETLOCAL(i) != NULL)
4222 continue;
4223 name = PyTuple_GET_ITEM(co->co_varnames, i);
4224 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004225 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004226 if (def) {
4227 Py_INCREF(def);
4228 SETLOCAL(i, def);
4229 continue;
4230 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004231 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004232 goto fail;
4233 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004234 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004235 missing++;
4236 }
4237 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004238 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004239 goto fail;
4240 }
4241 }
4242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004244 vars into frame. */
4245 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004247 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004248 /* Possibly account for the cell variable being an argument. */
4249 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004250 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004251 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004252 /* Clear the local copy. */
4253 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004254 }
4255 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004256 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004257 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004258 if (c == NULL)
4259 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004260 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004262
4263 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004264 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4265 PyObject *o = PyTuple_GET_ITEM(closure, i);
4266 Py_INCREF(o);
4267 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004269
Yury Selivanoveb636452016-09-08 22:01:51 -07004270 /* Handle generator/coroutine/asynchronous generator */
4271 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004272 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004273 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 /* Don't need to keep the reference to f_back, it will be set
4276 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004277 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 /* Create a new generator that owns the ready to run frame
4280 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004281 if (is_coro) {
4282 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004283 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4284 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004285 } else {
4286 gen = PyGen_NewWithQualName(f, name, qualname);
4287 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004288 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004289 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004290 }
INADA Naoki9c157762016-12-26 18:52:46 +09004291
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004292 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004293
Yury Selivanov75445082015-05-11 22:57:16 -04004294 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004296
Victor Stinnerb9e68122019-11-14 12:20:46 +01004297 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004298
Thomas Woutersce272b62007-09-19 21:19:28 +00004299fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 /* decref'ing the frame can cause __del__ methods to get invoked,
4302 which can call back into Python. While we're done with the
4303 current Python frame (f), the associated C stack is still in use,
4304 so recursion_depth must be boosted for the duration.
4305 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004306 if (Py_REFCNT(f) > 1) {
4307 Py_DECREF(f);
4308 _PyObject_GC_TRACK(f);
4309 }
4310 else {
4311 ++tstate->recursion_depth;
4312 Py_DECREF(f);
4313 --tstate->recursion_depth;
4314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004316}
4317
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004318
4319PyObject *
4320_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4321 PyObject *const *args, Py_ssize_t argcount,
4322 PyObject *const *kwnames, PyObject *const *kwargs,
4323 Py_ssize_t kwcount, int kwstep,
4324 PyObject *const *defs, Py_ssize_t defcount,
4325 PyObject *kwdefs, PyObject *closure,
4326 PyObject *name, PyObject *qualname)
4327{
4328 PyThreadState *tstate = _PyThreadState_GET();
4329 return _PyEval_EvalCode(tstate, _co, globals, locals,
4330 args, argcount,
4331 kwnames, kwargs,
4332 kwcount, kwstep,
4333 defs, defcount,
4334 kwdefs, closure,
4335 name, qualname);
4336}
4337
Victor Stinner40ee3012014-06-16 15:59:28 +02004338PyObject *
4339PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004340 PyObject *const *args, int argcount,
4341 PyObject *const *kws, int kwcount,
4342 PyObject *const *defs, int defcount,
4343 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004344{
4345 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004346 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004347 kws, kws != NULL ? kws + 1 : NULL,
4348 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004349 defs, defcount,
4350 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004351 NULL, NULL);
4352}
Tim Peters5ca576e2001-06-18 22:08:13 +00004353
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004354static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004355special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004358 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004359 if (res == NULL && !_PyErr_Occurred(tstate)) {
4360 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 return NULL;
4362 }
4363 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004364}
4365
4366
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004367/* Logic for the raise statement (too complicated for inlining).
4368 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004369static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004370do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (exc == NULL) {
4375 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004376 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004378 type = exc_info->exc_type;
4379 value = exc_info->exc_value;
4380 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004381 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004382 _PyErr_SetString(tstate, PyExc_RuntimeError,
4383 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004384 return 0;
4385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 Py_XINCREF(type);
4387 Py_XINCREF(value);
4388 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004389 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004390 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 /* We support the following forms of raise:
4394 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004395 raise <instance>
4396 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 if (PyExceptionClass_Check(exc)) {
4399 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004400 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 if (value == NULL)
4402 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004403 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004404 _PyErr_Format(tstate, PyExc_TypeError,
4405 "calling %R should have returned an instance of "
4406 "BaseException, not %R",
4407 type, Py_TYPE(value));
4408 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 }
4411 else if (PyExceptionInstance_Check(exc)) {
4412 value = exc;
4413 type = PyExceptionInstance_Class(exc);
4414 Py_INCREF(type);
4415 }
4416 else {
4417 /* Not something you can raise. You get an exception
4418 anyway, just not what you specified :-) */
4419 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004420 _PyErr_SetString(tstate, PyExc_TypeError,
4421 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 goto raise_error;
4423 }
Collin Winter828f04a2007-08-31 00:04:24 +00004424
Serhiy Storchakac0191582016-09-27 11:37:10 +03004425 assert(type != NULL);
4426 assert(value != NULL);
4427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (cause) {
4429 PyObject *fixed_cause;
4430 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004431 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 if (fixed_cause == NULL)
4433 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004434 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004436 else if (PyExceptionInstance_Check(cause)) {
4437 fixed_cause = cause;
4438 }
4439 else if (cause == Py_None) {
4440 Py_DECREF(cause);
4441 fixed_cause = NULL;
4442 }
4443 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004444 _PyErr_SetString(tstate, PyExc_TypeError,
4445 "exception causes must derive from "
4446 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 goto raise_error;
4448 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004449 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 }
Collin Winter828f04a2007-08-31 00:04:24 +00004451
Victor Stinner438a12d2019-05-24 17:01:38 +02004452 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004453 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004454 Py_DECREF(value);
4455 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004456 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004457
4458raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 Py_XDECREF(value);
4460 Py_XDECREF(type);
4461 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004462 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004463}
4464
Tim Petersd6d010b2001-06-21 02:49:55 +00004465/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004466 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004467
Guido van Rossum0368b722007-05-11 16:50:42 +00004468 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4469 with a variable target.
4470*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004471
Barry Warsawe42b18f1997-08-25 22:13:04 +00004472static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004473unpack_iterable(PyThreadState *tstate, PyObject *v,
4474 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 int i = 0, j = 0;
4477 Py_ssize_t ll = 0;
4478 PyObject *it; /* iter(v) */
4479 PyObject *w;
4480 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004485 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004486 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004487 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004488 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004489 _PyErr_Format(tstate, PyExc_TypeError,
4490 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004491 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004492 }
4493 return 0;
4494 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 for (; i < argcnt; i++) {
4497 w = PyIter_Next(it);
4498 if (w == NULL) {
4499 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004500 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004501 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 _PyErr_Format(tstate, PyExc_ValueError,
4503 "not enough values to unpack "
4504 "(expected %d, got %d)",
4505 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004506 }
4507 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004508 _PyErr_Format(tstate, PyExc_ValueError,
4509 "not enough values to unpack "
4510 "(expected at least %d, got %d)",
4511 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 }
4514 goto Error;
4515 }
4516 *--sp = w;
4517 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 if (argcntafter == -1) {
4520 /* We better have exhausted the iterator now. */
4521 w = PyIter_Next(it);
4522 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 goto Error;
4525 Py_DECREF(it);
4526 return 1;
4527 }
4528 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004529 _PyErr_Format(tstate, PyExc_ValueError,
4530 "too many values to unpack (expected %d)",
4531 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 goto Error;
4533 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 l = PySequence_List(it);
4536 if (l == NULL)
4537 goto Error;
4538 *--sp = l;
4539 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 ll = PyList_GET_SIZE(l);
4542 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004544 "not enough values to unpack (expected at least %d, got %zd)",
4545 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 goto Error;
4547 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 /* Pop the "after-variable" args off the list. */
4550 for (j = argcntafter; j > 0; j--, i++) {
4551 *--sp = PyList_GET_ITEM(l, ll - j);
4552 }
4553 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004554 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 Py_DECREF(it);
4556 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004557
Tim Petersd6d010b2001-06-21 02:49:55 +00004558Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 for (; i > 0; i--, sp++)
4560 Py_DECREF(*sp);
4561 Py_XDECREF(it);
4562 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004563}
4564
4565
Guido van Rossum96a42c81992-01-12 02:29:51 +00004566#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004567static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004568prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004571 if (PyObject_Print(v, stdout, 0) != 0) {
4572 /* Don't know what else to do */
4573 _PyErr_Clear(tstate);
4574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 printf("\n");
4576 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004577}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004578#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004579
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004580static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004581call_exc_trace(Py_tracefunc func, PyObject *self,
4582 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004583{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004584 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004586 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 if (value == NULL) {
4588 value = Py_None;
4589 Py_INCREF(value);
4590 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004591 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004592 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 arg = PyTuple_Pack(3, type, value, traceback);
4594 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004595 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 return;
4597 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004598 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004600 if (err == 0) {
4601 _PyErr_Restore(tstate, type, value, orig_traceback);
4602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 else {
4604 Py_XDECREF(type);
4605 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004606 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004608}
4609
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004610static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004611call_trace_protected(Py_tracefunc func, PyObject *obj,
4612 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 PyObject *type, *value, *traceback;
4616 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004617 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004618 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 if (err == 0)
4620 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004621 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 return 0;
4623 }
4624 else {
4625 Py_XDECREF(type);
4626 Py_XDECREF(value);
4627 Py_XDECREF(traceback);
4628 return -1;
4629 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004630}
4631
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004632static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004633call_trace(Py_tracefunc func, PyObject *obj,
4634 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 int result;
4638 if (tstate->tracing)
4639 return 0;
4640 tstate->tracing++;
4641 tstate->use_tracing = 0;
4642 result = func(obj, frame, what, arg);
4643 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4644 || (tstate->c_profilefunc != NULL));
4645 tstate->tracing--;
4646 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004647}
4648
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004649PyObject *
4650_PyEval_CallTracing(PyObject *func, PyObject *args)
4651{
Victor Stinner50b48572018-11-01 01:51:40 +01004652 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 int save_tracing = tstate->tracing;
4654 int save_use_tracing = tstate->use_tracing;
4655 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 tstate->tracing = 0;
4658 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4659 || (tstate->c_profilefunc != NULL));
4660 result = PyObject_Call(func, args, NULL);
4661 tstate->tracing = save_tracing;
4662 tstate->use_tracing = save_use_tracing;
4663 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004664}
4665
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004666/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004667static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004668maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004669 PyThreadState *tstate, PyFrameObject *frame,
4670 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 int result = 0;
4673 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 /* If the last instruction executed isn't in the current
4676 instruction window, reset the window.
4677 */
4678 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4679 PyAddrPair bounds;
4680 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4681 &bounds);
4682 *instr_lb = bounds.ap_lower;
4683 *instr_ub = bounds.ap_upper;
4684 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004685 /* If the last instruction falls at the start of a line or if it
4686 represents a jump backwards, update the frame's line number and
4687 then call the trace function if we're tracing source lines.
4688 */
4689 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004691 if (frame->f_trace_lines) {
4692 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 }
George King20faa682017-10-18 17:44:22 -07004695 /* Always emit an opcode event if we're tracing all opcodes. */
4696 if (frame->f_trace_opcodes) {
4697 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4698 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 *instr_prev = frame->f_lasti;
4700 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004701}
4702
Victor Stinner309d7cc2020-03-13 16:39:12 +01004703int
4704_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4705{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004706 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004707 /* The caller must hold the GIL */
4708 assert(PyGILState_Check());
4709
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004710 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004711 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004712 PyThreadState *current_tstate = _PyThreadState_GET();
4713 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004714 return -1;
4715 }
4716
4717 PyObject *profileobj = tstate->c_profileobj;
4718
4719 tstate->c_profilefunc = NULL;
4720 tstate->c_profileobj = NULL;
4721 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4722 tstate->use_tracing = tstate->c_tracefunc != NULL;
4723 Py_XDECREF(profileobj);
4724
4725 Py_XINCREF(arg);
4726 tstate->c_profileobj = arg;
4727 tstate->c_profilefunc = func;
4728
4729 /* Flag that tracing or profiling is turned on */
4730 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4731 return 0;
4732}
4733
Fred Drake5755ce62001-06-27 19:19:46 +00004734void
4735PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004736{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004737 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004738 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004739 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004740 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4741 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004742}
4743
4744int
4745_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4746{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004747 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004748 /* The caller must hold the GIL */
4749 assert(PyGILState_Check());
4750
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004751 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004752 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004753 PyThreadState *current_tstate = _PyThreadState_GET();
4754 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004755 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004756 }
4757
Victor Stinnerda2914d2020-03-20 09:29:08 +01004758 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004759 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004760 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004761
4762 tstate->c_tracefunc = NULL;
4763 tstate->c_traceobj = NULL;
4764 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4765 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4766 Py_XDECREF(traceobj);
4767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004769 tstate->c_traceobj = arg;
4770 tstate->c_tracefunc = func;
4771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004773 tstate->use_tracing = ((func != NULL)
4774 || (tstate->c_profilefunc != NULL));
4775
4776 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004777}
4778
4779void
4780PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4781{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004783 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004784 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004785 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4786 }
Fred Draked0838392001-06-16 21:02:31 +00004787}
4788
Victor Stinner309d7cc2020-03-13 16:39:12 +01004789
Yury Selivanov75445082015-05-11 22:57:16 -04004790void
Victor Stinner838f2642019-06-13 22:41:23 +02004791_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004792{
4793 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004794 tstate->coroutine_origin_tracking_depth = new_depth;
4795}
4796
4797int
4798_PyEval_GetCoroutineOriginTrackingDepth(void)
4799{
Victor Stinner50b48572018-11-01 01:51:40 +01004800 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004801 return tstate->coroutine_origin_tracking_depth;
4802}
4803
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004804int
Yury Selivanoveb636452016-09-08 22:01:51 -07004805_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4806{
Victor Stinner50b48572018-11-01 01:51:40 +01004807 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004808
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004809 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004810 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004811 }
4812
Yury Selivanoveb636452016-09-08 22:01:51 -07004813 Py_XINCREF(firstiter);
4814 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004815 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004816}
4817
4818PyObject *
4819_PyEval_GetAsyncGenFirstiter(void)
4820{
Victor Stinner50b48572018-11-01 01:51:40 +01004821 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004822 return tstate->async_gen_firstiter;
4823}
4824
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004825int
Yury Selivanoveb636452016-09-08 22:01:51 -07004826_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4827{
Victor Stinner50b48572018-11-01 01:51:40 +01004828 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004829
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004830 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004831 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004832 }
4833
Yury Selivanoveb636452016-09-08 22:01:51 -07004834 Py_XINCREF(finalizer);
4835 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004836 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004837}
4838
4839PyObject *
4840_PyEval_GetAsyncGenFinalizer(void)
4841{
Victor Stinner50b48572018-11-01 01:51:40 +01004842 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004843 return tstate->async_gen_finalizer;
4844}
4845
Victor Stinner438a12d2019-05-24 17:01:38 +02004846PyFrameObject *
4847PyEval_GetFrame(void)
4848{
4849 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004850 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004851}
4852
Guido van Rossumb209a111997-04-29 18:18:01 +00004853PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004854PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004855{
Victor Stinner438a12d2019-05-24 17:01:38 +02004856 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004857 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004859 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 else
4861 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004862}
4863
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004864/* Convenience function to get a builtin from its name */
4865PyObject *
4866_PyEval_GetBuiltinId(_Py_Identifier *name)
4867{
Victor Stinner438a12d2019-05-24 17:01:38 +02004868 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004869 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4870 if (attr) {
4871 Py_INCREF(attr);
4872 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004873 else if (!_PyErr_Occurred(tstate)) {
4874 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004875 }
4876 return attr;
4877}
4878
Guido van Rossumb209a111997-04-29 18:18:01 +00004879PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004880PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004881{
Victor Stinner438a12d2019-05-24 17:01:38 +02004882 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004883 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004884 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004885 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004887 }
4888
Victor Stinner438a12d2019-05-24 17:01:38 +02004889 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004890 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004891 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004892
4893 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004895}
4896
Guido van Rossumb209a111997-04-29 18:18:01 +00004897PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004898PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004899{
Victor Stinner438a12d2019-05-24 17:01:38 +02004900 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004901 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004902 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004904 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004905
4906 assert(current_frame->f_globals != NULL);
4907 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004908}
4909
Guido van Rossum6135a871995-01-09 17:53:26 +00004910int
Tim Peters5ba58662001-07-16 02:29:45 +00004911PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004912{
Victor Stinner438a12d2019-05-24 17:01:38 +02004913 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004914 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 if (current_frame != NULL) {
4918 const int codeflags = current_frame->f_code->co_flags;
4919 const int compilerflags = codeflags & PyCF_MASK;
4920 if (compilerflags) {
4921 result = 1;
4922 cf->cf_flags |= compilerflags;
4923 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004924#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 if (codeflags & CO_GENERATOR_ALLOWED) {
4926 result = 1;
4927 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4928 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004929#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 }
4931 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004932}
4933
Guido van Rossum3f5da241990-12-20 15:06:42 +00004934
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004935const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004936PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 if (PyMethod_Check(func))
4939 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4940 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004941 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 else if (PyCFunction_Check(func))
4943 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4944 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004945 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004946}
4947
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004948const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004949PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 if (PyMethod_Check(func))
4952 return "()";
4953 else if (PyFunction_Check(func))
4954 return "()";
4955 else if (PyCFunction_Check(func))
4956 return "()";
4957 else
4958 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004959}
4960
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004961#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004962if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004963 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4964 tstate, tstate->frame, \
4965 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004966 x = NULL; \
4967 } \
4968 else { \
4969 x = call; \
4970 if (tstate->c_profilefunc != NULL) { \
4971 if (x == NULL) { \
4972 call_trace_protected(tstate->c_profilefunc, \
4973 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004974 tstate, tstate->frame, \
4975 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 /* XXX should pass (type, value, tb) */ \
4977 } else { \
4978 if (call_trace(tstate->c_profilefunc, \
4979 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004980 tstate, tstate->frame, \
4981 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 Py_DECREF(x); \
4983 x = NULL; \
4984 } \
4985 } \
4986 } \
4987 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004988} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 x = call; \
4990 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004991
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004992
4993static PyObject *
4994trace_call_function(PyThreadState *tstate,
4995 PyObject *func,
4996 PyObject **args, Py_ssize_t nargs,
4997 PyObject *kwnames)
4998{
4999 PyObject *x;
5000 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005001 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005002 return x;
5003 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005004 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005005 /* We need to create a temporary bound method as argument
5006 for profiling.
5007
5008 If nargs == 0, then this cannot work because we have no
5009 "self". In any case, the call itself would raise
5010 TypeError (foo needs an argument), so we just skip
5011 profiling. */
5012 PyObject *self = args[0];
5013 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5014 if (func == NULL) {
5015 return NULL;
5016 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005017 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005018 args+1, nargs-1,
5019 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005020 Py_DECREF(func);
5021 return x;
5022 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005023 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005024}
5025
Victor Stinner415c5102017-01-11 00:54:57 +01005026/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5027 to reduce the stack consumption. */
5028Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005029call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005030{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005031 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 PyObject *func = *pfunc;
5033 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005034 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5035 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005036 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005037
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005038 if (tstate->use_tracing) {
5039 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005040 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005041 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005042 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005044
Victor Stinner438a12d2019-05-24 17:01:38 +02005045 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005046
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005047 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 while ((*pp_stack) > pfunc) {
5049 w = EXT_POP(*pp_stack);
5050 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005054}
5055
Jeremy Hylton52820442001-01-03 23:52:36 +00005056static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005057do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005058{
jdemeyere89de732018-09-19 12:06:20 +02005059 PyObject *result;
5060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005062 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005063 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005065 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005066 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5067 if (nargs > 0 && tstate->use_tracing) {
5068 /* We need to create a temporary bound method as argument
5069 for profiling.
5070
5071 If nargs == 0, then this cannot work because we have no
5072 "self". In any case, the call itself would raise
5073 TypeError (foo needs an argument), so we just skip
5074 profiling. */
5075 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5076 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5077 if (func == NULL) {
5078 return NULL;
5079 }
5080
Victor Stinner4d231bc2019-11-14 13:36:21 +01005081 C_TRACE(result, _PyObject_FastCallDictTstate(
5082 tstate, func,
5083 &_PyTuple_ITEMS(callargs)[1],
5084 nargs - 1,
5085 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005086 Py_DECREF(func);
5087 return result;
5088 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005089 }
jdemeyere89de732018-09-19 12:06:20 +02005090 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005091}
5092
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005093/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005094 nb_index slot defined, and store in *pi.
5095 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005096 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005097 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005098*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005099int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005100_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005101{
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005103 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005105 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005107 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 return 0;
5109 }
5110 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005111 _PyErr_SetString(tstate, PyExc_TypeError,
5112 "slice indices must be integers or "
5113 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 return 0;
5115 }
5116 *pi = x;
5117 }
5118 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005119}
5120
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005121int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005122_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005123{
Victor Stinner438a12d2019-05-24 17:01:38 +02005124 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005125 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005126 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005127 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005128 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005129 return 0;
5130 }
5131 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005132 _PyErr_SetString(tstate, PyExc_TypeError,
5133 "slice indices must be integers or "
5134 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005135 return 0;
5136 }
5137 *pi = x;
5138 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005139}
5140
Thomas Wouters52152252000-08-17 22:55:00 +00005141static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005142import_name(PyThreadState *tstate, PyFrameObject *f,
5143 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005144{
5145 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005146 PyObject *import_func, *res;
5147 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005148
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005149 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005150 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005151 if (!_PyErr_Occurred(tstate)) {
5152 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005153 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005154 return NULL;
5155 }
5156
5157 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005159 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005160 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005161 return NULL;
5162 }
5163 res = PyImport_ImportModuleLevelObject(
5164 name,
5165 f->f_globals,
5166 f->f_locals == NULL ? Py_None : f->f_locals,
5167 fromlist,
5168 ilevel);
5169 return res;
5170 }
5171
5172 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005173
5174 stack[0] = name;
5175 stack[1] = f->f_globals;
5176 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5177 stack[3] = fromlist;
5178 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005179 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005180 Py_DECREF(import_func);
5181 return res;
5182}
5183
5184static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005185import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005188 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005189
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005190 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005191 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005192 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005193 /* Issue #17636: in case this failed because of a circular relative
5194 import, try to fallback on reading the module directly from
5195 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005196 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005197 if (pkgname == NULL) {
5198 goto error;
5199 }
Oren Milman6db70332017-09-19 14:23:01 +03005200 if (!PyUnicode_Check(pkgname)) {
5201 Py_CLEAR(pkgname);
5202 goto error;
5203 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005204 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005205 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005206 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005207 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005208 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005209 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005210 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005211 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005212 goto error;
5213 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005214 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005216 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005217 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005218 if (pkgname == NULL) {
5219 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5220 if (pkgname_or_unknown == NULL) {
5221 Py_XDECREF(pkgpath);
5222 return NULL;
5223 }
5224 } else {
5225 pkgname_or_unknown = pkgname;
5226 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005227
5228 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005229 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005230 errmsg = PyUnicode_FromFormat(
5231 "cannot import name %R from %R (unknown location)",
5232 name, pkgname_or_unknown
5233 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005234 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005235 PyErr_SetImportError(errmsg, pkgname, NULL);
5236 }
5237 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005238 _Py_IDENTIFIER(__spec__);
5239 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005240 const char *fmt =
5241 _PyModuleSpec_IsInitializing(spec) ?
5242 "cannot import name %R from partially initialized module %R "
5243 "(most likely due to a circular import) (%S)" :
5244 "cannot import name %R from %R (%S)";
5245 Py_XDECREF(spec);
5246
5247 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005248 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005249 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005250 }
5251
Xiang Zhang4830f582017-03-21 11:13:42 +08005252 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005253 Py_XDECREF(pkgname_or_unknown);
5254 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005255 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005256}
Guido van Rossumac7be682001-01-17 15:42:30 +00005257
Thomas Wouters52152252000-08-17 22:55:00 +00005258static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005259import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005260{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005261 _Py_IDENTIFIER(__all__);
5262 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005263 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 int skip_leading_underscores = 0;
5265 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005266
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005267 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5268 return -1; /* Unexpected error */
5269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005271 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5272 return -1;
5273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005275 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005276 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 return -1;
5278 }
5279 all = PyMapping_Keys(dict);
5280 Py_DECREF(dict);
5281 if (all == NULL)
5282 return -1;
5283 skip_leading_underscores = 1;
5284 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 for (pos = 0, err = 0; ; pos++) {
5287 name = PySequence_GetItem(all, pos);
5288 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005289 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005291 }
5292 else {
5293 _PyErr_Clear(tstate);
5294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 break;
5296 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005297 if (!PyUnicode_Check(name)) {
5298 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5299 if (modname == NULL) {
5300 Py_DECREF(name);
5301 err = -1;
5302 break;
5303 }
5304 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005305 _PyErr_Format(tstate, PyExc_TypeError,
5306 "module __name__ must be a string, not %.100s",
5307 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005308 }
5309 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005310 _PyErr_Format(tstate, PyExc_TypeError,
5311 "%s in %U.%s must be str, not %.100s",
5312 skip_leading_underscores ? "Key" : "Item",
5313 modname,
5314 skip_leading_underscores ? "__dict__" : "__all__",
5315 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005316 }
5317 Py_DECREF(modname);
5318 Py_DECREF(name);
5319 err = -1;
5320 break;
5321 }
5322 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005323 if (PyUnicode_READY(name) == -1) {
5324 Py_DECREF(name);
5325 err = -1;
5326 break;
5327 }
5328 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5329 Py_DECREF(name);
5330 continue;
5331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 }
5333 value = PyObject_GetAttr(v, name);
5334 if (value == NULL)
5335 err = -1;
5336 else if (PyDict_CheckExact(locals))
5337 err = PyDict_SetItem(locals, name, value);
5338 else
5339 err = PyObject_SetItem(locals, name, value);
5340 Py_DECREF(name);
5341 Py_XDECREF(value);
5342 if (err != 0)
5343 break;
5344 }
5345 Py_DECREF(all);
5346 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005347}
5348
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005349static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005350check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005351{
Victor Stinnera102ed72020-02-07 02:24:48 +01005352 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005353 /* check_args_iterable() may be called with a live exception:
5354 * clear it to prevent calling _PyObject_FunctionStr() with an
5355 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005356 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005357 PyObject *funcstr = _PyObject_FunctionStr(func);
5358 if (funcstr != NULL) {
5359 _PyErr_Format(tstate, PyExc_TypeError,
5360 "%U argument after * must be an iterable, not %.200s",
5361 funcstr, Py_TYPE(args)->tp_name);
5362 Py_DECREF(funcstr);
5363 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005364 return -1;
5365 }
5366 return 0;
5367}
5368
5369static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005370format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005371{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005372 /* _PyDict_MergeEx raises attribute
5373 * error (percolated from an attempt
5374 * to get 'keys' attribute) instead of
5375 * a type error if its second argument
5376 * is not a mapping.
5377 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005379 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005380 PyObject *funcstr = _PyObject_FunctionStr(func);
5381 if (funcstr != NULL) {
5382 _PyErr_Format(
5383 tstate, PyExc_TypeError,
5384 "%U argument after ** must be a mapping, not %.200s",
5385 funcstr, Py_TYPE(kwargs)->tp_name);
5386 Py_DECREF(funcstr);
5387 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005388 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005389 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005390 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005391 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005392 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005393 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005394 PyObject *funcstr = _PyObject_FunctionStr(func);
5395 if (funcstr != NULL) {
5396 PyObject *key = PyTuple_GET_ITEM(val, 0);
5397 _PyErr_Format(
5398 tstate, PyExc_TypeError,
5399 "%U got multiple values for keyword argument '%S'",
5400 funcstr, key);
5401 Py_DECREF(funcstr);
5402 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005403 Py_XDECREF(exc);
5404 Py_XDECREF(val);
5405 Py_XDECREF(tb);
5406 }
5407 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005408 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005409 }
5410 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005411}
5412
Guido van Rossumac7be682001-01-17 15:42:30 +00005413static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005414format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5415 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005417 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 if (!obj)
5420 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005421
Serhiy Storchaka06515832016-11-20 09:13:07 +02005422 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 if (!obj_str)
5424 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005425
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005427}
Guido van Rossum950361c1997-01-24 13:49:28 +00005428
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005429static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005430format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005431{
5432 PyObject *name;
5433 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005434 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005435 return;
5436 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5437 name = PyTuple_GET_ITEM(co->co_cellvars,
5438 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005439 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005440 PyExc_UnboundLocalError,
5441 UNBOUNDLOCAL_ERROR_MSG,
5442 name);
5443 } else {
5444 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5445 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005446 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005447 UNBOUNDFREE_ERROR_MSG, name);
5448 }
5449}
5450
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005451static void
Mark Shannonfee55262019-11-21 09:11:43 +00005452format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005453{
5454 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5455 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005456 _PyErr_Format(tstate, PyExc_TypeError,
5457 "'async with' received an object from __aenter__ "
5458 "that does not implement __await__: %.100s",
5459 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005460 }
Mark Shannonfee55262019-11-21 09:11:43 +00005461 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005462 _PyErr_Format(tstate, PyExc_TypeError,
5463 "'async with' received an object from __aexit__ "
5464 "that does not implement __await__: %.100s",
5465 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005466 }
5467 }
5468}
5469
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005470static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005471unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005472 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005473{
5474 PyObject *res;
5475 if (Py_REFCNT(v) == 2) {
5476 /* In the common case, there are 2 references to the value
5477 * stored in 'variable' when the += is performed: one on the
5478 * value stack (in 'v') and one still stored in the
5479 * 'variable'. We try to delete the variable now to reduce
5480 * the refcnt to 1.
5481 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005482 int opcode, oparg;
5483 NEXTOPARG();
5484 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005485 case STORE_FAST:
5486 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005487 PyObject **fastlocals = f->f_localsplus;
5488 if (GETLOCAL(oparg) == v)
5489 SETLOCAL(oparg, NULL);
5490 break;
5491 }
5492 case STORE_DEREF:
5493 {
5494 PyObject **freevars = (f->f_localsplus +
5495 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005496 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005497 if (PyCell_GET(c) == v) {
5498 PyCell_SET(c, NULL);
5499 Py_DECREF(v);
5500 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005501 break;
5502 }
5503 case STORE_NAME:
5504 {
5505 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005506 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005507 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005508 if (locals && PyDict_CheckExact(locals)) {
5509 PyObject *w = PyDict_GetItemWithError(locals, name);
5510 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005511 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005512 {
5513 Py_DECREF(v);
5514 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005515 }
5516 }
5517 break;
5518 }
5519 }
5520 }
5521 res = v;
5522 PyUnicode_Append(&res, w);
5523 return res;
5524}
5525
Guido van Rossum950361c1997-01-24 13:49:28 +00005526#ifdef DYNAMIC_EXECUTION_PROFILE
5527
Skip Montanarof118cb12001-10-15 20:51:38 +00005528static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005529getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005531 int i;
5532 PyObject *l = PyList_New(256);
5533 if (l == NULL) return NULL;
5534 for (i = 0; i < 256; i++) {
5535 PyObject *x = PyLong_FromLong(a[i]);
5536 if (x == NULL) {
5537 Py_DECREF(l);
5538 return NULL;
5539 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005540 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 }
5542 for (i = 0; i < 256; i++)
5543 a[i] = 0;
5544 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005545}
5546
5547PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005548_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005549{
5550#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005552#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 int i;
5554 PyObject *l = PyList_New(257);
5555 if (l == NULL) return NULL;
5556 for (i = 0; i < 257; i++) {
5557 PyObject *x = getarray(dxpairs[i]);
5558 if (x == NULL) {
5559 Py_DECREF(l);
5560 return NULL;
5561 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005562 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 }
5564 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005565#endif
5566}
5567
5568#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005569
5570Py_ssize_t
5571_PyEval_RequestCodeExtraIndex(freefunc free)
5572{
Victor Stinner81a7be32020-04-14 15:14:01 +02005573 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005574 Py_ssize_t new_index;
5575
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005576 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005577 return -1;
5578 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005579 new_index = interp->co_extra_user_count++;
5580 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005581 return new_index;
5582}
Łukasz Langaa785c872016-09-09 17:37:37 -07005583
5584static void
5585dtrace_function_entry(PyFrameObject *f)
5586{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005587 const char *filename;
5588 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005589 int lineno;
5590
Victor Stinner6d86a232020-04-29 00:56:58 +02005591 PyCodeObject *code = f->f_code;
5592 filename = PyUnicode_AsUTF8(code->co_filename);
5593 funcname = PyUnicode_AsUTF8(code->co_name);
5594 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005595
Andy Lestere6be9b52020-02-11 20:28:35 -06005596 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005597}
5598
5599static void
5600dtrace_function_return(PyFrameObject *f)
5601{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005602 const char *filename;
5603 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005604 int lineno;
5605
Victor Stinner6d86a232020-04-29 00:56:58 +02005606 PyCodeObject *code = f->f_code;
5607 filename = PyUnicode_AsUTF8(code->co_filename);
5608 funcname = PyUnicode_AsUTF8(code->co_name);
5609 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005610
Andy Lestere6be9b52020-02-11 20:28:35 -06005611 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005612}
5613
5614/* DTrace equivalent of maybe_call_line_trace. */
5615static void
5616maybe_dtrace_line(PyFrameObject *frame,
5617 int *instr_lb, int *instr_ub, int *instr_prev)
5618{
5619 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005620 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005621
5622 /* If the last instruction executed isn't in the current
5623 instruction window, reset the window.
5624 */
5625 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5626 PyAddrPair bounds;
5627 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5628 &bounds);
5629 *instr_lb = bounds.ap_lower;
5630 *instr_ub = bounds.ap_upper;
5631 }
5632 /* If the last instruction falls at the start of a line or if
5633 it represents a jump backwards, update the frame's line
5634 number and call the trace function. */
5635 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5636 frame->f_lineno = line;
5637 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5638 if (!co_filename)
5639 co_filename = "?";
5640 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5641 if (!co_name)
5642 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005643 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005644 }
5645 *instr_prev = frame->f_lasti;
5646}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005647
5648
5649/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5650 for the limited API. */
5651
5652#undef Py_EnterRecursiveCall
5653
5654int Py_EnterRecursiveCall(const char *where)
5655{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005656 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005657}
5658
5659#undef Py_LeaveRecursiveCall
5660
5661void Py_LeaveRecursiveCall(void)
5662{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005663 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005664}