blob: b5854d34464639114e8bbdddc603a6457d84e47a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner4d231bc2019-11-14 13:36:21 +010014#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020015#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090016#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010017#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010018#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020019#include "pycore_pyerrors.h"
20#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner1c1e68c2020-03-27 15:11:45 +010023#include "pycore_sysmodule.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010024#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossumc6004111993-11-05 10:22:19 +000033#include <ctype.h>
34
Guido van Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000036/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define LLTRACE 1 /* Low-level trace feature */
38#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#endif
40
Victor Stinner5c75f372019-04-17 23:02:26 +020041#if !defined(Py_BUILD_CORE)
42# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43#endif
44
Hai Shi46874c22020-01-30 17:20:25 -060045_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000046
Guido van Rossum374a9221991-04-04 10:40:29 +000047/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020048Py_LOCAL_INLINE(PyObject *) call_function(
49 PyThreadState *tstate, PyObject ***pp_stack,
50 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
52 PyThreadState *tstate, PyObject *func,
53 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000054
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000056static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020057static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059static int call_trace(Py_tracefunc, PyObject *,
60 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000062static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010063 PyThreadState *, PyFrameObject *,
64 int, PyObject *);
65static void call_exc_trace(Py_tracefunc, PyObject *,
66 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000067static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068 PyThreadState *, PyFrameObject *,
69 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070070static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
71static void dtrace_function_entry(PyFrameObject *);
72static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000073
Victor Stinner438a12d2019-05-24 17:01:38 +020074static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000085static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900106#ifdef Py_DEBUG
107// --with-pydebug is used to find memory leak. opcache makes it harder.
108// So we disable opcache when Py_DEBUG is defined.
109// See bpo-37146
110#define OPCACHE_MIN_RUNS 0 /* disable opcache */
111#else
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900113#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900114#define OPCACHE_STATS 0 /* Enable stats */
115
116#if OPCACHE_STATS
117static size_t opcache_code_objects = 0;
118static size_t opcache_code_objects_extra_mem = 0;
119
120static size_t opcache_global_opts = 0;
121static size_t opcache_global_hits = 0;
122static size_t opcache_global_misses = 0;
123#endif
124
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100125
Victor Stinnerda2914d2020-03-20 09:29:08 +0100126#ifndef NDEBUG
127/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
128 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
129 when a thread continues to run after Python finalization, especially
130 daemon threads. */
131static int
132is_tstate_valid(PyThreadState *tstate)
133{
134 assert(!_PyMem_IsPtrFreed(tstate));
135 assert(!_PyMem_IsPtrFreed(tstate->interp));
136 return 1;
137}
138#endif
139
140
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000141/* This can set eval_breaker to 0 even though gil_drop_request became
142 1. We believe this is all right because the eval loop will release
143 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100144static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200145COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200146 struct _ceval_runtime_state *ceval,
147 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100148{
Victor Stinner299b8c62020-05-05 17:40:18 +0200149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200155 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100156}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200160SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100161{
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 struct _ceval_state *ceval2 = &interp->ceval;
163 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
164 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200169RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170{
Victor Stinner299b8c62020-05-05 17:40:18 +0200171 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
172 struct _ceval_state *ceval2 = &interp->ceval;
173 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
174 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Eric Snowfdf282d2019-01-11 14:26:55 -0700177
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200179SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180{
Victor Stinner299b8c62020-05-05 17:40:18 +0200181 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
184 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Eric Snowfdf282d2019-01-11 14:26:55 -0700186
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
196
197
198static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200199SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200203 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100204 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinner299b8c62020-05-05 17:40:18 +0200205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
207
208
209static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200210UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200214 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200215 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216}
217
218
219static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200220SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223 ceval2->pending.async_exc = 1;
224 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
225}
226
227
228static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200229UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100230{
Victor Stinner299b8c62020-05-05 17:40:18 +0200231 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
232 struct _ceval_state *ceval2 = &interp->ceval;
233 ceval2->pending.async_exc = 0;
234 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236
237
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000240#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100243static void
244ensure_tstate_not_null(const char *func, PyThreadState *tstate)
245{
246 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100247 _Py_FatalErrorFunc(func,
248 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100249 }
250}
251
252
Tim Peters7f468f22004-10-11 02:40:51 +0000253int
Victor Stinner175a7042020-03-10 00:37:48 +0100254_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
255{
256 return gil_created(&runtime->ceval.gil);
257}
258
259int
Tim Peters7f468f22004-10-11 02:40:51 +0000260PyEval_ThreadsInitialized(void)
261{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100262 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100263 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000264}
265
Victor Stinner111e4ee2020-03-09 21:24:14 +0100266PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200267_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200269 if (!_Py_IsMainInterpreter(tstate)) {
270 /* Currently, the GIL is shared by all interpreters,
271 and only the main interpreter is responsible to create
272 and destroy it. */
273 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100274 }
275
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200276 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
277 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100278
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200279 PyThread_init_thread();
280 create_gil(gil);
281
282 take_gil(tstate);
283
284 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100285 return _PyStatus_OK();
286}
287
288void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200289_PyEval_FiniGIL(PyThreadState *tstate)
290{
291 if (!_Py_IsMainInterpreter(tstate)) {
292 /* Currently, the GIL is shared by all interpreters,
293 and only the main interpreter is responsible to create
294 and destroy it. */
295 return;
296 }
297
298 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
299 if (!gil_created(gil)) {
300 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
301 yet: do nothing. */
302 return;
303 }
304
305 destroy_gil(gil);
306 assert(!gil_created(gil));
307}
308
309void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100310PyEval_InitThreads(void)
311{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100312 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Inada Naoki91234a12019-06-03 21:30:58 +0900316_PyEval_Fini(void)
317{
318#if OPCACHE_STATS
319 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
320 opcache_code_objects);
321
322 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
323 opcache_code_objects_extra_mem);
324
325 fprintf(stderr, "\n");
326
327 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
328 opcache_global_hits,
329 (int) (100.0 * opcache_global_hits /
330 (opcache_global_hits + opcache_global_misses)));
331
332 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
333 opcache_global_misses,
334 (int) (100.0 * opcache_global_misses /
335 (opcache_global_hits + opcache_global_misses)));
336
337 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
338 opcache_global_opts);
339
340 fprintf(stderr, "\n");
341#endif
342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000346{
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200348 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100349 ensure_tstate_not_null(__func__, tstate);
350
Victor Stinner85f5a692020-03-09 22:12:04 +0100351 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352}
353
354void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000355PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356{
Victor Stinner09532fe2019-05-10 23:39:09 +0200357 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200358 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100360 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100361 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200362 struct _ceval_runtime_state *ceval = &runtime->ceval;
363 struct _ceval_state *ceval2 = &tstate->interp->ceval;
364 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365}
366
367void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100368_PyEval_ReleaseLock(PyThreadState *tstate)
369{
370 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200371 struct _ceval_state *ceval2 = &tstate->interp->ceval;
372 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100373}
374
375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000377{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100378 ensure_tstate_not_null(__func__, tstate);
379
Victor Stinner85f5a692020-03-09 22:12:04 +0100380 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200381
Victor Stinner85f5a692020-03-09 22:12:04 +0100382 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200383#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
384 (void)_PyThreadState_Swap(gilstate, tstate);
385#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100386 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100387 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200388 }
Victor Stinnere838a932020-05-05 19:56:48 +0200389#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000390}
391
392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000394{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100395 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200396
Victor Stinner01b1cc12019-11-20 02:27:56 +0100397 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200398 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
399 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100400 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200401 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200402 struct _ceval_runtime_state *ceval = &runtime->ceval;
403 struct _ceval_state *ceval2 = &tstate->interp->ceval;
404 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000405}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000406
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900407#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200408/* This function is called from PyOS_AfterFork_Child to destroy all threads
409 * which are not running in the child process, and clear internal locks
410 * which might be held by those threads.
411 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000412
413void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200414_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000415{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100416 struct _gil_runtime_state *gil = &runtime->ceval.gil;
417 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200419 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100420 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100421 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
422 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100423
424 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700425
Victor Stinner50e6e992020-03-19 02:41:21 +0100426 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900427 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700428 Py_FatalError("Can't initialize threads for pending calls");
429 }
Jesse Nollera8513972008-07-17 16:49:17 +0000430
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200431 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100432 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000433}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900434#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000435
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000436/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600437 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000438
439void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100440_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000441{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200442 assert(is_tstate_valid(tstate));
443 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000444}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000446PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000447PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000448{
Victor Stinner09532fe2019-05-10 23:39:09 +0200449 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200450#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
451 PyThreadState *old_tstate = _PyThreadState_GET();
452 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
453#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200454 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200455#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100456 ensure_tstate_not_null(__func__, tstate);
457
Victor Stinner0b1e3302020-05-05 16:14:31 +0200458 struct _ceval_runtime_state *ceval = &runtime->ceval;
459 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200460 assert(gil_created(&ceval->gil));
Victor Stinner0b1e3302020-05-05 16:14:31 +0200461 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000463}
464
465void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000466PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000467{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100468 ensure_tstate_not_null(__func__, tstate);
469
Victor Stinner85f5a692020-03-09 22:12:04 +0100470 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100471
Victor Stinner85f5a692020-03-09 22:12:04 +0100472 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
473 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000474}
475
476
Guido van Rossuma9672091994-09-14 13:31:22 +0000477/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
478 signal handlers or Mac I/O completion routines) can schedule calls
479 to a function to be called synchronously.
480 The synchronous function is called with one void* argument.
481 It should return 0 for success or -1 for failure -- failure should
482 be accompanied by an exception.
483
484 If registry succeeds, the registry function returns 0; if it fails
485 (e.g. due to too many pending calls) it returns -1 (without setting
486 an exception condition).
487
488 Note that because registry may occur from within signal handlers,
489 or other asynchronous events, calling malloc() is unsafe!
490
Guido van Rossuma9672091994-09-14 13:31:22 +0000491 Any thread can schedule pending calls, but only the main thread
492 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493 There is no facility to schedule calls to a particular thread, but
494 that should be easy to change, should that ever be required. In
495 that case, the static variables here should go into the python
496 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000498
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200499void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200500_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200501{
502 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100503 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200504 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200505 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200506}
507
Eric Snow5be45a62019-03-08 22:47:07 -0700508/* Push one item onto the queue while holding the lock. */
509static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200510_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600511 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700512{
Eric Snow842a2f02019-03-15 15:47:51 -0600513 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700514 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600515 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700516 return -1; /* Queue full */
517 }
Eric Snow842a2f02019-03-15 15:47:51 -0600518 pending->calls[i].func = func;
519 pending->calls[i].arg = arg;
520 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700521 return 0;
522}
523
524/* Pop one item off the queue while holding the lock. */
525static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200526_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600527 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700528{
Eric Snow842a2f02019-03-15 15:47:51 -0600529 int i = pending->first;
530 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700531 return; /* Queue empty */
532 }
533
Eric Snow842a2f02019-03-15 15:47:51 -0600534 *func = pending->calls[i].func;
535 *arg = pending->calls[i].arg;
536 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700537}
538
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200539/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000540 scheduling to be made from any thread, and even from an executing
541 callback.
542 */
543
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200545_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200546 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000547{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200548 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600549
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200550 /* Ensure that _PyEval_InitPendingCalls() was called
551 and that _PyEval_FiniPendingCalls() is not called yet. */
552 assert(pending->lock != NULL);
553
Eric Snow842a2f02019-03-15 15:47:51 -0600554 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200555 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600556 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700557
Victor Stinnere225beb2019-06-03 18:14:24 +0200558 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200559 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000561}
562
Victor Stinner09532fe2019-05-10 23:39:09 +0200563int
564Py_AddPendingCall(int (*func)(void *), void *arg)
565{
Victor Stinner50e6e992020-03-19 02:41:21 +0100566 /* Best-effort to support subinterpreters and calls with the GIL released.
567
568 First attempt _PyThreadState_GET() since it supports subinterpreters.
569
570 If the GIL is released, _PyThreadState_GET() returns NULL . In this
571 case, use PyGILState_GetThisThreadState() which works even if the GIL
572 is released.
573
574 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
575 see bpo-10915 and bpo-15751.
576
Victor Stinner8849e592020-03-18 19:28:53 +0100577 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100578 PyThreadState *tstate = _PyThreadState_GET();
579 if (tstate == NULL) {
580 tstate = PyGILState_GetThisThreadState();
581 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200582
583 PyInterpreterState *interp;
584 if (tstate != NULL) {
585 interp = tstate->interp;
586 }
587 else {
588 /* Last resort: use the main interpreter */
589 interp = _PyRuntime.interpreters.main;
590 }
591 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200592}
593
Eric Snowfdf282d2019-01-11 14:26:55 -0700594static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100595handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700596{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200597 assert(is_tstate_valid(tstate));
598 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700599 return 0;
600 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700601
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200602 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100603 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
604 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200605 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700606 return -1;
607 }
608 return 0;
609}
610
611static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100612make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000613{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200614 assert(is_tstate_valid(tstate));
615
Victor Stinnerd8316882020-03-20 14:50:35 +0100616 /* only execute pending calls on main thread */
617 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200618 return 0;
619 }
620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100622 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700623 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700625 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200626 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100627
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200628 /* unsignal before starting to call callbacks, so that any callback
629 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200630 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700631 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100634 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700635 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700636 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 void *arg = NULL;
638
639 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600640 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200641 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600642 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700643
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100644 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700645 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100646 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700647 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700648 res = func(arg);
649 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200650 goto error;
651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200653
Charles-François Natalif23339a2011-07-23 18:15:43 +0200654 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700655 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200656
657error:
658 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200659 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700660 return res;
661}
662
Eric Snow842a2f02019-03-15 15:47:51 -0600663void
Victor Stinner2b1df452020-01-13 18:46:59 +0100664_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600665{
Eric Snow842a2f02019-03-15 15:47:51 -0600666 assert(PyGILState_Check());
667
Victor Stinner50e6e992020-03-19 02:41:21 +0100668 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200669
Eric Snow842a2f02019-03-15 15:47:51 -0600670 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
671 return;
672 }
673
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100674 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200675 PyObject *exc, *val, *tb;
676 _PyErr_Fetch(tstate, &exc, &val, &tb);
677 PyErr_BadInternalCall();
678 _PyErr_ChainExceptions(exc, val, tb);
679 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600680 }
681}
682
Eric Snowfdf282d2019-01-11 14:26:55 -0700683/* Py_MakePendingCalls() is a simple wrapper for the sake
684 of backward-compatibility. */
685int
686Py_MakePendingCalls(void)
687{
688 assert(PyGILState_Check());
689
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100690 PyThreadState *tstate = _PyThreadState_GET();
691
Eric Snowfdf282d2019-01-11 14:26:55 -0700692 /* Python signal handler doesn't really queue a callback: it only signals
693 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100694 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700695 if (res != 0) {
696 return res;
697 }
698
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100699 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600700 if (res != 0) {
701 return res;
702 }
703
704 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000705}
706
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000707/* The interpreter's recursion limit */
708
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000709#ifndef Py_DEFAULT_RECURSION_LIMIT
710#define Py_DEFAULT_RECURSION_LIMIT 1000
711#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600712
Eric Snow05351c12017-09-05 21:43:08 -0700713int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000714
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600715void
Victor Stinnerdab84232020-03-17 18:56:44 +0100716_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600717{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600718 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100719 _gil_initialize(&ceval->gil);
720}
721
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200722int
Victor Stinnerdab84232020-03-17 18:56:44 +0100723_PyEval_InitState(struct _ceval_state *ceval)
724{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200725 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
726
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200727 struct _pending_calls *pending = &ceval->pending;
728 assert(pending->lock == NULL);
729
730 pending->lock = PyThread_allocate_lock();
731 if (pending->lock == NULL) {
732 return -1;
733 }
734 return 0;
735}
736
737void
738_PyEval_FiniState(struct _ceval_state *ceval)
739{
740 struct _pending_calls *pending = &ceval->pending;
741 if (pending->lock != NULL) {
742 PyThread_free_lock(pending->lock);
743 pending->lock = NULL;
744 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600745}
746
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000747int
748Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000749{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200750 PyThreadState *tstate = _PyThreadState_GET();
751 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000752}
753
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000754void
755Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000756{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200757 PyThreadState *tstate = _PyThreadState_GET();
758 tstate->interp->ceval.recursion_limit = new_limit;
759 if (_Py_IsMainInterpreter(tstate)) {
760 _Py_CheckRecursionLimit = new_limit;
761 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000762}
763
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100764/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000765 if the recursion_depth reaches _Py_CheckRecursionLimit.
766 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
767 to guarantee that _Py_CheckRecursiveCall() is regularly called.
768 Without USE_STACKCHECK, there is no need for this. */
769int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100770_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000771{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200772 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000773
774#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700775 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 if (PyOS_CheckStack()) {
777 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200778 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return -1;
780 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200781 if (_Py_IsMainInterpreter(tstate)) {
782 /* Needed for ABI backwards-compatibility (see bpo-31857) */
783 _Py_CheckRecursionLimit = recursion_limit;
784 }
pdox18967932017-10-25 23:03:01 -0700785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (tstate->recursion_critical)
787 /* Somebody asked that we don't check for recursion. */
788 return 0;
789 if (tstate->overflowed) {
790 if (tstate->recursion_depth > recursion_limit + 50) {
791 /* Overflowing while handling an overflow. Give up. */
792 Py_FatalError("Cannot recover from stack overflow.");
793 }
794 return 0;
795 }
796 if (tstate->recursion_depth > recursion_limit) {
797 --tstate->recursion_depth;
798 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200799 _PyErr_Format(tstate, PyExc_RecursionError,
800 "maximum recursion depth exceeded%s",
801 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 return -1;
803 }
804 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000805}
806
Victor Stinner09532fe2019-05-10 23:39:09 +0200807static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200808static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000809
Victor Stinnere225beb2019-06-03 18:14:24 +0200810#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000811
Guido van Rossum374a9221991-04-04 10:40:29 +0000812
Guido van Rossumb209a111997-04-29 18:18:01 +0000813PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000814PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 return PyEval_EvalCodeEx(co,
817 globals, locals,
818 (PyObject **)NULL, 0,
819 (PyObject **)NULL, 0,
820 (PyObject **)NULL, 0,
821 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000822}
823
824
825/* Interpreter main loop */
826
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000827PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100828PyEval_EvalFrame(PyFrameObject *f)
829{
Victor Stinner0b72b232020-03-12 23:18:39 +0100830 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100831 PyThreadState *tstate = _PyThreadState_GET();
832 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000833}
834
835PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000836PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000837{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100838 PyThreadState *tstate = _PyThreadState_GET();
839 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700840}
841
Victor Stinnerda2914d2020-03-20 09:29:08 +0100842
843/* Handle signals, pending calls, GIL drop request
844 and asynchronous exception */
845static int
846eval_frame_handle_pending(PyThreadState *tstate)
847{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100848 _PyRuntimeState * const runtime = &_PyRuntime;
849 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200850
851 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200852 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100853 if (handle_signals(tstate) != 0) {
854 return -1;
855 }
856 }
857
858 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200859 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100860 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
861 if (make_pending_calls(tstate) != 0) {
862 return -1;
863 }
864 }
865
866 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200867 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100868 /* Give another thread a chance */
869 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
870 Py_FatalError("tstate mix-up");
871 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200872 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100873
874 /* Other threads may run now */
875
876 take_gil(tstate);
877
Victor Stinnere838a932020-05-05 19:56:48 +0200878#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
879 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
880#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100881 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
882 Py_FatalError("orphan tstate");
883 }
Victor Stinnere838a932020-05-05 19:56:48 +0200884#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100885 }
886
887 /* Check for asynchronous exception. */
888 if (tstate->async_exc != NULL) {
889 PyObject *exc = tstate->async_exc;
890 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200891 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100892 _PyErr_SetNone(tstate, exc);
893 Py_DECREF(exc);
894 return -1;
895 }
896
897 return 0;
898}
899
Victor Stinnerc6944e72016-11-11 02:13:35 +0100900PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100901_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700902{
Victor Stinner0b72b232020-03-12 23:18:39 +0100903 ensure_tstate_not_null(__func__, tstate);
904
Guido van Rossum950361c1997-01-24 13:49:28 +0000905#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000907#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200908 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300909 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200910 int opcode; /* Current opcode */
911 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200912 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100914 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100915 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 is true when the line being executed has changed. The
923 initial values are such as to make this false the first
924 time it is tested. */
925 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000926
Serhiy Storchakaab874002016-09-11 13:48:15 +0300927 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject *names;
929 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900930 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000931
Brett Cannon368b4b72012-04-02 12:17:59 -0400932#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200933 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400934#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200935
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936/* Computed GOTOs, or
937 the-optimization-commonly-but-improperly-known-as-"threaded code"
938 using gcc's labels-as-values extension
939 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
940
941 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000943 combined with a lookup table of jump addresses. However, since the
944 indirect jump instruction is shared by all opcodes, the CPU will have a
945 hard time making the right prediction for where to jump next (actually,
946 it will be always wrong except in the uncommon case of a sequence of
947 several identical opcodes).
948
949 "Threaded code" in contrast, uses an explicit jump table and an explicit
950 indirect jump instruction at the end of each opcode. Since the jump
951 instruction is at a different address for each opcode, the CPU will make a
952 separate prediction for each of these instructions, which is equivalent to
953 predicting the second opcode of each opcode pair. These predictions have
954 a much better chance to turn out valid, especially in small bytecode loops.
955
956 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000958 and potentially many more instructions (depending on the pipeline width).
959 A correctly predicted branch, however, is nearly free.
960
961 At the time of this writing, the "threaded code" version is up to 15-20%
962 faster than the normal "switch" version, depending on the compiler and the
963 CPU architecture.
964
965 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
966 because it would render the measurements invalid.
967
968
969 NOTE: care must be taken that the compiler doesn't try to "optimize" the
970 indirect jumps by sharing them between all opcodes. Such optimizations
971 can be disabled on gcc by using the -fno-gcse flag (or possibly
972 -fno-crossjumping).
973*/
974
Antoine Pitrou042b1282010-08-13 21:15:58 +0000975#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000976#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000977#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000978#endif
979
Antoine Pitrou042b1282010-08-13 21:15:58 +0000980#ifdef HAVE_COMPUTED_GOTOS
981 #ifndef USE_COMPUTED_GOTOS
982 #define USE_COMPUTED_GOTOS 1
983 #endif
984#else
985 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
986 #error "Computed gotos are not supported on this compiler."
987 #endif
988 #undef USE_COMPUTED_GOTOS
989 #define USE_COMPUTED_GOTOS 0
990#endif
991
992#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000993/* Import the static jump table */
994#include "opcode_targets.h"
995
Antoine Pitroub52ec782009-01-25 16:34:23 +0000996#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700997 op: \
998 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000999
Antoine Pitroub52ec782009-01-25 16:34:23 +00001000#ifdef LLTRACE
1001#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001003 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001005 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001006 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 } \
1008 goto fast_next_opcode; \
1009 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001010#else
1011#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001013 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001015 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001016 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 } \
1018 goto fast_next_opcode; \
1019 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020#endif
1021
Victor Stinner09532fe2019-05-10 23:39:09 +02001022#define DISPATCH() \
1023 { \
1024 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1025 FAST_DISPATCH(); \
1026 } \
1027 continue; \
1028 }
1029
Antoine Pitroub52ec782009-01-25 16:34:23 +00001030#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001031#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001032#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001033#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001034#endif
1035
1036
Neal Norwitza81d2202002-07-14 00:27:26 +00001037/* Tuple access macros */
1038
1039#ifndef Py_DEBUG
1040#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1041#else
1042#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1043#endif
1044
Guido van Rossum374a9221991-04-04 10:40:29 +00001045/* Code access macros */
1046
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001047/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001048#define INSTR_OFFSET() \
1049 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001050#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001051 _Py_CODEUNIT word = *next_instr; \
1052 opcode = _Py_OPCODE(word); \
1053 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001054 next_instr++; \
1055 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001056#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1057#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001058
Raymond Hettingerf606f872003-03-16 03:11:04 +00001059/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 Some opcodes tend to come in pairs thus making it possible to
1061 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001062 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 Verifying the prediction costs a single high-speed test of a register
1065 variable against a constant. If the pairing was good, then the
1066 processor's own internal branch predication has a high likelihood of
1067 success, resulting in a nearly zero-overhead transition to the
1068 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001069 including its unpredictable switch-case branch. Combined with the
1070 processor's internal branch prediction, a successful PREDICT has the
1071 effect of making the two opcodes run as if they were a single new opcode
1072 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001073
Georg Brandl86b2fb92008-07-16 03:43:04 +00001074 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 predictions turned-on and interpret the results as if some opcodes
1076 had been combined or turn-off predictions so that the opcode frequency
1077 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001078
1079 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 the CPU to record separate branch prediction information for each
1081 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001082
Raymond Hettingerf606f872003-03-16 03:11:04 +00001083*/
1084
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001085#define PREDICT_ID(op) PRED_##op
1086
Antoine Pitrou042b1282010-08-13 21:15:58 +00001087#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001088#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001089#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001090#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001091 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001092 _Py_CODEUNIT word = *next_instr; \
1093 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001094 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001095 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001096 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001097 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001098 } \
1099 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001100#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001101#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001102
Raymond Hettingerf606f872003-03-16 03:11:04 +00001103
Guido van Rossum374a9221991-04-04 10:40:29 +00001104/* Stack manipulation macros */
1105
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106/* The stack can grow at most MAXINT deep, as co_nlocals and
1107 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001108#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1109#define EMPTY() (STACK_LEVEL() == 0)
1110#define TOP() (stack_pointer[-1])
1111#define SECOND() (stack_pointer[-2])
1112#define THIRD() (stack_pointer[-3])
1113#define FOURTH() (stack_pointer[-4])
1114#define PEEK(n) (stack_pointer[-(n)])
1115#define SET_TOP(v) (stack_pointer[-1] = (v))
1116#define SET_SECOND(v) (stack_pointer[-2] = (v))
1117#define SET_THIRD(v) (stack_pointer[-3] = (v))
1118#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1119#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1120#define BASIC_STACKADJ(n) (stack_pointer += n)
1121#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1122#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001123
Guido van Rossum96a42c81992-01-12 02:29:51 +00001124#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001126 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001127 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001128#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001129 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001130#define STACK_GROW(n) do { \
1131 assert(n >= 0); \
1132 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001133 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001134 assert(STACK_LEVEL() <= co->co_stacksize); \
1135 } while (0)
1136#define STACK_SHRINK(n) do { \
1137 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001138 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001139 (void)(BASIC_STACKADJ(-n)); \
1140 assert(STACK_LEVEL() <= co->co_stacksize); \
1141 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001142#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001143 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001144 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001145#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001146#define PUSH(v) BASIC_PUSH(v)
1147#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001148#define STACK_GROW(n) BASIC_STACKADJ(n)
1149#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001150#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001151#endif
1152
Guido van Rossum681d79a1995-07-18 14:51:37 +00001153/* Local variable macros */
1154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001156
1157/* The SETLOCAL() macro must not DECREF the local variable in-place and
1158 then store the new value; it must copy the old value to a temporary
1159 value, then store the new value, and then DECREF the temporary value.
1160 This is because it is possible that during the DECREF the frame is
1161 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1162 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001164 GETLOCAL(i) = value; \
1165 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001166
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001167
1168#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 while (STACK_LEVEL() > (b)->b_level) { \
1170 PyObject *v = POP(); \
1171 Py_XDECREF(v); \
1172 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001173
1174#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001175 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001177 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1179 while (STACK_LEVEL() > (b)->b_level + 3) { \
1180 value = POP(); \
1181 Py_XDECREF(value); \
1182 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001183 exc_info = tstate->exc_info; \
1184 type = exc_info->exc_type; \
1185 value = exc_info->exc_value; \
1186 traceback = exc_info->exc_traceback; \
1187 exc_info->exc_type = POP(); \
1188 exc_info->exc_value = POP(); \
1189 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_XDECREF(type); \
1191 Py_XDECREF(value); \
1192 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001193 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001194
Inada Naoki91234a12019-06-03 21:30:58 +09001195 /* macros for opcode cache */
1196#define OPCACHE_CHECK() \
1197 do { \
1198 co_opcache = NULL; \
1199 if (co->co_opcache != NULL) { \
1200 unsigned char co_opt_offset = \
1201 co->co_opcache_map[next_instr - first_instr]; \
1202 if (co_opt_offset > 0) { \
1203 assert(co_opt_offset <= co->co_opcache_size); \
1204 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1205 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001206 } \
1207 } \
1208 } while (0)
1209
1210#if OPCACHE_STATS
1211
1212#define OPCACHE_STAT_GLOBAL_HIT() \
1213 do { \
1214 if (co->co_opcache != NULL) opcache_global_hits++; \
1215 } while (0)
1216
1217#define OPCACHE_STAT_GLOBAL_MISS() \
1218 do { \
1219 if (co->co_opcache != NULL) opcache_global_misses++; \
1220 } while (0)
1221
1222#define OPCACHE_STAT_GLOBAL_OPT() \
1223 do { \
1224 if (co->co_opcache != NULL) opcache_global_opts++; \
1225 } while (0)
1226
1227#else /* OPCACHE_STATS */
1228
1229#define OPCACHE_STAT_GLOBAL_HIT()
1230#define OPCACHE_STAT_GLOBAL_MISS()
1231#define OPCACHE_STAT_GLOBAL_OPT()
1232
1233#endif
1234
Guido van Rossuma027efa1997-05-05 20:56:21 +00001235/* Start of code */
1236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001238 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001240 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (tstate->use_tracing) {
1245 if (tstate->c_tracefunc != NULL) {
1246 /* tstate->c_tracefunc, if defined, is a
1247 function that will be called on *every* entry
1248 to a code block. Its return value, if not
1249 None, is a function that will be called at
1250 the start of each executed line of code.
1251 (Actually, the function must return itself
1252 in order to continue tracing.) The trace
1253 functions are called with three arguments:
1254 a pointer to the current frame, a string
1255 indicating why the function is called, and
1256 an argument which depends on the situation.
1257 The global trace function is also called
1258 whenever an exception is detected. */
1259 if (call_trace_protected(tstate->c_tracefunc,
1260 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001261 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 /* Trace function raised an error */
1263 goto exit_eval_frame;
1264 }
1265 }
1266 if (tstate->c_profilefunc != NULL) {
1267 /* Similar for c_profilefunc, except it needn't
1268 return itself and isn't called for "line" events */
1269 if (call_trace_protected(tstate->c_profilefunc,
1270 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001271 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 /* Profile function raised an error */
1273 goto exit_eval_frame;
1274 }
1275 }
1276 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001277
Łukasz Langaa785c872016-09-09 17:37:37 -07001278 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1279 dtrace_function_entry(f);
1280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 co = f->f_code;
1282 names = co->co_names;
1283 consts = co->co_consts;
1284 fastlocals = f->f_localsplus;
1285 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001286 assert(PyBytes_Check(co->co_code));
1287 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001288 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1289 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1290 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001291 /*
1292 f->f_lasti refers to the index of the last instruction,
1293 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001295 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001296 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 When the PREDICT() macros are enabled, some opcode pairs follow in
1299 direct succession without updating f->f_lasti. A successful
1300 prediction effectively links the two codes together as if they
1301 were a single new opcode; accordingly,f->f_lasti will point to
1302 the first code in the pair (for instance, GET_ITER followed by
1303 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001304 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001306 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001307 next_instr = first_instr;
1308 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001309 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1310 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 stack_pointer = f->f_stacktop;
1313 assert(stack_pointer != NULL);
1314 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001315 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001316
Inada Naoki91234a12019-06-03 21:30:58 +09001317 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1318 co->co_opcache_flag++;
1319 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1320 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001321 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001322 }
1323#if OPCACHE_STATS
1324 opcache_code_objects_extra_mem +=
1325 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1326 sizeof(_PyOpcache) * co->co_opcache_size;
1327 opcache_code_objects++;
1328#endif
1329 }
1330 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001331
Tim Peters5ca576e2001-06-18 22:08:13 +00001332#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001333 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001334#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 if (throwflag) /* support for generator.throw() */
1337 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001338
Victor Stinnerace47d72013-07-18 01:41:08 +02001339#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001340 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001341 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001342 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001343 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001344#endif
1345
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001346main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1349 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001350 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* Do periodic things. Doing this every time through
1353 the loop would add too much overhead, so we do it
1354 only every Nth instruction. We also do it if
1355 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1356 event needs attention (e.g. a signal handler or
1357 async I/O handler); see Py_AddPendingCall() and
1358 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Eric Snow7bda9de2019-03-08 17:25:54 -07001360 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001361 opcode = _Py_OPCODE(*next_instr);
1362 if (opcode == SETUP_FINALLY ||
1363 opcode == SETUP_WITH ||
1364 opcode == BEFORE_ASYNC_WITH ||
1365 opcode == YIELD_FROM) {
1366 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001367 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001368 - If we're about to enter the 'with:'. It will prevent
1369 emitting a resource warning in the common idiom
1370 'with open(path) as file:'.
1371 - If we're about to enter the 'async with:'.
1372 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001373 *very* useful, but might help in some cases and it's
1374 traditional)
1375 - If we're resuming a chain of nested 'yield from' or
1376 'await' calls, then each frame is parked with YIELD_FROM
1377 as its next opcode. If the user hit control-C we want to
1378 wait until we've reached the innermost frame before
1379 running the signal handler and raising KeyboardInterrupt
1380 (see bpo-30039).
1381 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 goto fast_next_opcode;
1383 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001384
Victor Stinnerda2914d2020-03-20 09:29:08 +01001385 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 }
1388 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 fast_next_opcode:
1391 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Łukasz Langaa785c872016-09-09 17:37:37 -07001393 if (PyDTrace_LINE_ENABLED())
1394 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001397
Victor Stinnerdab84232020-03-17 18:56:44 +01001398 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001399 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* see maybe_call_line_trace
1402 for expository comments */
1403 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 err = maybe_call_line_trace(tstate->c_tracefunc,
1406 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001407 tstate, f,
1408 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* Reload possibly changed frame fields */
1410 JUMPTO(f->f_lasti);
1411 if (f->f_stacktop != NULL) {
1412 stack_pointer = f->f_stacktop;
1413 f->f_stacktop = NULL;
1414 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001421
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001422 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001423 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001424#ifdef DYNAMIC_EXECUTION_PROFILE
1425#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 dxpairs[lastopcode][opcode]++;
1427 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001428#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001430#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001431
Guido van Rossum96a42c81992-01-12 02:29:51 +00001432#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (lltrace) {
1436 if (HAS_ARG(opcode)) {
1437 printf("%d: %d, %d\n",
1438 f->f_lasti, opcode, oparg);
1439 }
1440 else {
1441 printf("%d: %d\n",
1442 f->f_lasti, opcode);
1443 }
1444 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001445#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001450 It is essential that any operation that fails must goto error
1451 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Benjamin Petersonddd19492018-09-16 22:38:02 -07001453 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001455 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 PyObject *value = GETLOCAL(oparg);
1459 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001460 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 UNBOUNDLOCAL_ERROR_MSG,
1462 PyTuple_GetItem(co->co_varnames, oparg));
1463 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 Py_INCREF(value);
1466 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 }
1469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(LOAD_CONST): {
1471 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 PyObject *value = GETITEM(consts, oparg);
1473 Py_INCREF(value);
1474 PUSH(value);
1475 FAST_DISPATCH();
1476 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001477
Benjamin Petersonddd19492018-09-16 22:38:02 -07001478 case TARGET(STORE_FAST): {
1479 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 PyObject *value = POP();
1481 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001484
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 PyObject *value = POP();
1487 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Benjamin Petersonddd19492018-09-16 22:38:02 -07001491 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001492 PyObject *top = TOP();
1493 PyObject *second = SECOND();
1494 SET_TOP(second);
1495 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Petersonddd19492018-09-16 22:38:02 -07001499 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 PyObject *top = TOP();
1501 PyObject *second = SECOND();
1502 PyObject *third = THIRD();
1503 SET_TOP(second);
1504 SET_SECOND(third);
1505 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Petersonddd19492018-09-16 22:38:02 -07001509 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001510 PyObject *top = TOP();
1511 PyObject *second = SECOND();
1512 PyObject *third = THIRD();
1513 PyObject *fourth = FOURTH();
1514 SET_TOP(second);
1515 SET_SECOND(third);
1516 SET_THIRD(fourth);
1517 SET_FOURTH(top);
1518 FAST_DISPATCH();
1519 }
1520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *top = TOP();
1523 Py_INCREF(top);
1524 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001531 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001533 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 SET_TOP(top);
1535 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001536 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 PyObject *value = TOP();
1541 PyObject *res = PyNumber_Positive(value);
1542 Py_DECREF(value);
1543 SET_TOP(res);
1544 if (res == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *value = TOP();
1551 PyObject *res = PyNumber_Negative(value);
1552 Py_DECREF(value);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Petersonddd19492018-09-16 22:38:02 -07001559 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 PyObject *value = TOP();
1561 int err = PyObject_IsTrue(value);
1562 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (err == 0) {
1564 Py_INCREF(Py_True);
1565 SET_TOP(Py_True);
1566 DISPATCH();
1567 }
1568 else if (err > 0) {
1569 Py_INCREF(Py_False);
1570 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 DISPATCH();
1572 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001573 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 goto error;
1575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Benjamin Petersonddd19492018-09-16 22:38:02 -07001577 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 PyObject *value = TOP();
1579 PyObject *res = PyNumber_Invert(value);
1580 Py_DECREF(value);
1581 SET_TOP(res);
1582 if (res == NULL)
1583 goto error;
1584 DISPATCH();
1585 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Benjamin Petersonddd19492018-09-16 22:38:02 -07001587 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001588 PyObject *exp = POP();
1589 PyObject *base = TOP();
1590 PyObject *res = PyNumber_Power(base, exp, Py_None);
1591 Py_DECREF(base);
1592 Py_DECREF(exp);
1593 SET_TOP(res);
1594 if (res == NULL)
1595 goto error;
1596 DISPATCH();
1597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Benjamin Petersonddd19492018-09-16 22:38:02 -07001599 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001600 PyObject *right = POP();
1601 PyObject *left = TOP();
1602 PyObject *res = PyNumber_Multiply(left, right);
1603 Py_DECREF(left);
1604 Py_DECREF(right);
1605 SET_TOP(res);
1606 if (res == NULL)
1607 goto error;
1608 DISPATCH();
1609 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Benjamin Petersonddd19492018-09-16 22:38:02 -07001611 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001612 PyObject *right = POP();
1613 PyObject *left = TOP();
1614 PyObject *res = PyNumber_MatrixMultiply(left, right);
1615 Py_DECREF(left);
1616 Py_DECREF(right);
1617 SET_TOP(res);
1618 if (res == NULL)
1619 goto error;
1620 DISPATCH();
1621 }
1622
Benjamin Petersonddd19492018-09-16 22:38:02 -07001623 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 PyObject *divisor = POP();
1625 PyObject *dividend = TOP();
1626 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1627 Py_DECREF(dividend);
1628 Py_DECREF(divisor);
1629 SET_TOP(quotient);
1630 if (quotient == NULL)
1631 goto error;
1632 DISPATCH();
1633 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001634
Benjamin Petersonddd19492018-09-16 22:38:02 -07001635 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001636 PyObject *divisor = POP();
1637 PyObject *dividend = TOP();
1638 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1639 Py_DECREF(dividend);
1640 Py_DECREF(divisor);
1641 SET_TOP(quotient);
1642 if (quotient == NULL)
1643 goto error;
1644 DISPATCH();
1645 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001646
Benjamin Petersonddd19492018-09-16 22:38:02 -07001647 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 PyObject *divisor = POP();
1649 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001650 PyObject *res;
1651 if (PyUnicode_CheckExact(dividend) && (
1652 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1653 // fast path; string formatting, but not if the RHS is a str subclass
1654 // (see issue28598)
1655 res = PyUnicode_Format(dividend, divisor);
1656 } else {
1657 res = PyNumber_Remainder(dividend, divisor);
1658 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 Py_DECREF(divisor);
1660 Py_DECREF(dividend);
1661 SET_TOP(res);
1662 if (res == NULL)
1663 goto error;
1664 DISPATCH();
1665 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *right = POP();
1669 PyObject *left = TOP();
1670 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001671 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1672 CPython using bytecode, it is simply worthless.
1673 See http://bugs.python.org/issue21955 and
1674 http://bugs.python.org/issue10044 for the discussion. In short,
1675 no patch shown any impact on a realistic benchmark, only a minor
1676 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 if (PyUnicode_CheckExact(left) &&
1678 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001679 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001680 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001681 }
1682 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 sum = PyNumber_Add(left, right);
1684 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001685 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 Py_DECREF(right);
1687 SET_TOP(sum);
1688 if (sum == NULL)
1689 goto error;
1690 DISPATCH();
1691 }
1692
Benjamin Petersonddd19492018-09-16 22:38:02 -07001693 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 PyObject *right = POP();
1695 PyObject *left = TOP();
1696 PyObject *diff = PyNumber_Subtract(left, right);
1697 Py_DECREF(right);
1698 Py_DECREF(left);
1699 SET_TOP(diff);
1700 if (diff == NULL)
1701 goto error;
1702 DISPATCH();
1703 }
1704
Benjamin Petersonddd19492018-09-16 22:38:02 -07001705 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 PyObject *sub = POP();
1707 PyObject *container = TOP();
1708 PyObject *res = PyObject_GetItem(container, sub);
1709 Py_DECREF(container);
1710 Py_DECREF(sub);
1711 SET_TOP(res);
1712 if (res == NULL)
1713 goto error;
1714 DISPATCH();
1715 }
1716
Benjamin Petersonddd19492018-09-16 22:38:02 -07001717 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 PyObject *right = POP();
1719 PyObject *left = TOP();
1720 PyObject *res = PyNumber_Lshift(left, right);
1721 Py_DECREF(left);
1722 Py_DECREF(right);
1723 SET_TOP(res);
1724 if (res == NULL)
1725 goto error;
1726 DISPATCH();
1727 }
1728
Benjamin Petersonddd19492018-09-16 22:38:02 -07001729 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 PyObject *right = POP();
1731 PyObject *left = TOP();
1732 PyObject *res = PyNumber_Rshift(left, right);
1733 Py_DECREF(left);
1734 Py_DECREF(right);
1735 SET_TOP(res);
1736 if (res == NULL)
1737 goto error;
1738 DISPATCH();
1739 }
1740
Benjamin Petersonddd19492018-09-16 22:38:02 -07001741 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 PyObject *right = POP();
1743 PyObject *left = TOP();
1744 PyObject *res = PyNumber_And(left, right);
1745 Py_DECREF(left);
1746 Py_DECREF(right);
1747 SET_TOP(res);
1748 if (res == NULL)
1749 goto error;
1750 DISPATCH();
1751 }
1752
Benjamin Petersonddd19492018-09-16 22:38:02 -07001753 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 PyObject *right = POP();
1755 PyObject *left = TOP();
1756 PyObject *res = PyNumber_Xor(left, right);
1757 Py_DECREF(left);
1758 Py_DECREF(right);
1759 SET_TOP(res);
1760 if (res == NULL)
1761 goto error;
1762 DISPATCH();
1763 }
1764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 PyObject *right = POP();
1767 PyObject *left = TOP();
1768 PyObject *res = PyNumber_Or(left, right);
1769 Py_DECREF(left);
1770 Py_DECREF(right);
1771 SET_TOP(res);
1772 if (res == NULL)
1773 goto error;
1774 DISPATCH();
1775 }
1776
Benjamin Petersonddd19492018-09-16 22:38:02 -07001777 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 PyObject *v = POP();
1779 PyObject *list = PEEK(oparg);
1780 int err;
1781 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 if (err != 0)
1784 goto error;
1785 PREDICT(JUMP_ABSOLUTE);
1786 DISPATCH();
1787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Petersonddd19492018-09-16 22:38:02 -07001789 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001791 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 int err;
1793 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 if (err != 0)
1796 goto error;
1797 PREDICT(JUMP_ABSOLUTE);
1798 DISPATCH();
1799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Benjamin Petersonddd19492018-09-16 22:38:02 -07001801 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 PyObject *exp = POP();
1803 PyObject *base = TOP();
1804 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1805 Py_DECREF(base);
1806 Py_DECREF(exp);
1807 SET_TOP(res);
1808 if (res == NULL)
1809 goto error;
1810 DISPATCH();
1811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Benjamin Petersonddd19492018-09-16 22:38:02 -07001813 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 PyObject *right = POP();
1815 PyObject *left = TOP();
1816 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1817 Py_DECREF(left);
1818 Py_DECREF(right);
1819 SET_TOP(res);
1820 if (res == NULL)
1821 goto error;
1822 DISPATCH();
1823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Benjamin Petersonddd19492018-09-16 22:38:02 -07001825 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001826 PyObject *right = POP();
1827 PyObject *left = TOP();
1828 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1829 Py_DECREF(left);
1830 Py_DECREF(right);
1831 SET_TOP(res);
1832 if (res == NULL)
1833 goto error;
1834 DISPATCH();
1835 }
1836
Benjamin Petersonddd19492018-09-16 22:38:02 -07001837 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 PyObject *divisor = POP();
1839 PyObject *dividend = TOP();
1840 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1841 Py_DECREF(dividend);
1842 Py_DECREF(divisor);
1843 SET_TOP(quotient);
1844 if (quotient == NULL)
1845 goto error;
1846 DISPATCH();
1847 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001848
Benjamin Petersonddd19492018-09-16 22:38:02 -07001849 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001850 PyObject *divisor = POP();
1851 PyObject *dividend = TOP();
1852 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1853 Py_DECREF(dividend);
1854 Py_DECREF(divisor);
1855 SET_TOP(quotient);
1856 if (quotient == NULL)
1857 goto error;
1858 DISPATCH();
1859 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Benjamin Petersonddd19492018-09-16 22:38:02 -07001861 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 PyObject *right = POP();
1863 PyObject *left = TOP();
1864 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1865 Py_DECREF(left);
1866 Py_DECREF(right);
1867 SET_TOP(mod);
1868 if (mod == NULL)
1869 goto error;
1870 DISPATCH();
1871 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001872
Benjamin Petersonddd19492018-09-16 22:38:02 -07001873 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 PyObject *right = POP();
1875 PyObject *left = TOP();
1876 PyObject *sum;
1877 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001878 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001879 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001880 }
1881 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 sum = PyNumber_InPlaceAdd(left, right);
1883 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001884 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 Py_DECREF(right);
1886 SET_TOP(sum);
1887 if (sum == NULL)
1888 goto error;
1889 DISPATCH();
1890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Benjamin Petersonddd19492018-09-16 22:38:02 -07001892 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 PyObject *right = POP();
1894 PyObject *left = TOP();
1895 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1896 Py_DECREF(left);
1897 Py_DECREF(right);
1898 SET_TOP(diff);
1899 if (diff == NULL)
1900 goto error;
1901 DISPATCH();
1902 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001903
Benjamin Petersonddd19492018-09-16 22:38:02 -07001904 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 PyObject *right = POP();
1906 PyObject *left = TOP();
1907 PyObject *res = PyNumber_InPlaceLshift(left, right);
1908 Py_DECREF(left);
1909 Py_DECREF(right);
1910 SET_TOP(res);
1911 if (res == NULL)
1912 goto error;
1913 DISPATCH();
1914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 PyObject *right = POP();
1918 PyObject *left = TOP();
1919 PyObject *res = PyNumber_InPlaceRshift(left, right);
1920 Py_DECREF(left);
1921 Py_DECREF(right);
1922 SET_TOP(res);
1923 if (res == NULL)
1924 goto error;
1925 DISPATCH();
1926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *right = POP();
1930 PyObject *left = TOP();
1931 PyObject *res = PyNumber_InPlaceAnd(left, right);
1932 Py_DECREF(left);
1933 Py_DECREF(right);
1934 SET_TOP(res);
1935 if (res == NULL)
1936 goto error;
1937 DISPATCH();
1938 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Benjamin Petersonddd19492018-09-16 22:38:02 -07001940 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyObject *right = POP();
1942 PyObject *left = TOP();
1943 PyObject *res = PyNumber_InPlaceXor(left, right);
1944 Py_DECREF(left);
1945 Py_DECREF(right);
1946 SET_TOP(res);
1947 if (res == NULL)
1948 goto error;
1949 DISPATCH();
1950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Benjamin Petersonddd19492018-09-16 22:38:02 -07001952 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *right = POP();
1954 PyObject *left = TOP();
1955 PyObject *res = PyNumber_InPlaceOr(left, right);
1956 Py_DECREF(left);
1957 Py_DECREF(right);
1958 SET_TOP(res);
1959 if (res == NULL)
1960 goto error;
1961 DISPATCH();
1962 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001963
Benjamin Petersonddd19492018-09-16 22:38:02 -07001964 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 PyObject *sub = TOP();
1966 PyObject *container = SECOND();
1967 PyObject *v = THIRD();
1968 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001969 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001970 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 Py_DECREF(container);
1974 Py_DECREF(sub);
1975 if (err != 0)
1976 goto error;
1977 DISPATCH();
1978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Benjamin Petersonddd19492018-09-16 22:38:02 -07001980 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 PyObject *sub = TOP();
1982 PyObject *container = SECOND();
1983 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001984 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001985 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 err = PyObject_DelItem(container, sub);
1987 Py_DECREF(container);
1988 Py_DECREF(sub);
1989 if (err != 0)
1990 goto error;
1991 DISPATCH();
1992 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001995 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001997 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001998 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002000 _PyErr_SetString(tstate, PyExc_RuntimeError,
2001 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 Py_DECREF(value);
2003 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002005 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 Py_DECREF(value);
2007 if (res == NULL)
2008 goto error;
2009 Py_DECREF(res);
2010 DISPATCH();
2011 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002012
Benjamin Petersonddd19492018-09-16 22:38:02 -07002013 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 switch (oparg) {
2016 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002018 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002021 /* fall through */
2022 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002023 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002024 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 break;
2027 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002028 _PyErr_SetString(tstate, PyExc_SystemError,
2029 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 break;
2031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002032 goto error;
2033 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Benjamin Petersonddd19492018-09-16 22:38:02 -07002035 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002037 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002038 assert(EMPTY());
2039 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002041
Benjamin Petersonddd19492018-09-16 22:38:02 -07002042 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002043 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002044 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002045 PyObject *obj = TOP();
2046 PyTypeObject *type = Py_TYPE(obj);
2047
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002048 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002049 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002050 }
Yury Selivanov75445082015-05-11 22:57:16 -04002051
2052 if (getter != NULL) {
2053 iter = (*getter)(obj);
2054 Py_DECREF(obj);
2055 if (iter == NULL) {
2056 SET_TOP(NULL);
2057 goto error;
2058 }
2059 }
2060 else {
2061 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002062 _PyErr_Format(tstate, PyExc_TypeError,
2063 "'async for' requires an object with "
2064 "__aiter__ method, got %.100s",
2065 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002066 Py_DECREF(obj);
2067 goto error;
2068 }
2069
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002070 if (Py_TYPE(iter)->tp_as_async == NULL ||
2071 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002072
Yury Selivanov398ff912017-03-02 22:20:00 -05002073 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002074 _PyErr_Format(tstate, PyExc_TypeError,
2075 "'async for' received an object from __aiter__ "
2076 "that does not implement __anext__: %.100s",
2077 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002078 Py_DECREF(iter);
2079 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002080 }
2081
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002082 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002083 DISPATCH();
2084 }
2085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002087 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002088 PyObject *next_iter = NULL;
2089 PyObject *awaitable = NULL;
2090 PyObject *aiter = TOP();
2091 PyTypeObject *type = Py_TYPE(aiter);
2092
Yury Selivanoveb636452016-09-08 22:01:51 -07002093 if (PyAsyncGen_CheckExact(aiter)) {
2094 awaitable = type->tp_as_async->am_anext(aiter);
2095 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002096 goto error;
2097 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002098 } else {
2099 if (type->tp_as_async != NULL){
2100 getter = type->tp_as_async->am_anext;
2101 }
Yury Selivanov75445082015-05-11 22:57:16 -04002102
Yury Selivanoveb636452016-09-08 22:01:51 -07002103 if (getter != NULL) {
2104 next_iter = (*getter)(aiter);
2105 if (next_iter == NULL) {
2106 goto error;
2107 }
2108 }
2109 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002110 _PyErr_Format(tstate, PyExc_TypeError,
2111 "'async for' requires an iterator with "
2112 "__anext__ method, got %.100s",
2113 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002114 goto error;
2115 }
Yury Selivanov75445082015-05-11 22:57:16 -04002116
Yury Selivanoveb636452016-09-08 22:01:51 -07002117 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2118 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002119 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002120 PyExc_TypeError,
2121 "'async for' received an invalid object "
2122 "from __anext__: %.100s",
2123 Py_TYPE(next_iter)->tp_name);
2124
2125 Py_DECREF(next_iter);
2126 goto error;
2127 } else {
2128 Py_DECREF(next_iter);
2129 }
2130 }
Yury Selivanov75445082015-05-11 22:57:16 -04002131
2132 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002133 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002134 DISPATCH();
2135 }
2136
Benjamin Petersonddd19492018-09-16 22:38:02 -07002137 case TARGET(GET_AWAITABLE): {
2138 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002139 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002140 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002141
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002142 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002143 int opcode_at_minus_3 = 0;
2144 if ((next_instr - first_instr) > 2) {
2145 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2146 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002147 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002148 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002149 _Py_OPCODE(next_instr[-2]));
2150 }
2151
Yury Selivanov75445082015-05-11 22:57:16 -04002152 Py_DECREF(iterable);
2153
Yury Selivanovc724bae2016-03-02 11:30:46 -05002154 if (iter != NULL && PyCoro_CheckExact(iter)) {
2155 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2156 if (yf != NULL) {
2157 /* `iter` is a coroutine object that is being
2158 awaited, `yf` is a pointer to the current awaitable
2159 being awaited on. */
2160 Py_DECREF(yf);
2161 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002162 _PyErr_SetString(tstate, PyExc_RuntimeError,
2163 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002164 /* The code below jumps to `error` if `iter` is NULL. */
2165 }
2166 }
2167
Yury Selivanov75445082015-05-11 22:57:16 -04002168 SET_TOP(iter); /* Even if it's NULL */
2169
2170 if (iter == NULL) {
2171 goto error;
2172 }
2173
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002174 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002175 DISPATCH();
2176 }
2177
Benjamin Petersonddd19492018-09-16 22:38:02 -07002178 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002180 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002182 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2183 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002184 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002185 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002187 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002188 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002189 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002190 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 Py_DECREF(v);
2192 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002193 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002194 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002195 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002196 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002197 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 if (err < 0)
2199 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002200 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 SET_TOP(val);
2202 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002203 }
Martin Panter95f53c12016-07-18 08:23:26 +00002204 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002205 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002206 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002207 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002208 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002209 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002211
Benjamin Petersonddd19492018-09-16 22:38:02 -07002212 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002214
2215 if (co->co_flags & CO_ASYNC_GENERATOR) {
2216 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2217 Py_DECREF(retval);
2218 if (w == NULL) {
2219 retval = NULL;
2220 goto error;
2221 }
2222 retval = w;
2223 }
2224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002226 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002228
Benjamin Petersonddd19492018-09-16 22:38:02 -07002229 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002230 PyObject *type, *value, *traceback;
2231 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 PyTryBlock *b = PyFrame_BlockPop(f);
2233 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002234 _PyErr_SetString(tstate, PyExc_SystemError,
2235 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002238 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2239 STACK_LEVEL() <= (b)->b_level + 4);
2240 exc_info = tstate->exc_info;
2241 type = exc_info->exc_type;
2242 value = exc_info->exc_value;
2243 traceback = exc_info->exc_traceback;
2244 exc_info->exc_type = POP();
2245 exc_info->exc_value = POP();
2246 exc_info->exc_traceback = POP();
2247 Py_XDECREF(type);
2248 Py_XDECREF(value);
2249 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002252
Benjamin Petersonddd19492018-09-16 22:38:02 -07002253 case TARGET(POP_BLOCK): {
2254 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002255 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002258
Mark Shannonfee55262019-11-21 09:11:43 +00002259 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002260 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002261 PyObject *val = POP();
2262 PyObject *tb = POP();
2263 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002264 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002265 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002267
Benjamin Petersonddd19492018-09-16 22:38:02 -07002268 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002269 PyObject *exc = POP();
2270 assert(PyExceptionClass_Check(exc));
2271 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2272 PyTryBlock *b = PyFrame_BlockPop(f);
2273 assert(b->b_type == EXCEPT_HANDLER);
2274 Py_DECREF(exc);
2275 UNWIND_EXCEPT_HANDLER(b);
2276 Py_DECREF(POP());
2277 JUMPBY(oparg);
2278 FAST_DISPATCH();
2279 }
2280 else {
2281 PyObject *val = POP();
2282 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002283 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002284 goto exception_unwind;
2285 }
2286 }
2287
Zackery Spytzce6a0702019-08-25 03:44:09 -06002288 case TARGET(LOAD_ASSERTION_ERROR): {
2289 PyObject *value = PyExc_AssertionError;
2290 Py_INCREF(value);
2291 PUSH(value);
2292 FAST_DISPATCH();
2293 }
2294
Benjamin Petersonddd19492018-09-16 22:38:02 -07002295 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002296 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002299 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002300 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002302 if (!_PyErr_Occurred(tstate)) {
2303 _PyErr_SetString(tstate, PyExc_NameError,
2304 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002309 }
2310 else {
2311 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2312 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002313 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2315 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002316 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2317 _PyErr_SetString(tstate, PyExc_NameError,
2318 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002323 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Petersonddd19492018-09-16 22:38:02 -07002326 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 PyObject *name = GETITEM(names, oparg);
2328 PyObject *v = POP();
2329 PyObject *ns = f->f_locals;
2330 int err;
2331 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002332 _PyErr_Format(tstate, PyExc_SystemError,
2333 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 if (PyDict_CheckExact(ns))
2338 err = PyDict_SetItem(ns, name, v);
2339 else
2340 err = PyObject_SetItem(ns, name, v);
2341 Py_DECREF(v);
2342 if (err != 0)
2343 goto error;
2344 DISPATCH();
2345 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002346
Benjamin Petersonddd19492018-09-16 22:38:02 -07002347 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 PyObject *name = GETITEM(names, oparg);
2349 PyObject *ns = f->f_locals;
2350 int err;
2351 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002352 _PyErr_Format(tstate, PyExc_SystemError,
2353 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 err = PyObject_DelItem(ns, name);
2357 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 NAME_ERROR_MSG,
2360 name);
2361 goto error;
2362 }
2363 DISPATCH();
2364 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002365
Benjamin Petersonddd19492018-09-16 22:38:02 -07002366 case TARGET(UNPACK_SEQUENCE): {
2367 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PyObject *seq = POP(), *item, **items;
2369 if (PyTuple_CheckExact(seq) &&
2370 PyTuple_GET_SIZE(seq) == oparg) {
2371 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 item = items[oparg];
2374 Py_INCREF(item);
2375 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 } else if (PyList_CheckExact(seq) &&
2378 PyList_GET_SIZE(seq) == oparg) {
2379 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 item = items[oparg];
2382 Py_INCREF(item);
2383 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002385 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002387 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 } else {
2389 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 Py_DECREF(seq);
2391 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002394 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2399 PyObject *seq = POP();
2400
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 stack_pointer + totalargs)) {
2403 stack_pointer += totalargs;
2404 } else {
2405 Py_DECREF(seq);
2406 goto error;
2407 }
2408 Py_DECREF(seq);
2409 DISPATCH();
2410 }
2411
Benjamin Petersonddd19492018-09-16 22:38:02 -07002412 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 PyObject *name = GETITEM(names, oparg);
2414 PyObject *owner = TOP();
2415 PyObject *v = SECOND();
2416 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002417 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 Py_DECREF(owner);
2421 if (err != 0)
2422 goto error;
2423 DISPATCH();
2424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002425
Benjamin Petersonddd19492018-09-16 22:38:02 -07002426 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *name = GETITEM(names, oparg);
2428 PyObject *owner = POP();
2429 int err;
2430 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2431 Py_DECREF(owner);
2432 if (err != 0)
2433 goto error;
2434 DISPATCH();
2435 }
2436
Benjamin Petersonddd19492018-09-16 22:38:02 -07002437 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 PyObject *name = GETITEM(names, oparg);
2439 PyObject *v = POP();
2440 int err;
2441 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 if (err != 0)
2444 goto error;
2445 DISPATCH();
2446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002447
Benjamin Petersonddd19492018-09-16 22:38:02 -07002448 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 PyObject *name = GETITEM(names, oparg);
2450 int err;
2451 err = PyDict_DelItem(f->f_globals, name);
2452 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002453 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2454 format_exc_check_arg(tstate, PyExc_NameError,
2455 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002456 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002458 }
2459 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Benjamin Petersonddd19492018-09-16 22:38:02 -07002462 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 PyObject *name = GETITEM(names, oparg);
2464 PyObject *locals = f->f_locals;
2465 PyObject *v;
2466 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 _PyErr_Format(tstate, PyExc_SystemError,
2468 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002472 v = PyDict_GetItemWithError(locals, name);
2473 if (v != NULL) {
2474 Py_INCREF(v);
2475 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002477 goto error;
2478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 }
2480 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002482 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002483 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002484 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002485 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 }
2487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002489 v = PyDict_GetItemWithError(f->f_globals, name);
2490 if (v != NULL) {
2491 Py_INCREF(v);
2492 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002494 goto error;
2495 }
2496 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002497 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002498 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002500 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002501 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002508 }
2509 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 v = PyObject_GetItem(f->f_builtins, name);
2511 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002512 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002513 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002514 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002516 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002518 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002522 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002525
Benjamin Petersonddd19492018-09-16 22:38:02 -07002526 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002527 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002529 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002530 && PyDict_CheckExact(f->f_builtins))
2531 {
Inada Naoki91234a12019-06-03 21:30:58 +09002532 OPCACHE_CHECK();
2533 if (co_opcache != NULL && co_opcache->optimized > 0) {
2534 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2535
2536 if (lg->globals_ver ==
2537 ((PyDictObject *)f->f_globals)->ma_version_tag
2538 && lg->builtins_ver ==
2539 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2540 {
2541 PyObject *ptr = lg->ptr;
2542 OPCACHE_STAT_GLOBAL_HIT();
2543 assert(ptr != NULL);
2544 Py_INCREF(ptr);
2545 PUSH(ptr);
2546 DISPATCH();
2547 }
2548 }
2549
2550 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002552 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 name);
2554 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002555 if (!_PyErr_OCCURRED()) {
2556 /* _PyDict_LoadGlobal() returns NULL without raising
2557 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002558 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002559 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 }
Inada Naoki91234a12019-06-03 21:30:58 +09002563
2564 if (co_opcache != NULL) {
2565 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2566
2567 if (co_opcache->optimized == 0) {
2568 /* Wasn't optimized before. */
2569 OPCACHE_STAT_GLOBAL_OPT();
2570 } else {
2571 OPCACHE_STAT_GLOBAL_MISS();
2572 }
2573
2574 co_opcache->optimized = 1;
2575 lg->globals_ver =
2576 ((PyDictObject *)f->f_globals)->ma_version_tag;
2577 lg->builtins_ver =
2578 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2579 lg->ptr = v; /* borrowed */
2580 }
2581
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002584 else {
2585 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002586
2587 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002588 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 v = PyObject_GetItem(f->f_globals, name);
2590 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002591 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002592 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002593 }
2594 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002595
Victor Stinnerb4efc962015-11-20 09:24:02 +01002596 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 v = PyObject_GetItem(f->f_builtins, name);
2598 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002599 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002600 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002601 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002602 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002603 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002605 }
2606 }
2607 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002611
Benjamin Petersonddd19492018-09-16 22:38:02 -07002612 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 PyObject *v = GETLOCAL(oparg);
2614 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 SETLOCAL(oparg, NULL);
2616 DISPATCH();
2617 }
2618 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002619 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 UNBOUNDLOCAL_ERROR_MSG,
2621 PyTuple_GetItem(co->co_varnames, oparg)
2622 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 goto error;
2624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002625
Benjamin Petersonddd19492018-09-16 22:38:02 -07002626 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002627 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002628 PyObject *oldobj = PyCell_GET(cell);
2629 if (oldobj != NULL) {
2630 PyCell_SET(cell, NULL);
2631 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002632 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002633 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002634 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 goto error;
2636 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002637
Benjamin Petersonddd19492018-09-16 22:38:02 -07002638 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 PyObject *cell = freevars[oparg];
2640 Py_INCREF(cell);
2641 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002643 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002644
Benjamin Petersonddd19492018-09-16 22:38:02 -07002645 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002646 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002647 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002648 assert(locals);
2649 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2650 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2651 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2652 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2653 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002654 value = PyDict_GetItemWithError(locals, name);
2655 if (value != NULL) {
2656 Py_INCREF(value);
2657 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002658 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002659 goto error;
2660 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002661 }
2662 else {
2663 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002664 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002665 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002666 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002667 }
2668 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002669 }
2670 }
2671 if (!value) {
2672 PyObject *cell = freevars[oparg];
2673 value = PyCell_GET(cell);
2674 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002675 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002676 goto error;
2677 }
2678 Py_INCREF(value);
2679 }
2680 PUSH(value);
2681 DISPATCH();
2682 }
2683
Benjamin Petersonddd19492018-09-16 22:38:02 -07002684 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 PyObject *cell = freevars[oparg];
2686 PyObject *value = PyCell_GET(cell);
2687 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002688 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 Py_INCREF(value);
2692 PUSH(value);
2693 DISPATCH();
2694 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002695
Benjamin Petersonddd19492018-09-16 22:38:02 -07002696 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 PyObject *v = POP();
2698 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002699 PyObject *oldobj = PyCell_GET(cell);
2700 PyCell_SET(cell, v);
2701 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 DISPATCH();
2703 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002704
Benjamin Petersonddd19492018-09-16 22:38:02 -07002705 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002706 PyObject *str;
2707 PyObject *empty = PyUnicode_New(0, 0);
2708 if (empty == NULL) {
2709 goto error;
2710 }
2711 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2712 Py_DECREF(empty);
2713 if (str == NULL)
2714 goto error;
2715 while (--oparg >= 0) {
2716 PyObject *item = POP();
2717 Py_DECREF(item);
2718 }
2719 PUSH(str);
2720 DISPATCH();
2721 }
2722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 PyObject *tup = PyTuple_New(oparg);
2725 if (tup == NULL)
2726 goto error;
2727 while (--oparg >= 0) {
2728 PyObject *item = POP();
2729 PyTuple_SET_ITEM(tup, oparg, item);
2730 }
2731 PUSH(tup);
2732 DISPATCH();
2733 }
2734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *list = PyList_New(oparg);
2737 if (list == NULL)
2738 goto error;
2739 while (--oparg >= 0) {
2740 PyObject *item = POP();
2741 PyList_SET_ITEM(list, oparg, item);
2742 }
2743 PUSH(list);
2744 DISPATCH();
2745 }
2746
Mark Shannon13bc1392020-01-23 09:25:17 +00002747 case TARGET(LIST_TO_TUPLE): {
2748 PyObject *list = POP();
2749 PyObject *tuple = PyList_AsTuple(list);
2750 Py_DECREF(list);
2751 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002753 }
2754 PUSH(tuple);
2755 DISPATCH();
2756 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002757
Mark Shannon13bc1392020-01-23 09:25:17 +00002758 case TARGET(LIST_EXTEND): {
2759 PyObject *iterable = POP();
2760 PyObject *list = PEEK(oparg);
2761 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2762 if (none_val == NULL) {
2763 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002764 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002765 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002766 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002767 _PyErr_Format(tstate, PyExc_TypeError,
2768 "Value after * must be an iterable, not %.200s",
2769 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002771 Py_DECREF(iterable);
2772 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002774 Py_DECREF(none_val);
2775 Py_DECREF(iterable);
2776 DISPATCH();
2777 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002778
Mark Shannon13bc1392020-01-23 09:25:17 +00002779 case TARGET(SET_UPDATE): {
2780 PyObject *iterable = POP();
2781 PyObject *set = PEEK(oparg);
2782 int err = _PySet_Update(set, iterable);
2783 Py_DECREF(iterable);
2784 if (err < 0) {
2785 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002786 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002787 DISPATCH();
2788 }
2789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 PyObject *set = PySet_New(NULL);
2792 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002793 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 if (set == NULL)
2795 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002796 for (i = oparg; i > 0; i--) {
2797 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 if (err == 0)
2799 err = PySet_Add(set, item);
2800 Py_DECREF(item);
2801 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002802 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 if (err != 0) {
2804 Py_DECREF(set);
2805 goto error;
2806 }
2807 PUSH(set);
2808 DISPATCH();
2809 }
2810
Benjamin Petersonddd19492018-09-16 22:38:02 -07002811 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002812 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2814 if (map == NULL)
2815 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002816 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002817 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002818 PyObject *key = PEEK(2*i);
2819 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002820 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002821 if (err != 0) {
2822 Py_DECREF(map);
2823 goto error;
2824 }
2825 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002826
2827 while (oparg--) {
2828 Py_DECREF(POP());
2829 Py_DECREF(POP());
2830 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PUSH(map);
2832 DISPATCH();
2833 }
2834
Benjamin Petersonddd19492018-09-16 22:38:02 -07002835 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002836 _Py_IDENTIFIER(__annotations__);
2837 int err;
2838 PyObject *ann_dict;
2839 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002840 _PyErr_Format(tstate, PyExc_SystemError,
2841 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002842 goto error;
2843 }
2844 /* check if __annotations__ in locals()... */
2845 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002846 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002847 &PyId___annotations__);
2848 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002849 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002850 goto error;
2851 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002852 /* ...if not, create a new one */
2853 ann_dict = PyDict_New();
2854 if (ann_dict == NULL) {
2855 goto error;
2856 }
2857 err = _PyDict_SetItemId(f->f_locals,
2858 &PyId___annotations__, ann_dict);
2859 Py_DECREF(ann_dict);
2860 if (err != 0) {
2861 goto error;
2862 }
2863 }
2864 }
2865 else {
2866 /* do the same if locals() is not a dict */
2867 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2868 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002869 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002870 }
2871 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2872 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002873 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002874 goto error;
2875 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002876 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002877 ann_dict = PyDict_New();
2878 if (ann_dict == NULL) {
2879 goto error;
2880 }
2881 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2882 Py_DECREF(ann_dict);
2883 if (err != 0) {
2884 goto error;
2885 }
2886 }
2887 else {
2888 Py_DECREF(ann_dict);
2889 }
2890 }
2891 DISPATCH();
2892 }
2893
Benjamin Petersonddd19492018-09-16 22:38:02 -07002894 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002895 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002896 PyObject *map;
2897 PyObject *keys = TOP();
2898 if (!PyTuple_CheckExact(keys) ||
2899 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002900 _PyErr_SetString(tstate, PyExc_SystemError,
2901 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002902 goto error;
2903 }
2904 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2905 if (map == NULL) {
2906 goto error;
2907 }
2908 for (i = oparg; i > 0; i--) {
2909 int err;
2910 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2911 PyObject *value = PEEK(i + 1);
2912 err = PyDict_SetItem(map, key, value);
2913 if (err != 0) {
2914 Py_DECREF(map);
2915 goto error;
2916 }
2917 }
2918
2919 Py_DECREF(POP());
2920 while (oparg--) {
2921 Py_DECREF(POP());
2922 }
2923 PUSH(map);
2924 DISPATCH();
2925 }
2926
Mark Shannon8a4cd702020-01-27 09:57:45 +00002927 case TARGET(DICT_UPDATE): {
2928 PyObject *update = POP();
2929 PyObject *dict = PEEK(oparg);
2930 if (PyDict_Update(dict, update) < 0) {
2931 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2932 _PyErr_Format(tstate, PyExc_TypeError,
2933 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002934 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002935 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002936 Py_DECREF(update);
2937 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002938 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002939 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002940 DISPATCH();
2941 }
2942
Mark Shannon8a4cd702020-01-27 09:57:45 +00002943 case TARGET(DICT_MERGE): {
2944 PyObject *update = POP();
2945 PyObject *dict = PEEK(oparg);
2946
2947 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2948 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2949 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002950 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002951 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002952 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002953 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002954 DISPATCH();
2955 }
2956
Benjamin Petersonddd19492018-09-16 22:38:02 -07002957 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002958 PyObject *value = TOP();
2959 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 PyObject *map;
2961 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002962 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002963 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002965 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 Py_DECREF(value);
2967 Py_DECREF(key);
2968 if (err != 0)
2969 goto error;
2970 PREDICT(JUMP_ABSOLUTE);
2971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 PyObject *name = GETITEM(names, oparg);
2976 PyObject *owner = TOP();
2977 PyObject *res = PyObject_GetAttr(owner, name);
2978 Py_DECREF(owner);
2979 SET_TOP(res);
2980 if (res == NULL)
2981 goto error;
2982 DISPATCH();
2983 }
2984
Benjamin Petersonddd19492018-09-16 22:38:02 -07002985 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002986 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 PyObject *right = POP();
2988 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002989 PyObject *res = PyObject_RichCompare(left, right, oparg);
2990 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 Py_DECREF(left);
2992 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 if (res == NULL)
2994 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 PREDICT(POP_JUMP_IF_FALSE);
2996 PREDICT(POP_JUMP_IF_TRUE);
2997 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002998 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002999
Mark Shannon9af0e472020-01-14 10:12:45 +00003000 case TARGET(IS_OP): {
3001 PyObject *right = POP();
3002 PyObject *left = TOP();
3003 int res = (left == right)^oparg;
3004 PyObject *b = res ? Py_True : Py_False;
3005 Py_INCREF(b);
3006 SET_TOP(b);
3007 Py_DECREF(left);
3008 Py_DECREF(right);
3009 PREDICT(POP_JUMP_IF_FALSE);
3010 PREDICT(POP_JUMP_IF_TRUE);
3011 FAST_DISPATCH();
3012 }
3013
3014 case TARGET(CONTAINS_OP): {
3015 PyObject *right = POP();
3016 PyObject *left = POP();
3017 int res = PySequence_Contains(right, left);
3018 Py_DECREF(left);
3019 Py_DECREF(right);
3020 if (res < 0) {
3021 goto error;
3022 }
3023 PyObject *b = (res^oparg) ? Py_True : Py_False;
3024 Py_INCREF(b);
3025 PUSH(b);
3026 PREDICT(POP_JUMP_IF_FALSE);
3027 PREDICT(POP_JUMP_IF_TRUE);
3028 FAST_DISPATCH();
3029 }
3030
3031#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3032 "BaseException is not allowed"
3033
3034 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3035 PyObject *right = POP();
3036 PyObject *left = POP();
3037 if (PyTuple_Check(right)) {
3038 Py_ssize_t i, length;
3039 length = PyTuple_GET_SIZE(right);
3040 for (i = 0; i < length; i++) {
3041 PyObject *exc = PyTuple_GET_ITEM(right, i);
3042 if (!PyExceptionClass_Check(exc)) {
3043 _PyErr_SetString(tstate, PyExc_TypeError,
3044 CANNOT_CATCH_MSG);
3045 Py_DECREF(left);
3046 Py_DECREF(right);
3047 goto error;
3048 }
3049 }
3050 }
3051 else {
3052 if (!PyExceptionClass_Check(right)) {
3053 _PyErr_SetString(tstate, PyExc_TypeError,
3054 CANNOT_CATCH_MSG);
3055 Py_DECREF(left);
3056 Py_DECREF(right);
3057 goto error;
3058 }
3059 }
3060 int res = PyErr_GivenExceptionMatches(left, right);
3061 Py_DECREF(left);
3062 Py_DECREF(right);
3063 if (res > 0) {
3064 /* Exception matches -- Do nothing */;
3065 }
3066 else if (res == 0) {
3067 JUMPTO(oparg);
3068 }
3069 else {
3070 goto error;
3071 }
3072 DISPATCH();
3073 }
3074
Benjamin Petersonddd19492018-09-16 22:38:02 -07003075 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003077 PyObject *fromlist = POP();
3078 PyObject *level = TOP();
3079 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003080 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003081 Py_DECREF(level);
3082 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 SET_TOP(res);
3084 if (res == NULL)
3085 goto error;
3086 DISPATCH();
3087 }
3088
Benjamin Petersonddd19492018-09-16 22:38:02 -07003089 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 PyObject *from = POP(), *locals;
3091 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003092 if (PyFrame_FastToLocalsWithError(f) < 0) {
3093 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003094 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003095 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003096
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 locals = f->f_locals;
3098 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003099 _PyErr_SetString(tstate, PyExc_SystemError,
3100 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003101 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003104 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 Py_DECREF(from);
3107 if (err != 0)
3108 goto error;
3109 DISPATCH();
3110 }
Guido van Rossum25831651993-05-19 14:50:45 +00003111
Benjamin Petersonddd19492018-09-16 22:38:02 -07003112 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 PyObject *name = GETITEM(names, oparg);
3114 PyObject *from = TOP();
3115 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003116 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PUSH(res);
3118 if (res == NULL)
3119 goto error;
3120 DISPATCH();
3121 }
Thomas Wouters52152252000-08-17 22:55:00 +00003122
Benjamin Petersonddd19492018-09-16 22:38:02 -07003123 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 JUMPBY(oparg);
3125 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003127
Benjamin Petersonddd19492018-09-16 22:38:02 -07003128 case TARGET(POP_JUMP_IF_FALSE): {
3129 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 PyObject *cond = POP();
3131 int err;
3132 if (cond == Py_True) {
3133 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 FAST_DISPATCH();
3135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 if (cond == Py_False) {
3137 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 JUMPTO(oparg);
3139 FAST_DISPATCH();
3140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 err = PyObject_IsTrue(cond);
3142 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003144 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 else if (err == 0)
3146 JUMPTO(oparg);
3147 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003151
Benjamin Petersonddd19492018-09-16 22:38:02 -07003152 case TARGET(POP_JUMP_IF_TRUE): {
3153 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 PyObject *cond = POP();
3155 int err;
3156 if (cond == Py_False) {
3157 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 FAST_DISPATCH();
3159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 if (cond == Py_True) {
3161 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 JUMPTO(oparg);
3163 FAST_DISPATCH();
3164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 err = PyObject_IsTrue(cond);
3166 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 JUMPTO(oparg);
3169 }
3170 else if (err == 0)
3171 ;
3172 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003176
Benjamin Petersonddd19492018-09-16 22:38:02 -07003177 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 PyObject *cond = TOP();
3179 int err;
3180 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003181 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 FAST_DISPATCH();
3184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 JUMPTO(oparg);
3187 FAST_DISPATCH();
3188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003191 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 }
3194 else if (err == 0)
3195 JUMPTO(oparg);
3196 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003200
Benjamin Petersonddd19492018-09-16 22:38:02 -07003201 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 PyObject *cond = TOP();
3203 int err;
3204 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003205 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 FAST_DISPATCH();
3208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 JUMPTO(oparg);
3211 FAST_DISPATCH();
3212 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 JUMPTO(oparg);
3216 }
3217 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003218 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 }
3221 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003225
Benjamin Petersonddd19492018-09-16 22:38:02 -07003226 case TARGET(JUMP_ABSOLUTE): {
3227 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003229#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 /* Enabling this path speeds-up all while and for-loops by bypassing
3231 the per-loop checks for signals. By default, this should be turned-off
3232 because it prevents detection of a control-break in tight loops like
3233 "while 1: pass". Compile with this option turned-on when you need
3234 the speed-up and do not need break checking inside tight loops (ones
3235 that contain only instructions ending with FAST_DISPATCH).
3236 */
3237 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003238#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003240#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003246 PyObject *iter = PyObject_GetIter(iterable);
3247 Py_DECREF(iterable);
3248 SET_TOP(iter);
3249 if (iter == NULL)
3250 goto error;
3251 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003252 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003253 DISPATCH();
3254 }
3255
Benjamin Petersonddd19492018-09-16 22:38:02 -07003256 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003257 /* before: [obj]; after [getiter(obj)] */
3258 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003259 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003260 if (PyCoro_CheckExact(iterable)) {
3261 /* `iterable` is a coroutine */
3262 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3263 /* and it is used in a 'yield from' expression of a
3264 regular generator. */
3265 Py_DECREF(iterable);
3266 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003267 _PyErr_SetString(tstate, PyExc_TypeError,
3268 "cannot 'yield from' a coroutine object "
3269 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003270 goto error;
3271 }
3272 }
3273 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003274 /* `iterable` is not a generator. */
3275 iter = PyObject_GetIter(iterable);
3276 Py_DECREF(iterable);
3277 SET_TOP(iter);
3278 if (iter == NULL)
3279 goto error;
3280 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003281 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 DISPATCH();
3283 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003284
Benjamin Petersonddd19492018-09-16 22:38:02 -07003285 case TARGET(FOR_ITER): {
3286 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003289 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 if (next != NULL) {
3291 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 PREDICT(STORE_FAST);
3293 PREDICT(UNPACK_SEQUENCE);
3294 DISPATCH();
3295 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003296 if (_PyErr_Occurred(tstate)) {
3297 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003299 }
3300 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003301 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003302 }
3303 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 }
3305 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003306 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003309 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003311 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003312
Benjamin Petersonddd19492018-09-16 22:38:02 -07003313 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003314 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 STACK_LEVEL());
3316 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003318
Benjamin Petersonddd19492018-09-16 22:38:02 -07003319 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003320 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003321 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003322 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003323 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003324 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003325 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003326 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003327 }
3328 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3329 if (exit == NULL) {
3330 Py_DECREF(enter);
3331 goto error;
3332 }
Yury Selivanov75445082015-05-11 22:57:16 -04003333 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003334 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003335 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003336 Py_DECREF(enter);
3337 if (res == NULL)
3338 goto error;
3339 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003340 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003341 DISPATCH();
3342 }
3343
Benjamin Petersonddd19492018-09-16 22:38:02 -07003344 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003345 PyObject *res = POP();
3346 /* Setup the finally block before pushing the result
3347 of __aenter__ on the stack. */
3348 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3349 STACK_LEVEL());
3350 PUSH(res);
3351 DISPATCH();
3352 }
3353
Benjamin Petersonddd19492018-09-16 22:38:02 -07003354 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003355 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003356 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003358 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003360 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003361 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003362 }
3363 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003364 if (exit == NULL) {
3365 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003369 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003370 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 Py_DECREF(enter);
3372 if (res == NULL)
3373 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 /* Setup the finally block before pushing the result
3375 of __enter__ on the stack. */
3376 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3377 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003378
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 DISPATCH();
3381 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003382
Mark Shannonfee55262019-11-21 09:11:43 +00003383 case TARGET(WITH_EXCEPT_START): {
3384 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003386 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3387 - SEVENTH: the context.__exit__ bound method
3388 We call SEVENTH(TOP, SECOND, THIRD).
3389 Then we push again the TOP exception and the __exit__
3390 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003393 PyObject *exc, *val, *tb, *res;
3394
Victor Stinner842cfff2016-12-01 14:45:31 +01003395 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003396 val = SECOND();
3397 tb = THIRD();
3398 assert(exc != Py_None);
3399 assert(!PyLong_Check(exc));
3400 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003401 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003402 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003403 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 if (res == NULL)
3405 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003406
Yury Selivanov75445082015-05-11 22:57:16 -04003407 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003408 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003410
Benjamin Petersonddd19492018-09-16 22:38:02 -07003411 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003412 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003413 PyObject *name = GETITEM(names, oparg);
3414 PyObject *obj = TOP();
3415 PyObject *meth = NULL;
3416
3417 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3418
Yury Selivanovf2392132016-12-13 19:03:51 -05003419 if (meth == NULL) {
3420 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003421 goto error;
3422 }
3423
3424 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003425 /* We can bypass temporary bound method object.
3426 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003427
INADA Naoki015bce62017-01-16 17:23:30 +09003428 meth | self | arg1 | ... | argN
3429 */
3430 SET_TOP(meth);
3431 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003432 }
3433 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003434 /* meth is not an unbound method (but a regular attr, or
3435 something was returned by a descriptor protocol). Set
3436 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003437 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003438
3439 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003440 */
INADA Naoki015bce62017-01-16 17:23:30 +09003441 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003442 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003443 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003444 }
3445 DISPATCH();
3446 }
3447
Benjamin Petersonddd19492018-09-16 22:38:02 -07003448 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003449 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003450 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003451
3452 sp = stack_pointer;
3453
INADA Naoki015bce62017-01-16 17:23:30 +09003454 meth = PEEK(oparg + 2);
3455 if (meth == NULL) {
3456 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3457 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003458
3459 Stack layout:
3460
INADA Naoki015bce62017-01-16 17:23:30 +09003461 ... | NULL | callable | arg1 | ... | argN
3462 ^- TOP()
3463 ^- (-oparg)
3464 ^- (-oparg-1)
3465 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003466
Ville Skyttä49b27342017-08-03 09:00:59 +03003467 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003468 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003469 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003470 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003471 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003472 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003473 }
3474 else {
3475 /* This is a method call. Stack layout:
3476
INADA Naoki015bce62017-01-16 17:23:30 +09003477 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003478 ^- TOP()
3479 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003480 ^- (-oparg-1)
3481 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003482
INADA Naoki015bce62017-01-16 17:23:30 +09003483 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003485 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003487 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 stack_pointer = sp;
3489 }
3490
3491 PUSH(res);
3492 if (res == NULL)
3493 goto error;
3494 DISPATCH();
3495 }
3496
Benjamin Petersonddd19492018-09-16 22:38:02 -07003497 case TARGET(CALL_FUNCTION): {
3498 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003501 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003503 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003504 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003507 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003509
Benjamin Petersonddd19492018-09-16 22:38:02 -07003510 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003511 PyObject **sp, *res, *names;
3512
3513 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003514 assert(PyTuple_Check(names));
3515 assert(PyTuple_GET_SIZE(names) <= oparg);
3516 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003518 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003520 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003521 Py_DECREF(names);
3522
3523 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003525 }
3526 DISPATCH();
3527 }
3528
Benjamin Petersonddd19492018-09-16 22:38:02 -07003529 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003530 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003531 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003532 if (oparg & 0x01) {
3533 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003534 if (!PyDict_CheckExact(kwargs)) {
3535 PyObject *d = PyDict_New();
3536 if (d == NULL)
3537 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003538 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003539 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003540 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003541 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003542 goto error;
3543 }
3544 Py_DECREF(kwargs);
3545 kwargs = d;
3546 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003547 assert(PyDict_CheckExact(kwargs));
3548 }
3549 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003550 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003551 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003552 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003553 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003554 goto error;
3555 }
3556 Py_SETREF(callargs, PySequence_Tuple(callargs));
3557 if (callargs == NULL) {
3558 goto error;
3559 }
3560 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003561 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003562
Victor Stinner09532fe2019-05-10 23:39:09 +02003563 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003564 Py_DECREF(func);
3565 Py_DECREF(callargs);
3566 Py_XDECREF(kwargs);
3567
3568 SET_TOP(result);
3569 if (result == NULL) {
3570 goto error;
3571 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003572 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003574
Benjamin Petersonddd19492018-09-16 22:38:02 -07003575 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003576 PyObject *qualname = POP();
3577 PyObject *codeobj = POP();
3578 PyFunctionObject *func = (PyFunctionObject *)
3579 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003580
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003581 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003583 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003586
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003587 if (oparg & 0x08) {
3588 assert(PyTuple_CheckExact(TOP()));
3589 func ->func_closure = POP();
3590 }
3591 if (oparg & 0x04) {
3592 assert(PyDict_CheckExact(TOP()));
3593 func->func_annotations = POP();
3594 }
3595 if (oparg & 0x02) {
3596 assert(PyDict_CheckExact(TOP()));
3597 func->func_kwdefaults = POP();
3598 }
3599 if (oparg & 0x01) {
3600 assert(PyTuple_CheckExact(TOP()));
3601 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003603
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003604 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003605 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003607
Benjamin Petersonddd19492018-09-16 22:38:02 -07003608 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003609 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003611 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003613 step = NULL;
3614 stop = POP();
3615 start = TOP();
3616 slice = PySlice_New(start, stop, step);
3617 Py_DECREF(start);
3618 Py_DECREF(stop);
3619 Py_XDECREF(step);
3620 SET_TOP(slice);
3621 if (slice == NULL)
3622 goto error;
3623 DISPATCH();
3624 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003625
Benjamin Petersonddd19492018-09-16 22:38:02 -07003626 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003627 /* Handles f-string value formatting. */
3628 PyObject *result;
3629 PyObject *fmt_spec;
3630 PyObject *value;
3631 PyObject *(*conv_fn)(PyObject *);
3632 int which_conversion = oparg & FVC_MASK;
3633 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3634
3635 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003636 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003637
3638 /* See if any conversion is specified. */
3639 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003640 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003641 case FVC_STR: conv_fn = PyObject_Str; break;
3642 case FVC_REPR: conv_fn = PyObject_Repr; break;
3643 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003644 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003645 _PyErr_Format(tstate, PyExc_SystemError,
3646 "unexpected conversion flag %d",
3647 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003648 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003649 }
3650
3651 /* If there's a conversion function, call it and replace
3652 value with that result. Otherwise, just use value,
3653 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003654 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003655 result = conv_fn(value);
3656 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003657 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003658 Py_XDECREF(fmt_spec);
3659 goto error;
3660 }
3661 value = result;
3662 }
3663
3664 /* If value is a unicode object, and there's no fmt_spec,
3665 then we know the result of format(value) is value
3666 itself. In that case, skip calling format(). I plan to
3667 move this optimization in to PyObject_Format()
3668 itself. */
3669 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3670 /* Do nothing, just transfer ownership to result. */
3671 result = value;
3672 } else {
3673 /* Actually call format(). */
3674 result = PyObject_Format(value, fmt_spec);
3675 Py_DECREF(value);
3676 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003677 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003678 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003679 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003680 }
3681
Eric V. Smith135d5f42016-02-05 18:23:08 -05003682 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003683 DISPATCH();
3684 }
3685
Benjamin Petersonddd19492018-09-16 22:38:02 -07003686 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003687 int oldoparg = oparg;
3688 NEXTOPARG();
3689 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003691 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003692
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003693
Antoine Pitrou042b1282010-08-13 21:15:58 +00003694#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003696#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 default:
3698 fprintf(stderr,
3699 "XXX lineno: %d, opcode: %d\n",
3700 PyFrame_GetLineNumber(f),
3701 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003702 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003703 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003706
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003707 /* This should never be reached. Every opcode should end with DISPATCH()
3708 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003709 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003710
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003712 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003713#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003714 if (!_PyErr_Occurred(tstate)) {
3715 _PyErr_SetString(tstate, PyExc_SystemError,
3716 "error return without exception set");
3717 }
Victor Stinner365b6932013-07-12 00:11:58 +02003718#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003719 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003720#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003721
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003722 /* Log traceback info. */
3723 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003724
Benjamin Peterson51f46162013-01-23 08:38:47 -05003725 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003726 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3727 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003728
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003729exception_unwind:
3730 /* Unwind stacks if an exception occurred */
3731 while (f->f_iblock > 0) {
3732 /* Pop the current block. */
3733 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 if (b->b_type == EXCEPT_HANDLER) {
3736 UNWIND_EXCEPT_HANDLER(b);
3737 continue;
3738 }
3739 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003740 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 PyObject *exc, *val, *tb;
3742 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003743 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 /* Beware, this invalidates all b->b_* fields */
3745 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003746 PUSH(exc_info->exc_traceback);
3747 PUSH(exc_info->exc_value);
3748 if (exc_info->exc_type != NULL) {
3749 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 }
3751 else {
3752 Py_INCREF(Py_None);
3753 PUSH(Py_None);
3754 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003755 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 /* Make the raw exception data
3757 available to the handler,
3758 so a program can emulate the
3759 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003760 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003761 if (tb != NULL)
3762 PyException_SetTraceback(val, tb);
3763 else
3764 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003766 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003768 exc_info->exc_value = val;
3769 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 if (tb == NULL)
3771 tb = Py_None;
3772 Py_INCREF(tb);
3773 PUSH(tb);
3774 PUSH(val);
3775 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003777 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003778 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3779 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3780 /* Make sure that we trace line after exception if we are in a new execution
3781 * window or we don't need a line update and we are not in the first instruction
3782 * of the line. */
3783 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3784 instr_prev = INT_MAX;
3785 }
Mark Shannonfee55262019-11-21 09:11:43 +00003786 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003787 /* Resume normal execution */
3788 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
3790 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003791
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003792 /* End the loop as we still have an error */
3793 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003795
Pablo Galindof00828a2019-05-09 16:52:02 +01003796 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003797 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 /* Pop remaining stack entries. */
3800 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 PyObject *o = POP();
3802 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003804
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003805exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003807 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003808 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3809 tstate, f, PyTrace_RETURN, retval)) {
3810 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 }
3812 }
3813 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003814 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3815 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003816 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 }
3818 }
3819 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003822exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003823 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3824 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003825 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003826 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003828
Victor Stinner0b72b232020-03-12 23:18:39 +01003829 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003830}
3831
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003833format_missing(PyThreadState *tstate, const char *kind,
3834 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003835{
3836 int err;
3837 Py_ssize_t len = PyList_GET_SIZE(names);
3838 PyObject *name_str, *comma, *tail, *tmp;
3839
3840 assert(PyList_CheckExact(names));
3841 assert(len >= 1);
3842 /* Deal with the joys of natural language. */
3843 switch (len) {
3844 case 1:
3845 name_str = PyList_GET_ITEM(names, 0);
3846 Py_INCREF(name_str);
3847 break;
3848 case 2:
3849 name_str = PyUnicode_FromFormat("%U and %U",
3850 PyList_GET_ITEM(names, len - 2),
3851 PyList_GET_ITEM(names, len - 1));
3852 break;
3853 default:
3854 tail = PyUnicode_FromFormat(", %U, and %U",
3855 PyList_GET_ITEM(names, len - 2),
3856 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003857 if (tail == NULL)
3858 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003859 /* Chop off the last two objects in the list. This shouldn't actually
3860 fail, but we can't be too careful. */
3861 err = PyList_SetSlice(names, len - 2, len, NULL);
3862 if (err == -1) {
3863 Py_DECREF(tail);
3864 return;
3865 }
3866 /* Stitch everything up into a nice comma-separated list. */
3867 comma = PyUnicode_FromString(", ");
3868 if (comma == NULL) {
3869 Py_DECREF(tail);
3870 return;
3871 }
3872 tmp = PyUnicode_Join(comma, names);
3873 Py_DECREF(comma);
3874 if (tmp == NULL) {
3875 Py_DECREF(tail);
3876 return;
3877 }
3878 name_str = PyUnicode_Concat(tmp, tail);
3879 Py_DECREF(tmp);
3880 Py_DECREF(tail);
3881 break;
3882 }
3883 if (name_str == NULL)
3884 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003885 _PyErr_Format(tstate, PyExc_TypeError,
3886 "%U() missing %i required %s argument%s: %U",
3887 co->co_name,
3888 len,
3889 kind,
3890 len == 1 ? "" : "s",
3891 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003892 Py_DECREF(name_str);
3893}
3894
3895static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003896missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3897 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003898 PyObject **fastlocals)
3899{
Victor Stinner74319ae2016-08-25 00:04:09 +02003900 Py_ssize_t i, j = 0;
3901 Py_ssize_t start, end;
3902 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003903 const char *kind = positional ? "positional" : "keyword-only";
3904 PyObject *missing_names;
3905
3906 /* Compute the names of the arguments that are missing. */
3907 missing_names = PyList_New(missing);
3908 if (missing_names == NULL)
3909 return;
3910 if (positional) {
3911 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003912 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003913 }
3914 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003915 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003916 end = start + co->co_kwonlyargcount;
3917 }
3918 for (i = start; i < end; i++) {
3919 if (GETLOCAL(i) == NULL) {
3920 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3921 PyObject *name = PyObject_Repr(raw);
3922 if (name == NULL) {
3923 Py_DECREF(missing_names);
3924 return;
3925 }
3926 PyList_SET_ITEM(missing_names, j++, name);
3927 }
3928 }
3929 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003930 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003931 Py_DECREF(missing_names);
3932}
3933
3934static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003935too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3936 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938{
3939 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003940 Py_ssize_t kwonly_given = 0;
3941 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003943 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944
Benjamin Petersone109c702011-06-24 09:37:26 -05003945 assert((co->co_flags & CO_VARARGS) == 0);
3946 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003947 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003948 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003950 }
3951 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003952 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003953 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003955 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003956 }
3957 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003958 plural = (co_argcount != 1);
3959 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003960 }
3961 if (sig == NULL)
3962 return;
3963 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003964 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3965 kwonly_sig = PyUnicode_FromFormat(format,
3966 given != 1 ? "s" : "",
3967 kwonly_given,
3968 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 if (kwonly_sig == NULL) {
3970 Py_DECREF(sig);
3971 return;
3972 }
3973 }
3974 else {
3975 /* This will not fail. */
3976 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003977 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003979 _PyErr_Format(tstate, PyExc_TypeError,
3980 "%U() takes %U positional argument%s but %zd%U %s given",
3981 co->co_name,
3982 sig,
3983 plural ? "s" : "",
3984 given,
3985 kwonly_sig,
3986 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003987 Py_DECREF(sig);
3988 Py_DECREF(kwonly_sig);
3989}
3990
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003991static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003992positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3993 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003994{
3995 int posonly_conflicts = 0;
3996 PyObject* posonly_names = PyList_New(0);
3997
3998 for(int k=0; k < co->co_posonlyargcount; k++){
3999 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4000
4001 for (int k2=0; k2<kwcount; k2++){
4002 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4003 PyObject* kwname = kwnames[k2];
4004 if (kwname == posonly_name){
4005 if(PyList_Append(posonly_names, kwname) != 0) {
4006 goto fail;
4007 }
4008 posonly_conflicts++;
4009 continue;
4010 }
4011
4012 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4013
4014 if ( cmp > 0) {
4015 if(PyList_Append(posonly_names, kwname) != 0) {
4016 goto fail;
4017 }
4018 posonly_conflicts++;
4019 } else if (cmp < 0) {
4020 goto fail;
4021 }
4022
4023 }
4024 }
4025 if (posonly_conflicts) {
4026 PyObject* comma = PyUnicode_FromString(", ");
4027 if (comma == NULL) {
4028 goto fail;
4029 }
4030 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4031 Py_DECREF(comma);
4032 if (error_names == NULL) {
4033 goto fail;
4034 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004035 _PyErr_Format(tstate, PyExc_TypeError,
4036 "%U() got some positional-only arguments passed"
4037 " as keyword arguments: '%U'",
4038 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004039 Py_DECREF(error_names);
4040 goto fail;
4041 }
4042
4043 Py_DECREF(posonly_names);
4044 return 0;
4045
4046fail:
4047 Py_XDECREF(posonly_names);
4048 return 1;
4049
4050}
4051
Guido van Rossumc2e20742006-02-27 22:32:47 +00004052/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004053 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004054 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004055
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004056PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004057_PyEval_EvalCode(PyThreadState *tstate,
4058 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004059 PyObject *const *args, Py_ssize_t argcount,
4060 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004061 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004062 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004063 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004064 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004065{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004066 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004067
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004068 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004069 PyFrameObject *f;
4070 PyObject *retval = NULL;
4071 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004073 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004074 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004075 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004078 _PyErr_SetString(tstate, PyExc_SystemError,
4079 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 return NULL;
4081 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004082
Victor Stinnerc7020012016-08-16 23:40:29 +02004083 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004084 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004085 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004087 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 fastlocals = f->f_localsplus;
4089 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004090
Victor Stinnerc7020012016-08-16 23:40:29 +02004091 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004092 if (co->co_flags & CO_VARKEYWORDS) {
4093 kwdict = PyDict_New();
4094 if (kwdict == NULL)
4095 goto fail;
4096 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004097 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004098 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004099 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 SETLOCAL(i, kwdict);
4101 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004102 else {
4103 kwdict = NULL;
4104 }
4105
Pablo Galindocd74e662019-06-01 18:08:04 +01004106 /* Copy all positional arguments into local variables */
4107 if (argcount > co->co_argcount) {
4108 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004109 }
4110 else {
4111 n = argcount;
4112 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004113 for (j = 0; j < n; j++) {
4114 x = args[j];
4115 Py_INCREF(x);
4116 SETLOCAL(j, x);
4117 }
4118
Victor Stinnerc7020012016-08-16 23:40:29 +02004119 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004120 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004121 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004122 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004123 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004124 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004126 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004127
Serhiy Storchakab7281052016-09-12 00:52:40 +03004128 /* Handle keyword arguments passed as two strided arrays */
4129 kwcount *= kwstep;
4130 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004131 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004132 PyObject *keyword = kwnames[i];
4133 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004134 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004135
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004137 _PyErr_Format(tstate, PyExc_TypeError,
4138 "%U() keywords must be strings",
4139 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004140 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004142
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 /* Speed hack: do raw pointer compares. As names are
4144 normally interned this should almost always hit. */
4145 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004146 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004147 PyObject *name = co_varnames[j];
4148 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004149 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004150 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004151 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004152
Benjamin Petersonb204a422011-06-05 22:04:07 -05004153 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004154 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004155 PyObject *name = co_varnames[j];
4156 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4157 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004158 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004159 }
4160 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004162 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004163 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004164
Victor Stinner231d1f32017-01-11 02:12:06 +01004165 assert(j >= total_args);
4166 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004167
Victor Stinner438a12d2019-05-24 17:01:38 +02004168 if (co->co_posonlyargcount
4169 && positional_only_passed_as_keyword(tstate, co,
4170 kwcount, kwnames))
4171 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004172 goto fail;
4173 }
4174
Victor Stinner438a12d2019-05-24 17:01:38 +02004175 _PyErr_Format(tstate, PyExc_TypeError,
4176 "%U() got an unexpected keyword argument '%S'",
4177 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004180
Christian Heimes0bd447f2013-07-20 14:48:10 +02004181 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4182 goto fail;
4183 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004184 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004185
Benjamin Petersonb204a422011-06-05 22:04:07 -05004186 kw_found:
4187 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004188 _PyErr_Format(tstate, PyExc_TypeError,
4189 "%U() got multiple values for argument '%S'",
4190 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004191 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004193 Py_INCREF(value);
4194 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004196
4197 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004198 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004199 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 goto fail;
4201 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004202
4203 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004204 if (argcount < co->co_argcount) {
4205 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004206 Py_ssize_t missing = 0;
4207 for (i = argcount; i < m; i++) {
4208 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004209 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004210 }
4211 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004212 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004213 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004214 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004215 }
4216 if (n > m)
4217 i = n - m;
4218 else
4219 i = 0;
4220 for (; i < defcount; i++) {
4221 if (GETLOCAL(m+i) == NULL) {
4222 PyObject *def = defs[i];
4223 Py_INCREF(def);
4224 SETLOCAL(m+i, def);
4225 }
4226 }
4227 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004228
4229 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004230 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004231 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004232 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233 PyObject *name;
4234 if (GETLOCAL(i) != NULL)
4235 continue;
4236 name = PyTuple_GET_ITEM(co->co_varnames, i);
4237 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004238 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004239 if (def) {
4240 Py_INCREF(def);
4241 SETLOCAL(i, def);
4242 continue;
4243 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004244 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004245 goto fail;
4246 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004247 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004248 missing++;
4249 }
4250 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004251 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004252 goto fail;
4253 }
4254 }
4255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004257 vars into frame. */
4258 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004260 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004261 /* Possibly account for the cell variable being an argument. */
4262 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004263 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004264 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004265 /* Clear the local copy. */
4266 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004267 }
4268 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004269 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004270 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004271 if (c == NULL)
4272 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004273 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004275
4276 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004277 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4278 PyObject *o = PyTuple_GET_ITEM(closure, i);
4279 Py_INCREF(o);
4280 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004282
Yury Selivanoveb636452016-09-08 22:01:51 -07004283 /* Handle generator/coroutine/asynchronous generator */
4284 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004285 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004286 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 /* Don't need to keep the reference to f_back, it will be set
4289 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004290 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 /* Create a new generator that owns the ready to run frame
4293 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004294 if (is_coro) {
4295 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004296 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4297 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004298 } else {
4299 gen = PyGen_NewWithQualName(f, name, qualname);
4300 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004301 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004302 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004303 }
INADA Naoki9c157762016-12-26 18:52:46 +09004304
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004305 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004306
Yury Selivanov75445082015-05-11 22:57:16 -04004307 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004309
Victor Stinnerb9e68122019-11-14 12:20:46 +01004310 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004311
Thomas Woutersce272b62007-09-19 21:19:28 +00004312fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 /* decref'ing the frame can cause __del__ methods to get invoked,
4315 which can call back into Python. While we're done with the
4316 current Python frame (f), the associated C stack is still in use,
4317 so recursion_depth must be boosted for the duration.
4318 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004319 if (Py_REFCNT(f) > 1) {
4320 Py_DECREF(f);
4321 _PyObject_GC_TRACK(f);
4322 }
4323 else {
4324 ++tstate->recursion_depth;
4325 Py_DECREF(f);
4326 --tstate->recursion_depth;
4327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004329}
4330
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004331
4332PyObject *
4333_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4334 PyObject *const *args, Py_ssize_t argcount,
4335 PyObject *const *kwnames, PyObject *const *kwargs,
4336 Py_ssize_t kwcount, int kwstep,
4337 PyObject *const *defs, Py_ssize_t defcount,
4338 PyObject *kwdefs, PyObject *closure,
4339 PyObject *name, PyObject *qualname)
4340{
4341 PyThreadState *tstate = _PyThreadState_GET();
4342 return _PyEval_EvalCode(tstate, _co, globals, locals,
4343 args, argcount,
4344 kwnames, kwargs,
4345 kwcount, kwstep,
4346 defs, defcount,
4347 kwdefs, closure,
4348 name, qualname);
4349}
4350
Victor Stinner40ee3012014-06-16 15:59:28 +02004351PyObject *
4352PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004353 PyObject *const *args, int argcount,
4354 PyObject *const *kws, int kwcount,
4355 PyObject *const *defs, int defcount,
4356 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004357{
4358 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004359 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004360 kws, kws != NULL ? kws + 1 : NULL,
4361 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004362 defs, defcount,
4363 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004364 NULL, NULL);
4365}
Tim Peters5ca576e2001-06-18 22:08:13 +00004366
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004367static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004368special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004371 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004372 if (res == NULL && !_PyErr_Occurred(tstate)) {
4373 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 return NULL;
4375 }
4376 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004377}
4378
4379
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004380/* Logic for the raise statement (too complicated for inlining).
4381 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004382static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004383do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 if (exc == NULL) {
4388 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004389 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004391 type = exc_info->exc_type;
4392 value = exc_info->exc_value;
4393 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004394 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004395 _PyErr_SetString(tstate, PyExc_RuntimeError,
4396 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004397 return 0;
4398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 Py_XINCREF(type);
4400 Py_XINCREF(value);
4401 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004402 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004403 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 /* We support the following forms of raise:
4407 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004408 raise <instance>
4409 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 if (PyExceptionClass_Check(exc)) {
4412 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004413 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (value == NULL)
4415 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004416 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004417 _PyErr_Format(tstate, PyExc_TypeError,
4418 "calling %R should have returned an instance of "
4419 "BaseException, not %R",
4420 type, Py_TYPE(value));
4421 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 }
4424 else if (PyExceptionInstance_Check(exc)) {
4425 value = exc;
4426 type = PyExceptionInstance_Class(exc);
4427 Py_INCREF(type);
4428 }
4429 else {
4430 /* Not something you can raise. You get an exception
4431 anyway, just not what you specified :-) */
4432 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004433 _PyErr_SetString(tstate, PyExc_TypeError,
4434 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 goto raise_error;
4436 }
Collin Winter828f04a2007-08-31 00:04:24 +00004437
Serhiy Storchakac0191582016-09-27 11:37:10 +03004438 assert(type != NULL);
4439 assert(value != NULL);
4440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 if (cause) {
4442 PyObject *fixed_cause;
4443 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004444 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 if (fixed_cause == NULL)
4446 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004447 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004449 else if (PyExceptionInstance_Check(cause)) {
4450 fixed_cause = cause;
4451 }
4452 else if (cause == Py_None) {
4453 Py_DECREF(cause);
4454 fixed_cause = NULL;
4455 }
4456 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004457 _PyErr_SetString(tstate, PyExc_TypeError,
4458 "exception causes must derive from "
4459 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 goto raise_error;
4461 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004462 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 }
Collin Winter828f04a2007-08-31 00:04:24 +00004464
Victor Stinner438a12d2019-05-24 17:01:38 +02004465 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004466 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004467 Py_DECREF(value);
4468 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004469 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004470
4471raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 Py_XDECREF(value);
4473 Py_XDECREF(type);
4474 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004475 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004476}
4477
Tim Petersd6d010b2001-06-21 02:49:55 +00004478/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004479 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004480
Guido van Rossum0368b722007-05-11 16:50:42 +00004481 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4482 with a variable target.
4483*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004484
Barry Warsawe42b18f1997-08-25 22:13:04 +00004485static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004486unpack_iterable(PyThreadState *tstate, PyObject *v,
4487 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 int i = 0, j = 0;
4490 Py_ssize_t ll = 0;
4491 PyObject *it; /* iter(v) */
4492 PyObject *w;
4493 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004498 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004499 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004500 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004501 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 _PyErr_Format(tstate, PyExc_TypeError,
4503 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004504 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004505 }
4506 return 0;
4507 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 for (; i < argcnt; i++) {
4510 w = PyIter_Next(it);
4511 if (w == NULL) {
4512 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004513 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004514 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004515 _PyErr_Format(tstate, PyExc_ValueError,
4516 "not enough values to unpack "
4517 "(expected %d, got %d)",
4518 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004519 }
4520 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004521 _PyErr_Format(tstate, PyExc_ValueError,
4522 "not enough values to unpack "
4523 "(expected at least %d, got %d)",
4524 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004525 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 }
4527 goto Error;
4528 }
4529 *--sp = w;
4530 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 if (argcntafter == -1) {
4533 /* We better have exhausted the iterator now. */
4534 w = PyIter_Next(it);
4535 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004536 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 goto Error;
4538 Py_DECREF(it);
4539 return 1;
4540 }
4541 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004542 _PyErr_Format(tstate, PyExc_ValueError,
4543 "too many values to unpack (expected %d)",
4544 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 goto Error;
4546 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 l = PySequence_List(it);
4549 if (l == NULL)
4550 goto Error;
4551 *--sp = l;
4552 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 ll = PyList_GET_SIZE(l);
4555 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004556 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004557 "not enough values to unpack (expected at least %d, got %zd)",
4558 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 goto Error;
4560 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 /* Pop the "after-variable" args off the list. */
4563 for (j = argcntafter; j > 0; j--, i++) {
4564 *--sp = PyList_GET_ITEM(l, ll - j);
4565 }
4566 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004567 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 Py_DECREF(it);
4569 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004570
Tim Petersd6d010b2001-06-21 02:49:55 +00004571Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 for (; i > 0; i--, sp++)
4573 Py_DECREF(*sp);
4574 Py_XDECREF(it);
4575 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004576}
4577
4578
Guido van Rossum96a42c81992-01-12 02:29:51 +00004579#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004580static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004581prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004584 if (PyObject_Print(v, stdout, 0) != 0) {
4585 /* Don't know what else to do */
4586 _PyErr_Clear(tstate);
4587 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 printf("\n");
4589 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004590}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004591#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004592
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004593static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004594call_exc_trace(Py_tracefunc func, PyObject *self,
4595 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004596{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004597 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004599 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 if (value == NULL) {
4601 value = Py_None;
4602 Py_INCREF(value);
4603 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004604 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004605 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 arg = PyTuple_Pack(3, type, value, traceback);
4607 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004608 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 return;
4610 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004611 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004613 if (err == 0) {
4614 _PyErr_Restore(tstate, type, value, orig_traceback);
4615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 else {
4617 Py_XDECREF(type);
4618 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004619 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004621}
4622
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004623static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004624call_trace_protected(Py_tracefunc func, PyObject *obj,
4625 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 PyObject *type, *value, *traceback;
4629 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004630 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004631 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 if (err == 0)
4633 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004634 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 return 0;
4636 }
4637 else {
4638 Py_XDECREF(type);
4639 Py_XDECREF(value);
4640 Py_XDECREF(traceback);
4641 return -1;
4642 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004643}
4644
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004645static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004646call_trace(Py_tracefunc func, PyObject *obj,
4647 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 int result;
4651 if (tstate->tracing)
4652 return 0;
4653 tstate->tracing++;
4654 tstate->use_tracing = 0;
4655 result = func(obj, frame, what, arg);
4656 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4657 || (tstate->c_profilefunc != NULL));
4658 tstate->tracing--;
4659 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004660}
4661
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004662PyObject *
4663_PyEval_CallTracing(PyObject *func, PyObject *args)
4664{
Victor Stinner50b48572018-11-01 01:51:40 +01004665 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 int save_tracing = tstate->tracing;
4667 int save_use_tracing = tstate->use_tracing;
4668 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 tstate->tracing = 0;
4671 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4672 || (tstate->c_profilefunc != NULL));
4673 result = PyObject_Call(func, args, NULL);
4674 tstate->tracing = save_tracing;
4675 tstate->use_tracing = save_use_tracing;
4676 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004677}
4678
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004679/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004680static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004681maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004682 PyThreadState *tstate, PyFrameObject *frame,
4683 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 int result = 0;
4686 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 /* If the last instruction executed isn't in the current
4689 instruction window, reset the window.
4690 */
4691 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4692 PyAddrPair bounds;
4693 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4694 &bounds);
4695 *instr_lb = bounds.ap_lower;
4696 *instr_ub = bounds.ap_upper;
4697 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004698 /* If the last instruction falls at the start of a line or if it
4699 represents a jump backwards, update the frame's line number and
4700 then call the trace function if we're tracing source lines.
4701 */
4702 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004704 if (frame->f_trace_lines) {
4705 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4706 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 }
George King20faa682017-10-18 17:44:22 -07004708 /* Always emit an opcode event if we're tracing all opcodes. */
4709 if (frame->f_trace_opcodes) {
4710 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 *instr_prev = frame->f_lasti;
4713 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004714}
4715
Victor Stinner309d7cc2020-03-13 16:39:12 +01004716int
4717_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4718{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004719 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004720 /* The caller must hold the GIL */
4721 assert(PyGILState_Check());
4722
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004723 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004724 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004725 PyThreadState *current_tstate = _PyThreadState_GET();
4726 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004727 return -1;
4728 }
4729
4730 PyObject *profileobj = tstate->c_profileobj;
4731
4732 tstate->c_profilefunc = NULL;
4733 tstate->c_profileobj = NULL;
4734 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4735 tstate->use_tracing = tstate->c_tracefunc != NULL;
4736 Py_XDECREF(profileobj);
4737
4738 Py_XINCREF(arg);
4739 tstate->c_profileobj = arg;
4740 tstate->c_profilefunc = func;
4741
4742 /* Flag that tracing or profiling is turned on */
4743 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4744 return 0;
4745}
4746
Fred Drake5755ce62001-06-27 19:19:46 +00004747void
4748PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004749{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004750 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004751 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004752 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004753 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4754 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004755}
4756
4757int
4758_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4759{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004760 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004761 /* The caller must hold the GIL */
4762 assert(PyGILState_Check());
4763
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004764 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004765 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004766 PyThreadState *current_tstate = _PyThreadState_GET();
4767 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004768 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004769 }
4770
Victor Stinnerda2914d2020-03-20 09:29:08 +01004771 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004772 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004773 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004774
4775 tstate->c_tracefunc = NULL;
4776 tstate->c_traceobj = NULL;
4777 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4778 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4779 Py_XDECREF(traceobj);
4780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782 tstate->c_traceobj = arg;
4783 tstate->c_tracefunc = func;
4784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004786 tstate->use_tracing = ((func != NULL)
4787 || (tstate->c_profilefunc != NULL));
4788
4789 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004790}
4791
4792void
4793PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4794{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004795 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004796 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004797 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004798 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4799 }
Fred Draked0838392001-06-16 21:02:31 +00004800}
4801
Victor Stinner309d7cc2020-03-13 16:39:12 +01004802
Yury Selivanov75445082015-05-11 22:57:16 -04004803void
Victor Stinner838f2642019-06-13 22:41:23 +02004804_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004805{
4806 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004807 tstate->coroutine_origin_tracking_depth = new_depth;
4808}
4809
4810int
4811_PyEval_GetCoroutineOriginTrackingDepth(void)
4812{
Victor Stinner50b48572018-11-01 01:51:40 +01004813 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004814 return tstate->coroutine_origin_tracking_depth;
4815}
4816
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004817int
Yury Selivanoveb636452016-09-08 22:01:51 -07004818_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4819{
Victor Stinner50b48572018-11-01 01:51:40 +01004820 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004821
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004822 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004823 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004824 }
4825
Yury Selivanoveb636452016-09-08 22:01:51 -07004826 Py_XINCREF(firstiter);
4827 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004828 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004829}
4830
4831PyObject *
4832_PyEval_GetAsyncGenFirstiter(void)
4833{
Victor Stinner50b48572018-11-01 01:51:40 +01004834 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004835 return tstate->async_gen_firstiter;
4836}
4837
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004838int
Yury Selivanoveb636452016-09-08 22:01:51 -07004839_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4840{
Victor Stinner50b48572018-11-01 01:51:40 +01004841 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004842
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004843 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004844 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004845 }
4846
Yury Selivanoveb636452016-09-08 22:01:51 -07004847 Py_XINCREF(finalizer);
4848 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004849 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004850}
4851
4852PyObject *
4853_PyEval_GetAsyncGenFinalizer(void)
4854{
Victor Stinner50b48572018-11-01 01:51:40 +01004855 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004856 return tstate->async_gen_finalizer;
4857}
4858
Victor Stinner438a12d2019-05-24 17:01:38 +02004859PyFrameObject *
4860PyEval_GetFrame(void)
4861{
4862 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004863 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004864}
4865
Guido van Rossumb209a111997-04-29 18:18:01 +00004866PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004867PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004868{
Victor Stinner438a12d2019-05-24 17:01:38 +02004869 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004870 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004872 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 else
4874 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004875}
4876
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004877/* Convenience function to get a builtin from its name */
4878PyObject *
4879_PyEval_GetBuiltinId(_Py_Identifier *name)
4880{
Victor Stinner438a12d2019-05-24 17:01:38 +02004881 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004882 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4883 if (attr) {
4884 Py_INCREF(attr);
4885 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004886 else if (!_PyErr_Occurred(tstate)) {
4887 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004888 }
4889 return attr;
4890}
4891
Guido van Rossumb209a111997-04-29 18:18:01 +00004892PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004893PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004894{
Victor Stinner438a12d2019-05-24 17:01:38 +02004895 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004896 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004897 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004898 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004900 }
4901
Victor Stinner438a12d2019-05-24 17:01:38 +02004902 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004903 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004904 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004905
4906 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004908}
4909
Guido van Rossumb209a111997-04-29 18:18:01 +00004910PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004911PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004912{
Victor Stinner438a12d2019-05-24 17:01:38 +02004913 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004914 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004917 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004918
4919 assert(current_frame->f_globals != NULL);
4920 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004921}
4922
Guido van Rossum6135a871995-01-09 17:53:26 +00004923int
Tim Peters5ba58662001-07-16 02:29:45 +00004924PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004925{
Victor Stinner438a12d2019-05-24 17:01:38 +02004926 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004927 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 if (current_frame != NULL) {
4931 const int codeflags = current_frame->f_code->co_flags;
4932 const int compilerflags = codeflags & PyCF_MASK;
4933 if (compilerflags) {
4934 result = 1;
4935 cf->cf_flags |= compilerflags;
4936 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004937#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 if (codeflags & CO_GENERATOR_ALLOWED) {
4939 result = 1;
4940 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4941 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 }
4944 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004945}
4946
Guido van Rossum3f5da241990-12-20 15:06:42 +00004947
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004948const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004949PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 if (PyMethod_Check(func))
4952 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4953 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004954 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 else if (PyCFunction_Check(func))
4956 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4957 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004958 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004959}
4960
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004961const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004962PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004964 if (PyMethod_Check(func))
4965 return "()";
4966 else if (PyFunction_Check(func))
4967 return "()";
4968 else if (PyCFunction_Check(func))
4969 return "()";
4970 else
4971 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004972}
4973
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004974#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004975if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004976 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4977 tstate, tstate->frame, \
4978 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 x = NULL; \
4980 } \
4981 else { \
4982 x = call; \
4983 if (tstate->c_profilefunc != NULL) { \
4984 if (x == NULL) { \
4985 call_trace_protected(tstate->c_profilefunc, \
4986 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004987 tstate, tstate->frame, \
4988 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 /* XXX should pass (type, value, tb) */ \
4990 } else { \
4991 if (call_trace(tstate->c_profilefunc, \
4992 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004993 tstate, tstate->frame, \
4994 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 Py_DECREF(x); \
4996 x = NULL; \
4997 } \
4998 } \
4999 } \
5000 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005001} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 x = call; \
5003 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005004
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005005
5006static PyObject *
5007trace_call_function(PyThreadState *tstate,
5008 PyObject *func,
5009 PyObject **args, Py_ssize_t nargs,
5010 PyObject *kwnames)
5011{
5012 PyObject *x;
5013 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005014 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005015 return x;
5016 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005017 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005018 /* We need to create a temporary bound method as argument
5019 for profiling.
5020
5021 If nargs == 0, then this cannot work because we have no
5022 "self". In any case, the call itself would raise
5023 TypeError (foo needs an argument), so we just skip
5024 profiling. */
5025 PyObject *self = args[0];
5026 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5027 if (func == NULL) {
5028 return NULL;
5029 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005030 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005031 args+1, nargs-1,
5032 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005033 Py_DECREF(func);
5034 return x;
5035 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005036 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005037}
5038
Victor Stinner415c5102017-01-11 00:54:57 +01005039/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5040 to reduce the stack consumption. */
5041Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005042call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005043{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005044 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 PyObject *func = *pfunc;
5046 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005047 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5048 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005049 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005050
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005051 if (tstate->use_tracing) {
5052 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005053 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005054 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005055 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005057
Victor Stinner438a12d2019-05-24 17:01:38 +02005058 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005059
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005060 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 while ((*pp_stack) > pfunc) {
5062 w = EXT_POP(*pp_stack);
5063 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005067}
5068
Jeremy Hylton52820442001-01-03 23:52:36 +00005069static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005070do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005071{
jdemeyere89de732018-09-19 12:06:20 +02005072 PyObject *result;
5073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005075 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005076 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005078 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005079 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5080 if (nargs > 0 && tstate->use_tracing) {
5081 /* We need to create a temporary bound method as argument
5082 for profiling.
5083
5084 If nargs == 0, then this cannot work because we have no
5085 "self". In any case, the call itself would raise
5086 TypeError (foo needs an argument), so we just skip
5087 profiling. */
5088 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5089 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5090 if (func == NULL) {
5091 return NULL;
5092 }
5093
Victor Stinner4d231bc2019-11-14 13:36:21 +01005094 C_TRACE(result, _PyObject_FastCallDictTstate(
5095 tstate, func,
5096 &_PyTuple_ITEMS(callargs)[1],
5097 nargs - 1,
5098 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005099 Py_DECREF(func);
5100 return result;
5101 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005102 }
jdemeyere89de732018-09-19 12:06:20 +02005103 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005104}
5105
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005106/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005107 nb_index slot defined, and store in *pi.
5108 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005109 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005110 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005111*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005112int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005113_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005114{
Victor Stinner438a12d2019-05-24 17:01:38 +02005115 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005116 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005118 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005120 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 return 0;
5122 }
5123 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005124 _PyErr_SetString(tstate, PyExc_TypeError,
5125 "slice indices must be integers or "
5126 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 return 0;
5128 }
5129 *pi = x;
5130 }
5131 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005132}
5133
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005134int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005135_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005136{
Victor Stinner438a12d2019-05-24 17:01:38 +02005137 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005138 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005139 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005140 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005141 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005142 return 0;
5143 }
5144 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005145 _PyErr_SetString(tstate, PyExc_TypeError,
5146 "slice indices must be integers or "
5147 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005148 return 0;
5149 }
5150 *pi = x;
5151 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005152}
5153
Thomas Wouters52152252000-08-17 22:55:00 +00005154static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005155import_name(PyThreadState *tstate, PyFrameObject *f,
5156 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005157{
5158 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005159 PyObject *import_func, *res;
5160 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005161
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005162 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005163 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005164 if (!_PyErr_Occurred(tstate)) {
5165 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005166 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005167 return NULL;
5168 }
5169
5170 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005171 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005172 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005173 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005174 return NULL;
5175 }
5176 res = PyImport_ImportModuleLevelObject(
5177 name,
5178 f->f_globals,
5179 f->f_locals == NULL ? Py_None : f->f_locals,
5180 fromlist,
5181 ilevel);
5182 return res;
5183 }
5184
5185 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005186
5187 stack[0] = name;
5188 stack[1] = f->f_globals;
5189 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5190 stack[3] = fromlist;
5191 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005192 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005193 Py_DECREF(import_func);
5194 return res;
5195}
5196
5197static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005198import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005201 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005202
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005203 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005204 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005205 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005206 /* Issue #17636: in case this failed because of a circular relative
5207 import, try to fallback on reading the module directly from
5208 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005209 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005210 if (pkgname == NULL) {
5211 goto error;
5212 }
Oren Milman6db70332017-09-19 14:23:01 +03005213 if (!PyUnicode_Check(pkgname)) {
5214 Py_CLEAR(pkgname);
5215 goto error;
5216 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005217 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005218 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005219 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005220 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005221 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005222 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005223 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005224 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005225 goto error;
5226 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005227 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005229 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005230 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005231 if (pkgname == NULL) {
5232 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5233 if (pkgname_or_unknown == NULL) {
5234 Py_XDECREF(pkgpath);
5235 return NULL;
5236 }
5237 } else {
5238 pkgname_or_unknown = pkgname;
5239 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005240
5241 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005242 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005243 errmsg = PyUnicode_FromFormat(
5244 "cannot import name %R from %R (unknown location)",
5245 name, pkgname_or_unknown
5246 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005247 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005248 PyErr_SetImportError(errmsg, pkgname, NULL);
5249 }
5250 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005251 _Py_IDENTIFIER(__spec__);
5252 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005253 const char *fmt =
5254 _PyModuleSpec_IsInitializing(spec) ?
5255 "cannot import name %R from partially initialized module %R "
5256 "(most likely due to a circular import) (%S)" :
5257 "cannot import name %R from %R (%S)";
5258 Py_XDECREF(spec);
5259
5260 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005261 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005262 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005263 }
5264
Xiang Zhang4830f582017-03-21 11:13:42 +08005265 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005266 Py_XDECREF(pkgname_or_unknown);
5267 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005268 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005269}
Guido van Rossumac7be682001-01-17 15:42:30 +00005270
Thomas Wouters52152252000-08-17 22:55:00 +00005271static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005272import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005273{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005274 _Py_IDENTIFIER(__all__);
5275 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005276 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 int skip_leading_underscores = 0;
5278 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005279
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005280 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5281 return -1; /* Unexpected error */
5282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005284 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5285 return -1;
5286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005288 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005289 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 return -1;
5291 }
5292 all = PyMapping_Keys(dict);
5293 Py_DECREF(dict);
5294 if (all == NULL)
5295 return -1;
5296 skip_leading_underscores = 1;
5297 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 for (pos = 0, err = 0; ; pos++) {
5300 name = PySequence_GetItem(all, pos);
5301 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005302 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 }
5305 else {
5306 _PyErr_Clear(tstate);
5307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 break;
5309 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005310 if (!PyUnicode_Check(name)) {
5311 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5312 if (modname == NULL) {
5313 Py_DECREF(name);
5314 err = -1;
5315 break;
5316 }
5317 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005318 _PyErr_Format(tstate, PyExc_TypeError,
5319 "module __name__ must be a string, not %.100s",
5320 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005321 }
5322 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 _PyErr_Format(tstate, PyExc_TypeError,
5324 "%s in %U.%s must be str, not %.100s",
5325 skip_leading_underscores ? "Key" : "Item",
5326 modname,
5327 skip_leading_underscores ? "__dict__" : "__all__",
5328 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005329 }
5330 Py_DECREF(modname);
5331 Py_DECREF(name);
5332 err = -1;
5333 break;
5334 }
5335 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005336 if (PyUnicode_READY(name) == -1) {
5337 Py_DECREF(name);
5338 err = -1;
5339 break;
5340 }
5341 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5342 Py_DECREF(name);
5343 continue;
5344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 }
5346 value = PyObject_GetAttr(v, name);
5347 if (value == NULL)
5348 err = -1;
5349 else if (PyDict_CheckExact(locals))
5350 err = PyDict_SetItem(locals, name, value);
5351 else
5352 err = PyObject_SetItem(locals, name, value);
5353 Py_DECREF(name);
5354 Py_XDECREF(value);
5355 if (err != 0)
5356 break;
5357 }
5358 Py_DECREF(all);
5359 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005360}
5361
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005362static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005363check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005364{
Victor Stinnera102ed72020-02-07 02:24:48 +01005365 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005366 /* check_args_iterable() may be called with a live exception:
5367 * clear it to prevent calling _PyObject_FunctionStr() with an
5368 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005369 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005370 PyObject *funcstr = _PyObject_FunctionStr(func);
5371 if (funcstr != NULL) {
5372 _PyErr_Format(tstate, PyExc_TypeError,
5373 "%U argument after * must be an iterable, not %.200s",
5374 funcstr, Py_TYPE(args)->tp_name);
5375 Py_DECREF(funcstr);
5376 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005377 return -1;
5378 }
5379 return 0;
5380}
5381
5382static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005383format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005384{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005385 /* _PyDict_MergeEx raises attribute
5386 * error (percolated from an attempt
5387 * to get 'keys' attribute) instead of
5388 * a type error if its second argument
5389 * is not a mapping.
5390 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005391 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005392 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005393 PyObject *funcstr = _PyObject_FunctionStr(func);
5394 if (funcstr != NULL) {
5395 _PyErr_Format(
5396 tstate, PyExc_TypeError,
5397 "%U argument after ** must be a mapping, not %.200s",
5398 funcstr, Py_TYPE(kwargs)->tp_name);
5399 Py_DECREF(funcstr);
5400 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005401 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005402 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005403 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005404 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005405 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005406 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005407 PyObject *funcstr = _PyObject_FunctionStr(func);
5408 if (funcstr != NULL) {
5409 PyObject *key = PyTuple_GET_ITEM(val, 0);
5410 _PyErr_Format(
5411 tstate, PyExc_TypeError,
5412 "%U got multiple values for keyword argument '%S'",
5413 funcstr, key);
5414 Py_DECREF(funcstr);
5415 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005416 Py_XDECREF(exc);
5417 Py_XDECREF(val);
5418 Py_XDECREF(tb);
5419 }
5420 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005422 }
5423 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005424}
5425
Guido van Rossumac7be682001-01-17 15:42:30 +00005426static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005427format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5428 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 if (!obj)
5433 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005434
Serhiy Storchaka06515832016-11-20 09:13:07 +02005435 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 if (!obj_str)
5437 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005438
Victor Stinner438a12d2019-05-24 17:01:38 +02005439 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005440}
Guido van Rossum950361c1997-01-24 13:49:28 +00005441
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005442static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005443format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005444{
5445 PyObject *name;
5446 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005447 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005448 return;
5449 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5450 name = PyTuple_GET_ITEM(co->co_cellvars,
5451 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005452 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005453 PyExc_UnboundLocalError,
5454 UNBOUNDLOCAL_ERROR_MSG,
5455 name);
5456 } else {
5457 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5458 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005459 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005460 UNBOUNDFREE_ERROR_MSG, name);
5461 }
5462}
5463
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005464static void
Mark Shannonfee55262019-11-21 09:11:43 +00005465format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005466{
5467 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5468 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005469 _PyErr_Format(tstate, PyExc_TypeError,
5470 "'async with' received an object from __aenter__ "
5471 "that does not implement __await__: %.100s",
5472 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005473 }
Mark Shannonfee55262019-11-21 09:11:43 +00005474 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005475 _PyErr_Format(tstate, PyExc_TypeError,
5476 "'async with' received an object from __aexit__ "
5477 "that does not implement __await__: %.100s",
5478 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005479 }
5480 }
5481}
5482
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005483static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005484unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005485 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005486{
5487 PyObject *res;
5488 if (Py_REFCNT(v) == 2) {
5489 /* In the common case, there are 2 references to the value
5490 * stored in 'variable' when the += is performed: one on the
5491 * value stack (in 'v') and one still stored in the
5492 * 'variable'. We try to delete the variable now to reduce
5493 * the refcnt to 1.
5494 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005495 int opcode, oparg;
5496 NEXTOPARG();
5497 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005498 case STORE_FAST:
5499 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005500 PyObject **fastlocals = f->f_localsplus;
5501 if (GETLOCAL(oparg) == v)
5502 SETLOCAL(oparg, NULL);
5503 break;
5504 }
5505 case STORE_DEREF:
5506 {
5507 PyObject **freevars = (f->f_localsplus +
5508 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005509 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005510 if (PyCell_GET(c) == v) {
5511 PyCell_SET(c, NULL);
5512 Py_DECREF(v);
5513 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005514 break;
5515 }
5516 case STORE_NAME:
5517 {
5518 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005519 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005520 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005521 if (locals && PyDict_CheckExact(locals)) {
5522 PyObject *w = PyDict_GetItemWithError(locals, name);
5523 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005524 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005525 {
5526 Py_DECREF(v);
5527 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005528 }
5529 }
5530 break;
5531 }
5532 }
5533 }
5534 res = v;
5535 PyUnicode_Append(&res, w);
5536 return res;
5537}
5538
Guido van Rossum950361c1997-01-24 13:49:28 +00005539#ifdef DYNAMIC_EXECUTION_PROFILE
5540
Skip Montanarof118cb12001-10-15 20:51:38 +00005541static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005542getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 int i;
5545 PyObject *l = PyList_New(256);
5546 if (l == NULL) return NULL;
5547 for (i = 0; i < 256; i++) {
5548 PyObject *x = PyLong_FromLong(a[i]);
5549 if (x == NULL) {
5550 Py_DECREF(l);
5551 return NULL;
5552 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005553 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 }
5555 for (i = 0; i < 256; i++)
5556 a[i] = 0;
5557 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005558}
5559
5560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005561_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005562{
5563#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005565#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005566 int i;
5567 PyObject *l = PyList_New(257);
5568 if (l == NULL) return NULL;
5569 for (i = 0; i < 257; i++) {
5570 PyObject *x = getarray(dxpairs[i]);
5571 if (x == NULL) {
5572 Py_DECREF(l);
5573 return NULL;
5574 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005575 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 }
5577 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005578#endif
5579}
5580
5581#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005582
5583Py_ssize_t
5584_PyEval_RequestCodeExtraIndex(freefunc free)
5585{
Victor Stinner81a7be32020-04-14 15:14:01 +02005586 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005587 Py_ssize_t new_index;
5588
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005589 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005590 return -1;
5591 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005592 new_index = interp->co_extra_user_count++;
5593 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005594 return new_index;
5595}
Łukasz Langaa785c872016-09-09 17:37:37 -07005596
5597static void
5598dtrace_function_entry(PyFrameObject *f)
5599{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005600 const char *filename;
5601 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005602 int lineno;
5603
Victor Stinner6d86a232020-04-29 00:56:58 +02005604 PyCodeObject *code = f->f_code;
5605 filename = PyUnicode_AsUTF8(code->co_filename);
5606 funcname = PyUnicode_AsUTF8(code->co_name);
5607 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005608
Andy Lestere6be9b52020-02-11 20:28:35 -06005609 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005610}
5611
5612static void
5613dtrace_function_return(PyFrameObject *f)
5614{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005615 const char *filename;
5616 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005617 int lineno;
5618
Victor Stinner6d86a232020-04-29 00:56:58 +02005619 PyCodeObject *code = f->f_code;
5620 filename = PyUnicode_AsUTF8(code->co_filename);
5621 funcname = PyUnicode_AsUTF8(code->co_name);
5622 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005623
Andy Lestere6be9b52020-02-11 20:28:35 -06005624 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005625}
5626
5627/* DTrace equivalent of maybe_call_line_trace. */
5628static void
5629maybe_dtrace_line(PyFrameObject *frame,
5630 int *instr_lb, int *instr_ub, int *instr_prev)
5631{
5632 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005633 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005634
5635 /* If the last instruction executed isn't in the current
5636 instruction window, reset the window.
5637 */
5638 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5639 PyAddrPair bounds;
5640 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5641 &bounds);
5642 *instr_lb = bounds.ap_lower;
5643 *instr_ub = bounds.ap_upper;
5644 }
5645 /* If the last instruction falls at the start of a line or if
5646 it represents a jump backwards, update the frame's line
5647 number and call the trace function. */
5648 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5649 frame->f_lineno = line;
5650 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5651 if (!co_filename)
5652 co_filename = "?";
5653 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5654 if (!co_name)
5655 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005656 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005657 }
5658 *instr_prev = frame->f_lasti;
5659}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005660
5661
5662/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5663 for the limited API. */
5664
5665#undef Py_EnterRecursiveCall
5666
5667int Py_EnterRecursiveCall(const char *where)
5668{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005669 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005670}
5671
5672#undef Py_LeaveRecursiveCall
5673
5674void Py_LeaveRecursiveCall(void)
5675{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005676 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005677}