blob: 3392cd0365a28541a69932e4c0afebbe8e4ddf63 [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 Stinner6d62dc12020-06-03 20:16:39 +0200243void _Py_NO_RETURN
244_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100245{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200246 _Py_FatalErrorFunc(func,
247 "the function must be called with the GIL held, "
248 "but the GIL is released "
249 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100250}
251
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 Stinner6d62dc12020-06-03 20:16:39 +0200349 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100350
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 Stinner6d62dc12020-06-03 20:16:39 +0200378 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100379
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 Stinner7be4e352020-05-05 20:27:47 +0200412 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner6d62dc12020-06-03 20:16:39 +0200413 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner7be4e352020-05-05 20:27:47 +0200414
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100415 struct _gil_runtime_state *gil = &runtime->ceval.gil;
416 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200418 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100419 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100420
421 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700422
Victor Stinner50e6e992020-03-19 02:41:21 +0100423 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900424 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700425 Py_FatalError("Can't initialize threads for pending calls");
426 }
Jesse Nollera8513972008-07-17 16:49:17 +0000427
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200428 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100429 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000430}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900431#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000432
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000433/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600434 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000435
436void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100437_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000438{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200439 assert(is_tstate_valid(tstate));
440 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000441}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000442
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000443PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000444PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445{
Victor Stinner09532fe2019-05-10 23:39:09 +0200446 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200447 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinner6d62dc12020-06-03 20:16:39 +0200448 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100449
Victor Stinner0b1e3302020-05-05 16:14:31 +0200450 struct _ceval_runtime_state *ceval = &runtime->ceval;
451 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200452 assert(gil_created(&ceval->gil));
Victor Stinner0b1e3302020-05-05 16:14:31 +0200453 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455}
456
457void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000458PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200460 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100461
Victor Stinner85f5a692020-03-09 22:12:04 +0100462 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100463
Victor Stinner85f5a692020-03-09 22:12:04 +0100464 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
465 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466}
467
468
Guido van Rossuma9672091994-09-14 13:31:22 +0000469/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
470 signal handlers or Mac I/O completion routines) can schedule calls
471 to a function to be called synchronously.
472 The synchronous function is called with one void* argument.
473 It should return 0 for success or -1 for failure -- failure should
474 be accompanied by an exception.
475
476 If registry succeeds, the registry function returns 0; if it fails
477 (e.g. due to too many pending calls) it returns -1 (without setting
478 an exception condition).
479
480 Note that because registry may occur from within signal handlers,
481 or other asynchronous events, calling malloc() is unsafe!
482
Guido van Rossuma9672091994-09-14 13:31:22 +0000483 Any thread can schedule pending calls, but only the main thread
484 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485 There is no facility to schedule calls to a particular thread, but
486 that should be easy to change, should that ever be required. In
487 that case, the static variables here should go into the python
488 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000490
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200491void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200492_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200493{
494 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100495 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200496 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200497 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200498}
499
Eric Snow5be45a62019-03-08 22:47:07 -0700500/* Push one item onto the queue while holding the lock. */
501static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200502_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600503 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700504{
Eric Snow842a2f02019-03-15 15:47:51 -0600505 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700506 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600507 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700508 return -1; /* Queue full */
509 }
Eric Snow842a2f02019-03-15 15:47:51 -0600510 pending->calls[i].func = func;
511 pending->calls[i].arg = arg;
512 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700513 return 0;
514}
515
516/* Pop one item off the queue while holding the lock. */
517static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200518_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600519 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700520{
Eric Snow842a2f02019-03-15 15:47:51 -0600521 int i = pending->first;
522 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700523 return; /* Queue empty */
524 }
525
Eric Snow842a2f02019-03-15 15:47:51 -0600526 *func = pending->calls[i].func;
527 *arg = pending->calls[i].arg;
528 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700529}
530
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200531/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532 scheduling to be made from any thread, and even from an executing
533 callback.
534 */
535
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200537_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200538 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000539{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200540 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600541
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200542 /* Ensure that _PyEval_InitPendingCalls() was called
543 and that _PyEval_FiniPendingCalls() is not called yet. */
544 assert(pending->lock != NULL);
545
Eric Snow842a2f02019-03-15 15:47:51 -0600546 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200547 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600548 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700549
Victor Stinnere225beb2019-06-03 18:14:24 +0200550 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200551 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000553}
554
Victor Stinner09532fe2019-05-10 23:39:09 +0200555int
556Py_AddPendingCall(int (*func)(void *), void *arg)
557{
Victor Stinner50e6e992020-03-19 02:41:21 +0100558 /* Best-effort to support subinterpreters and calls with the GIL released.
559
560 First attempt _PyThreadState_GET() since it supports subinterpreters.
561
562 If the GIL is released, _PyThreadState_GET() returns NULL . In this
563 case, use PyGILState_GetThisThreadState() which works even if the GIL
564 is released.
565
566 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
567 see bpo-10915 and bpo-15751.
568
Victor Stinner8849e592020-03-18 19:28:53 +0100569 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100570 PyThreadState *tstate = _PyThreadState_GET();
571 if (tstate == NULL) {
572 tstate = PyGILState_GetThisThreadState();
573 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200574
575 PyInterpreterState *interp;
576 if (tstate != NULL) {
577 interp = tstate->interp;
578 }
579 else {
580 /* Last resort: use the main interpreter */
581 interp = _PyRuntime.interpreters.main;
582 }
583 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200584}
585
Eric Snowfdf282d2019-01-11 14:26:55 -0700586static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100587handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700588{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200589 assert(is_tstate_valid(tstate));
590 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700591 return 0;
592 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700593
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200594 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100595 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
596 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200597 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700598 return -1;
599 }
600 return 0;
601}
602
603static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100604make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000605{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200606 assert(is_tstate_valid(tstate));
607
Victor Stinnerd8316882020-03-20 14:50:35 +0100608 /* only execute pending calls on main thread */
609 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200610 return 0;
611 }
612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100614 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700615 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700617 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200618 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100619
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200620 /* unsignal before starting to call callbacks, so that any callback
621 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200622 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700623 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100626 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700627 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700628 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 void *arg = NULL;
630
631 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600632 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200633 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600634 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700635
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100636 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700637 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100638 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700639 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700640 res = func(arg);
641 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200642 goto error;
643 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200645
Charles-François Natalif23339a2011-07-23 18:15:43 +0200646 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700647 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200648
649error:
650 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200651 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700652 return res;
653}
654
Eric Snow842a2f02019-03-15 15:47:51 -0600655void
Victor Stinner2b1df452020-01-13 18:46:59 +0100656_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600657{
Eric Snow842a2f02019-03-15 15:47:51 -0600658 assert(PyGILState_Check());
659
Victor Stinner50e6e992020-03-19 02:41:21 +0100660 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200661
Eric Snow842a2f02019-03-15 15:47:51 -0600662 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
663 return;
664 }
665
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100666 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200667 PyObject *exc, *val, *tb;
668 _PyErr_Fetch(tstate, &exc, &val, &tb);
669 PyErr_BadInternalCall();
670 _PyErr_ChainExceptions(exc, val, tb);
671 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600672 }
673}
674
Eric Snowfdf282d2019-01-11 14:26:55 -0700675/* Py_MakePendingCalls() is a simple wrapper for the sake
676 of backward-compatibility. */
677int
678Py_MakePendingCalls(void)
679{
680 assert(PyGILState_Check());
681
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100682 PyThreadState *tstate = _PyThreadState_GET();
683
Eric Snowfdf282d2019-01-11 14:26:55 -0700684 /* Python signal handler doesn't really queue a callback: it only signals
685 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100686 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700687 if (res != 0) {
688 return res;
689 }
690
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100691 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600692 if (res != 0) {
693 return res;
694 }
695
696 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000697}
698
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000699/* The interpreter's recursion limit */
700
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000701#ifndef Py_DEFAULT_RECURSION_LIMIT
702#define Py_DEFAULT_RECURSION_LIMIT 1000
703#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600704
Eric Snow05351c12017-09-05 21:43:08 -0700705int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000706
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600707void
Victor Stinnerdab84232020-03-17 18:56:44 +0100708_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600709{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600710 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100711 _gil_initialize(&ceval->gil);
712}
713
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200714int
Victor Stinnerdab84232020-03-17 18:56:44 +0100715_PyEval_InitState(struct _ceval_state *ceval)
716{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200717 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
718
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200719 struct _pending_calls *pending = &ceval->pending;
720 assert(pending->lock == NULL);
721
722 pending->lock = PyThread_allocate_lock();
723 if (pending->lock == NULL) {
724 return -1;
725 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200726
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200727 return 0;
728}
729
730void
731_PyEval_FiniState(struct _ceval_state *ceval)
732{
733 struct _pending_calls *pending = &ceval->pending;
734 if (pending->lock != NULL) {
735 PyThread_free_lock(pending->lock);
736 pending->lock = NULL;
737 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600738}
739
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000740int
741Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000742{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200743 PyThreadState *tstate = _PyThreadState_GET();
744 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000745}
746
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000747void
748Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000749{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200750 PyThreadState *tstate = _PyThreadState_GET();
751 tstate->interp->ceval.recursion_limit = new_limit;
752 if (_Py_IsMainInterpreter(tstate)) {
753 _Py_CheckRecursionLimit = new_limit;
754 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000755}
756
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100757/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000758 if the recursion_depth reaches _Py_CheckRecursionLimit.
759 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
760 to guarantee that _Py_CheckRecursiveCall() is regularly called.
761 Without USE_STACKCHECK, there is no need for this. */
762int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100763_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000764{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200765 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000766
767#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700768 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (PyOS_CheckStack()) {
770 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200771 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 return -1;
773 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200774 if (_Py_IsMainInterpreter(tstate)) {
775 /* Needed for ABI backwards-compatibility (see bpo-31857) */
776 _Py_CheckRecursionLimit = recursion_limit;
777 }
pdox18967932017-10-25 23:03:01 -0700778#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (tstate->recursion_critical)
780 /* Somebody asked that we don't check for recursion. */
781 return 0;
782 if (tstate->overflowed) {
783 if (tstate->recursion_depth > recursion_limit + 50) {
784 /* Overflowing while handling an overflow. Give up. */
785 Py_FatalError("Cannot recover from stack overflow.");
786 }
787 return 0;
788 }
789 if (tstate->recursion_depth > recursion_limit) {
790 --tstate->recursion_depth;
791 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200792 _PyErr_Format(tstate, PyExc_RecursionError,
793 "maximum recursion depth exceeded%s",
794 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return -1;
796 }
797 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000798}
799
Victor Stinner09532fe2019-05-10 23:39:09 +0200800static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200801static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000802
Victor Stinnere225beb2019-06-03 18:14:24 +0200803#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000804
Guido van Rossum374a9221991-04-04 10:40:29 +0000805
Guido van Rossumb209a111997-04-29 18:18:01 +0000806PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000807PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 return PyEval_EvalCodeEx(co,
810 globals, locals,
811 (PyObject **)NULL, 0,
812 (PyObject **)NULL, 0,
813 (PyObject **)NULL, 0,
814 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000815}
816
817
818/* Interpreter main loop */
819
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000820PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100821PyEval_EvalFrame(PyFrameObject *f)
822{
Victor Stinner0b72b232020-03-12 23:18:39 +0100823 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100824 PyThreadState *tstate = _PyThreadState_GET();
825 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000826}
827
828PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000830{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100831 PyThreadState *tstate = _PyThreadState_GET();
832 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700833}
834
Victor Stinnerda2914d2020-03-20 09:29:08 +0100835
836/* Handle signals, pending calls, GIL drop request
837 and asynchronous exception */
838static int
839eval_frame_handle_pending(PyThreadState *tstate)
840{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100841 _PyRuntimeState * const runtime = &_PyRuntime;
842 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200843
844 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200845 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100846 if (handle_signals(tstate) != 0) {
847 return -1;
848 }
849 }
850
851 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200852 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100853 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
854 if (make_pending_calls(tstate) != 0) {
855 return -1;
856 }
857 }
858
859 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200860 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100861 /* Give another thread a chance */
862 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
863 Py_FatalError("tstate mix-up");
864 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200865 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100866
867 /* Other threads may run now */
868
869 take_gil(tstate);
870
871 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
872 Py_FatalError("orphan tstate");
873 }
874 }
875
876 /* Check for asynchronous exception. */
877 if (tstate->async_exc != NULL) {
878 PyObject *exc = tstate->async_exc;
879 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200880 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100881 _PyErr_SetNone(tstate, exc);
882 Py_DECREF(exc);
883 return -1;
884 }
885
886 return 0;
887}
888
Victor Stinnerc6944e72016-11-11 02:13:35 +0100889PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100890_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700891{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200892 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100893
Guido van Rossum950361c1997-01-24 13:49:28 +0000894#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000896#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200897 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300898 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200899 int opcode; /* Current opcode */
900 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200901 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100903 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100904 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 is true when the line being executed has changed. The
912 initial values are such as to make this false the first
913 time it is tested. */
914 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000915
Serhiy Storchakaab874002016-09-11 13:48:15 +0300916 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyObject *names;
918 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900919 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000920
Brett Cannon368b4b72012-04-02 12:17:59 -0400921#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200922 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400923#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200924
Antoine Pitroub52ec782009-01-25 16:34:23 +0000925/* Computed GOTOs, or
926 the-optimization-commonly-but-improperly-known-as-"threaded code"
927 using gcc's labels-as-values extension
928 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
929
930 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000932 combined with a lookup table of jump addresses. However, since the
933 indirect jump instruction is shared by all opcodes, the CPU will have a
934 hard time making the right prediction for where to jump next (actually,
935 it will be always wrong except in the uncommon case of a sequence of
936 several identical opcodes).
937
938 "Threaded code" in contrast, uses an explicit jump table and an explicit
939 indirect jump instruction at the end of each opcode. Since the jump
940 instruction is at a different address for each opcode, the CPU will make a
941 separate prediction for each of these instructions, which is equivalent to
942 predicting the second opcode of each opcode pair. These predictions have
943 a much better chance to turn out valid, especially in small bytecode loops.
944
945 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000947 and potentially many more instructions (depending on the pipeline width).
948 A correctly predicted branch, however, is nearly free.
949
950 At the time of this writing, the "threaded code" version is up to 15-20%
951 faster than the normal "switch" version, depending on the compiler and the
952 CPU architecture.
953
954 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
955 because it would render the measurements invalid.
956
957
958 NOTE: care must be taken that the compiler doesn't try to "optimize" the
959 indirect jumps by sharing them between all opcodes. Such optimizations
960 can be disabled on gcc by using the -fno-gcse flag (or possibly
961 -fno-crossjumping).
962*/
963
Antoine Pitrou042b1282010-08-13 21:15:58 +0000964#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000965#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000966#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000967#endif
968
Antoine Pitrou042b1282010-08-13 21:15:58 +0000969#ifdef HAVE_COMPUTED_GOTOS
970 #ifndef USE_COMPUTED_GOTOS
971 #define USE_COMPUTED_GOTOS 1
972 #endif
973#else
974 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
975 #error "Computed gotos are not supported on this compiler."
976 #endif
977 #undef USE_COMPUTED_GOTOS
978 #define USE_COMPUTED_GOTOS 0
979#endif
980
981#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000982/* Import the static jump table */
983#include "opcode_targets.h"
984
Antoine Pitroub52ec782009-01-25 16:34:23 +0000985#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700986 op: \
987 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000988
Antoine Pitroub52ec782009-01-25 16:34:23 +0000989#ifdef LLTRACE
990#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100992 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300994 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300995 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 } \
997 goto fast_next_opcode; \
998 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999#else
1000#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001002 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001004 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001005 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 } \
1007 goto fast_next_opcode; \
1008 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001009#endif
1010
Victor Stinner09532fe2019-05-10 23:39:09 +02001011#define DISPATCH() \
1012 { \
1013 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1014 FAST_DISPATCH(); \
1015 } \
1016 continue; \
1017 }
1018
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001020#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001022#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001023#endif
1024
1025
Neal Norwitza81d2202002-07-14 00:27:26 +00001026/* Tuple access macros */
1027
1028#ifndef Py_DEBUG
1029#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1030#else
1031#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1032#endif
1033
Guido van Rossum374a9221991-04-04 10:40:29 +00001034/* Code access macros */
1035
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001036/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001037#define INSTR_OFFSET() \
1038 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001039#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001040 _Py_CODEUNIT word = *next_instr; \
1041 opcode = _Py_OPCODE(word); \
1042 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001043 next_instr++; \
1044 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001045#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1046#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001047
Raymond Hettingerf606f872003-03-16 03:11:04 +00001048/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 Some opcodes tend to come in pairs thus making it possible to
1050 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001051 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 Verifying the prediction costs a single high-speed test of a register
1054 variable against a constant. If the pairing was good, then the
1055 processor's own internal branch predication has a high likelihood of
1056 success, resulting in a nearly zero-overhead transition to the
1057 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001058 including its unpredictable switch-case branch. Combined with the
1059 processor's internal branch prediction, a successful PREDICT has the
1060 effect of making the two opcodes run as if they were a single new opcode
1061 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001062
Georg Brandl86b2fb92008-07-16 03:43:04 +00001063 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 predictions turned-on and interpret the results as if some opcodes
1065 had been combined or turn-off predictions so that the opcode frequency
1066 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001067
1068 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 the CPU to record separate branch prediction information for each
1070 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001071
Raymond Hettingerf606f872003-03-16 03:11:04 +00001072*/
1073
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001074#define PREDICT_ID(op) PRED_##op
1075
Antoine Pitrou042b1282010-08-13 21:15:58 +00001076#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001077#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001078#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001079#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001080 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001081 _Py_CODEUNIT word = *next_instr; \
1082 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001083 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001084 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001085 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001086 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001087 } \
1088 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001089#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001090#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001091
Raymond Hettingerf606f872003-03-16 03:11:04 +00001092
Guido van Rossum374a9221991-04-04 10:40:29 +00001093/* Stack manipulation macros */
1094
Martin v. Löwis18e16552006-02-15 17:27:45 +00001095/* The stack can grow at most MAXINT deep, as co_nlocals and
1096 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001097#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1098#define EMPTY() (STACK_LEVEL() == 0)
1099#define TOP() (stack_pointer[-1])
1100#define SECOND() (stack_pointer[-2])
1101#define THIRD() (stack_pointer[-3])
1102#define FOURTH() (stack_pointer[-4])
1103#define PEEK(n) (stack_pointer[-(n)])
1104#define SET_TOP(v) (stack_pointer[-1] = (v))
1105#define SET_SECOND(v) (stack_pointer[-2] = (v))
1106#define SET_THIRD(v) (stack_pointer[-3] = (v))
1107#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1108#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1109#define BASIC_STACKADJ(n) (stack_pointer += n)
1110#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1111#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001112
Guido van Rossum96a42c81992-01-12 02:29:51 +00001113#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001115 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001116 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001117#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001118 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001119#define STACK_GROW(n) do { \
1120 assert(n >= 0); \
1121 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001122 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001123 assert(STACK_LEVEL() <= co->co_stacksize); \
1124 } while (0)
1125#define STACK_SHRINK(n) do { \
1126 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001127 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001128 (void)(BASIC_STACKADJ(-n)); \
1129 assert(STACK_LEVEL() <= co->co_stacksize); \
1130 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001131#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001132 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001133 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001134#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001135#define PUSH(v) BASIC_PUSH(v)
1136#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001137#define STACK_GROW(n) BASIC_STACKADJ(n)
1138#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001139#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001140#endif
1141
Guido van Rossum681d79a1995-07-18 14:51:37 +00001142/* Local variable macros */
1143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001145
1146/* The SETLOCAL() macro must not DECREF the local variable in-place and
1147 then store the new value; it must copy the old value to a temporary
1148 value, then store the new value, and then DECREF the temporary value.
1149 This is because it is possible that during the DECREF the frame is
1150 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1151 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001153 GETLOCAL(i) = value; \
1154 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001155
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001156
1157#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 while (STACK_LEVEL() > (b)->b_level) { \
1159 PyObject *v = POP(); \
1160 Py_XDECREF(v); \
1161 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001162
1163#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001164 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001166 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1168 while (STACK_LEVEL() > (b)->b_level + 3) { \
1169 value = POP(); \
1170 Py_XDECREF(value); \
1171 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001172 exc_info = tstate->exc_info; \
1173 type = exc_info->exc_type; \
1174 value = exc_info->exc_value; \
1175 traceback = exc_info->exc_traceback; \
1176 exc_info->exc_type = POP(); \
1177 exc_info->exc_value = POP(); \
1178 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 Py_XDECREF(type); \
1180 Py_XDECREF(value); \
1181 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001182 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001183
Inada Naoki91234a12019-06-03 21:30:58 +09001184 /* macros for opcode cache */
1185#define OPCACHE_CHECK() \
1186 do { \
1187 co_opcache = NULL; \
1188 if (co->co_opcache != NULL) { \
1189 unsigned char co_opt_offset = \
1190 co->co_opcache_map[next_instr - first_instr]; \
1191 if (co_opt_offset > 0) { \
1192 assert(co_opt_offset <= co->co_opcache_size); \
1193 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1194 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001195 } \
1196 } \
1197 } while (0)
1198
1199#if OPCACHE_STATS
1200
1201#define OPCACHE_STAT_GLOBAL_HIT() \
1202 do { \
1203 if (co->co_opcache != NULL) opcache_global_hits++; \
1204 } while (0)
1205
1206#define OPCACHE_STAT_GLOBAL_MISS() \
1207 do { \
1208 if (co->co_opcache != NULL) opcache_global_misses++; \
1209 } while (0)
1210
1211#define OPCACHE_STAT_GLOBAL_OPT() \
1212 do { \
1213 if (co->co_opcache != NULL) opcache_global_opts++; \
1214 } while (0)
1215
1216#else /* OPCACHE_STATS */
1217
1218#define OPCACHE_STAT_GLOBAL_HIT()
1219#define OPCACHE_STAT_GLOBAL_MISS()
1220#define OPCACHE_STAT_GLOBAL_OPT()
1221
1222#endif
1223
Guido van Rossuma027efa1997-05-05 20:56:21 +00001224/* Start of code */
1225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001227 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001229 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (tstate->use_tracing) {
1234 if (tstate->c_tracefunc != NULL) {
1235 /* tstate->c_tracefunc, if defined, is a
1236 function that will be called on *every* entry
1237 to a code block. Its return value, if not
1238 None, is a function that will be called at
1239 the start of each executed line of code.
1240 (Actually, the function must return itself
1241 in order to continue tracing.) The trace
1242 functions are called with three arguments:
1243 a pointer to the current frame, a string
1244 indicating why the function is called, and
1245 an argument which depends on the situation.
1246 The global trace function is also called
1247 whenever an exception is detected. */
1248 if (call_trace_protected(tstate->c_tracefunc,
1249 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001250 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 /* Trace function raised an error */
1252 goto exit_eval_frame;
1253 }
1254 }
1255 if (tstate->c_profilefunc != NULL) {
1256 /* Similar for c_profilefunc, except it needn't
1257 return itself and isn't called for "line" events */
1258 if (call_trace_protected(tstate->c_profilefunc,
1259 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001260 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* Profile function raised an error */
1262 goto exit_eval_frame;
1263 }
1264 }
1265 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001266
Łukasz Langaa785c872016-09-09 17:37:37 -07001267 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1268 dtrace_function_entry(f);
1269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 co = f->f_code;
1271 names = co->co_names;
1272 consts = co->co_consts;
1273 fastlocals = f->f_localsplus;
1274 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001275 assert(PyBytes_Check(co->co_code));
1276 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001277 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1278 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1279 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001280 /*
1281 f->f_lasti refers to the index of the last instruction,
1282 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001283
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001284 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001285 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 When the PREDICT() macros are enabled, some opcode pairs follow in
1288 direct succession without updating f->f_lasti. A successful
1289 prediction effectively links the two codes together as if they
1290 were a single new opcode; accordingly,f->f_lasti will point to
1291 the first code in the pair (for instance, GET_ITER followed by
1292 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001293 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001295 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001296 next_instr = first_instr;
1297 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001298 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1299 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 stack_pointer = f->f_stacktop;
1302 assert(stack_pointer != NULL);
1303 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001304 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001305
Inada Naoki91234a12019-06-03 21:30:58 +09001306 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1307 co->co_opcache_flag++;
1308 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1309 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001310 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001311 }
1312#if OPCACHE_STATS
1313 opcache_code_objects_extra_mem +=
1314 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1315 sizeof(_PyOpcache) * co->co_opcache_size;
1316 opcache_code_objects++;
1317#endif
1318 }
1319 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001320
Tim Peters5ca576e2001-06-18 22:08:13 +00001321#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001322 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001323#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001325 if (throwflag) /* support for generator.throw() */
1326 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001327
Victor Stinnerace47d72013-07-18 01:41:08 +02001328#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001329 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001330 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001331 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001332 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001333#endif
1334
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001335main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1338 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001339 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 /* Do periodic things. Doing this every time through
1342 the loop would add too much overhead, so we do it
1343 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001344 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 event needs attention (e.g. a signal handler or
1346 async I/O handler); see Py_AddPendingCall() and
1347 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Eric Snow7bda9de2019-03-08 17:25:54 -07001349 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001350 opcode = _Py_OPCODE(*next_instr);
1351 if (opcode == SETUP_FINALLY ||
1352 opcode == SETUP_WITH ||
1353 opcode == BEFORE_ASYNC_WITH ||
1354 opcode == YIELD_FROM) {
1355 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001356 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001357 - If we're about to enter the 'with:'. It will prevent
1358 emitting a resource warning in the common idiom
1359 'with open(path) as file:'.
1360 - If we're about to enter the 'async with:'.
1361 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001362 *very* useful, but might help in some cases and it's
1363 traditional)
1364 - If we're resuming a chain of nested 'yield from' or
1365 'await' calls, then each frame is parked with YIELD_FROM
1366 as its next opcode. If the user hit control-C we want to
1367 wait until we've reached the innermost frame before
1368 running the signal handler and raising KeyboardInterrupt
1369 (see bpo-30039).
1370 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 goto fast_next_opcode;
1372 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001373
Victor Stinnerda2914d2020-03-20 09:29:08 +01001374 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
1377 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 fast_next_opcode:
1380 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Łukasz Langaa785c872016-09-09 17:37:37 -07001382 if (PyDTrace_LINE_ENABLED())
1383 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001386
Victor Stinnerdab84232020-03-17 18:56:44 +01001387 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001388 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 /* see maybe_call_line_trace
1391 for expository comments */
1392 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 err = maybe_call_line_trace(tstate->c_tracefunc,
1395 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001396 tstate, f,
1397 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* Reload possibly changed frame fields */
1399 JUMPTO(f->f_lasti);
1400 if (f->f_stacktop != NULL) {
1401 stack_pointer = f->f_stacktop;
1402 f->f_stacktop = NULL;
1403 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001410
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001411 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001412 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001413#ifdef DYNAMIC_EXECUTION_PROFILE
1414#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 dxpairs[lastopcode][opcode]++;
1416 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001417#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001419#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001420
Guido van Rossum96a42c81992-01-12 02:29:51 +00001421#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (lltrace) {
1425 if (HAS_ARG(opcode)) {
1426 printf("%d: %d, %d\n",
1427 f->f_lasti, opcode, oparg);
1428 }
1429 else {
1430 printf("%d: %d\n",
1431 f->f_lasti, opcode);
1432 }
1433 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001434#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001439 It is essential that any operation that fails must goto error
1440 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Benjamin Petersonddd19492018-09-16 22:38:02 -07001442 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001444 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001445
Benjamin Petersonddd19492018-09-16 22:38:02 -07001446 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 PyObject *value = GETLOCAL(oparg);
1448 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001449 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 UNBOUNDLOCAL_ERROR_MSG,
1451 PyTuple_GetItem(co->co_varnames, oparg));
1452 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 Py_INCREF(value);
1455 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 }
1458
Benjamin Petersonddd19492018-09-16 22:38:02 -07001459 case TARGET(LOAD_CONST): {
1460 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 PyObject *value = GETITEM(consts, oparg);
1462 Py_INCREF(value);
1463 PUSH(value);
1464 FAST_DISPATCH();
1465 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001466
Benjamin Petersonddd19492018-09-16 22:38:02 -07001467 case TARGET(STORE_FAST): {
1468 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 PyObject *value = POP();
1470 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001473
Benjamin Petersonddd19492018-09-16 22:38:02 -07001474 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *value = POP();
1476 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Benjamin Petersonddd19492018-09-16 22:38:02 -07001480 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 PyObject *top = TOP();
1482 PyObject *second = SECOND();
1483 SET_TOP(second);
1484 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 PyObject *top = TOP();
1490 PyObject *second = SECOND();
1491 PyObject *third = THIRD();
1492 SET_TOP(second);
1493 SET_SECOND(third);
1494 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001497
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001499 PyObject *top = TOP();
1500 PyObject *second = SECOND();
1501 PyObject *third = THIRD();
1502 PyObject *fourth = FOURTH();
1503 SET_TOP(second);
1504 SET_SECOND(third);
1505 SET_THIRD(fourth);
1506 SET_FOURTH(top);
1507 FAST_DISPATCH();
1508 }
1509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 PyObject *top = TOP();
1512 Py_INCREF(top);
1513 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Benjamin Petersonddd19492018-09-16 22:38:02 -07001517 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001518 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001520 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001522 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 SET_TOP(top);
1524 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001525 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *value = TOP();
1530 PyObject *res = PyNumber_Positive(value);
1531 Py_DECREF(value);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *value = TOP();
1540 PyObject *res = PyNumber_Negative(value);
1541 Py_DECREF(value);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *value = TOP();
1550 int err = PyObject_IsTrue(value);
1551 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (err == 0) {
1553 Py_INCREF(Py_True);
1554 SET_TOP(Py_True);
1555 DISPATCH();
1556 }
1557 else if (err > 0) {
1558 Py_INCREF(Py_False);
1559 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 DISPATCH();
1561 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001562 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 goto error;
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *value = TOP();
1568 PyObject *res = PyNumber_Invert(value);
1569 Py_DECREF(value);
1570 SET_TOP(res);
1571 if (res == NULL)
1572 goto error;
1573 DISPATCH();
1574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Benjamin Petersonddd19492018-09-16 22:38:02 -07001576 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 PyObject *exp = POP();
1578 PyObject *base = TOP();
1579 PyObject *res = PyNumber_Power(base, exp, Py_None);
1580 Py_DECREF(base);
1581 Py_DECREF(exp);
1582 SET_TOP(res);
1583 if (res == NULL)
1584 goto error;
1585 DISPATCH();
1586 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Benjamin Petersonddd19492018-09-16 22:38:02 -07001588 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 PyObject *right = POP();
1590 PyObject *left = TOP();
1591 PyObject *res = PyNumber_Multiply(left, right);
1592 Py_DECREF(left);
1593 Py_DECREF(right);
1594 SET_TOP(res);
1595 if (res == NULL)
1596 goto error;
1597 DISPATCH();
1598 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001599
Benjamin Petersonddd19492018-09-16 22:38:02 -07001600 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001601 PyObject *right = POP();
1602 PyObject *left = TOP();
1603 PyObject *res = PyNumber_MatrixMultiply(left, right);
1604 Py_DECREF(left);
1605 Py_DECREF(right);
1606 SET_TOP(res);
1607 if (res == NULL)
1608 goto error;
1609 DISPATCH();
1610 }
1611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 PyObject *divisor = POP();
1614 PyObject *dividend = TOP();
1615 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1616 Py_DECREF(dividend);
1617 Py_DECREF(divisor);
1618 SET_TOP(quotient);
1619 if (quotient == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 PyObject *divisor = POP();
1626 PyObject *dividend = TOP();
1627 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1628 Py_DECREF(dividend);
1629 Py_DECREF(divisor);
1630 SET_TOP(quotient);
1631 if (quotient == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001635
Benjamin Petersonddd19492018-09-16 22:38:02 -07001636 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 PyObject *divisor = POP();
1638 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001639 PyObject *res;
1640 if (PyUnicode_CheckExact(dividend) && (
1641 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1642 // fast path; string formatting, but not if the RHS is a str subclass
1643 // (see issue28598)
1644 res = PyUnicode_Format(dividend, divisor);
1645 } else {
1646 res = PyNumber_Remainder(dividend, divisor);
1647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 Py_DECREF(divisor);
1649 Py_DECREF(dividend);
1650 SET_TOP(res);
1651 if (res == NULL)
1652 goto error;
1653 DISPATCH();
1654 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Benjamin Petersonddd19492018-09-16 22:38:02 -07001656 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *right = POP();
1658 PyObject *left = TOP();
1659 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001660 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1661 CPython using bytecode, it is simply worthless.
1662 See http://bugs.python.org/issue21955 and
1663 http://bugs.python.org/issue10044 for the discussion. In short,
1664 no patch shown any impact on a realistic benchmark, only a minor
1665 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 if (PyUnicode_CheckExact(left) &&
1667 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001668 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001669 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001670 }
1671 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 sum = PyNumber_Add(left, right);
1673 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001674 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 Py_DECREF(right);
1676 SET_TOP(sum);
1677 if (sum == NULL)
1678 goto error;
1679 DISPATCH();
1680 }
1681
Benjamin Petersonddd19492018-09-16 22:38:02 -07001682 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 PyObject *right = POP();
1684 PyObject *left = TOP();
1685 PyObject *diff = PyNumber_Subtract(left, right);
1686 Py_DECREF(right);
1687 Py_DECREF(left);
1688 SET_TOP(diff);
1689 if (diff == NULL)
1690 goto error;
1691 DISPATCH();
1692 }
1693
Benjamin Petersonddd19492018-09-16 22:38:02 -07001694 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001695 PyObject *sub = POP();
1696 PyObject *container = TOP();
1697 PyObject *res = PyObject_GetItem(container, sub);
1698 Py_DECREF(container);
1699 Py_DECREF(sub);
1700 SET_TOP(res);
1701 if (res == NULL)
1702 goto error;
1703 DISPATCH();
1704 }
1705
Benjamin Petersonddd19492018-09-16 22:38:02 -07001706 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 PyObject *right = POP();
1708 PyObject *left = TOP();
1709 PyObject *res = PyNumber_Lshift(left, right);
1710 Py_DECREF(left);
1711 Py_DECREF(right);
1712 SET_TOP(res);
1713 if (res == NULL)
1714 goto error;
1715 DISPATCH();
1716 }
1717
Benjamin Petersonddd19492018-09-16 22:38:02 -07001718 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 PyObject *right = POP();
1720 PyObject *left = TOP();
1721 PyObject *res = PyNumber_Rshift(left, right);
1722 Py_DECREF(left);
1723 Py_DECREF(right);
1724 SET_TOP(res);
1725 if (res == NULL)
1726 goto error;
1727 DISPATCH();
1728 }
1729
Benjamin Petersonddd19492018-09-16 22:38:02 -07001730 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 PyObject *right = POP();
1732 PyObject *left = TOP();
1733 PyObject *res = PyNumber_And(left, right);
1734 Py_DECREF(left);
1735 Py_DECREF(right);
1736 SET_TOP(res);
1737 if (res == NULL)
1738 goto error;
1739 DISPATCH();
1740 }
1741
Benjamin Petersonddd19492018-09-16 22:38:02 -07001742 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 PyObject *right = POP();
1744 PyObject *left = TOP();
1745 PyObject *res = PyNumber_Xor(left, right);
1746 Py_DECREF(left);
1747 Py_DECREF(right);
1748 SET_TOP(res);
1749 if (res == NULL)
1750 goto error;
1751 DISPATCH();
1752 }
1753
Benjamin Petersonddd19492018-09-16 22:38:02 -07001754 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001755 PyObject *right = POP();
1756 PyObject *left = TOP();
1757 PyObject *res = PyNumber_Or(left, right);
1758 Py_DECREF(left);
1759 Py_DECREF(right);
1760 SET_TOP(res);
1761 if (res == NULL)
1762 goto error;
1763 DISPATCH();
1764 }
1765
Benjamin Petersonddd19492018-09-16 22:38:02 -07001766 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 PyObject *v = POP();
1768 PyObject *list = PEEK(oparg);
1769 int err;
1770 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 if (err != 0)
1773 goto error;
1774 PREDICT(JUMP_ABSOLUTE);
1775 DISPATCH();
1776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Benjamin Petersonddd19492018-09-16 22:38:02 -07001778 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001780 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 int err;
1782 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 if (err != 0)
1785 goto error;
1786 PREDICT(JUMP_ABSOLUTE);
1787 DISPATCH();
1788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Benjamin Petersonddd19492018-09-16 22:38:02 -07001790 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 PyObject *exp = POP();
1792 PyObject *base = TOP();
1793 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1794 Py_DECREF(base);
1795 Py_DECREF(exp);
1796 SET_TOP(res);
1797 if (res == NULL)
1798 goto error;
1799 DISPATCH();
1800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Benjamin Petersonddd19492018-09-16 22:38:02 -07001802 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 PyObject *right = POP();
1804 PyObject *left = TOP();
1805 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1806 Py_DECREF(left);
1807 Py_DECREF(right);
1808 SET_TOP(res);
1809 if (res == NULL)
1810 goto error;
1811 DISPATCH();
1812 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Benjamin Petersonddd19492018-09-16 22:38:02 -07001814 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001815 PyObject *right = POP();
1816 PyObject *left = TOP();
1817 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1818 Py_DECREF(left);
1819 Py_DECREF(right);
1820 SET_TOP(res);
1821 if (res == NULL)
1822 goto error;
1823 DISPATCH();
1824 }
1825
Benjamin Petersonddd19492018-09-16 22:38:02 -07001826 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 PyObject *divisor = POP();
1828 PyObject *dividend = TOP();
1829 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1830 Py_DECREF(dividend);
1831 Py_DECREF(divisor);
1832 SET_TOP(quotient);
1833 if (quotient == NULL)
1834 goto error;
1835 DISPATCH();
1836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001837
Benjamin Petersonddd19492018-09-16 22:38:02 -07001838 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 PyObject *divisor = POP();
1840 PyObject *dividend = TOP();
1841 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1842 Py_DECREF(dividend);
1843 Py_DECREF(divisor);
1844 SET_TOP(quotient);
1845 if (quotient == NULL)
1846 goto error;
1847 DISPATCH();
1848 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001849
Benjamin Petersonddd19492018-09-16 22:38:02 -07001850 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 PyObject *right = POP();
1852 PyObject *left = TOP();
1853 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1854 Py_DECREF(left);
1855 Py_DECREF(right);
1856 SET_TOP(mod);
1857 if (mod == NULL)
1858 goto error;
1859 DISPATCH();
1860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001861
Benjamin Petersonddd19492018-09-16 22:38:02 -07001862 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 PyObject *right = POP();
1864 PyObject *left = TOP();
1865 PyObject *sum;
1866 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001867 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001868 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001869 }
1870 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 sum = PyNumber_InPlaceAdd(left, right);
1872 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 Py_DECREF(right);
1875 SET_TOP(sum);
1876 if (sum == NULL)
1877 goto error;
1878 DISPATCH();
1879 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Benjamin Petersonddd19492018-09-16 22:38:02 -07001881 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 PyObject *right = POP();
1883 PyObject *left = TOP();
1884 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1885 Py_DECREF(left);
1886 Py_DECREF(right);
1887 SET_TOP(diff);
1888 if (diff == NULL)
1889 goto error;
1890 DISPATCH();
1891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyObject *right = POP();
1895 PyObject *left = TOP();
1896 PyObject *res = PyNumber_InPlaceLshift(left, right);
1897 Py_DECREF(left);
1898 Py_DECREF(right);
1899 SET_TOP(res);
1900 if (res == NULL)
1901 goto error;
1902 DISPATCH();
1903 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Benjamin Petersonddd19492018-09-16 22:38:02 -07001905 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PyObject *right = POP();
1907 PyObject *left = TOP();
1908 PyObject *res = PyNumber_InPlaceRshift(left, right);
1909 Py_DECREF(left);
1910 Py_DECREF(right);
1911 SET_TOP(res);
1912 if (res == NULL)
1913 goto error;
1914 DISPATCH();
1915 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 PyObject *right = POP();
1919 PyObject *left = TOP();
1920 PyObject *res = PyNumber_InPlaceAnd(left, right);
1921 Py_DECREF(left);
1922 Py_DECREF(right);
1923 SET_TOP(res);
1924 if (res == NULL)
1925 goto error;
1926 DISPATCH();
1927 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Benjamin Petersonddd19492018-09-16 22:38:02 -07001929 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 PyObject *right = POP();
1931 PyObject *left = TOP();
1932 PyObject *res = PyNumber_InPlaceXor(left, right);
1933 Py_DECREF(left);
1934 Py_DECREF(right);
1935 SET_TOP(res);
1936 if (res == NULL)
1937 goto error;
1938 DISPATCH();
1939 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Benjamin Petersonddd19492018-09-16 22:38:02 -07001941 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 PyObject *right = POP();
1943 PyObject *left = TOP();
1944 PyObject *res = PyNumber_InPlaceOr(left, right);
1945 Py_DECREF(left);
1946 Py_DECREF(right);
1947 SET_TOP(res);
1948 if (res == NULL)
1949 goto error;
1950 DISPATCH();
1951 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001952
Benjamin Petersonddd19492018-09-16 22:38:02 -07001953 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001954 PyObject *sub = TOP();
1955 PyObject *container = SECOND();
1956 PyObject *v = THIRD();
1957 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001958 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001959 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 Py_DECREF(container);
1963 Py_DECREF(sub);
1964 if (err != 0)
1965 goto error;
1966 DISPATCH();
1967 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Benjamin Petersonddd19492018-09-16 22:38:02 -07001969 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 PyObject *sub = TOP();
1971 PyObject *container = SECOND();
1972 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001973 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001974 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 err = PyObject_DelItem(container, sub);
1976 Py_DECREF(container);
1977 Py_DECREF(sub);
1978 if (err != 0)
1979 goto error;
1980 DISPATCH();
1981 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001982
Benjamin Petersonddd19492018-09-16 22:38:02 -07001983 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001984 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001986 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001987 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001989 _PyErr_SetString(tstate, PyExc_RuntimeError,
1990 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 Py_DECREF(value);
1992 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001994 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 Py_DECREF(value);
1996 if (res == NULL)
1997 goto error;
1998 Py_DECREF(res);
1999 DISPATCH();
2000 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002001
Benjamin Petersonddd19492018-09-16 22:38:02 -07002002 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 switch (oparg) {
2005 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002007 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002010 /* fall through */
2011 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002012 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002013 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 break;
2016 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002017 _PyErr_SetString(tstate, PyExc_SystemError,
2018 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 break;
2020 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002021 goto error;
2022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002026 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002027 assert(EMPTY());
2028 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002030
Benjamin Petersonddd19492018-09-16 22:38:02 -07002031 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002032 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002033 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002034 PyObject *obj = TOP();
2035 PyTypeObject *type = Py_TYPE(obj);
2036
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002037 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002038 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002039 }
Yury Selivanov75445082015-05-11 22:57:16 -04002040
2041 if (getter != NULL) {
2042 iter = (*getter)(obj);
2043 Py_DECREF(obj);
2044 if (iter == NULL) {
2045 SET_TOP(NULL);
2046 goto error;
2047 }
2048 }
2049 else {
2050 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002051 _PyErr_Format(tstate, PyExc_TypeError,
2052 "'async for' requires an object with "
2053 "__aiter__ method, got %.100s",
2054 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002055 Py_DECREF(obj);
2056 goto error;
2057 }
2058
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002059 if (Py_TYPE(iter)->tp_as_async == NULL ||
2060 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002061
Yury Selivanov398ff912017-03-02 22:20:00 -05002062 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002063 _PyErr_Format(tstate, PyExc_TypeError,
2064 "'async for' received an object from __aiter__ "
2065 "that does not implement __anext__: %.100s",
2066 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002067 Py_DECREF(iter);
2068 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002069 }
2070
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002071 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002072 DISPATCH();
2073 }
2074
Benjamin Petersonddd19492018-09-16 22:38:02 -07002075 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002076 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002077 PyObject *next_iter = NULL;
2078 PyObject *awaitable = NULL;
2079 PyObject *aiter = TOP();
2080 PyTypeObject *type = Py_TYPE(aiter);
2081
Yury Selivanoveb636452016-09-08 22:01:51 -07002082 if (PyAsyncGen_CheckExact(aiter)) {
2083 awaitable = type->tp_as_async->am_anext(aiter);
2084 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002085 goto error;
2086 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002087 } else {
2088 if (type->tp_as_async != NULL){
2089 getter = type->tp_as_async->am_anext;
2090 }
Yury Selivanov75445082015-05-11 22:57:16 -04002091
Yury Selivanoveb636452016-09-08 22:01:51 -07002092 if (getter != NULL) {
2093 next_iter = (*getter)(aiter);
2094 if (next_iter == NULL) {
2095 goto error;
2096 }
2097 }
2098 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002099 _PyErr_Format(tstate, PyExc_TypeError,
2100 "'async for' requires an iterator with "
2101 "__anext__ method, got %.100s",
2102 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002103 goto error;
2104 }
Yury Selivanov75445082015-05-11 22:57:16 -04002105
Yury Selivanoveb636452016-09-08 22:01:51 -07002106 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2107 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002108 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002109 PyExc_TypeError,
2110 "'async for' received an invalid object "
2111 "from __anext__: %.100s",
2112 Py_TYPE(next_iter)->tp_name);
2113
2114 Py_DECREF(next_iter);
2115 goto error;
2116 } else {
2117 Py_DECREF(next_iter);
2118 }
2119 }
Yury Selivanov75445082015-05-11 22:57:16 -04002120
2121 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002122 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002123 DISPATCH();
2124 }
2125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(GET_AWAITABLE): {
2127 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002128 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002129 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002130
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002131 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002132 int opcode_at_minus_3 = 0;
2133 if ((next_instr - first_instr) > 2) {
2134 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2135 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002136 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002137 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002138 _Py_OPCODE(next_instr[-2]));
2139 }
2140
Yury Selivanov75445082015-05-11 22:57:16 -04002141 Py_DECREF(iterable);
2142
Yury Selivanovc724bae2016-03-02 11:30:46 -05002143 if (iter != NULL && PyCoro_CheckExact(iter)) {
2144 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2145 if (yf != NULL) {
2146 /* `iter` is a coroutine object that is being
2147 awaited, `yf` is a pointer to the current awaitable
2148 being awaited on. */
2149 Py_DECREF(yf);
2150 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002151 _PyErr_SetString(tstate, PyExc_RuntimeError,
2152 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002153 /* The code below jumps to `error` if `iter` is NULL. */
2154 }
2155 }
2156
Yury Selivanov75445082015-05-11 22:57:16 -04002157 SET_TOP(iter); /* Even if it's NULL */
2158
2159 if (iter == NULL) {
2160 goto error;
2161 }
2162
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002163 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002164 DISPATCH();
2165 }
2166
Benjamin Petersonddd19492018-09-16 22:38:02 -07002167 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002169 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002171 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2172 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002173 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002174 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002176 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002177 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002178 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 Py_DECREF(v);
2181 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002182 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002183 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002184 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002185 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002186 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 if (err < 0)
2188 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002189 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 SET_TOP(val);
2191 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002192 }
Martin Panter95f53c12016-07-18 08:23:26 +00002193 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002194 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002195 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002196 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002197 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002198 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002200
Benjamin Petersonddd19492018-09-16 22:38:02 -07002201 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002203
2204 if (co->co_flags & CO_ASYNC_GENERATOR) {
2205 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2206 Py_DECREF(retval);
2207 if (w == NULL) {
2208 retval = NULL;
2209 goto error;
2210 }
2211 retval = w;
2212 }
2213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002215 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002219 PyObject *type, *value, *traceback;
2220 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyTryBlock *b = PyFrame_BlockPop(f);
2222 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002223 _PyErr_SetString(tstate, PyExc_SystemError,
2224 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002227 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2228 STACK_LEVEL() <= (b)->b_level + 4);
2229 exc_info = tstate->exc_info;
2230 type = exc_info->exc_type;
2231 value = exc_info->exc_value;
2232 traceback = exc_info->exc_traceback;
2233 exc_info->exc_type = POP();
2234 exc_info->exc_value = POP();
2235 exc_info->exc_traceback = POP();
2236 Py_XDECREF(type);
2237 Py_XDECREF(value);
2238 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(POP_BLOCK): {
2243 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002244 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Mark Shannonfee55262019-11-21 09:11:43 +00002248 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002249 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002250 PyObject *val = POP();
2251 PyObject *tb = POP();
2252 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002253 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002254 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Benjamin Petersonddd19492018-09-16 22:38:02 -07002257 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002258 PyObject *exc = POP();
2259 assert(PyExceptionClass_Check(exc));
2260 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2261 PyTryBlock *b = PyFrame_BlockPop(f);
2262 assert(b->b_type == EXCEPT_HANDLER);
2263 Py_DECREF(exc);
2264 UNWIND_EXCEPT_HANDLER(b);
2265 Py_DECREF(POP());
2266 JUMPBY(oparg);
2267 FAST_DISPATCH();
2268 }
2269 else {
2270 PyObject *val = POP();
2271 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002272 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002273 goto exception_unwind;
2274 }
2275 }
2276
Zackery Spytzce6a0702019-08-25 03:44:09 -06002277 case TARGET(LOAD_ASSERTION_ERROR): {
2278 PyObject *value = PyExc_AssertionError;
2279 Py_INCREF(value);
2280 PUSH(value);
2281 FAST_DISPATCH();
2282 }
2283
Benjamin Petersonddd19492018-09-16 22:38:02 -07002284 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002285 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002288 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002289 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002291 if (!_PyErr_Occurred(tstate)) {
2292 _PyErr_SetString(tstate, PyExc_NameError,
2293 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002296 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002298 }
2299 else {
2300 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2301 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002302 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2304 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002305 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2306 _PyErr_SetString(tstate, PyExc_NameError,
2307 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002312 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Benjamin Petersonddd19492018-09-16 22:38:02 -07002315 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 PyObject *name = GETITEM(names, oparg);
2317 PyObject *v = POP();
2318 PyObject *ns = f->f_locals;
2319 int err;
2320 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002321 _PyErr_Format(tstate, PyExc_SystemError,
2322 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 if (PyDict_CheckExact(ns))
2327 err = PyDict_SetItem(ns, name, v);
2328 else
2329 err = PyObject_SetItem(ns, name, v);
2330 Py_DECREF(v);
2331 if (err != 0)
2332 goto error;
2333 DISPATCH();
2334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Benjamin Petersonddd19492018-09-16 22:38:02 -07002336 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 PyObject *name = GETITEM(names, oparg);
2338 PyObject *ns = f->f_locals;
2339 int err;
2340 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002341 _PyErr_Format(tstate, PyExc_SystemError,
2342 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 err = PyObject_DelItem(ns, name);
2346 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002347 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 NAME_ERROR_MSG,
2349 name);
2350 goto error;
2351 }
2352 DISPATCH();
2353 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002354
Benjamin Petersonddd19492018-09-16 22:38:02 -07002355 case TARGET(UNPACK_SEQUENCE): {
2356 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 PyObject *seq = POP(), *item, **items;
2358 if (PyTuple_CheckExact(seq) &&
2359 PyTuple_GET_SIZE(seq) == oparg) {
2360 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 item = items[oparg];
2363 Py_INCREF(item);
2364 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 } else if (PyList_CheckExact(seq) &&
2367 PyList_GET_SIZE(seq) == oparg) {
2368 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 item = items[oparg];
2371 Py_INCREF(item);
2372 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002374 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002376 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 } else {
2378 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 Py_DECREF(seq);
2380 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002383 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2388 PyObject *seq = POP();
2389
Victor Stinner438a12d2019-05-24 17:01:38 +02002390 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 stack_pointer + totalargs)) {
2392 stack_pointer += totalargs;
2393 } else {
2394 Py_DECREF(seq);
2395 goto error;
2396 }
2397 Py_DECREF(seq);
2398 DISPATCH();
2399 }
2400
Benjamin Petersonddd19492018-09-16 22:38:02 -07002401 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 PyObject *name = GETITEM(names, oparg);
2403 PyObject *owner = TOP();
2404 PyObject *v = SECOND();
2405 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002406 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 Py_DECREF(owner);
2410 if (err != 0)
2411 goto error;
2412 DISPATCH();
2413 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002414
Benjamin Petersonddd19492018-09-16 22:38:02 -07002415 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 PyObject *name = GETITEM(names, oparg);
2417 PyObject *owner = POP();
2418 int err;
2419 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2420 Py_DECREF(owner);
2421 if (err != 0)
2422 goto error;
2423 DISPATCH();
2424 }
2425
Benjamin Petersonddd19492018-09-16 22:38:02 -07002426 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *name = GETITEM(names, oparg);
2428 PyObject *v = POP();
2429 int err;
2430 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 if (err != 0)
2433 goto error;
2434 DISPATCH();
2435 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 PyObject *name = GETITEM(names, oparg);
2439 int err;
2440 err = PyDict_DelItem(f->f_globals, name);
2441 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002442 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2443 format_exc_check_arg(tstate, PyExc_NameError,
2444 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002445 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002447 }
2448 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Benjamin Petersonddd19492018-09-16 22:38:02 -07002451 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 PyObject *name = GETITEM(names, oparg);
2453 PyObject *locals = f->f_locals;
2454 PyObject *v;
2455 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002456 _PyErr_Format(tstate, PyExc_SystemError,
2457 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002461 v = PyDict_GetItemWithError(locals, name);
2462 if (v != NULL) {
2463 Py_INCREF(v);
2464 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002466 goto error;
2467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 }
2469 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002471 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002473 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 }
2476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002478 v = PyDict_GetItemWithError(f->f_globals, name);
2479 if (v != NULL) {
2480 Py_INCREF(v);
2481 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002482 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002483 goto error;
2484 }
2485 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002486 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002487 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002489 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002490 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002491 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002493 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002497 }
2498 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 v = PyObject_GetItem(f->f_builtins, name);
2500 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002501 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002502 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002503 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002507 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Benjamin Petersonddd19492018-09-16 22:38:02 -07002515 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002516 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002518 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002519 && PyDict_CheckExact(f->f_builtins))
2520 {
Inada Naoki91234a12019-06-03 21:30:58 +09002521 OPCACHE_CHECK();
2522 if (co_opcache != NULL && co_opcache->optimized > 0) {
2523 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2524
2525 if (lg->globals_ver ==
2526 ((PyDictObject *)f->f_globals)->ma_version_tag
2527 && lg->builtins_ver ==
2528 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2529 {
2530 PyObject *ptr = lg->ptr;
2531 OPCACHE_STAT_GLOBAL_HIT();
2532 assert(ptr != NULL);
2533 Py_INCREF(ptr);
2534 PUSH(ptr);
2535 DISPATCH();
2536 }
2537 }
2538
2539 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002541 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 name);
2543 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002544 if (!_PyErr_OCCURRED()) {
2545 /* _PyDict_LoadGlobal() returns NULL without raising
2546 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002547 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002548 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002549 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 }
Inada Naoki91234a12019-06-03 21:30:58 +09002552
2553 if (co_opcache != NULL) {
2554 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2555
2556 if (co_opcache->optimized == 0) {
2557 /* Wasn't optimized before. */
2558 OPCACHE_STAT_GLOBAL_OPT();
2559 } else {
2560 OPCACHE_STAT_GLOBAL_MISS();
2561 }
2562
2563 co_opcache->optimized = 1;
2564 lg->globals_ver =
2565 ((PyDictObject *)f->f_globals)->ma_version_tag;
2566 lg->builtins_ver =
2567 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2568 lg->ptr = v; /* borrowed */
2569 }
2570
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 else {
2574 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002575
2576 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002577 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 v = PyObject_GetItem(f->f_globals, name);
2579 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002581 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002582 }
2583 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002584
Victor Stinnerb4efc962015-11-20 09:24:02 +01002585 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 v = PyObject_GetItem(f->f_builtins, name);
2587 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002588 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002589 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002590 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002591 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002592 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002594 }
2595 }
2596 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002600
Benjamin Petersonddd19492018-09-16 22:38:02 -07002601 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 PyObject *v = GETLOCAL(oparg);
2603 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 SETLOCAL(oparg, NULL);
2605 DISPATCH();
2606 }
2607 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002608 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 UNBOUNDLOCAL_ERROR_MSG,
2610 PyTuple_GetItem(co->co_varnames, oparg)
2611 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 goto error;
2613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002614
Benjamin Petersonddd19492018-09-16 22:38:02 -07002615 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002617 PyObject *oldobj = PyCell_GET(cell);
2618 if (oldobj != NULL) {
2619 PyCell_SET(cell, NULL);
2620 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002621 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002622 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002623 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 goto error;
2625 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002626
Benjamin Petersonddd19492018-09-16 22:38:02 -07002627 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 PyObject *cell = freevars[oparg];
2629 Py_INCREF(cell);
2630 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002633
Benjamin Petersonddd19492018-09-16 22:38:02 -07002634 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002635 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002636 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002637 assert(locals);
2638 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2639 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2640 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2641 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2642 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002643 value = PyDict_GetItemWithError(locals, name);
2644 if (value != NULL) {
2645 Py_INCREF(value);
2646 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002648 goto error;
2649 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002650 }
2651 else {
2652 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002653 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002654 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002655 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002656 }
2657 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002658 }
2659 }
2660 if (!value) {
2661 PyObject *cell = freevars[oparg];
2662 value = PyCell_GET(cell);
2663 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002664 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002665 goto error;
2666 }
2667 Py_INCREF(value);
2668 }
2669 PUSH(value);
2670 DISPATCH();
2671 }
2672
Benjamin Petersonddd19492018-09-16 22:38:02 -07002673 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 PyObject *cell = freevars[oparg];
2675 PyObject *value = PyCell_GET(cell);
2676 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002677 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 Py_INCREF(value);
2681 PUSH(value);
2682 DISPATCH();
2683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002684
Benjamin Petersonddd19492018-09-16 22:38:02 -07002685 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 PyObject *v = POP();
2687 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002688 PyObject *oldobj = PyCell_GET(cell);
2689 PyCell_SET(cell, v);
2690 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 DISPATCH();
2692 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002693
Benjamin Petersonddd19492018-09-16 22:38:02 -07002694 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002695 PyObject *str;
2696 PyObject *empty = PyUnicode_New(0, 0);
2697 if (empty == NULL) {
2698 goto error;
2699 }
2700 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2701 Py_DECREF(empty);
2702 if (str == NULL)
2703 goto error;
2704 while (--oparg >= 0) {
2705 PyObject *item = POP();
2706 Py_DECREF(item);
2707 }
2708 PUSH(str);
2709 DISPATCH();
2710 }
2711
Benjamin Petersonddd19492018-09-16 22:38:02 -07002712 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 PyObject *tup = PyTuple_New(oparg);
2714 if (tup == NULL)
2715 goto error;
2716 while (--oparg >= 0) {
2717 PyObject *item = POP();
2718 PyTuple_SET_ITEM(tup, oparg, item);
2719 }
2720 PUSH(tup);
2721 DISPATCH();
2722 }
2723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 PyObject *list = PyList_New(oparg);
2726 if (list == NULL)
2727 goto error;
2728 while (--oparg >= 0) {
2729 PyObject *item = POP();
2730 PyList_SET_ITEM(list, oparg, item);
2731 }
2732 PUSH(list);
2733 DISPATCH();
2734 }
2735
Mark Shannon13bc1392020-01-23 09:25:17 +00002736 case TARGET(LIST_TO_TUPLE): {
2737 PyObject *list = POP();
2738 PyObject *tuple = PyList_AsTuple(list);
2739 Py_DECREF(list);
2740 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002742 }
2743 PUSH(tuple);
2744 DISPATCH();
2745 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002746
Mark Shannon13bc1392020-01-23 09:25:17 +00002747 case TARGET(LIST_EXTEND): {
2748 PyObject *iterable = POP();
2749 PyObject *list = PEEK(oparg);
2750 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2751 if (none_val == NULL) {
2752 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002753 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002754 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002755 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002756 _PyErr_Format(tstate, PyExc_TypeError,
2757 "Value after * must be an iterable, not %.200s",
2758 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002759 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002760 Py_DECREF(iterable);
2761 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002762 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002763 Py_DECREF(none_val);
2764 Py_DECREF(iterable);
2765 DISPATCH();
2766 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767
Mark Shannon13bc1392020-01-23 09:25:17 +00002768 case TARGET(SET_UPDATE): {
2769 PyObject *iterable = POP();
2770 PyObject *set = PEEK(oparg);
2771 int err = _PySet_Update(set, iterable);
2772 Py_DECREF(iterable);
2773 if (err < 0) {
2774 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002775 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 DISPATCH();
2777 }
2778
Benjamin Petersonddd19492018-09-16 22:38:02 -07002779 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *set = PySet_New(NULL);
2781 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002782 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 if (set == NULL)
2784 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002785 for (i = oparg; i > 0; i--) {
2786 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 if (err == 0)
2788 err = PySet_Add(set, item);
2789 Py_DECREF(item);
2790 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002791 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 if (err != 0) {
2793 Py_DECREF(set);
2794 goto error;
2795 }
2796 PUSH(set);
2797 DISPATCH();
2798 }
2799
Benjamin Petersonddd19492018-09-16 22:38:02 -07002800 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002801 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2803 if (map == NULL)
2804 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002805 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002807 PyObject *key = PEEK(2*i);
2808 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 if (err != 0) {
2811 Py_DECREF(map);
2812 goto error;
2813 }
2814 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002815
2816 while (oparg--) {
2817 Py_DECREF(POP());
2818 Py_DECREF(POP());
2819 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 PUSH(map);
2821 DISPATCH();
2822 }
2823
Benjamin Petersonddd19492018-09-16 22:38:02 -07002824 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002825 _Py_IDENTIFIER(__annotations__);
2826 int err;
2827 PyObject *ann_dict;
2828 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002829 _PyErr_Format(tstate, PyExc_SystemError,
2830 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002831 goto error;
2832 }
2833 /* check if __annotations__ in locals()... */
2834 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002835 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002836 &PyId___annotations__);
2837 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002838 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002839 goto error;
2840 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002841 /* ...if not, create a new one */
2842 ann_dict = PyDict_New();
2843 if (ann_dict == NULL) {
2844 goto error;
2845 }
2846 err = _PyDict_SetItemId(f->f_locals,
2847 &PyId___annotations__, ann_dict);
2848 Py_DECREF(ann_dict);
2849 if (err != 0) {
2850 goto error;
2851 }
2852 }
2853 }
2854 else {
2855 /* do the same if locals() is not a dict */
2856 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2857 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002858 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002859 }
2860 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2861 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002862 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002863 goto error;
2864 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002865 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002866 ann_dict = PyDict_New();
2867 if (ann_dict == NULL) {
2868 goto error;
2869 }
2870 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2871 Py_DECREF(ann_dict);
2872 if (err != 0) {
2873 goto error;
2874 }
2875 }
2876 else {
2877 Py_DECREF(ann_dict);
2878 }
2879 }
2880 DISPATCH();
2881 }
2882
Benjamin Petersonddd19492018-09-16 22:38:02 -07002883 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002884 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002885 PyObject *map;
2886 PyObject *keys = TOP();
2887 if (!PyTuple_CheckExact(keys) ||
2888 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002889 _PyErr_SetString(tstate, PyExc_SystemError,
2890 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002891 goto error;
2892 }
2893 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2894 if (map == NULL) {
2895 goto error;
2896 }
2897 for (i = oparg; i > 0; i--) {
2898 int err;
2899 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2900 PyObject *value = PEEK(i + 1);
2901 err = PyDict_SetItem(map, key, value);
2902 if (err != 0) {
2903 Py_DECREF(map);
2904 goto error;
2905 }
2906 }
2907
2908 Py_DECREF(POP());
2909 while (oparg--) {
2910 Py_DECREF(POP());
2911 }
2912 PUSH(map);
2913 DISPATCH();
2914 }
2915
Mark Shannon8a4cd702020-01-27 09:57:45 +00002916 case TARGET(DICT_UPDATE): {
2917 PyObject *update = POP();
2918 PyObject *dict = PEEK(oparg);
2919 if (PyDict_Update(dict, update) < 0) {
2920 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2921 _PyErr_Format(tstate, PyExc_TypeError,
2922 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002923 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002924 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002925 Py_DECREF(update);
2926 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002927 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002928 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002929 DISPATCH();
2930 }
2931
Mark Shannon8a4cd702020-01-27 09:57:45 +00002932 case TARGET(DICT_MERGE): {
2933 PyObject *update = POP();
2934 PyObject *dict = PEEK(oparg);
2935
2936 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2937 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2938 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002939 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002940 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002941 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002942 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002943 DISPATCH();
2944 }
2945
Benjamin Petersonddd19492018-09-16 22:38:02 -07002946 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002947 PyObject *value = TOP();
2948 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 PyObject *map;
2950 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002951 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002952 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002954 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 Py_DECREF(value);
2956 Py_DECREF(key);
2957 if (err != 0)
2958 goto error;
2959 PREDICT(JUMP_ABSOLUTE);
2960 DISPATCH();
2961 }
2962
Benjamin Petersonddd19492018-09-16 22:38:02 -07002963 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 PyObject *name = GETITEM(names, oparg);
2965 PyObject *owner = TOP();
2966 PyObject *res = PyObject_GetAttr(owner, name);
2967 Py_DECREF(owner);
2968 SET_TOP(res);
2969 if (res == NULL)
2970 goto error;
2971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002975 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 PyObject *right = POP();
2977 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002978 PyObject *res = PyObject_RichCompare(left, right, oparg);
2979 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 Py_DECREF(left);
2981 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 if (res == NULL)
2983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 PREDICT(POP_JUMP_IF_FALSE);
2985 PREDICT(POP_JUMP_IF_TRUE);
2986 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002987 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Mark Shannon9af0e472020-01-14 10:12:45 +00002989 case TARGET(IS_OP): {
2990 PyObject *right = POP();
2991 PyObject *left = TOP();
2992 int res = (left == right)^oparg;
2993 PyObject *b = res ? Py_True : Py_False;
2994 Py_INCREF(b);
2995 SET_TOP(b);
2996 Py_DECREF(left);
2997 Py_DECREF(right);
2998 PREDICT(POP_JUMP_IF_FALSE);
2999 PREDICT(POP_JUMP_IF_TRUE);
3000 FAST_DISPATCH();
3001 }
3002
3003 case TARGET(CONTAINS_OP): {
3004 PyObject *right = POP();
3005 PyObject *left = POP();
3006 int res = PySequence_Contains(right, left);
3007 Py_DECREF(left);
3008 Py_DECREF(right);
3009 if (res < 0) {
3010 goto error;
3011 }
3012 PyObject *b = (res^oparg) ? Py_True : Py_False;
3013 Py_INCREF(b);
3014 PUSH(b);
3015 PREDICT(POP_JUMP_IF_FALSE);
3016 PREDICT(POP_JUMP_IF_TRUE);
3017 FAST_DISPATCH();
3018 }
3019
3020#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3021 "BaseException is not allowed"
3022
3023 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3024 PyObject *right = POP();
3025 PyObject *left = POP();
3026 if (PyTuple_Check(right)) {
3027 Py_ssize_t i, length;
3028 length = PyTuple_GET_SIZE(right);
3029 for (i = 0; i < length; i++) {
3030 PyObject *exc = PyTuple_GET_ITEM(right, i);
3031 if (!PyExceptionClass_Check(exc)) {
3032 _PyErr_SetString(tstate, PyExc_TypeError,
3033 CANNOT_CATCH_MSG);
3034 Py_DECREF(left);
3035 Py_DECREF(right);
3036 goto error;
3037 }
3038 }
3039 }
3040 else {
3041 if (!PyExceptionClass_Check(right)) {
3042 _PyErr_SetString(tstate, PyExc_TypeError,
3043 CANNOT_CATCH_MSG);
3044 Py_DECREF(left);
3045 Py_DECREF(right);
3046 goto error;
3047 }
3048 }
3049 int res = PyErr_GivenExceptionMatches(left, right);
3050 Py_DECREF(left);
3051 Py_DECREF(right);
3052 if (res > 0) {
3053 /* Exception matches -- Do nothing */;
3054 }
3055 else if (res == 0) {
3056 JUMPTO(oparg);
3057 }
3058 else {
3059 goto error;
3060 }
3061 DISPATCH();
3062 }
3063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003066 PyObject *fromlist = POP();
3067 PyObject *level = TOP();
3068 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003069 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003070 Py_DECREF(level);
3071 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 SET_TOP(res);
3073 if (res == NULL)
3074 goto error;
3075 DISPATCH();
3076 }
3077
Benjamin Petersonddd19492018-09-16 22:38:02 -07003078 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 PyObject *from = POP(), *locals;
3080 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003081 if (PyFrame_FastToLocalsWithError(f) < 0) {
3082 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003083 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003084 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 locals = f->f_locals;
3087 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003088 _PyErr_SetString(tstate, PyExc_SystemError,
3089 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003090 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003093 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 Py_DECREF(from);
3096 if (err != 0)
3097 goto error;
3098 DISPATCH();
3099 }
Guido van Rossum25831651993-05-19 14:50:45 +00003100
Benjamin Petersonddd19492018-09-16 22:38:02 -07003101 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 PyObject *name = GETITEM(names, oparg);
3103 PyObject *from = TOP();
3104 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003105 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 PUSH(res);
3107 if (res == NULL)
3108 goto error;
3109 DISPATCH();
3110 }
Thomas Wouters52152252000-08-17 22:55:00 +00003111
Benjamin Petersonddd19492018-09-16 22:38:02 -07003112 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 JUMPBY(oparg);
3114 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(POP_JUMP_IF_FALSE): {
3118 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PyObject *cond = POP();
3120 int err;
3121 if (cond == Py_True) {
3122 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 FAST_DISPATCH();
3124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 if (cond == Py_False) {
3126 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 JUMPTO(oparg);
3128 FAST_DISPATCH();
3129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 err = PyObject_IsTrue(cond);
3131 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003133 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 else if (err == 0)
3135 JUMPTO(oparg);
3136 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003140
Benjamin Petersonddd19492018-09-16 22:38:02 -07003141 case TARGET(POP_JUMP_IF_TRUE): {
3142 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 PyObject *cond = POP();
3144 int err;
3145 if (cond == Py_False) {
3146 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 FAST_DISPATCH();
3148 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 if (cond == Py_True) {
3150 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 JUMPTO(oparg);
3152 FAST_DISPATCH();
3153 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 err = PyObject_IsTrue(cond);
3155 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 JUMPTO(oparg);
3158 }
3159 else if (err == 0)
3160 ;
3161 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003165
Benjamin Petersonddd19492018-09-16 22:38:02 -07003166 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 PyObject *cond = TOP();
3168 int err;
3169 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003170 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 FAST_DISPATCH();
3173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003174 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 JUMPTO(oparg);
3176 FAST_DISPATCH();
3177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003180 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 }
3183 else if (err == 0)
3184 JUMPTO(oparg);
3185 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003189
Benjamin Petersonddd19492018-09-16 22:38:02 -07003190 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 PyObject *cond = TOP();
3192 int err;
3193 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003194 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 FAST_DISPATCH();
3197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 JUMPTO(oparg);
3200 FAST_DISPATCH();
3201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 JUMPTO(oparg);
3205 }
3206 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003207 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 }
3210 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003214
Benjamin Petersonddd19492018-09-16 22:38:02 -07003215 case TARGET(JUMP_ABSOLUTE): {
3216 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003218#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 /* Enabling this path speeds-up all while and for-loops by bypassing
3220 the per-loop checks for signals. By default, this should be turned-off
3221 because it prevents detection of a control-break in tight loops like
3222 "while 1: pass". Compile with this option turned-on when you need
3223 the speed-up and do not need break checking inside tight loops (ones
3224 that contain only instructions ending with FAST_DISPATCH).
3225 */
3226 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003227#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003229#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003231
Benjamin Petersonddd19492018-09-16 22:38:02 -07003232 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003235 PyObject *iter = PyObject_GetIter(iterable);
3236 Py_DECREF(iterable);
3237 SET_TOP(iter);
3238 if (iter == NULL)
3239 goto error;
3240 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003241 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003242 DISPATCH();
3243 }
3244
Benjamin Petersonddd19492018-09-16 22:38:02 -07003245 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003246 /* before: [obj]; after [getiter(obj)] */
3247 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003248 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003249 if (PyCoro_CheckExact(iterable)) {
3250 /* `iterable` is a coroutine */
3251 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3252 /* and it is used in a 'yield from' expression of a
3253 regular generator. */
3254 Py_DECREF(iterable);
3255 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003256 _PyErr_SetString(tstate, PyExc_TypeError,
3257 "cannot 'yield from' a coroutine object "
3258 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003259 goto error;
3260 }
3261 }
3262 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003263 /* `iterable` is not a generator. */
3264 iter = PyObject_GetIter(iterable);
3265 Py_DECREF(iterable);
3266 SET_TOP(iter);
3267 if (iter == NULL)
3268 goto error;
3269 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003270 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 DISPATCH();
3272 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003273
Benjamin Petersonddd19492018-09-16 22:38:02 -07003274 case TARGET(FOR_ITER): {
3275 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003278 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 if (next != NULL) {
3280 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 PREDICT(STORE_FAST);
3282 PREDICT(UNPACK_SEQUENCE);
3283 DISPATCH();
3284 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003285 if (_PyErr_Occurred(tstate)) {
3286 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003288 }
3289 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003290 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003291 }
3292 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 }
3294 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003295 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003298 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003303 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 STACK_LEVEL());
3305 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003307
Benjamin Petersonddd19492018-09-16 22:38:02 -07003308 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003309 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003310 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003311 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003312 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003313 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003314 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003315 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003316 }
3317 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3318 if (exit == NULL) {
3319 Py_DECREF(enter);
3320 goto error;
3321 }
Yury Selivanov75445082015-05-11 22:57:16 -04003322 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003323 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003324 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003325 Py_DECREF(enter);
3326 if (res == NULL)
3327 goto error;
3328 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003329 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003330 DISPATCH();
3331 }
3332
Benjamin Petersonddd19492018-09-16 22:38:02 -07003333 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003334 PyObject *res = POP();
3335 /* Setup the finally block before pushing the result
3336 of __aenter__ on the stack. */
3337 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3338 STACK_LEVEL());
3339 PUSH(res);
3340 DISPATCH();
3341 }
3342
Benjamin Petersonddd19492018-09-16 22:38:02 -07003343 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003344 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003345 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003347 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003349 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003350 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003351 }
3352 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003353 if (exit == NULL) {
3354 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003359 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 Py_DECREF(enter);
3361 if (res == NULL)
3362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 /* Setup the finally block before pushing the result
3364 of __enter__ on the stack. */
3365 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3366 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003367
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 DISPATCH();
3370 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003371
Mark Shannonfee55262019-11-21 09:11:43 +00003372 case TARGET(WITH_EXCEPT_START): {
3373 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003375 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3376 - SEVENTH: the context.__exit__ bound method
3377 We call SEVENTH(TOP, SECOND, THIRD).
3378 Then we push again the TOP exception and the __exit__
3379 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003382 PyObject *exc, *val, *tb, *res;
3383
Victor Stinner842cfff2016-12-01 14:45:31 +01003384 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003385 val = SECOND();
3386 tb = THIRD();
3387 assert(exc != Py_None);
3388 assert(!PyLong_Check(exc));
3389 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003390 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003391 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003392 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 if (res == NULL)
3394 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003395
Yury Selivanov75445082015-05-11 22:57:16 -04003396 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003399
Benjamin Petersonddd19492018-09-16 22:38:02 -07003400 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003401 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003402 PyObject *name = GETITEM(names, oparg);
3403 PyObject *obj = TOP();
3404 PyObject *meth = NULL;
3405
3406 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3407
Yury Selivanovf2392132016-12-13 19:03:51 -05003408 if (meth == NULL) {
3409 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003410 goto error;
3411 }
3412
3413 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003414 /* We can bypass temporary bound method object.
3415 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003416
INADA Naoki015bce62017-01-16 17:23:30 +09003417 meth | self | arg1 | ... | argN
3418 */
3419 SET_TOP(meth);
3420 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003421 }
3422 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003423 /* meth is not an unbound method (but a regular attr, or
3424 something was returned by a descriptor protocol). Set
3425 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003426 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003427
3428 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003429 */
INADA Naoki015bce62017-01-16 17:23:30 +09003430 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003431 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003432 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003433 }
3434 DISPATCH();
3435 }
3436
Benjamin Petersonddd19492018-09-16 22:38:02 -07003437 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003438 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003439 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003440
3441 sp = stack_pointer;
3442
INADA Naoki015bce62017-01-16 17:23:30 +09003443 meth = PEEK(oparg + 2);
3444 if (meth == NULL) {
3445 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3446 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003447
3448 Stack layout:
3449
INADA Naoki015bce62017-01-16 17:23:30 +09003450 ... | NULL | callable | arg1 | ... | argN
3451 ^- TOP()
3452 ^- (-oparg)
3453 ^- (-oparg-1)
3454 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003455
Ville Skyttä49b27342017-08-03 09:00:59 +03003456 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003457 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003458 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003459 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003460 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003461 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003462 }
3463 else {
3464 /* This is a method call. Stack layout:
3465
INADA Naoki015bce62017-01-16 17:23:30 +09003466 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003467 ^- TOP()
3468 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003469 ^- (-oparg-1)
3470 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003471
INADA Naoki015bce62017-01-16 17:23:30 +09003472 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003473 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003474 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003475 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003476 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003477 stack_pointer = sp;
3478 }
3479
3480 PUSH(res);
3481 if (res == NULL)
3482 goto error;
3483 DISPATCH();
3484 }
3485
Benjamin Petersonddd19492018-09-16 22:38:02 -07003486 case TARGET(CALL_FUNCTION): {
3487 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003490 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003493 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003496 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Benjamin Petersonddd19492018-09-16 22:38:02 -07003499 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003500 PyObject **sp, *res, *names;
3501
3502 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003503 assert(PyTuple_Check(names));
3504 assert(PyTuple_GET_SIZE(names) <= oparg);
3505 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003507 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003510 Py_DECREF(names);
3511
3512 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003513 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003514 }
3515 DISPATCH();
3516 }
3517
Benjamin Petersonddd19492018-09-16 22:38:02 -07003518 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003519 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003520 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003521 if (oparg & 0x01) {
3522 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003523 if (!PyDict_CheckExact(kwargs)) {
3524 PyObject *d = PyDict_New();
3525 if (d == NULL)
3526 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003527 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003528 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003529 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003530 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003531 goto error;
3532 }
3533 Py_DECREF(kwargs);
3534 kwargs = d;
3535 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003536 assert(PyDict_CheckExact(kwargs));
3537 }
3538 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003539 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003540 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003541 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003542 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003543 goto error;
3544 }
3545 Py_SETREF(callargs, PySequence_Tuple(callargs));
3546 if (callargs == NULL) {
3547 goto error;
3548 }
3549 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003550 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003551
Victor Stinner09532fe2019-05-10 23:39:09 +02003552 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003553 Py_DECREF(func);
3554 Py_DECREF(callargs);
3555 Py_XDECREF(kwargs);
3556
3557 SET_TOP(result);
3558 if (result == NULL) {
3559 goto error;
3560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Benjamin Petersonddd19492018-09-16 22:38:02 -07003564 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003565 PyObject *qualname = POP();
3566 PyObject *codeobj = POP();
3567 PyFunctionObject *func = (PyFunctionObject *)
3568 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003569
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003570 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003571 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003572 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003573 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003575
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003576 if (oparg & 0x08) {
3577 assert(PyTuple_CheckExact(TOP()));
3578 func ->func_closure = POP();
3579 }
3580 if (oparg & 0x04) {
3581 assert(PyDict_CheckExact(TOP()));
3582 func->func_annotations = POP();
3583 }
3584 if (oparg & 0x02) {
3585 assert(PyDict_CheckExact(TOP()));
3586 func->func_kwdefaults = POP();
3587 }
3588 if (oparg & 0x01) {
3589 assert(PyTuple_CheckExact(TOP()));
3590 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003592
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003593 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003594 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003596
Benjamin Petersonddd19492018-09-16 22:38:02 -07003597 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003598 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003600 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 step = NULL;
3603 stop = POP();
3604 start = TOP();
3605 slice = PySlice_New(start, stop, step);
3606 Py_DECREF(start);
3607 Py_DECREF(stop);
3608 Py_XDECREF(step);
3609 SET_TOP(slice);
3610 if (slice == NULL)
3611 goto error;
3612 DISPATCH();
3613 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003614
Benjamin Petersonddd19492018-09-16 22:38:02 -07003615 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003616 /* Handles f-string value formatting. */
3617 PyObject *result;
3618 PyObject *fmt_spec;
3619 PyObject *value;
3620 PyObject *(*conv_fn)(PyObject *);
3621 int which_conversion = oparg & FVC_MASK;
3622 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3623
3624 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003625 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003626
3627 /* See if any conversion is specified. */
3628 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003629 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003630 case FVC_STR: conv_fn = PyObject_Str; break;
3631 case FVC_REPR: conv_fn = PyObject_Repr; break;
3632 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003633 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003634 _PyErr_Format(tstate, PyExc_SystemError,
3635 "unexpected conversion flag %d",
3636 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003637 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003638 }
3639
3640 /* If there's a conversion function, call it and replace
3641 value with that result. Otherwise, just use value,
3642 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003643 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003644 result = conv_fn(value);
3645 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003646 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003647 Py_XDECREF(fmt_spec);
3648 goto error;
3649 }
3650 value = result;
3651 }
3652
3653 /* If value is a unicode object, and there's no fmt_spec,
3654 then we know the result of format(value) is value
3655 itself. In that case, skip calling format(). I plan to
3656 move this optimization in to PyObject_Format()
3657 itself. */
3658 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3659 /* Do nothing, just transfer ownership to result. */
3660 result = value;
3661 } else {
3662 /* Actually call format(). */
3663 result = PyObject_Format(value, fmt_spec);
3664 Py_DECREF(value);
3665 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003666 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003667 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003668 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003669 }
3670
Eric V. Smith135d5f42016-02-05 18:23:08 -05003671 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003672 DISPATCH();
3673 }
3674
Benjamin Petersonddd19492018-09-16 22:38:02 -07003675 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003676 int oldoparg = oparg;
3677 NEXTOPARG();
3678 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003680 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003681
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003682
Antoine Pitrou042b1282010-08-13 21:15:58 +00003683#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003685#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 default:
3687 fprintf(stderr,
3688 "XXX lineno: %d, opcode: %d\n",
3689 PyFrame_GetLineNumber(f),
3690 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003691 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003696 /* This should never be reached. Every opcode should end with DISPATCH()
3697 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003698 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003699
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003700error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003702#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003703 if (!_PyErr_Occurred(tstate)) {
3704 _PyErr_SetString(tstate, PyExc_SystemError,
3705 "error return without exception set");
3706 }
Victor Stinner365b6932013-07-12 00:11:58 +02003707#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003708 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003709#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003710
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711 /* Log traceback info. */
3712 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003713
Benjamin Peterson51f46162013-01-23 08:38:47 -05003714 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003715 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3716 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003717
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003718exception_unwind:
3719 /* Unwind stacks if an exception occurred */
3720 while (f->f_iblock > 0) {
3721 /* Pop the current block. */
3722 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 if (b->b_type == EXCEPT_HANDLER) {
3725 UNWIND_EXCEPT_HANDLER(b);
3726 continue;
3727 }
3728 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003729 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 PyObject *exc, *val, *tb;
3731 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003732 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 /* Beware, this invalidates all b->b_* fields */
3734 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003735 PUSH(exc_info->exc_traceback);
3736 PUSH(exc_info->exc_value);
3737 if (exc_info->exc_type != NULL) {
3738 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 }
3740 else {
3741 Py_INCREF(Py_None);
3742 PUSH(Py_None);
3743 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003744 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 /* Make the raw exception data
3746 available to the handler,
3747 so a program can emulate the
3748 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003749 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003750 if (tb != NULL)
3751 PyException_SetTraceback(val, tb);
3752 else
3753 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003755 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003757 exc_info->exc_value = val;
3758 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 if (tb == NULL)
3760 tb = Py_None;
3761 Py_INCREF(tb);
3762 PUSH(tb);
3763 PUSH(val);
3764 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003766 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003767 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3768 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3769 /* Make sure that we trace line after exception if we are in a new execution
3770 * window or we don't need a line update and we are not in the first instruction
3771 * of the line. */
3772 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3773 instr_prev = INT_MAX;
3774 }
Mark Shannonfee55262019-11-21 09:11:43 +00003775 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003776 /* Resume normal execution */
3777 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
3779 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003780
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003781 /* End the loop as we still have an error */
3782 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003784
Pablo Galindof00828a2019-05-09 16:52:02 +01003785 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003786 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 /* Pop remaining stack entries. */
3789 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003790 PyObject *o = POP();
3791 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003793
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003794exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003796 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003797 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3798 tstate, f, PyTrace_RETURN, retval)) {
3799 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
3801 }
3802 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003803 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3804 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003805 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 }
3807 }
3808 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003811exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003812 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3813 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003814 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003815 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003817
Victor Stinner0b72b232020-03-12 23:18:39 +01003818 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003819}
3820
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003822format_missing(PyThreadState *tstate, const char *kind,
3823 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003824{
3825 int err;
3826 Py_ssize_t len = PyList_GET_SIZE(names);
3827 PyObject *name_str, *comma, *tail, *tmp;
3828
3829 assert(PyList_CheckExact(names));
3830 assert(len >= 1);
3831 /* Deal with the joys of natural language. */
3832 switch (len) {
3833 case 1:
3834 name_str = PyList_GET_ITEM(names, 0);
3835 Py_INCREF(name_str);
3836 break;
3837 case 2:
3838 name_str = PyUnicode_FromFormat("%U and %U",
3839 PyList_GET_ITEM(names, len - 2),
3840 PyList_GET_ITEM(names, len - 1));
3841 break;
3842 default:
3843 tail = PyUnicode_FromFormat(", %U, and %U",
3844 PyList_GET_ITEM(names, len - 2),
3845 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003846 if (tail == NULL)
3847 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003848 /* Chop off the last two objects in the list. This shouldn't actually
3849 fail, but we can't be too careful. */
3850 err = PyList_SetSlice(names, len - 2, len, NULL);
3851 if (err == -1) {
3852 Py_DECREF(tail);
3853 return;
3854 }
3855 /* Stitch everything up into a nice comma-separated list. */
3856 comma = PyUnicode_FromString(", ");
3857 if (comma == NULL) {
3858 Py_DECREF(tail);
3859 return;
3860 }
3861 tmp = PyUnicode_Join(comma, names);
3862 Py_DECREF(comma);
3863 if (tmp == NULL) {
3864 Py_DECREF(tail);
3865 return;
3866 }
3867 name_str = PyUnicode_Concat(tmp, tail);
3868 Py_DECREF(tmp);
3869 Py_DECREF(tail);
3870 break;
3871 }
3872 if (name_str == NULL)
3873 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003874 _PyErr_Format(tstate, PyExc_TypeError,
3875 "%U() missing %i required %s argument%s: %U",
3876 co->co_name,
3877 len,
3878 kind,
3879 len == 1 ? "" : "s",
3880 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003881 Py_DECREF(name_str);
3882}
3883
3884static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003885missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3886 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003887 PyObject **fastlocals)
3888{
Victor Stinner74319ae2016-08-25 00:04:09 +02003889 Py_ssize_t i, j = 0;
3890 Py_ssize_t start, end;
3891 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003892 const char *kind = positional ? "positional" : "keyword-only";
3893 PyObject *missing_names;
3894
3895 /* Compute the names of the arguments that are missing. */
3896 missing_names = PyList_New(missing);
3897 if (missing_names == NULL)
3898 return;
3899 if (positional) {
3900 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003901 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003902 }
3903 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003904 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003905 end = start + co->co_kwonlyargcount;
3906 }
3907 for (i = start; i < end; i++) {
3908 if (GETLOCAL(i) == NULL) {
3909 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3910 PyObject *name = PyObject_Repr(raw);
3911 if (name == NULL) {
3912 Py_DECREF(missing_names);
3913 return;
3914 }
3915 PyList_SET_ITEM(missing_names, j++, name);
3916 }
3917 }
3918 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003919 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003920 Py_DECREF(missing_names);
3921}
3922
3923static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003924too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3925 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003926 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927{
3928 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003929 Py_ssize_t kwonly_given = 0;
3930 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003932 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933
Benjamin Petersone109c702011-06-24 09:37:26 -05003934 assert((co->co_flags & CO_VARARGS) == 0);
3935 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003936 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003939 }
3940 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003941 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003942 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003944 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 }
3946 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003947 plural = (co_argcount != 1);
3948 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 }
3950 if (sig == NULL)
3951 return;
3952 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003953 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3954 kwonly_sig = PyUnicode_FromFormat(format,
3955 given != 1 ? "s" : "",
3956 kwonly_given,
3957 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 if (kwonly_sig == NULL) {
3959 Py_DECREF(sig);
3960 return;
3961 }
3962 }
3963 else {
3964 /* This will not fail. */
3965 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003966 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003968 _PyErr_Format(tstate, PyExc_TypeError,
3969 "%U() takes %U positional argument%s but %zd%U %s given",
3970 co->co_name,
3971 sig,
3972 plural ? "s" : "",
3973 given,
3974 kwonly_sig,
3975 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003976 Py_DECREF(sig);
3977 Py_DECREF(kwonly_sig);
3978}
3979
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003980static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003981positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3982 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003983{
3984 int posonly_conflicts = 0;
3985 PyObject* posonly_names = PyList_New(0);
3986
3987 for(int k=0; k < co->co_posonlyargcount; k++){
3988 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3989
3990 for (int k2=0; k2<kwcount; k2++){
3991 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3992 PyObject* kwname = kwnames[k2];
3993 if (kwname == posonly_name){
3994 if(PyList_Append(posonly_names, kwname) != 0) {
3995 goto fail;
3996 }
3997 posonly_conflicts++;
3998 continue;
3999 }
4000
4001 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4002
4003 if ( cmp > 0) {
4004 if(PyList_Append(posonly_names, kwname) != 0) {
4005 goto fail;
4006 }
4007 posonly_conflicts++;
4008 } else if (cmp < 0) {
4009 goto fail;
4010 }
4011
4012 }
4013 }
4014 if (posonly_conflicts) {
4015 PyObject* comma = PyUnicode_FromString(", ");
4016 if (comma == NULL) {
4017 goto fail;
4018 }
4019 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4020 Py_DECREF(comma);
4021 if (error_names == NULL) {
4022 goto fail;
4023 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004024 _PyErr_Format(tstate, PyExc_TypeError,
4025 "%U() got some positional-only arguments passed"
4026 " as keyword arguments: '%U'",
4027 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004028 Py_DECREF(error_names);
4029 goto fail;
4030 }
4031
4032 Py_DECREF(posonly_names);
4033 return 0;
4034
4035fail:
4036 Py_XDECREF(posonly_names);
4037 return 1;
4038
4039}
4040
Guido van Rossumc2e20742006-02-27 22:32:47 +00004041/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004042 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004043 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004044
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004045PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004046_PyEval_EvalCode(PyThreadState *tstate,
4047 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004048 PyObject *const *args, Py_ssize_t argcount,
4049 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004050 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004051 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004052 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004053 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004054{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004055 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004056
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004057 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004058 PyFrameObject *f;
4059 PyObject *retval = NULL;
4060 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004062 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004063 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004064 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004067 _PyErr_SetString(tstate, PyExc_SystemError,
4068 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 return NULL;
4070 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004071
Victor Stinnerc7020012016-08-16 23:40:29 +02004072 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004073 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004074 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 fastlocals = f->f_localsplus;
4078 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004079
Victor Stinnerc7020012016-08-16 23:40:29 +02004080 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004081 if (co->co_flags & CO_VARKEYWORDS) {
4082 kwdict = PyDict_New();
4083 if (kwdict == NULL)
4084 goto fail;
4085 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004086 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004087 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004088 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004089 SETLOCAL(i, kwdict);
4090 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004091 else {
4092 kwdict = NULL;
4093 }
4094
Pablo Galindocd74e662019-06-01 18:08:04 +01004095 /* Copy all positional arguments into local variables */
4096 if (argcount > co->co_argcount) {
4097 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004098 }
4099 else {
4100 n = argcount;
4101 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004102 for (j = 0; j < n; j++) {
4103 x = args[j];
4104 Py_INCREF(x);
4105 SETLOCAL(j, x);
4106 }
4107
Victor Stinnerc7020012016-08-16 23:40:29 +02004108 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004109 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004110 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004111 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004113 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004115 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004116
Serhiy Storchakab7281052016-09-12 00:52:40 +03004117 /* Handle keyword arguments passed as two strided arrays */
4118 kwcount *= kwstep;
4119 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004120 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004121 PyObject *keyword = kwnames[i];
4122 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004123 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004124
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004126 _PyErr_Format(tstate, PyExc_TypeError,
4127 "%U() keywords must be strings",
4128 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004129 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004131
Benjamin Petersonb204a422011-06-05 22:04:07 -05004132 /* Speed hack: do raw pointer compares. As names are
4133 normally interned this should almost always hit. */
4134 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004135 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004136 PyObject *name = co_varnames[j];
4137 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004138 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004139 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004140 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004141
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004143 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004144 PyObject *name = co_varnames[j];
4145 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4146 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004147 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004148 }
4149 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004151 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004153
Victor Stinner231d1f32017-01-11 02:12:06 +01004154 assert(j >= total_args);
4155 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004156
Victor Stinner438a12d2019-05-24 17:01:38 +02004157 if (co->co_posonlyargcount
4158 && positional_only_passed_as_keyword(tstate, co,
4159 kwcount, kwnames))
4160 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004161 goto fail;
4162 }
4163
Victor Stinner438a12d2019-05-24 17:01:38 +02004164 _PyErr_Format(tstate, PyExc_TypeError,
4165 "%U() got an unexpected keyword argument '%S'",
4166 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004167 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004169
Christian Heimes0bd447f2013-07-20 14:48:10 +02004170 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4171 goto fail;
4172 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004173 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004174
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 kw_found:
4176 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004177 _PyErr_Format(tstate, PyExc_TypeError,
4178 "%U() got multiple values for argument '%S'",
4179 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004182 Py_INCREF(value);
4183 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004185
4186 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004187 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004188 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 goto fail;
4190 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004191
4192 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004193 if (argcount < co->co_argcount) {
4194 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004195 Py_ssize_t missing = 0;
4196 for (i = argcount; i < m; i++) {
4197 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004198 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004199 }
4200 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004201 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004202 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004203 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004204 }
4205 if (n > m)
4206 i = n - m;
4207 else
4208 i = 0;
4209 for (; i < defcount; i++) {
4210 if (GETLOCAL(m+i) == NULL) {
4211 PyObject *def = defs[i];
4212 Py_INCREF(def);
4213 SETLOCAL(m+i, def);
4214 }
4215 }
4216 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004217
4218 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004219 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004220 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004221 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004222 PyObject *name;
4223 if (GETLOCAL(i) != NULL)
4224 continue;
4225 name = PyTuple_GET_ITEM(co->co_varnames, i);
4226 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004227 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004228 if (def) {
4229 Py_INCREF(def);
4230 SETLOCAL(i, def);
4231 continue;
4232 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004233 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004234 goto fail;
4235 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004236 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004237 missing++;
4238 }
4239 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004240 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004241 goto fail;
4242 }
4243 }
4244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004246 vars into frame. */
4247 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004249 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004250 /* Possibly account for the cell variable being an argument. */
4251 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004252 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004253 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004254 /* Clear the local copy. */
4255 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004256 }
4257 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004258 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004259 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004260 if (c == NULL)
4261 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004262 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004264
4265 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004266 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4267 PyObject *o = PyTuple_GET_ITEM(closure, i);
4268 Py_INCREF(o);
4269 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004271
Yury Selivanoveb636452016-09-08 22:01:51 -07004272 /* Handle generator/coroutine/asynchronous generator */
4273 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004274 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004275 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 /* Don't need to keep the reference to f_back, it will be set
4278 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004279 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 /* Create a new generator that owns the ready to run frame
4282 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004283 if (is_coro) {
4284 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004285 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4286 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004287 } else {
4288 gen = PyGen_NewWithQualName(f, name, qualname);
4289 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004290 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004291 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004292 }
INADA Naoki9c157762016-12-26 18:52:46 +09004293
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004294 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004295
Yury Selivanov75445082015-05-11 22:57:16 -04004296 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004298
Victor Stinnerb9e68122019-11-14 12:20:46 +01004299 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004300
Thomas Woutersce272b62007-09-19 21:19:28 +00004301fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 /* decref'ing the frame can cause __del__ methods to get invoked,
4304 which can call back into Python. While we're done with the
4305 current Python frame (f), the associated C stack is still in use,
4306 so recursion_depth must be boosted for the duration.
4307 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004308 if (Py_REFCNT(f) > 1) {
4309 Py_DECREF(f);
4310 _PyObject_GC_TRACK(f);
4311 }
4312 else {
4313 ++tstate->recursion_depth;
4314 Py_DECREF(f);
4315 --tstate->recursion_depth;
4316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004318}
4319
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004320
4321PyObject *
4322_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4323 PyObject *const *args, Py_ssize_t argcount,
4324 PyObject *const *kwnames, PyObject *const *kwargs,
4325 Py_ssize_t kwcount, int kwstep,
4326 PyObject *const *defs, Py_ssize_t defcount,
4327 PyObject *kwdefs, PyObject *closure,
4328 PyObject *name, PyObject *qualname)
4329{
4330 PyThreadState *tstate = _PyThreadState_GET();
4331 return _PyEval_EvalCode(tstate, _co, globals, locals,
4332 args, argcount,
4333 kwnames, kwargs,
4334 kwcount, kwstep,
4335 defs, defcount,
4336 kwdefs, closure,
4337 name, qualname);
4338}
4339
Victor Stinner40ee3012014-06-16 15:59:28 +02004340PyObject *
4341PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004342 PyObject *const *args, int argcount,
4343 PyObject *const *kws, int kwcount,
4344 PyObject *const *defs, int defcount,
4345 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004346{
4347 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004348 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004349 kws, kws != NULL ? kws + 1 : NULL,
4350 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004351 defs, defcount,
4352 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004353 NULL, NULL);
4354}
Tim Peters5ca576e2001-06-18 22:08:13 +00004355
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004356static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004357special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004360 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004361 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004362 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 return NULL;
4364 }
4365 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004366}
4367
4368
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004369/* Logic for the raise statement (too complicated for inlining).
4370 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004371static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004372do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 if (exc == NULL) {
4377 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004378 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004380 type = exc_info->exc_type;
4381 value = exc_info->exc_value;
4382 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004383 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004384 _PyErr_SetString(tstate, PyExc_RuntimeError,
4385 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004386 return 0;
4387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 Py_XINCREF(type);
4389 Py_XINCREF(value);
4390 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004391 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004392 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 /* We support the following forms of raise:
4396 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004397 raise <instance>
4398 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 if (PyExceptionClass_Check(exc)) {
4401 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004402 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 if (value == NULL)
4404 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004405 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004406 _PyErr_Format(tstate, PyExc_TypeError,
4407 "calling %R should have returned an instance of "
4408 "BaseException, not %R",
4409 type, Py_TYPE(value));
4410 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 }
4413 else if (PyExceptionInstance_Check(exc)) {
4414 value = exc;
4415 type = PyExceptionInstance_Class(exc);
4416 Py_INCREF(type);
4417 }
4418 else {
4419 /* Not something you can raise. You get an exception
4420 anyway, just not what you specified :-) */
4421 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004422 _PyErr_SetString(tstate, PyExc_TypeError,
4423 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 goto raise_error;
4425 }
Collin Winter828f04a2007-08-31 00:04:24 +00004426
Serhiy Storchakac0191582016-09-27 11:37:10 +03004427 assert(type != NULL);
4428 assert(value != NULL);
4429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 if (cause) {
4431 PyObject *fixed_cause;
4432 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004433 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 if (fixed_cause == NULL)
4435 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004436 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004438 else if (PyExceptionInstance_Check(cause)) {
4439 fixed_cause = cause;
4440 }
4441 else if (cause == Py_None) {
4442 Py_DECREF(cause);
4443 fixed_cause = NULL;
4444 }
4445 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004446 _PyErr_SetString(tstate, PyExc_TypeError,
4447 "exception causes must derive from "
4448 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 goto raise_error;
4450 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004451 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 }
Collin Winter828f04a2007-08-31 00:04:24 +00004453
Victor Stinner438a12d2019-05-24 17:01:38 +02004454 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004455 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004456 Py_DECREF(value);
4457 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004458 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004459
4460raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 Py_XDECREF(value);
4462 Py_XDECREF(type);
4463 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004464 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004465}
4466
Tim Petersd6d010b2001-06-21 02:49:55 +00004467/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004468 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004469
Guido van Rossum0368b722007-05-11 16:50:42 +00004470 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4471 with a variable target.
4472*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004473
Barry Warsawe42b18f1997-08-25 22:13:04 +00004474static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004475unpack_iterable(PyThreadState *tstate, PyObject *v,
4476 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 int i = 0, j = 0;
4479 Py_ssize_t ll = 0;
4480 PyObject *it; /* iter(v) */
4481 PyObject *w;
4482 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004487 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004489 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004490 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004491 _PyErr_Format(tstate, PyExc_TypeError,
4492 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004493 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004494 }
4495 return 0;
4496 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 for (; i < argcnt; i++) {
4499 w = PyIter_Next(it);
4500 if (w == NULL) {
4501 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004503 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004504 _PyErr_Format(tstate, PyExc_ValueError,
4505 "not enough values to unpack "
4506 "(expected %d, got %d)",
4507 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004508 }
4509 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004510 _PyErr_Format(tstate, PyExc_ValueError,
4511 "not enough values to unpack "
4512 "(expected at least %d, got %d)",
4513 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 }
4516 goto Error;
4517 }
4518 *--sp = w;
4519 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 if (argcntafter == -1) {
4522 /* We better have exhausted the iterator now. */
4523 w = PyIter_Next(it);
4524 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004525 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 goto Error;
4527 Py_DECREF(it);
4528 return 1;
4529 }
4530 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004531 _PyErr_Format(tstate, PyExc_ValueError,
4532 "too many values to unpack (expected %d)",
4533 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 goto Error;
4535 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 l = PySequence_List(it);
4538 if (l == NULL)
4539 goto Error;
4540 *--sp = l;
4541 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 ll = PyList_GET_SIZE(l);
4544 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004545 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004546 "not enough values to unpack (expected at least %d, got %zd)",
4547 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 goto Error;
4549 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 /* Pop the "after-variable" args off the list. */
4552 for (j = argcntafter; j > 0; j--, i++) {
4553 *--sp = PyList_GET_ITEM(l, ll - j);
4554 }
4555 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004556 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 Py_DECREF(it);
4558 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004559
Tim Petersd6d010b2001-06-21 02:49:55 +00004560Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 for (; i > 0; i--, sp++)
4562 Py_DECREF(*sp);
4563 Py_XDECREF(it);
4564 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004565}
4566
4567
Guido van Rossum96a42c81992-01-12 02:29:51 +00004568#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004569static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004570prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004573 if (PyObject_Print(v, stdout, 0) != 0) {
4574 /* Don't know what else to do */
4575 _PyErr_Clear(tstate);
4576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 printf("\n");
4578 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004579}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004580#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004581
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004582static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004583call_exc_trace(Py_tracefunc func, PyObject *self,
4584 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004585{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004586 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004588 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 if (value == NULL) {
4590 value = Py_None;
4591 Py_INCREF(value);
4592 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004593 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004594 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 arg = PyTuple_Pack(3, type, value, traceback);
4596 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004597 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 return;
4599 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004600 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004602 if (err == 0) {
4603 _PyErr_Restore(tstate, type, value, orig_traceback);
4604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 else {
4606 Py_XDECREF(type);
4607 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004608 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004610}
4611
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004612static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004613call_trace_protected(Py_tracefunc func, PyObject *obj,
4614 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 PyObject *type, *value, *traceback;
4618 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004619 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004620 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 if (err == 0)
4622 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004623 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 return 0;
4625 }
4626 else {
4627 Py_XDECREF(type);
4628 Py_XDECREF(value);
4629 Py_XDECREF(traceback);
4630 return -1;
4631 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004632}
4633
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004634static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004635call_trace(Py_tracefunc func, PyObject *obj,
4636 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 int result;
4640 if (tstate->tracing)
4641 return 0;
4642 tstate->tracing++;
4643 tstate->use_tracing = 0;
4644 result = func(obj, frame, what, arg);
4645 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4646 || (tstate->c_profilefunc != NULL));
4647 tstate->tracing--;
4648 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004649}
4650
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004651PyObject *
4652_PyEval_CallTracing(PyObject *func, PyObject *args)
4653{
Victor Stinner50b48572018-11-01 01:51:40 +01004654 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 int save_tracing = tstate->tracing;
4656 int save_use_tracing = tstate->use_tracing;
4657 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 tstate->tracing = 0;
4660 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4661 || (tstate->c_profilefunc != NULL));
4662 result = PyObject_Call(func, args, NULL);
4663 tstate->tracing = save_tracing;
4664 tstate->use_tracing = save_use_tracing;
4665 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004666}
4667
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004668/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004669static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004670maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004671 PyThreadState *tstate, PyFrameObject *frame,
4672 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 int result = 0;
4675 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 /* If the last instruction executed isn't in the current
4678 instruction window, reset the window.
4679 */
4680 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4681 PyAddrPair bounds;
4682 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4683 &bounds);
4684 *instr_lb = bounds.ap_lower;
4685 *instr_ub = bounds.ap_upper;
4686 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004687 /* If the last instruction falls at the start of a line or if it
4688 represents a jump backwards, update the frame's line number and
4689 then call the trace function if we're tracing source lines.
4690 */
4691 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004693 if (frame->f_trace_lines) {
4694 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 }
George King20faa682017-10-18 17:44:22 -07004697 /* Always emit an opcode event if we're tracing all opcodes. */
4698 if (frame->f_trace_opcodes) {
4699 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 *instr_prev = frame->f_lasti;
4702 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004703}
4704
Victor Stinner309d7cc2020-03-13 16:39:12 +01004705int
4706_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4707{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004708 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004709 /* The caller must hold the GIL */
4710 assert(PyGILState_Check());
4711
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004712 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004713 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004714 PyThreadState *current_tstate = _PyThreadState_GET();
4715 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004716 return -1;
4717 }
4718
4719 PyObject *profileobj = tstate->c_profileobj;
4720
4721 tstate->c_profilefunc = NULL;
4722 tstate->c_profileobj = NULL;
4723 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4724 tstate->use_tracing = tstate->c_tracefunc != NULL;
4725 Py_XDECREF(profileobj);
4726
4727 Py_XINCREF(arg);
4728 tstate->c_profileobj = arg;
4729 tstate->c_profilefunc = func;
4730
4731 /* Flag that tracing or profiling is turned on */
4732 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4733 return 0;
4734}
4735
Fred Drake5755ce62001-06-27 19:19:46 +00004736void
4737PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004738{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004739 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004740 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004741 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004742 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4743 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004744}
4745
4746int
4747_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4748{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004749 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004750 /* The caller must hold the GIL */
4751 assert(PyGILState_Check());
4752
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004753 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004754 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004755 PyThreadState *current_tstate = _PyThreadState_GET();
4756 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004757 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004758 }
4759
Victor Stinnerda2914d2020-03-20 09:29:08 +01004760 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004761 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004762 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004763
4764 tstate->c_tracefunc = NULL;
4765 tstate->c_traceobj = NULL;
4766 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4767 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4768 Py_XDECREF(traceobj);
4769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004771 tstate->c_traceobj = arg;
4772 tstate->c_tracefunc = func;
4773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004775 tstate->use_tracing = ((func != NULL)
4776 || (tstate->c_profilefunc != NULL));
4777
4778 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004779}
4780
4781void
4782PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4783{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004784 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004785 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004786 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004787 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4788 }
Fred Draked0838392001-06-16 21:02:31 +00004789}
4790
Victor Stinner309d7cc2020-03-13 16:39:12 +01004791
Yury Selivanov75445082015-05-11 22:57:16 -04004792void
Victor Stinner838f2642019-06-13 22:41:23 +02004793_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004794{
4795 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004796 tstate->coroutine_origin_tracking_depth = new_depth;
4797}
4798
4799int
4800_PyEval_GetCoroutineOriginTrackingDepth(void)
4801{
Victor Stinner50b48572018-11-01 01:51:40 +01004802 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004803 return tstate->coroutine_origin_tracking_depth;
4804}
4805
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004806int
Yury Selivanoveb636452016-09-08 22:01:51 -07004807_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4808{
Victor Stinner50b48572018-11-01 01:51:40 +01004809 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004810
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004811 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004812 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004813 }
4814
Yury Selivanoveb636452016-09-08 22:01:51 -07004815 Py_XINCREF(firstiter);
4816 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004817 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004818}
4819
4820PyObject *
4821_PyEval_GetAsyncGenFirstiter(void)
4822{
Victor Stinner50b48572018-11-01 01:51:40 +01004823 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004824 return tstate->async_gen_firstiter;
4825}
4826
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004827int
Yury Selivanoveb636452016-09-08 22:01:51 -07004828_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4829{
Victor Stinner50b48572018-11-01 01:51:40 +01004830 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004831
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004832 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004833 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004834 }
4835
Yury Selivanoveb636452016-09-08 22:01:51 -07004836 Py_XINCREF(finalizer);
4837 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004838 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004839}
4840
4841PyObject *
4842_PyEval_GetAsyncGenFinalizer(void)
4843{
Victor Stinner50b48572018-11-01 01:51:40 +01004844 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004845 return tstate->async_gen_finalizer;
4846}
4847
Victor Stinner438a12d2019-05-24 17:01:38 +02004848PyFrameObject *
4849PyEval_GetFrame(void)
4850{
4851 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004852 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004853}
4854
Guido van Rossumb209a111997-04-29 18:18:01 +00004855PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004856PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004857{
Victor Stinner438a12d2019-05-24 17:01:38 +02004858 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004859 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004861 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 else
4863 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004864}
4865
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004866/* Convenience function to get a builtin from its name */
4867PyObject *
4868_PyEval_GetBuiltinId(_Py_Identifier *name)
4869{
Victor Stinner438a12d2019-05-24 17:01:38 +02004870 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004871 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4872 if (attr) {
4873 Py_INCREF(attr);
4874 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004875 else if (!_PyErr_Occurred(tstate)) {
4876 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004877 }
4878 return attr;
4879}
4880
Guido van Rossumb209a111997-04-29 18:18:01 +00004881PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004882PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004883{
Victor Stinner438a12d2019-05-24 17:01:38 +02004884 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004885 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004886 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004887 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004889 }
4890
Victor Stinner438a12d2019-05-24 17:01:38 +02004891 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004892 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004893 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004894
4895 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004897}
4898
Guido van Rossumb209a111997-04-29 18:18:01 +00004899PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004900PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004901{
Victor Stinner438a12d2019-05-24 17:01:38 +02004902 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004903 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004904 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004906 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004907
4908 assert(current_frame->f_globals != NULL);
4909 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004910}
4911
Guido van Rossum6135a871995-01-09 17:53:26 +00004912int
Tim Peters5ba58662001-07-16 02:29:45 +00004913PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004914{
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004916 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 if (current_frame != NULL) {
4920 const int codeflags = current_frame->f_code->co_flags;
4921 const int compilerflags = codeflags & PyCF_MASK;
4922 if (compilerflags) {
4923 result = 1;
4924 cf->cf_flags |= compilerflags;
4925 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004926#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 if (codeflags & CO_GENERATOR_ALLOWED) {
4928 result = 1;
4929 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4930 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004931#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 }
4933 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004934}
4935
Guido van Rossum3f5da241990-12-20 15:06:42 +00004936
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004937const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004938PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 if (PyMethod_Check(func))
4941 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4942 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004943 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 else if (PyCFunction_Check(func))
4945 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4946 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004947 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004948}
4949
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004950const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004951PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 if (PyMethod_Check(func))
4954 return "()";
4955 else if (PyFunction_Check(func))
4956 return "()";
4957 else if (PyCFunction_Check(func))
4958 return "()";
4959 else
4960 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004961}
4962
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004963#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004964if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004965 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4966 tstate, tstate->frame, \
4967 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 x = NULL; \
4969 } \
4970 else { \
4971 x = call; \
4972 if (tstate->c_profilefunc != NULL) { \
4973 if (x == NULL) { \
4974 call_trace_protected(tstate->c_profilefunc, \
4975 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004976 tstate, tstate->frame, \
4977 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004978 /* XXX should pass (type, value, tb) */ \
4979 } else { \
4980 if (call_trace(tstate->c_profilefunc, \
4981 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004982 tstate, tstate->frame, \
4983 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 Py_DECREF(x); \
4985 x = NULL; \
4986 } \
4987 } \
4988 } \
4989 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004990} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 x = call; \
4992 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004993
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004994
4995static PyObject *
4996trace_call_function(PyThreadState *tstate,
4997 PyObject *func,
4998 PyObject **args, Py_ssize_t nargs,
4999 PyObject *kwnames)
5000{
5001 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005002 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005003 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005004 return x;
5005 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005006 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005007 /* We need to create a temporary bound method as argument
5008 for profiling.
5009
5010 If nargs == 0, then this cannot work because we have no
5011 "self". In any case, the call itself would raise
5012 TypeError (foo needs an argument), so we just skip
5013 profiling. */
5014 PyObject *self = args[0];
5015 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5016 if (func == NULL) {
5017 return NULL;
5018 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005019 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005020 args+1, nargs-1,
5021 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005022 Py_DECREF(func);
5023 return x;
5024 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005025 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005026}
5027
Victor Stinner415c5102017-01-11 00:54:57 +01005028/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5029 to reduce the stack consumption. */
5030Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005031call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005032{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005033 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 PyObject *func = *pfunc;
5035 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005036 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5037 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005038 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005039
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005040 if (tstate->use_tracing) {
5041 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005042 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005043 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005044 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005046
Victor Stinner438a12d2019-05-24 17:01:38 +02005047 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005048
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005049 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 while ((*pp_stack) > pfunc) {
5051 w = EXT_POP(*pp_stack);
5052 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005056}
5057
Jeremy Hylton52820442001-01-03 23:52:36 +00005058static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005059do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005060{
jdemeyere89de732018-09-19 12:06:20 +02005061 PyObject *result;
5062
scoder4c9ea092020-05-12 16:12:41 +02005063 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005064 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005065 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005067 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005068 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5069 if (nargs > 0 && tstate->use_tracing) {
5070 /* We need to create a temporary bound method as argument
5071 for profiling.
5072
5073 If nargs == 0, then this cannot work because we have no
5074 "self". In any case, the call itself would raise
5075 TypeError (foo needs an argument), so we just skip
5076 profiling. */
5077 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5078 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5079 if (func == NULL) {
5080 return NULL;
5081 }
5082
Victor Stinner4d231bc2019-11-14 13:36:21 +01005083 C_TRACE(result, _PyObject_FastCallDictTstate(
5084 tstate, func,
5085 &_PyTuple_ITEMS(callargs)[1],
5086 nargs - 1,
5087 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005088 Py_DECREF(func);
5089 return result;
5090 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005091 }
jdemeyere89de732018-09-19 12:06:20 +02005092 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005093}
5094
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005095/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005096 nb_index slot defined, and store in *pi.
5097 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005098 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005099 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005100*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005101int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005102_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005103{
Victor Stinner438a12d2019-05-24 17:01:38 +02005104 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005105 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005107 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005109 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 return 0;
5111 }
5112 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005113 _PyErr_SetString(tstate, PyExc_TypeError,
5114 "slice indices must be integers or "
5115 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 return 0;
5117 }
5118 *pi = x;
5119 }
5120 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005121}
5122
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005123int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005124_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005125{
Victor Stinner438a12d2019-05-24 17:01:38 +02005126 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005127 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005128 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005129 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005130 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005131 return 0;
5132 }
5133 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005134 _PyErr_SetString(tstate, PyExc_TypeError,
5135 "slice indices must be integers or "
5136 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005137 return 0;
5138 }
5139 *pi = x;
5140 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005141}
5142
Thomas Wouters52152252000-08-17 22:55:00 +00005143static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005144import_name(PyThreadState *tstate, PyFrameObject *f,
5145 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005146{
5147 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005148 PyObject *import_func, *res;
5149 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005150
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005151 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005152 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005153 if (!_PyErr_Occurred(tstate)) {
5154 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005155 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005156 return NULL;
5157 }
5158
5159 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005160 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005161 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005162 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005163 return NULL;
5164 }
5165 res = PyImport_ImportModuleLevelObject(
5166 name,
5167 f->f_globals,
5168 f->f_locals == NULL ? Py_None : f->f_locals,
5169 fromlist,
5170 ilevel);
5171 return res;
5172 }
5173
5174 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005175
5176 stack[0] = name;
5177 stack[1] = f->f_globals;
5178 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5179 stack[3] = fromlist;
5180 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005181 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005182 Py_DECREF(import_func);
5183 return res;
5184}
5185
5186static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005187import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005190 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005191
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005192 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005193 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005194 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005195 /* Issue #17636: in case this failed because of a circular relative
5196 import, try to fallback on reading the module directly from
5197 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005198 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005199 if (pkgname == NULL) {
5200 goto error;
5201 }
Oren Milman6db70332017-09-19 14:23:01 +03005202 if (!PyUnicode_Check(pkgname)) {
5203 Py_CLEAR(pkgname);
5204 goto error;
5205 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005206 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005207 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005208 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005209 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005210 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005211 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005212 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005213 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005214 goto error;
5215 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005216 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005218 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005219 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005220 if (pkgname == NULL) {
5221 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5222 if (pkgname_or_unknown == NULL) {
5223 Py_XDECREF(pkgpath);
5224 return NULL;
5225 }
5226 } else {
5227 pkgname_or_unknown = pkgname;
5228 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005229
5230 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005231 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005232 errmsg = PyUnicode_FromFormat(
5233 "cannot import name %R from %R (unknown location)",
5234 name, pkgname_or_unknown
5235 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005236 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005237 PyErr_SetImportError(errmsg, pkgname, NULL);
5238 }
5239 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005240 _Py_IDENTIFIER(__spec__);
5241 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005242 const char *fmt =
5243 _PyModuleSpec_IsInitializing(spec) ?
5244 "cannot import name %R from partially initialized module %R "
5245 "(most likely due to a circular import) (%S)" :
5246 "cannot import name %R from %R (%S)";
5247 Py_XDECREF(spec);
5248
5249 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005250 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005251 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005252 }
5253
Xiang Zhang4830f582017-03-21 11:13:42 +08005254 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005255 Py_XDECREF(pkgname_or_unknown);
5256 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005257 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005258}
Guido van Rossumac7be682001-01-17 15:42:30 +00005259
Thomas Wouters52152252000-08-17 22:55:00 +00005260static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005261import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005262{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005263 _Py_IDENTIFIER(__all__);
5264 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005265 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 int skip_leading_underscores = 0;
5267 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005268
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005269 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5270 return -1; /* Unexpected error */
5271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005273 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5274 return -1;
5275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005277 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005278 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 return -1;
5280 }
5281 all = PyMapping_Keys(dict);
5282 Py_DECREF(dict);
5283 if (all == NULL)
5284 return -1;
5285 skip_leading_underscores = 1;
5286 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 for (pos = 0, err = 0; ; pos++) {
5289 name = PySequence_GetItem(all, pos);
5290 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005291 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005293 }
5294 else {
5295 _PyErr_Clear(tstate);
5296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005297 break;
5298 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005299 if (!PyUnicode_Check(name)) {
5300 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5301 if (modname == NULL) {
5302 Py_DECREF(name);
5303 err = -1;
5304 break;
5305 }
5306 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005307 _PyErr_Format(tstate, PyExc_TypeError,
5308 "module __name__ must be a string, not %.100s",
5309 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005310 }
5311 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005312 _PyErr_Format(tstate, PyExc_TypeError,
5313 "%s in %U.%s must be str, not %.100s",
5314 skip_leading_underscores ? "Key" : "Item",
5315 modname,
5316 skip_leading_underscores ? "__dict__" : "__all__",
5317 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005318 }
5319 Py_DECREF(modname);
5320 Py_DECREF(name);
5321 err = -1;
5322 break;
5323 }
5324 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005325 if (PyUnicode_READY(name) == -1) {
5326 Py_DECREF(name);
5327 err = -1;
5328 break;
5329 }
5330 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5331 Py_DECREF(name);
5332 continue;
5333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 }
5335 value = PyObject_GetAttr(v, name);
5336 if (value == NULL)
5337 err = -1;
5338 else if (PyDict_CheckExact(locals))
5339 err = PyDict_SetItem(locals, name, value);
5340 else
5341 err = PyObject_SetItem(locals, name, value);
5342 Py_DECREF(name);
5343 Py_XDECREF(value);
5344 if (err != 0)
5345 break;
5346 }
5347 Py_DECREF(all);
5348 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005349}
5350
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005351static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005352check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005353{
Victor Stinnera102ed72020-02-07 02:24:48 +01005354 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005355 /* check_args_iterable() may be called with a live exception:
5356 * clear it to prevent calling _PyObject_FunctionStr() with an
5357 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005358 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005359 PyObject *funcstr = _PyObject_FunctionStr(func);
5360 if (funcstr != NULL) {
5361 _PyErr_Format(tstate, PyExc_TypeError,
5362 "%U argument after * must be an iterable, not %.200s",
5363 funcstr, Py_TYPE(args)->tp_name);
5364 Py_DECREF(funcstr);
5365 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005366 return -1;
5367 }
5368 return 0;
5369}
5370
5371static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005372format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005373{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005374 /* _PyDict_MergeEx raises attribute
5375 * error (percolated from an attempt
5376 * to get 'keys' attribute) instead of
5377 * a type error if its second argument
5378 * is not a mapping.
5379 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005381 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005382 PyObject *funcstr = _PyObject_FunctionStr(func);
5383 if (funcstr != NULL) {
5384 _PyErr_Format(
5385 tstate, PyExc_TypeError,
5386 "%U argument after ** must be a mapping, not %.200s",
5387 funcstr, Py_TYPE(kwargs)->tp_name);
5388 Py_DECREF(funcstr);
5389 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005390 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005391 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005392 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005393 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005394 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005395 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005396 PyObject *funcstr = _PyObject_FunctionStr(func);
5397 if (funcstr != NULL) {
5398 PyObject *key = PyTuple_GET_ITEM(val, 0);
5399 _PyErr_Format(
5400 tstate, PyExc_TypeError,
5401 "%U got multiple values for keyword argument '%S'",
5402 funcstr, key);
5403 Py_DECREF(funcstr);
5404 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005405 Py_XDECREF(exc);
5406 Py_XDECREF(val);
5407 Py_XDECREF(tb);
5408 }
5409 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005410 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005411 }
5412 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005413}
5414
Guido van Rossumac7be682001-01-17 15:42:30 +00005415static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005416format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5417 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005421 if (!obj)
5422 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005423
Serhiy Storchaka06515832016-11-20 09:13:07 +02005424 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005425 if (!obj_str)
5426 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005427
Victor Stinner438a12d2019-05-24 17:01:38 +02005428 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005429}
Guido van Rossum950361c1997-01-24 13:49:28 +00005430
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005431static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005432format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005433{
5434 PyObject *name;
5435 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005436 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005437 return;
5438 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5439 name = PyTuple_GET_ITEM(co->co_cellvars,
5440 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005441 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005442 PyExc_UnboundLocalError,
5443 UNBOUNDLOCAL_ERROR_MSG,
5444 name);
5445 } else {
5446 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5447 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005449 UNBOUNDFREE_ERROR_MSG, name);
5450 }
5451}
5452
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005453static void
Mark Shannonfee55262019-11-21 09:11:43 +00005454format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005455{
5456 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5457 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005458 _PyErr_Format(tstate, PyExc_TypeError,
5459 "'async with' received an object from __aenter__ "
5460 "that does not implement __await__: %.100s",
5461 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005462 }
Mark Shannonfee55262019-11-21 09:11:43 +00005463 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005464 _PyErr_Format(tstate, PyExc_TypeError,
5465 "'async with' received an object from __aexit__ "
5466 "that does not implement __await__: %.100s",
5467 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005468 }
5469 }
5470}
5471
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005472static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005473unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005474 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005475{
5476 PyObject *res;
5477 if (Py_REFCNT(v) == 2) {
5478 /* In the common case, there are 2 references to the value
5479 * stored in 'variable' when the += is performed: one on the
5480 * value stack (in 'v') and one still stored in the
5481 * 'variable'. We try to delete the variable now to reduce
5482 * the refcnt to 1.
5483 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005484 int opcode, oparg;
5485 NEXTOPARG();
5486 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005487 case STORE_FAST:
5488 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005489 PyObject **fastlocals = f->f_localsplus;
5490 if (GETLOCAL(oparg) == v)
5491 SETLOCAL(oparg, NULL);
5492 break;
5493 }
5494 case STORE_DEREF:
5495 {
5496 PyObject **freevars = (f->f_localsplus +
5497 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005498 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005499 if (PyCell_GET(c) == v) {
5500 PyCell_SET(c, NULL);
5501 Py_DECREF(v);
5502 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005503 break;
5504 }
5505 case STORE_NAME:
5506 {
5507 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005508 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005509 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005510 if (locals && PyDict_CheckExact(locals)) {
5511 PyObject *w = PyDict_GetItemWithError(locals, name);
5512 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005513 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005514 {
5515 Py_DECREF(v);
5516 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005517 }
5518 }
5519 break;
5520 }
5521 }
5522 }
5523 res = v;
5524 PyUnicode_Append(&res, w);
5525 return res;
5526}
5527
Guido van Rossum950361c1997-01-24 13:49:28 +00005528#ifdef DYNAMIC_EXECUTION_PROFILE
5529
Skip Montanarof118cb12001-10-15 20:51:38 +00005530static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005531getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 int i;
5534 PyObject *l = PyList_New(256);
5535 if (l == NULL) return NULL;
5536 for (i = 0; i < 256; i++) {
5537 PyObject *x = PyLong_FromLong(a[i]);
5538 if (x == NULL) {
5539 Py_DECREF(l);
5540 return NULL;
5541 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005542 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 }
5544 for (i = 0; i < 256; i++)
5545 a[i] = 0;
5546 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005547}
5548
5549PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005550_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005551{
5552#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005554#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 int i;
5556 PyObject *l = PyList_New(257);
5557 if (l == NULL) return NULL;
5558 for (i = 0; i < 257; i++) {
5559 PyObject *x = getarray(dxpairs[i]);
5560 if (x == NULL) {
5561 Py_DECREF(l);
5562 return NULL;
5563 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005564 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005565 }
5566 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005567#endif
5568}
5569
5570#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005571
5572Py_ssize_t
5573_PyEval_RequestCodeExtraIndex(freefunc free)
5574{
Victor Stinner81a7be32020-04-14 15:14:01 +02005575 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005576 Py_ssize_t new_index;
5577
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005578 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005579 return -1;
5580 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005581 new_index = interp->co_extra_user_count++;
5582 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005583 return new_index;
5584}
Łukasz Langaa785c872016-09-09 17:37:37 -07005585
5586static void
5587dtrace_function_entry(PyFrameObject *f)
5588{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005589 const char *filename;
5590 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005591 int lineno;
5592
Victor Stinner6d86a232020-04-29 00:56:58 +02005593 PyCodeObject *code = f->f_code;
5594 filename = PyUnicode_AsUTF8(code->co_filename);
5595 funcname = PyUnicode_AsUTF8(code->co_name);
5596 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005597
Andy Lestere6be9b52020-02-11 20:28:35 -06005598 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005599}
5600
5601static void
5602dtrace_function_return(PyFrameObject *f)
5603{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005604 const char *filename;
5605 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005606 int lineno;
5607
Victor Stinner6d86a232020-04-29 00:56:58 +02005608 PyCodeObject *code = f->f_code;
5609 filename = PyUnicode_AsUTF8(code->co_filename);
5610 funcname = PyUnicode_AsUTF8(code->co_name);
5611 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005612
Andy Lestere6be9b52020-02-11 20:28:35 -06005613 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005614}
5615
5616/* DTrace equivalent of maybe_call_line_trace. */
5617static void
5618maybe_dtrace_line(PyFrameObject *frame,
5619 int *instr_lb, int *instr_ub, int *instr_prev)
5620{
5621 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005622 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005623
5624 /* If the last instruction executed isn't in the current
5625 instruction window, reset the window.
5626 */
5627 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5628 PyAddrPair bounds;
5629 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5630 &bounds);
5631 *instr_lb = bounds.ap_lower;
5632 *instr_ub = bounds.ap_upper;
5633 }
5634 /* If the last instruction falls at the start of a line or if
5635 it represents a jump backwards, update the frame's line
5636 number and call the trace function. */
5637 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5638 frame->f_lineno = line;
5639 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5640 if (!co_filename)
5641 co_filename = "?";
5642 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5643 if (!co_name)
5644 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005645 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005646 }
5647 *instr_prev = frame->f_lasti;
5648}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005649
5650
5651/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5652 for the limited API. */
5653
5654#undef Py_EnterRecursiveCall
5655
5656int Py_EnterRecursiveCall(const char *where)
5657{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005658 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005659}
5660
5661#undef Py_LeaveRecursiveCall
5662
5663void Py_LeaveRecursiveCall(void)
5664{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005665 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005666}